7
"name": "23 - Tensor workflows",
9
"collapsed_sections": []
12
"display_name": "Python 3",
18
"cell_type": "markdown",
23
"# Tensor workflows\n",
25
"Many of the examples and use cases for txtai focus on transforming text. Makes sense as txt is even in the name! But that doesn't mean txtai only works with text.\n",
27
"This notebook will cover examples of how to efficiently process tensors using txtai workflows."
31
"cell_type": "markdown",
36
"# Install dependencies\n",
38
"Install `txtai` and all dependencies. We will install the api, pipeline and workflow optional extras packages, along with the datasets package. "
48
"!pip install git+https://github.com/neuml/txtai#egg=txtai[api,pipeline,workflow] datasets"
50
"execution_count": null,
54
"cell_type": "markdown",
59
"# Transform large tensor arrays\n",
61
"The first section attempts to apply a simple transform to a very large memory-mapped array (2,000,000 x 1024)."
69
"base_uri": "https://localhost:8080/",
72
"outputId": "143a6e4d-fe56-4353-e8ee-595ddfc12249"
75
"import numpy as np\n",
78
"# Generate large memory-mapped array\n",
79
"rows, cols = 2000000, 1024\n",
80
"data = np.memmap(\"data.npy\", dtype=np.float32, mode=\"w+\", shape=(rows, cols))\n",
83
"# Open memory-mapped array\n",
84
"data = np.memmap(\"data.npy\", dtype=np.float32, shape=(rows, cols))\n",
87
"tensor = torch.from_numpy(data).to(\"cuda:0\")\n",
89
"# Apply tanh transform to tensor\n",
90
"torch.tanh(tensor).shape"
92
"execution_count": null,
95
"output_type": "error",
96
"ename": "RuntimeError",
99
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
100
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
101
"\u001b[0;32m<ipython-input-2-a1fc94fedb69>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;31m# Apply tanh transform to tensor\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtanh\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
102
"\u001b[0;31mRuntimeError\u001b[0m: CUDA out of memory. Tried to allocate 7.63 GiB (GPU 0; 11.17 GiB total capacity; 7.63 GiB already allocated; 3.04 GiB free; 7.63 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF"
111
"base_uri": "https://localhost:8080/"
113
"id": "O8mKzPP01d_m",
114
"outputId": "929226a8-6948-4d17-ab70-025da2081abd"
117
"!ls -l --block-size=MB data.npy"
119
"execution_count": null,
122
"output_type": "stream",
125
"-rw-r--r-- 1 root root 8192MB Dec 6 23:24 data.npy\n"
131
"cell_type": "markdown",
136
"Not surprisingly this runs out of CUDA memory. The array needs `2,000,000 * 1024 * 4 = 8GB` which exceeds the amount of GPU memory available.\n",
138
"One of the great things about NumPy and PyTorch arrays is that they can be sliced without having to copy data. Additionally, PyTorch has methods to work directly on NumPy arrays without copying data, in other words both NumPy arrays and PyTorch arrays can share the same memory. This opens the door to efficient processing of tensor data in place. \n",
140
"Let's try applying a simple tanh transform in batches over the array."
147
"base_uri": "https://localhost:8080/"
149
"id": "ciD6unQYD-bJ",
150
"outputId": "d3b6a0c5-aea5-451d-d3e3-60d04ef33a9e"
155
" return torch.tanh(torch.from_numpy(x).to(\"cuda:0\")).cpu().numpy()\n",
157
"# Split into 250,000 rows per call\n",
160
"for x in range(0, len(data), batch):\n",
161
" for row in process(data[x : x + batch]):\n",
166
"execution_count": null,
169
"output_type": "stream",
186
"cell_type": "markdown",
191
"Iterating over the data array and selecting slices to operate on allows the transform to complete successfully! Each `torch.from_numpy` call is building a view of a portion the existing large NumPy data array. "
195
"cell_type": "markdown",
200
"# Enter workflows\n",
202
"The next section takes the same array and shows how workflows can apply transformations to tensors. "
209
"base_uri": "https://localhost:8080/"
211
"id": "ymqr92kW9hxd",
212
"outputId": "e4a4c7d1-be54-46c2-bc7c-dd849adfeb7e"
215
"from txtai.workflow import Task, Workflow\n",
217
"# Create workflow with a single task calling process for each batch\n",
218
"task = Task(process)\n",
219
"workflow = Workflow([task], batch)\n",
223
"for row in workflow(data):\n",
228
"execution_count": null,
231
"output_type": "stream",
248
"cell_type": "markdown",
253
"Workflows process the data in the same fashion as the code in the previous section. On top of that, workflows can handle text, images, video, audio, document, tensors and more. Workflow graphs can also be connected together to handle complex use cases."
257
"cell_type": "markdown",
262
"# Workflows with PyTorch models\n",
264
"The next example applies a PyTorch model to the same data. The model applies a series of transforms and outputs a single float per row."
271
"base_uri": "https://localhost:8080/"
273
"id": "N9UTfSTTIDaO",
274
"outputId": "000c7e13-a249-43d0-f419-28ddd62e8ba1"
277
"from torch import nn\n",
279
"class Model(nn.Module):\n",
280
" def __init__(self):\n",
281
" super().__init__()\n",
283
" self.gelu = nn.ReLU()\n",
284
" self.linear1 = nn.Linear(1024, 512)\n",
285
" self.dropout = nn.Dropout(0.5)\n",
286
" self.norm = nn.LayerNorm(512)\n",
287
" self.linear2 = nn.Linear(512, 1)\n",
289
" def forward(self, inputs):\n",
290
" outputs = self.gelu(inputs)\n",
291
" outputs = self.linear1(outputs)\n",
292
" outputs = self.dropout(outputs)\n",
293
" outputs = self.norm(outputs)\n",
294
" outputs = self.linear2(outputs)\n",
298
"model = Model().to(\"cuda:0\")\n",
301
" with torch.no_grad():\n",
302
" outputs = model(torch.from_numpy(x).to(\"cuda:0\")).cpu().numpy()\n",
303
" print(outputs.shape)\n",
306
"# Create workflow with a single task calling model for each batch\n",
307
"task = Task(process)\n",
308
"workflow = Workflow([task], batch)\n",
312
"for row in workflow(data):\n",
317
"execution_count": null,
320
"output_type": "stream",
337
"cell_type": "markdown",
342
"Once again the data can be processed in batches using workflows, even with a more complex model. Let's try a more interesting example."
346
"cell_type": "markdown",
351
"# Workflows in parallel\n",
353
"Workflows consist of a series of tasks. Each task can output one to many outputs per input element. Multi-output tasks have options available to [merge the data](https://neuml.github.io/txtai/workflow/task/#multi-action-task-merges) for downstream tasks.\n",
355
"The following example builds a workflow with a task having three separate actions. Each action takes text as an input an applies a sentiment classifier. This is followed by a task that merges the three outputs for each row using a mean transform. Essentially, this workflow builds a weighted sentiment classifier using the outputs of three models. "
366
"from datasets import load_dataset\n",
367
"from transformers import AutoTokenizer, AutoModelForSequenceClassification\n",
370
" def __init__(self, texts):\n",
371
" tokenizer = AutoTokenizer.from_pretrained(\"distilbert-base-uncased-finetuned-sst-2-english\")\n",
372
" tokens = tokenizer(texts, padding=True, return_tensors=\"pt\").to(\"cuda:0\")\n",
374
" self.inputs, self.attention = tokens[\"input_ids\"], tokens[\"attention_mask\"]\n",
376
" def __len__(self):\n",
377
" return len(self.inputs)\n",
379
" def __getitem__(self, value):\n",
380
" return (self.inputs[value], self.attention[value])\n",
383
" def __init__(self, model):\n",
384
" self.model = model\n",
386
" def __call__(self, tokens):\n",
387
" with torch.no_grad():\n",
388
" inputs, attention = tokens\n",
389
" outputs = self.model(input_ids=inputs, attention_mask=attention)\n",
390
" outputs = outputs[\"logits\"]\n",
394
"# Load reviews from the rotten tomatoes dataset\n",
395
"ds = load_dataset(\"rotten_tomatoes\")\n",
396
"texts = ds[\"train\"][\"text\"]\n",
398
"tokens = Tokens(texts)\n",
400
"model1 = AutoModelForSequenceClassification.from_pretrained(\"M-FAC/bert-tiny-finetuned-sst2\")\n",
401
"model1 = model1.to(\"cuda:0\")\n",
403
"model2 = AutoModelForSequenceClassification.from_pretrained(\"howey/electra-base-sst2\")\n",
404
"model2 = model2.to(\"cuda:0\")\n",
406
"model3 = AutoModelForSequenceClassification.from_pretrained(\"philschmid/MiniLM-L6-H384-uncased-sst2\")\n",
407
"model3 = model3.to(\"cuda:0\")\n",
409
"task1 = Task([Classify(model1), Classify(model2), Classify(model3)])\n",
410
"task2 = Task([lambda x: torch.sigmoid(x).mean(axis=1).cpu().numpy()])\n",
412
"workflow = Workflow([task1, task2], 250)\n",
414
"start = time.time()\n",
415
"for x in workflow(tokens):\n",
418
"print(f\"Took {time.time() - start} seconds\")"
420
"execution_count": null,
423
"output_type": "stream",
426
"Using custom data configuration default\n",
427
"Reusing dataset rotten_tomatoes_movie_review (/root/.cache/huggingface/datasets/rotten_tomatoes_movie_review/default/1.0.0/40d411e45a6ce3484deed7cc15b82a53dad9a72aafd9f86f8f227134bec5ca46)\n"
431
"output_type": "stream",
434
"Took 84.73194456100464 seconds\n"
440
"cell_type": "markdown",
445
"Note that while the task actions are parallel, that doesn't necessarily mean the operations are concurrent. In the case above, the actions are are executed sequentially.\n",
447
"Workflows have an additional option to run task actions concurrently. The two supported modes are \"thread\" and \"process\". I/O bound actions will do better with multithreading and CPU bound actions will do better with multiprocessing. More can be read in the [txtai documentation](https://neuml.github.io/txtai/workflow/task/#multi-action-task-concurrency). "
454
"base_uri": "https://localhost:8080/"
456
"id": "AB0onoOlVT-e",
457
"outputId": "a072d8a6-3b3b-4066-8881-8af9a8b96608"
460
"task1 = Task([Classify(model1), Classify(model2), Classify(model3)], concurrency=\"thread\")\n",
461
"task2 = Task([lambda x: torch.sigmoid(x).mean(axis=1).cpu().numpy()])\n",
463
"workflow = Workflow([task1, task2], 250)\n",
465
"start = time.time()\n",
466
"for x in workflow(tokens):\n",
469
"print(f\"Took {time.time() - start} seconds\")"
471
"execution_count": null,
474
"output_type": "stream",
477
"Took 85.21102929115295 seconds\n"
483
"cell_type": "markdown",
488
"In this case, concurrency doesn't improve performance. While the [GIL](https://wiki.python.org/moin/GlobalInterpreterLock) is a factor, a bigger factor is that the GPU is already fully loaded. This method would be more beneficial if the system had a second GPU or the primary GPU had idle cycles. "
492
"cell_type": "markdown",
499
"This notebook introduced a number of different ways to work with large-scale tensor data and process it efficiently. This notebook purposely didn't cover embeddings and pipelines to demonstrate how workflows can stand on their own. In addition to workflows, this notebook covered efficient methods to work with large tensor arrays in PyTorch and NumPy."