File size: 18,563 Bytes
c43c604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c1d0f7
c43c604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5567a0
c43c604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c1d0f7
c43c604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c1d0f7
c43c604
 
 
 
 
 
 
 
8c1d0f7
 
31cd561
8c1d0f7
 
31cd561
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c1d0f7
 
c43c604
 
31cd561
c43c604
 
31cd561
 
 
c43c604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ff5c7a97-02d5-4aea-8bd5-59be5e62bf01",
   "metadata": {},
   "source": [
    "---\n",
    "title: \"Accelerate, Three Powerful Sublibraries for PyTorch\"\n",
    "author: \"Zachary Mueller\"\n",
    "format: \n",
    "    revealjs:\n",
    "        theme: moon\n",
    "        fig-format: png\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45e61402-f734-4500-8eb6-fcdd6f17a0d4",
   "metadata": {},
   "source": [
    "## Who am I?\n",
    "\n",
    "- Zachary Mueller\n",
    "- Deep Learning Software Engineer at πŸ€—\n",
    "- API design geek"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f9864d2-5787-4af3-a08d-b372e5851a0f",
   "metadata": {},
   "source": [
    "## What is πŸ€— Accelerate?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "166b148a-e2f0-46b0-bc61-ac6e81da5ac5",
   "metadata": {},
   "source": [
    "```{mermaid}\n",
    "%%| fig-height: 6\n",
    "graph LR\n",
    "    A{\"πŸ€— Accelerate#32;\"}\n",
    "    A --> B[\"Launching<br>Interface#32;\"]\n",
    "    A --> C[\"Training Library#32;\"]\n",
    "    A --> D[\"Big Model<br>Inference#32;\"]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84d6fd12-18cd-4448-9123-821133673b95",
   "metadata": {},
   "source": [
    "# A Launching Interface\n",
    "\n",
    "Can't I just use `python do_the_thing.py`?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5488645-daa3-4353-be9f-7af765a52666",
   "metadata": {},
   "source": [
    "## A Launching Interface\n",
    "\n",
    "Launching scripts in different environments is complicated:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce856633-1909-4f18-9610-e934194dd584",
   "metadata": {},
   "source": [
    "- ```bash \n",
    "python script.py\n",
    "```\n",
    "\n",
    "- ```bash \n",
    "torchrun --nnodes=1 --nproc_per_node=2 script.py\n",
    "```\n",
    "\n",
    "- ```bash \n",
    "deepspeed --num_gpus=2 script.py\n",
    "```\n",
    "\n",
    "And more!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e6414d0-f8f8-4bd2-b06f-fe7f848320f1",
   "metadata": {
    "tags": []
   },
   "source": [
    "## A Launching Interface\n",
    "\n",
    "But it doesn't have to be:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dfd30c0-7240-4a13-9b51-061c4762b37e",
   "metadata": {},
   "source": [
    "```bash\n",
    "accelerate launch script.py\n",
    "```\n",
    "\n",
    "A single command to launch with `DeepSpeed`, Fully Sharded Data Parallelism, across single and multi CPUs and GPUs, and to train on TPUs[^1] too! \n",
    "\n",
    "[^1]: Without needing to modify your code and create a `_mp_fn`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0760c9a-4307-4143-9adc-bf1ce2ed4460",
   "metadata": {},
   "source": [
    "## A Launching Interface\n",
    "\n",
    "Generate a device-specific configuration through `accelerate config`\n",
    "\n",
    "![](CLI.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0f1dc7a-ec43-48ba-b0a0-1331981733d0",
   "metadata": {},
   "source": [
    "## A Launching Interface\n",
    "\n",
    "Or don't. `accelerate config` doesn't *have* to be done!\n",
    "\n",
    "```bash\n",
    "torchrun --nnodes=1 --nproc_per_node=2 script.py\n",
    "accelerate launch --multi_gpu --nproc_per_node=2 script.py\n",
    "```\n",
    "\n",
    "A quick default configuration can be made too:\n",
    "\n",
    "```bash \n",
    "accelerate config default\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff8d2c3d-5a08-4e5b-9896-1a0bcb77b5a6",
   "metadata": {},
   "source": [
    "## A Launching Interface"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a395af44-96f8-4f3a-ac47-3f65a6062d24",
   "metadata": {},
   "source": [
    "With the `notebook_launcher` it's also possible to launch code directly from your Jupyter environment too!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99b14b46-6be5-4ef4-a3ee-82876b1d7802",
   "metadata": {},
   "source": [
    "```python\n",
    "from accelerate import notebook_launcher\n",
    "notebook_launcher(\n",
    "    training_loop_function, \n",
    "    args, \n",
    "    num_processes=2\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a50e27a7-4235-4695-bf99-59c0f3d0e451",
   "metadata": {},
   "source": [
    "```python\n",
    "Launching training on 2 GPUs.\n",
    "epoch 0: 88.12\n",
    "epoch 1: 91.73\n",
    "epoch 2: 92.58\n",
    "epoch 3: 93.90\n",
    "epoch 4: 94.71\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2db4e66d-d8b0-4f3f-9236-e86c1c3ea5d2",
   "metadata": {},
   "source": [
    "# A Training Library\n",
    "\n",
    "Okay, will `accelerate launch` make `do_the_thing.py` use all my GPUs magically?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cd093ef-d3ce-4ea4-89a1-be145fbe5cc0",
   "metadata": {},
   "source": [
    "## A Training Library\n",
    "\n",
    "- Just showed that its possible using `accelerate launch` to *launch* a python script in various distributed environments\n",
    "- This does *not* mean that the script will just \"use\" that code and still run on the new compute efficiently.\n",
    "- Training on different computes often means *many* lines of code changed for each specific compute.\n",
    "- πŸ€— `accelerate` solves this by ensuring the same code can be ran on a CPU or GPU, multiples, and on TPUs!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0b12eb9-feeb-4040-a784-8e78966165be",
   "metadata": {},
   "source": [
    "## A Training Library\n",
    "\n",
    "\n",
    "```{.python}\n",
    "for batch in dataloader:\n",
    "    optimizer.zero_grad()\n",
    "    inputs, targets = batch\n",
    "    inputs = inputs.to(device)\n",
    "    targets = targets.to(device)\n",
    "    outputs = model(inputs)\n",
    "    loss = loss_function(outputs, targets)\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    scheduler.step()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbb72602-f86f-42f6-ab44-05fbd0dfcecd",
   "metadata": {},
   "source": [
    "## A Training Library {.smaller}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5f90b84-fff5-4c14-bde7-d1efbcc37781",
   "metadata": {},
   "source": [
    ":::: {.columns}\n",
    "::: {.column width=\"43%\"}\n",
    "<br><br><br>\n",
    "```{.python code-line-numbers=\"5-6,9\"}\n",
    "# For alignment purposes\n",
    "for batch in dataloader:\n",
    "    optimizer.zero_grad()\n",
    "    inputs, targets = batch\n",
    "    inputs = inputs.to(device)\n",
    "    targets = targets.to(device)\n",
    "    outputs = model(inputs)\n",
    "    loss = loss_function(outputs, targets)\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "    scheduler.step()\n",
    "```\n",
    ":::\n",
    "::: {.column width=\"57%\"}\n",
    "```{.python code-line-numbers=\"1-7,12-13,16\"}\n",
    "from accelerate import Accelerator\n",
    "accelerator = Accelerator()\n",
    "dataloader, model, optimizer scheduler = (\n",
    "    accelerator.prepare(\n",
    "        dataloader, model, optimizer, scheduler\n",
    "    )\n",
    ")\n",
    "\n",
    "for batch in dataloader:\n",
    "    optimizer.zero_grad()\n",
    "    inputs, targets = batch\n",
    "    # inputs = inputs.to(device)\n",
    "    # targets = targets.to(device)\n",
    "    outputs = model(inputs)\n",
    "    loss = loss_function(outputs, targets)\n",
    "    accelerator.backward(loss) # loss.backward()\n",
    "    optimizer.step()\n",
    "    scheduler.step()\n",
    "```\n",
    ":::\n",
    "\n",
    "::::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60c90913-2542-4b1d-8121-b2228c8a2ef7",
   "metadata": {
    "tags": []
   },
   "source": [
    "## A Training Library\n",
    "\n",
    "What all happened in `Accelerator.prepare`?\n",
    "\n",
    "::: {.incremental}\n",
    "1. `Accelerator` looked at the configuration\n",
    "2. The `dataloader` was converted into one that can dispatch each batch onto a seperate GPU\n",
    "3. The `model` was wrapped with the appropriate DDP wrapper from either `torch.distributed` or `torch_xla`\n",
    "4. The `optimizer` and `scheduler` were both converted into an `AcceleratedOptimizer` and `AcceleratedScheduler` which knows how to handle any distributed scenario\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59400a16-bce7-4a0a-8548-effd3c4c6cae",
   "metadata": {},
   "source": [
    "## A Training Library, Mixed Precision\n",
    "\n",
    "πŸ€— `accelerate` also supports *automatic mixed precision*. \n",
    "\n",
    "Through a single flag to the `Accelerator` object when calling `accelerator.backward()` the mixed precision of your choosing (such as `bf16` or `fp16`) will be applied:\n",
    "\n",
    "```{.python code-line-numbers=\"2,9\"}\n",
    "from accelerate import Accelerator\n",
    "accelerator = Accelerator(mixed_precision=\"fp16\")\n",
    "...\n",
    "for batch in dataloader:\n",
    "    optimizer.zero_grad()\n",
    "    inputs, targets = batch\n",
    "    outputs = model(inputs)\n",
    "    loss = loss_function(outputs, targets)\n",
    "    accelerator.backward(loss)\n",
    "    optimizer.step()\n",
    "    scheduler.step()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fde7ae10-4fbd-4e25-8f5d-9d47c849966d",
   "metadata": {},
   "source": [
    "## A Training Library, Gradient Accumulation\n",
    "\n",
    "Gradient accumulation in distributed setups often need extra care to ensure gradients are aligned when they need to be and the backward pass is computationally efficient.\n",
    "\n",
    "πŸ€— `accelerate` can just easily handle this for you:\n",
    "\n",
    "```{.python code-line-numbers=\"2,5\"}\n",
    "from accelerate import Accelerator\n",
    "accelerator = Accelerator(gradient_accumulation_steps=4)\n",
    "...\n",
    "for batch in dataloader:\n",
    "    with accelerator.accumulate(model):\n",
    "        optimizer.zero_grad()\n",
    "        inputs, targets = batch\n",
    "        outputs = model(inputs)\n",
    "        loss = loss_function(outputs, targets)\n",
    "        accelerator.backward(loss)\n",
    "        optimizer.step()\n",
    "        scheduler.step()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13f2d1e7-1e50-4a28-b7b4-55e09e15c176",
   "metadata": {},
   "source": [
    "## A Training Library, Gradient Accumulation\n",
    "\n",
    "```{.python code-line-numbers=\"5-7,10,11,12,15\"}\n",
    "ddp_model, dataloader = accelerator.prepare(model, dataloader)\n",
    "\n",
    "for index, batch in enumerate(dataloader):\n",
    "    inputs, targets = batch\n",
    "    if index != (len(dataloader)-1) or (index % 4) != 0:\n",
    "        # Gradients don't sync\n",
    "        with accelerator.no_sync(model):\n",
    "            outputs = ddp_model(inputs)\n",
    "            loss = loss_func(outputs, targets)\n",
    "            accelerator.backward(loss)\n",
    "    else:\n",
    "        # Gradients finally sync\n",
    "        outputs = ddp_model(inputs)\n",
    "        loss = loss_func(outputs)\n",
    "        accelerator.backward(loss)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93575b12-8000-4e8c-81fb-74af415fd76b",
   "metadata": {},
   "source": [
    "# Big Model Inference\n",
    "\n",
    "Stable Diffusion taking the world by storm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3026c5d-c051-4eac-a4be-af6559294225",
   "metadata": {},
   "source": [
    "## Bigger Models == Higher Compute\n",
    "\n",
    "As more large models were being released, Hugging Face quickly realized there must be a way to continue our decentralization of Machine Learning and have the day-to-day programmer be able to leverage these big models.\n",
    "\n",
    "Born out of this effort by Sylvain Gugger: \n",
    "\n",
    "πŸ€— Accelerate: Big Model Inference."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "303925bf-ce22-4e71-a239-69eb419d54d3",
   "metadata": {},
   "source": [
    "## The Basic Premise\n",
    "\n",
    "::: {.incremental}\n",
    "* In PyTorch, there exists the `meta` device. \n",
    "\n",
    "* Super small footprint to load in huge models quickly by not loading in their weights immediatly.\n",
    "\n",
    "* As an input gets passed through each layer, we can load and unload *parts* of the PyTorch model quickly so that only a small portion of the big model is loaded in at a single time.\n",
    "\n",
    "* The end result? Stable Diffusion v1 can be ran on < 800mb of vRAM\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6eef166-c64b-4229-9575-b197c3c03c59",
   "metadata": {},
   "source": [
    "## The Code\n",
    "\n",
    "Generally you start with something like so:\n",
    "\n",
    "```python\n",
    "import torch\n",
    "\n",
    "my_model = ModelClass(...)\n",
    "state_dict = torch.load(checkpoint_file)\n",
    "my_model.load_state_dict(state_dict)\n",
    "```\n",
    "\n",
    "But this has issues:\n",
    "\n",
    "1. The full version of the model is loaded at `3`\n",
    "2. Another version of the model is loaded into memory at `4`\n",
    "\n",
    "If a 6 *billion* parameter model is being loaded, each model class has a dictionary of 24GB so 48GB of vRAM is needed"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53651488-7303-4aa3-83bb-ea7331938a01",
   "metadata": {},
   "source": [
    "## Empty Model Weights\n",
    "\n",
    "We can fix step 1 by loading in an empty model skeleton at first:\n",
    "\n",
    "```{.python code-line-numbers=\"1,3-4\"}\n",
    "from accelerate import init_empty_weights\n",
    "\n",
    "with init_empty_weights():\n",
    "    my_model = ModelClass(...)\n",
    "state_dict = torch.load(checkpoint_file)\n",
    "my_model.load_state_dict(state_dict)\n",
    "```\n",
    "\n",
    "::: {.callout-important appearance=\"default\"}\n",
    "## This code will not run\n",
    "It is likely that just calling `my_model(x)` will fail as not all tensor operations are supported on the `meta` device.\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94a2b99a-b154-4cc3-93fd-431ba78ecfdf",
   "metadata": {},
   "source": [
    "## Sharded Checkpoints - The Concept\n",
    "\n",
    "The next step is to have \"Sharded Checkpoints\" saved for your model.\n",
    "\n",
    "Basically smaller chunks of your model weights stored that can be brought in at any particular time. \n",
    "\n",
    "This reduces the amount of memory step 2 takes in since we can just load in a \"chunk\" of the model at a time, then swap it out for a new chunk through PyTorch hooks"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11a55882-8bab-4d6b-b8ca-bfc886351156",
   "metadata": {},
   "source": [
    "## Sharded Checkpoints - The Code\n",
    "\n",
    "```{.python code-line-numbers=\"1,6-8\"}\n",
    "from accelerate import init_empty_weights, load_checkpoint_and_dispatch\n",
    "\n",
    "with init_empty_weights():\n",
    "    my_model = ModelClass(...)\n",
    "\n",
    "my_model = load_checkpoint_and_dispatch(\n",
    "    my_model, \"sharded-weights\", device_map=\"auto\"\n",
    ")\n",
    "```\n",
    "`device_map=\"auto\"` will tell πŸ€— Accelerate that it should determine where to put each layer of the model:\n",
    "\n",
    "1. Maximum space on the GPU(s)\n",
    "2. Maximum space on the CPU(s)\n",
    "3. Utilize disk space through memory-mapped tensors"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6796c0ac-77e4-4f88-b01a-25f428b29a87",
   "metadata": {},
   "source": [
    "## Big Model Inference Put Together\n",
    "\n",
    "```{.python}\n",
    "from accelerate import init_empty_weights, load_checkpoint_and_dispatch\n",
    "\n",
    "with init_empty_weights():\n",
    "    my_model = ModelClass(...)\n",
    "\n",
    "my_model = load_checkpoint_and_dispatch(\n",
    "    my_model, \"sharded-weights\", device_map=\"auto\"\n",
    ")\n",
    "my_model.eval()\n",
    "\n",
    "for batch in dataloader:\n",
    "    output = my_model(batch)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "083c7037-27a2-4347-b473-358feb3316b3",
   "metadata": {},
   "source": [
    "## Is there an easier way?\n",
    "\n",
    "The `transformers` library combined with the Hub makes all this code wrapping much easier for you with the `pipeline`\n",
    "\n",
    "```python\n",
    "import torch\n",
    "from transformers import pipeline\n",
    "pipe = pipeline(\n",
    "    task=\"text-generation\",\n",
    "    model=\"EleutherAI/gpt-j-6B\",\n",
    "    device_map=\"auto\",\n",
    "    torch_dtype=torch.float16\n",
    ")\n",
    "\n",
    "text = pipe(\"This is some generated text, I think\")\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45b99a09-ba10-40b0-9b05-87360016da31",
   "metadata": {},
   "source": [
    "# What about Stable Diffusion? \n",
    "\n",
    "A demo with `diffusers`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52f29e81-2e55-42d0-8e9d-83e692714909",
   "metadata": {},
   "source": [
    "## Some Handy Resources\n",
    "\n",
    "- [πŸ€— Accelerate documentation](https://hf.co/docs/accelerate)\n",
    "- [Launching distributed code](https://huggingface.co/docs/accelerate/basic_tutorials/launch)\n",
    "- [Distributed code and Jupyter Notebooks](https://huggingface.co/docs/accelerate/basic_tutorials/notebook)\n",
    "- [Migrating to πŸ€— Accelerate easily](https://huggingface.co/docs/accelerate/basic_tutorials/migration)\n",
    "- [Big Model Inference tutorial](https://huggingface.co/docs/accelerate/usage_guides/big_modeling)\n",
    "- [DeepSpeed and πŸ€— Accelerate](https://huggingface.co/docs/accelerate/usage_guides/deepspeed)\n",
    "- [Fully Sharded Data Parallelism and πŸ€— Accelerate](https://huggingface.co/docs/accelerate/usage_guides/fsdp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b9f6a92d-1275-470b-aa27-ff2be450d616",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}