File size: 30,116 Bytes
4c57f07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e28407ed",
   "metadata": {
    "cellId": "b3rvfwilbqlfhl99ot1adn"
   },
   "source": [
    "# Example of Fine-tuning 7.1 billion Bloom with 8-bit weights\n",
    "\n",
    "This notebook shows an example of how to fine tune Bloom with Low Rank Adapters. Heavily inspired by [Hivemind's work](https://colab.research.google.com/drive/1ft6wQU0BhqG5PRlwgaZJv2VukKKjU4Es)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f5a6af2",
   "metadata": {
    "cellId": "q43y9u4kj5g2qn01pdohou"
   },
   "source": [
    "### Load and convert original Bloom structure to 8-bit LoRA\n",
    "\n",
    "You can load an already compressed 8-bit version of Bloom from [joaoalvarenga/bloom-8bit](https://huggingface.co/joaoalvarenga/bloom-8bit), but first we need to make some adaptations into original model structure. Some of the following code is an adaptation from [Hivemind's GPT-J 8-bit fine-tuning notebook](https://colab.research.google.com/drive/1ft6wQU0BhqG5PRlwgaZJv2VukKKjU4Es)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "815f9f31",
   "metadata": {
    "cellId": "qwv8mzg52blrc6ghm3x9s"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/dm/.local/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!\n",
      "  warnings.warn(\"urllib3 ({}) or chardet ({}) doesn't match a supported \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "===================================BUG REPORT===================================\n",
      "Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n",
      "For effortless bug reporting copy-paste your error into this form: https://docs.google.com/forms/d/e/1FAIpQLScPB8emS3Thkp66nvqwmjTEgxp8Y9ufuWTzFyr9kJ5AoI47dQ/viewform?usp=sf_link\n",
      "================================================================================\n",
      "CUDA_SETUP: WARNING! libcudart.so not found in any environmental path. Searching /usr/local/cuda/lib64...\n",
      "WARNING: No libcudart.so found! Install CUDA or the cudatoolkit package (anaconda)!\n",
      "CUDA SETUP: Loading binary /home/dm/.local/lib/python3.8/site-packages/bitsandbytes/libbitsandbytes_cpu.so...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/dm/.local/lib/python3.8/site-packages/bitsandbytes/cuda_setup/paths.py:27: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('8bitexperiments/71572fb0-3a48-4729-a863-ee5aa6e60c92')}\n",
      "  warn(\n",
      "/home/dm/.local/lib/python3.8/site-packages/bitsandbytes/cuda_setup/paths.py:27: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('//matplotlib_inline.backend_inline'), PosixPath('module')}\n",
      "  warn(\n",
      "/home/dm/.local/lib/python3.8/site-packages/bitsandbytes/cuda_setup/paths.py:27: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/usr/local/cuda/lib64')}\n",
      "  warn(\n",
      "/home/dm/.local/lib/python3.8/site-packages/bitsandbytes/cextension.py:48: UserWarning: The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers and GPU quantization are unavailable.\n",
      "  warn(\n"
     ]
    }
   ],
   "source": [
    "import transformers\n",
    "\n",
    "import torch\n",
    "import torch.nn.functional as F\n",
    "from torch import nn\n",
    "from torch.cuda.amp import custom_fwd, custom_bwd\n",
    "\n",
    "from bitsandbytes.functional import quantize_blockwise, dequantize_blockwise\n",
    "\n",
    "from tqdm.auto import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "640b12f7",
   "metadata": {
    "cellId": "b5qu2yccmjrp0n86a0k2"
   },
   "outputs": [],
   "source": [
    "class FrozenBNBLinear(nn.Module):\n",
    "    def __init__(self, weight, absmax, code, bias=None):\n",
    "        assert isinstance(bias, nn.Parameter) or bias is None\n",
    "        super().__init__()\n",
    "        self.out_features, self.in_features = weight.shape\n",
    "        self.register_buffer(\"weight\", weight.requires_grad_(False))\n",
    "        self.register_buffer(\"absmax\", absmax.requires_grad_(False))\n",
    "        self.register_buffer(\"code\", code.requires_grad_(False))\n",
    "        self.adapter = None\n",
    "        self.bias = bias\n",
    " \n",
    "    def forward(self, input):\n",
    "        output = DequantizeAndLinear.apply(input, self.weight, self.absmax, self.code, self.bias)\n",
    "        if self.adapter:\n",
    "            output += self.adapter(input)\n",
    "        return output\n",
    " \n",
    "    @classmethod\n",
    "    def from_linear(cls, linear: nn.Linear) -> \"FrozenBNBLinear\":\n",
    "        weights_int8, state = quantize_blockise_lowmemory(linear.weight)\n",
    "        return cls(weights_int8, *state, linear.bias)\n",
    " \n",
    "    def __repr__(self):\n",
    "        return f\"{self.__class__.__name__}({self.in_features}, {self.out_features})\"\n",
    " \n",
    " \n",
    "class DequantizeAndLinear(torch.autograd.Function): \n",
    "    @staticmethod\n",
    "    @custom_fwd\n",
    "    def forward(ctx, input: torch.Tensor, weights_quantized: torch.ByteTensor,\n",
    "                absmax: torch.FloatTensor, code: torch.FloatTensor, bias: torch.FloatTensor):\n",
    "        weights_deq = dequantize_blockwise(weights_quantized, absmax=absmax, code=code)\n",
    "        ctx.save_for_backward(input, weights_quantized, absmax, code)\n",
    "        ctx._has_bias = bias is not None\n",
    "        return F.linear(input, weights_deq, bias)\n",
    " \n",
    "    @staticmethod\n",
    "    @custom_bwd\n",
    "    def backward(ctx, grad_output: torch.Tensor):\n",
    "        assert not ctx.needs_input_grad[1] and not ctx.needs_input_grad[2] and not ctx.needs_input_grad[3]\n",
    "        input, weights_quantized, absmax, code = ctx.saved_tensors\n",
    "        # grad_output: [*batch, out_features]\n",
    "        weights_deq = dequantize_blockwise(weights_quantized, absmax=absmax, code=code)\n",
    "        grad_input = grad_output @ weights_deq\n",
    "        grad_bias = grad_output.flatten(0, -2).sum(dim=0) if ctx._has_bias else None\n",
    "        return grad_input, None, None, None, grad_bias\n",
    " \n",
    " \n",
    "class FrozenBNBEmbedding(nn.Module):\n",
    "    def __init__(self, weight, absmax, code):\n",
    "        super().__init__()\n",
    "        self.num_embeddings, self.embedding_dim = weight.shape\n",
    "        self.register_buffer(\"weight\", weight.requires_grad_(False))\n",
    "        self.register_buffer(\"absmax\", absmax.requires_grad_(False))\n",
    "        self.register_buffer(\"code\", code.requires_grad_(False))\n",
    "        self.adapter = None\n",
    " \n",
    "    def forward(self, input, **kwargs):\n",
    "        with torch.no_grad():\n",
    "            # note: both quantuized weights and input indices are *not* differentiable\n",
    "            weight_deq = dequantize_blockwise(self.weight, absmax=self.absmax, code=self.code)\n",
    "            output = F.embedding(input, weight_deq, **kwargs)\n",
    "        if self.adapter:\n",
    "            output += self.adapter(input)\n",
    "        return output \n",
    " \n",
    "    @classmethod\n",
    "    def from_embedding(cls, embedding: nn.Embedding) -> \"FrozenBNBEmbedding\":\n",
    "        weights_int8, state = quantize_blockise_lowmemory(embedding.weight)\n",
    "        return cls(weights_int8, *state)\n",
    " \n",
    "    def __repr__(self):\n",
    "        return f\"{self.__class__.__name__}({self.num_embeddings}, {self.embedding_dim})\"\n",
    " \n",
    " \n",
    "def quantize_blockise_lowmemory(matrix: torch.Tensor, chunk_size: int = 2 ** 20):\n",
    "    assert chunk_size % 4096 == 0\n",
    "    code = None\n",
    "    chunks = []\n",
    "    absmaxes = []\n",
    "    flat_tensor = matrix.view(-1)\n",
    "    for i in range((matrix.numel() - 1) // chunk_size + 1):\n",
    "        input_chunk = flat_tensor[i * chunk_size: (i + 1) * chunk_size].clone()\n",
    "        quantized_chunk, (absmax_chunk, code) = quantize_blockwise(input_chunk, code=code)\n",
    "        chunks.append(quantized_chunk)\n",
    "        absmaxes.append(absmax_chunk)\n",
    " \n",
    "    matrix_i8 = torch.cat(chunks).reshape_as(matrix)\n",
    "    absmax = torch.cat(absmaxes)\n",
    "    return matrix_i8, (absmax, code)\n",
    "\n",
    "\n",
    "def convert_to_int8(model):\n",
    "    \"\"\"Convert linear and embedding modules to 8-bit with optional adapters\"\"\"\n",
    "    for module in list(model.modules()):\n",
    "        for name, child in module.named_children():\n",
    "            if isinstance(child, nn.Linear):\n",
    "                print(name, child)\n",
    "                setattr( \n",
    "                    module,\n",
    "                    name,\n",
    "                    FrozenBNBLinear(\n",
    "                        weight=torch.zeros(child.out_features, child.in_features, dtype=torch.uint8),\n",
    "                        absmax=torch.zeros((child.weight.numel() - 1) // 4096 + 1),\n",
    "                        code=torch.zeros(256),\n",
    "                        bias=child.bias,\n",
    "                    ),\n",
    "                )\n",
    "            elif isinstance(child, nn.Embedding):\n",
    "                setattr(\n",
    "                    module,\n",
    "                    name,\n",
    "                    FrozenBNBEmbedding(\n",
    "                        weight=torch.zeros(child.num_embeddings, child.embedding_dim, dtype=torch.uint8),\n",
    "                        absmax=torch.zeros((child.weight.numel() - 1) // 4096 + 1),\n",
    "                        code=torch.zeros(256),\n",
    "                    )\n",
    "                )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e0dbb262",
   "metadata": {
    "cellId": "j9ds51fcwxxy0blcplb6"
   },
   "outputs": [],
   "source": [
    "class BloomBlock(transformers.models.bloom.modeling_bloom.BloomBlock):\n",
    "    def __init__(self, config):\n",
    "        super().__init__(config)\n",
    "        convert_to_int8(self.self_attention)\n",
    "        convert_to_int8(self.mlp)\n",
    "\n",
    "\n",
    "class BloomModel(transformers.models.bloom.modeling_bloom.BloomModel):\n",
    "    def __init__(self, config):\n",
    "        super().__init__(config)\n",
    "        convert_to_int8(self)\n",
    "        \n",
    "\n",
    "class BloomForCausalLM(transformers.models.bloom.modeling_bloom.BloomForCausalLM):\n",
    "    def __init__(self, config):\n",
    "        super().__init__(config)\n",
    "        convert_to_int8(self)\n",
    "        \n",
    "transformers.models.bloom.modeling_bloom.BloomBlock = BloomBlock"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a68bbee4",
   "metadata": {
    "cellId": "a5he2q7ulm4wkwqno10wsg"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n",
      "query_key_value Linear(in_features=4096, out_features=12288, bias=True)\n",
      "dense Linear(in_features=4096, out_features=4096, bias=True)\n",
      "dense_h_to_4h Linear(in_features=4096, out_features=16384, bias=True)\n",
      "dense_4h_to_h Linear(in_features=16384, out_features=4096, bias=True)\n"
     ]
    }
   ],
   "source": [
    "#!g1.1\n",
    "from transformers import BloomForCausalLM, AutoModel\n",
    "tokenizer = transformers.AutoTokenizer.from_pretrained(\"bigscience/bloom-7b1\",  cache_dir=\"mycache\")\n",
    "model = BloomForCausalLM.from_pretrained('bloom-8bit-v4.pt')\n",
    "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
    "model.to(device)\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11bdaf76",
   "metadata": {},
   "outputs": [],
   "source": [
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c9feef8",
   "metadata": {},
   "outputs": [],
   "source": [
    "prefix = \"\"\"\"It is a fantasy role-play game.\n",
    "\n",
    "Game Master: You are John, a wizard living in the kingdom of Larion. You have a staff and a spellbook. You finish your long journey and finally arrive at the ruin you've been looking for. You have come here searching for a mystical spellbook of great power called the book of essence. You look around and see the ancient ruins of an elf tower. The ruins have not been touched for decades. You look at the tower, and you can see a set of stone stairs that seem to lead somewhere deep inside the tower.\n",
    "Player: I walk upstairs\n",
    "Game Master: You climb up the stairs in the ruined tower. There is a door on the second floor of the tower, the door seems to be made of enchanted wood.\n",
    "Player: I ask the door if I may to come in\n",
    "Game Master: The door sighs open and you walk into the room.\n",
    "Player: I take a look around\n",
    "Game Master:\"\"\"\n",
    "\n",
    "print(end=prefix)\n",
    "past_key_values = None  # used to keep track of conversation history\n",
    "input_dict = tokenizer([prefix], return_tensors='pt', padding=False)\n",
    "\n",
    "output=\"\"\n",
    "\n",
    "with torch.inference_mode():\n",
    "    for i in range(200):\n",
    "        outputs = model.forward(**input_dict, use_cache=True, past_key_values=past_key_values)\n",
    "        last_logits = outputs.logits[0, -1]\n",
    "        \n",
    "        last_logits[last_logits.topk(k=10).indices] += 10 # other logits are now e^10 times less likely to be chosen\n",
    "\n",
    "        past_key_values = outputs.past_key_values\n",
    "        token_ix = torch.multinomial(last_logits.softmax(-1), 1).item()\n",
    "        prefix = tokenizer.decode([token_ix])\n",
    "        output = output + tokenizer.decode([token_ix])\n",
    "        if 'player' in output or 'Player' in output:\n",
    "            break\n",
    "        if 'Master' in output:\n",
    "            break\n",
    "        print(end=tokenizer.decode([token_ix]), flush=True)\n",
    "\n",
    "        input_dict = dict(input_ids=torch.tensor([[token_ix]]))\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7eba8c3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "prefix = \"\"\"\"It is a fantasy role-play game.\n",
    "\n",
    "Game Master: You are John, a wizard living in the kingdom of Larion. You have a staff and a spellbook. You finish your long journey and finally arrive at the ruin you've been looking for. You have come here searching for a mystical spellbook of great power called the book of essence. You look around and see the ancient ruins of an elf tower. The ruins have not been touched for decades. You look at the tower, and you can see a set of stone stairs that seem to lead somewhere deep inside the tower.\n",
    "Player: I walk upstairs\n",
    "Game Master: You climb up the stairs in the ruined tower. There is a door on the second floor of the tower, the door seems to be made of enchanted wood.\n",
    "Player: I ask the door if I may to come in\n",
    "Game Master: The door sighs open and you walk into the room.\n",
    "Player: I take a look around\n",
    "Game Master:\"\"\"\n",
    "\n",
    "print(end=prefix)\n",
    "past_key_values = None  # used to keep track of conversation history\n",
    "input_dict = tokenizer([prefix], return_tensors='pt', padding=False)\n",
    "\n",
    "output = \"\"\n",
    "\n",
    "with torch.inference_mode():\n",
    "    for i in range(200):\n",
    "        outputs = model.forward(**input_dict, use_cache=True, past_key_values=past_key_values)\n",
    "        last_logits = outputs.logits[0, -1]\n",
    "        \n",
    "        last_logits[last_logits.topk(k=10).indices] += 10 # other logits are now e^10 times less likely to be chosen\n",
    "\n",
    "        past_key_values = outputs.past_key_values\n",
    "        token_ix = torch.multinomial(last_logits.softmax(-1), 1).item()\n",
    "        prefix = tokenizer.decode([token_ix])\n",
    "        output = output + tokenizer.decode([token_ix])\n",
    "        if 'player' in output or 'Player' in output:\n",
    "            break\n",
    "        if 'Master' in output:\n",
    "            break\n",
    "        print(end=tokenizer.decode([token_ix]), flush=True)\n",
    "\n",
    "        input_dict = dict(input_ids=torch.tensor([[token_ix]]))\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4db07557",
   "metadata": {},
   "outputs": [],
   "source": [
    "#!g1.1\n",
    "prompt = tokenizer(\"A cat sat on a mat and\", return_tensors='pt')\n",
    "out = model.generate(**prompt, min_length=10, max_length=10, do_sample=True)\n",
    "tokenizer.decode(out[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5398feef",
   "metadata": {
    "cellId": "uero3zs1ebpefelhzioy2t",
    "execution_id": "243e4f22-9ad3-412c-98cb-6b01253531c9"
   },
   "source": [
    "### Fine-tune and save model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2fdb897",
   "metadata": {
    "cellId": "wmrjusxrcomgqirlydiuj"
   },
   "outputs": [],
   "source": [
    "#!g1.1\n",
    "def add_adapters(model, adapter_dim=16):\n",
    "    assert adapter_dim > 0\n",
    "\n",
    "    for module in model.modules():\n",
    "        if isinstance(module, FrozenBNBLinear):\n",
    "            module.adapter = nn.Sequential(\n",
    "                nn.Linear(module.in_features, adapter_dim, bias=False),\n",
    "                nn.Linear(adapter_dim, module.out_features, bias=False),\n",
    "            )\n",
    "            nn.init.zeros_(module.adapter[1].weight)\n",
    "        elif isinstance(module, FrozenBNBEmbedding):\n",
    "            module.adapter = nn.Sequential(\n",
    "                nn.Embedding(module.num_embeddings, adapter_dim),\n",
    "                nn.Linear(adapter_dim, module.embedding_dim, bias=False),\n",
    "            )\n",
    "            nn.init.zeros_(module.adapter[1].weight)\n",
    "\n",
    "add_adapters(model)\n",
    "model.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ceac0236",
   "metadata": {
    "cellId": "mzznv2rl07bt6x7aybc1h"
   },
   "outputs": [],
   "source": [
    "#!g1.1\n",
    "from datasets import load_dataset\n",
    "from bitsandbytes.optim import Adam8bit\n",
    "\n",
    "model.gradient_checkpointing_enable()\n",
    "\n",
    "wikisql = load_dataset(\"wikisql\", streaming=True)\n",
    "optimizer = Adam8bit(model.parameters(), lr=1e-5)\n",
    "\n",
    "with torch.cuda.amp.autocast():\n",
    "    for row in tqdm(wikisql['train']):\n",
    "\n",
    "        batch = tokenizer(row['question'] + row['sql']['human_readable'], truncation=True, max_length=128, return_tensors='pt')\n",
    "        batch = {k: v.cuda() for k, v in batch.items()}\n",
    "\n",
    "        out = gpt.forward(**batch,)\n",
    "\n",
    "        loss = F.cross_entropy(out.logits[:, :-1, :].flatten(0, -2), batch['input_ids'][:, 1:].flatten(),\n",
    "                               reduction='mean')\n",
    "        print(loss)\n",
    "        loss.backward()\n",
    "\n",
    "        optimizer.step()\n",
    "        optimizer.zero_grad()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d1b3b65",
   "metadata": {
    "cellId": "mirxlhno0w8wrmaaxj4u7"
   },
   "outputs": [],
   "source": [
    "#!g1.1\n",
    "model.save_pretrained('bloom-8bit-fine-tuned')"
   ]
  }
 ],
 "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"
  },
  "notebookId": "433858c6-d0c2-461b-85f3-1153722e7367",
  "notebookPath": "untitled.ipynb"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}