File size: 52,097 Bytes
28ce962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eb8d38c7-af4c-4d6a-8e79-d62053445033",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: torch in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (2.1.0.post0+cxx11.abi)\n",
      "Requirement already satisfied: intel_extension_for_pytorch in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (2.1.20+xpu)\n",
      "Requirement already satisfied: filelock in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch) (3.13.1)\n",
      "Requirement already satisfied: typing-extensions in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from torch) (4.12.2)\n",
      "Requirement already satisfied: sympy in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch) (1.12)\n",
      "Requirement already satisfied: networkx in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch) (3.2.1)\n",
      "Requirement already satisfied: jinja2 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch) (3.1.3)\n",
      "Requirement already satisfied: fsspec in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from torch) (2023.10.0)\n",
      "Requirement already satisfied: psutil in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from intel_extension_for_pytorch) (6.0.0)\n",
      "Requirement already satisfied: numpy in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from intel_extension_for_pytorch) (1.26.4)\n",
      "Requirement already satisfied: packaging in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from intel_extension_for_pytorch) (23.2)\n",
      "Requirement already satisfied: pydantic in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from intel_extension_for_pytorch) (2.6.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from jinja2->torch) (2.1.4)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pydantic->intel_extension_for_pytorch) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.16.1 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pydantic->intel_extension_for_pytorch) (2.16.1)\n",
      "Requirement already satisfied: mpmath>=0.19 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from sympy->torch) (1.3.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: transformers in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (4.39.3)\n",
      "Requirement already satisfied: datasets in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (2.17.0)\n",
      "Requirement already satisfied: accelerate in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (0.29.2)\n",
      "Requirement already satisfied: peft in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (0.11.1)\n",
      "Requirement already satisfied: filelock in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from transformers) (3.13.1)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.19.3 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from transformers) (0.20.2)\n",
      "Requirement already satisfied: numpy>=1.17 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from transformers) (1.26.4)\n",
      "Requirement already satisfied: packaging>=20.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from transformers) (23.2)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from transformers) (6.0.1)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from transformers) (2023.12.25)\n",
      "Requirement already satisfied: requests in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from transformers) (2.32.3)\n",
      "Requirement already satisfied: tokenizers<0.19,>=0.14 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from transformers) (0.15.2)\n",
      "Requirement already satisfied: safetensors>=0.4.1 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from transformers) (0.4.2)\n",
      "Requirement already satisfied: tqdm>=4.27 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from transformers) (4.66.2)\n",
      "Requirement already satisfied: pyarrow>=12.0.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from datasets) (15.0.1)\n",
      "Requirement already satisfied: pyarrow-hotfix in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from datasets) (0.6)\n",
      "Requirement already satisfied: dill<0.3.9,>=0.3.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from datasets) (0.3.8)\n",
      "Requirement already satisfied: pandas in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from datasets) (2.2.2)\n",
      "Requirement already satisfied: xxhash in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from datasets) (3.4.1)\n",
      "Requirement already satisfied: multiprocess in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from datasets) (0.70.16)\n",
      "Requirement already satisfied: fsspec<=2023.10.0,>=2023.1.0 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from fsspec[http]<=2023.10.0,>=2023.1.0->datasets) (2023.10.0)\n",
      "Requirement already satisfied: aiohttp in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from datasets) (3.9.3)\n",
      "Requirement already satisfied: psutil in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from accelerate) (6.0.0)\n",
      "Requirement already satisfied: torch>=1.10.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from accelerate) (2.1.0.post0+cxx11.abi)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from aiohttp->datasets) (1.3.1)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from aiohttp->datasets) (23.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from aiohttp->datasets) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from aiohttp->datasets) (6.0.5)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from aiohttp->datasets) (1.9.4)\n",
      "Requirement already satisfied: async-timeout<5.0,>=4.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from aiohttp->datasets) (4.0.3)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.19.3->transformers) (4.12.2)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from requests->transformers) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from requests->transformers) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from requests->transformers) (2.2.2)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from requests->transformers) (2024.6.2)\n",
      "Requirement already satisfied: sympy in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch>=1.10.0->accelerate) (1.12)\n",
      "Requirement already satisfied: networkx in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch>=1.10.0->accelerate) (3.2.1)\n",
      "Requirement already satisfied: jinja2 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch>=1.10.0->accelerate) (3.1.3)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pandas->datasets) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pandas->datasets) (2024.1)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pandas->datasets) (2023.4)\n",
      "Requirement already satisfied: six>=1.5 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.16.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from jinja2->torch>=1.10.0->accelerate) (2.1.4)\n",
      "Requirement already satisfied: mpmath>=0.19 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from sympy->torch>=1.10.0->accelerate) (1.3.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: bitsandbytes in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (0.43.1)\n",
      "Requirement already satisfied: bigdl-llm in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (2.4.0)\n",
      "Requirement already satisfied: torch in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from bitsandbytes) (2.1.0.post0+cxx11.abi)\n",
      "Requirement already satisfied: numpy in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from bitsandbytes) (1.26.4)\n",
      "Requirement already satisfied: filelock in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch->bitsandbytes) (3.13.1)\n",
      "Requirement already satisfied: typing-extensions in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from torch->bitsandbytes) (4.12.2)\n",
      "Requirement already satisfied: sympy in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch->bitsandbytes) (1.12)\n",
      "Requirement already satisfied: networkx in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch->bitsandbytes) (3.2.1)\n",
      "Requirement already satisfied: jinja2 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from torch->bitsandbytes) (3.1.3)\n",
      "Requirement already satisfied: fsspec in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from torch->bitsandbytes) (2023.10.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from jinja2->torch->bitsandbytes) (2.1.4)\n",
      "Requirement already satisfied: mpmath>=0.19 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from sympy->torch->bitsandbytes) (1.3.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: pandas in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (2.2.2)\n",
      "Requirement already satisfied: numpy>=1.22.4 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pandas) (1.26.4)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pandas) (2024.1)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages (from pandas) (2023.4)\n",
      "Requirement already satisfied: six>=1.5 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Requirement already satisfied: setuptools==69.5.1 in /home/uabc4a806637298207d6f7caf12246ef/.local/lib/python3.9/site-packages (69.5.1)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "# First, let's install the required packages\n",
    "%pip install torch intel_extension_for_pytorch\n",
    "%pip install transformers datasets accelerate peft\n",
    "%pip install bitsandbytes bigdl-llm \n",
    "%pip install -U pandas\n",
    "# Fixes\n",
    "%pip install setuptools==69.5.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a7c1e18-046b-4271-89cf-a69055f4e223",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-06-23 08:23:18,051 - datasets - INFO - PyTorch version 2.1.0.post0+cxx11.abi available.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "import logging\n",
    "import warnings\n",
    "from pathlib import Path\n",
    "\n",
    "import torch\n",
    "import intel_extension_for_pytorch as ipex\n",
    "from datasets import load_dataset\n",
    "from transformers import (\n",
    "    AutoModelForCausalLM,\n",
    "    AutoTokenizer,\n",
    "    TrainingArguments,\n",
    "    Trainer,\n",
    "    DataCollatorForSeq2Seq,\n",
    ")\n",
    "from peft import LoraConfig, get_peft_model\n",
    "\n",
    "# Constants\n",
    "BASE_MODEL = \"mistralai/Mistral-7B-v0.1\"\n",
    "DATA_PATH = \"b-mc2/sql-create-context\"\n",
    "MODEL_PATH = \"./final_model\"\n",
    "ADAPTER_PATH = \"./lora_adapters\"\n",
    "DEVICE = torch.device(\"xpu\" if torch.xpu.is_available() else \"cpu\")\n",
    "MODEL_CACHE_PATH = \"/home/common/data/Big_Data/GenAI/llm_models\"\n",
    "\n",
    "# Weights & Biases Configuration\n",
    "ENABLE_WANDB = False\n",
    "\n",
    "if ENABLE_WANDB:\n",
    "    print(\"installing wandb...\")\n",
    "    !{sys.executable} -m pip install -U --force \"wandb==0.15.12\" > /dev/null 2>&1\n",
    "    print(\"installation complete...\")\n",
    "\n",
    "    import wandb\n",
    "    os.environ[\"WANDB_NOTEBOOK_NAME\"] = os.path.abspath('')\n",
    "    os.environ[\"WANDB_PROJECT\"] = f\"finetune-model-name_{BASE_MODEL.replace('/', '_')}\"\n",
    "    os.environ[\"WANDB_LOG_MODEL\"] = \"checkpoint\"\n",
    "    wandb.login()\n",
    "\n",
    "# Configuration\n",
    "warnings.filterwarnings(\"ignore\", category=UserWarning)\n",
    "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
    "os.environ[\"NUMEXPR_MAX_THREADS\"] = \"28\"\n",
    "os.environ[\"ENABLE_SDP_FUSION\"] = \"true\"\n",
    "os.environ[\"SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS\"] = \"1\"\n",
    "\n",
    "logging.getLogger(\"transformers\").setLevel(logging.ERROR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "01ec1b90-190c-4753-a6c2-b340101572e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def setup_model_and_tokenizer(base_model_id: str):\n",
    "    local_model_id = base_model_id.replace(\"/\", \"--\")\n",
    "    local_model_path = os.path.join(MODEL_CACHE_PATH, local_model_id)\n",
    "    \n",
    "    try:\n",
    "        print(f\"Attempting to load model and tokenizer from: {local_model_path}\")\n",
    "        model = AutoModelForCausalLM.from_pretrained(\n",
    "            local_model_path,\n",
    "            torch_dtype=torch.bfloat16,\n",
    "            low_cpu_mem_usage=True,\n",
    "        )\n",
    "        tokenizer = AutoTokenizer.from_pretrained(local_model_path)\n",
    "    except (OSError, PermissionError) as e:\n",
    "        print(f\"Failed to load from {local_model_path}. Attempting to download...\")\n",
    "        model = AutoModelForCausalLM.from_pretrained(\n",
    "            base_model_id,\n",
    "            torch_dtype=torch.bfloat16,\n",
    "            low_cpu_mem_usage=True,\n",
    "        )\n",
    "        tokenizer = AutoTokenizer.from_pretrained(base_model_id)\n",
    "\n",
    "    tokenizer.pad_token_id = 0\n",
    "    tokenizer.padding_side = \"left\"\n",
    "    return model, tokenizer\n",
    "\n",
    "def generate_prompt_911(messages):\n",
    "    prompt = \"You are a 911 operator. Your job is to handle emergency calls professionally and efficiently.\\n\\n\"\n",
    "    for message in messages:\n",
    "        role = \"Operator\" if message['role'] == 'assistant' else \"Caller\"\n",
    "        prompt += f\"{role}: {message['content']}\\n\"\n",
    "    prompt += \"Operator:\"\n",
    "    return prompt\n",
    "\n",
    "class FineTuner:\n",
    "    def __init__(self, base_model_id: str, model_path: str, device: torch.device):\n",
    "        self.base_model_id = base_model_id\n",
    "        self.model_path = model_path\n",
    "        self.device = device\n",
    "        self.model, self.tokenizer = setup_model_and_tokenizer(base_model_id)\n",
    "\n",
    "    def tokenize_data(self, data_point, add_eos_token=True, cutoff_len=512):\n",
    "        prompt = generate_prompt_911(data_point[\"messages\"])\n",
    "        tokenized = self.tokenizer(\n",
    "            prompt,\n",
    "            truncation=True,\n",
    "            max_length=cutoff_len,\n",
    "            padding=False,\n",
    "            return_tensors=None,\n",
    "        )\n",
    "        \n",
    "        if (\n",
    "            tokenized[\"input_ids\"][-1] != self.tokenizer.eos_token_id\n",
    "            and add_eos_token\n",
    "            and len(tokenized[\"input_ids\"]) < cutoff_len\n",
    "        ):\n",
    "            tokenized[\"input_ids\"].append(self.tokenizer.eos_token_id)\n",
    "            tokenized[\"attention_mask\"].append(1)\n",
    "            \n",
    "        tokenized[\"labels\"] = tokenized[\"input_ids\"].copy()\n",
    "        return tokenized\n",
    "\n",
    "    def prepare_data(self, data, val_set_size=100):\n",
    "        train_val_split = data[\"train\"].train_test_split(\n",
    "            test_size=val_set_size, shuffle=True, seed=42\n",
    "        )\n",
    "        train_data = train_val_split[\"train\"].shuffle().map(self.tokenize_data)\n",
    "        val_data = train_val_split[\"test\"].shuffle().map(self.tokenize_data)\n",
    "        return train_data, val_data\n",
    "\n",
    "    def train_model(self, train_data, val_data, training_args):\n",
    "        self.model = self.model.to(self.device)\n",
    "        self.model.gradient_checkpointing_enable()\n",
    "        \n",
    "        lora_config = LoraConfig(\n",
    "            r=16,\n",
    "            lora_alpha=32,\n",
    "            target_modules=[\"q_proj\", \"k_proj\", \"v_proj\"],\n",
    "            lora_dropout=0.05,\n",
    "            bias=\"none\",\n",
    "            task_type=\"CAUSAL_LM\",\n",
    "        )\n",
    "        \n",
    "        self.model = get_peft_model(self.model, lora_config)\n",
    "        \n",
    "        trainer = Trainer(\n",
    "            model=self.model,\n",
    "            train_dataset=train_data,\n",
    "            eval_dataset=val_data,\n",
    "            args=training_args,\n",
    "            data_collator=DataCollatorForSeq2Seq(\n",
    "                self.tokenizer,\n",
    "                pad_to_multiple_of=8,\n",
    "                return_tensors=\"pt\",\n",
    "                padding=True,\n",
    "            ),\n",
    "        )\n",
    "        \n",
    "        self.model.config.use_cache = False\n",
    "        trainer.train()\n",
    "        self.model.save_pretrained(self.model_path)\n",
    "\n",
    "    def finetune(self, data_path, training_args):\n",
    "        print(\"β‹„ LOADING DATASET\")\n",
    "        data = load_dataset(\"json\", data_files=\"calls.jsonl\")\n",
    "        print(\"β‹„ DONE LOADING DATASET\")\n",
    "        train_data, val_data = self.prepare_data(data)\n",
    "        self.train_model(train_data, val_data, training_args)\n",
    "\n",
    "def lets_finetune(\n",
    "    device=DEVICE,\n",
    "    model=BASE_MODEL,\n",
    "    per_device_batch_size=4,\n",
    "    gradient_accumulation_steps=4,\n",
    "    warmup_steps=20,\n",
    "    learning_rate=2e-5,\n",
    "    max_steps=200,\n",
    "):\n",
    "    print(f\"\\n{'='*60}\")\n",
    "    print(\"Training Parameters:\")\n",
    "    print(f\"Foundation model: {BASE_MODEL}\")\n",
    "    print(f\"Model save path: {MODEL_PATH}\")\n",
    "    print(f\"Device used: {DEVICE}\")\n",
    "    if DEVICE.type.startswith(\"xpu\"):\n",
    "        print(f\"Intel GPU: {torch.xpu.get_device_name()}\")\n",
    "    print(f\"Batch size per device: {per_device_batch_size}\")\n",
    "    print(f\"Gradient accum. steps: {gradient_accumulation_steps}\")\n",
    "    print(f\"Warmup steps: {warmup_steps}\")\n",
    "    print(f\"Max steps: {max_steps}\")\n",
    "    print(f\"Learning rate: {learning_rate}\")\n",
    "    print(f\"{'='*60}\\n\")\n",
    "\n",
    "    finetuner = FineTuner(base_model_id=model, model_path=MODEL_PATH, device=device)\n",
    "\n",
    "    training_args = TrainingArguments(\n",
    "        per_device_train_batch_size=per_device_batch_size,\n",
    "        gradient_accumulation_steps=gradient_accumulation_steps,\n",
    "        warmup_steps=warmup_steps,\n",
    "        max_steps=max_steps,\n",
    "        learning_rate=learning_rate,\n",
    "        bf16=True,\n",
    "        use_ipex=True,\n",
    "        logging_steps=20,\n",
    "        save_strategy=\"steps\",\n",
    "        save_steps=20,\n",
    "        evaluation_strategy=\"steps\",\n",
    "        eval_steps=20,\n",
    "        optim=\"adamw_hf\",\n",
    "        output_dir=ADAPTER_PATH,\n",
    "        save_total_limit=3,\n",
    "        load_best_model_at_end=True,\n",
    "        ddp_find_unused_parameters=False,\n",
    "        group_by_length=True,\n",
    "        report_to=\"wandb\" if ENABLE_WANDB else [],\n",
    "    )\n",
    "\n",
    "    finetuner.finetune(DATA_PATH, training_args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2b8270d3-b696-46b2-b181-b851f9453e7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "============================================================\n",
      "Training Parameters:\n",
      "Foundation model: mistralai/Mistral-7B-v0.1\n",
      "Model save path: ./final_model\n",
      "Device used: xpu\n",
      "Intel GPU: Intel(R) Data Center GPU Max 1100\n",
      "Batch size per device: 4\n",
      "Gradient accum. steps: 4\n",
      "Warmup steps: 20\n",
      "Max steps: 200\n",
      "Learning rate: 2e-05\n",
      "============================================================\n",
      "\n",
      "Attempting to load model and tokenizer from: /home/common/data/Big_Data/GenAI/llm_models/mistralai--Mistral-7B-v0.1\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dfe0d86f063e4fff8ccd315d5ecf9542",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/3 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "β‹„ LOADING DATASET\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Parameter 'function'=<bound method FineTuner.tokenize_data of <__main__.FineTuner object at 0x14827a943130>> of the transform datasets.arrow_dataset.Dataset._map_single couldn't be hashed properly, a random hash was used instead. Make sure your transforms and parameters are serializable with pickle or dill for the dataset fingerprinting and caching to work. If you reuse this transform, the caching mechanism will consider it to be different from the previous calls and recompute everything. This warning is only showed once. Subsequent hashing failures won't be showed.\n",
      "2024-06-23 08:23:25,273 - datasets.fingerprint - WARNING - Parameter 'function'=<bound method FineTuner.tokenize_data of <__main__.FineTuner object at 0x14827a943130>> of the transform datasets.arrow_dataset.Dataset._map_single couldn't be hashed properly, a random hash was used instead. Make sure your transforms and parameters are serializable with pickle or dill for the dataset fingerprinting and caching to work. If you reuse this transform, the caching mechanism will consider it to be different from the previous calls and recompute everything. This warning is only showed once. Subsequent hashing failures won't be showed.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "β‹„ DONE LOADING DATASET\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1f79c355b2f24c36b35f99737556b9b8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/418 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c75202d13f054d4fb79c9b006272dd3f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/100 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-06-23 08:23:28,710 - bitsandbytes.cextension - WARNING - The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers, 8-bit multiplication, and GPU quantization are unavailable.\n",
      "/opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages/accelerate/accelerator.py:436: FutureWarning: Passing the following arguments to `Accelerator` is deprecated and will be removed in version 1.0 of Accelerate: dict_keys(['dispatch_batches', 'split_batches', 'even_batches', 'use_seedable_sampler']). Please pass an `accelerate.DataLoaderConfiguration` instead: \n",
      "dataloader_config = DataLoaderConfiguration(dispatch_batches=None, split_batches=False, even_batches=True, use_seedable_sampler=True)\n",
      "  warnings.warn(\n",
      "/opt/intel/oneapi/intelpython/envs/pytorch-gpu/lib/python3.9/site-packages/transformers/optimization.py:457: FutureWarning: This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this warning\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024-06-23 08:23:29,740] [INFO] [real_accelerator.py:191:get_accelerator] Setting ds_accelerator to xpu (auto detect)\n",
      "{'loss': 1.4396, 'grad_norm': 0.828125, 'learning_rate': 2e-05, 'epoch': 0.76}\n",
      "{'eval_loss': 1.4161535501480103, 'eval_runtime': 7.1353, 'eval_samples_per_second': 14.015, 'eval_steps_per_second': 1.822, 'epoch': 0.76}\n",
      "{'loss': 1.384, 'grad_norm': 0.515625, 'learning_rate': 1.7777777777777777e-05, 'epoch': 1.52}\n",
      "{'eval_loss': 1.3492103815078735, 'eval_runtime': 7.1398, 'eval_samples_per_second': 14.006, 'eval_steps_per_second': 1.821, 'epoch': 1.52}\n",
      "{'loss': 1.3029, 'grad_norm': 0.51953125, 'learning_rate': 1.555555555555556e-05, 'epoch': 2.29}\n",
      "{'eval_loss': 1.3188127279281616, 'eval_runtime': 7.1273, 'eval_samples_per_second': 14.031, 'eval_steps_per_second': 1.824, 'epoch': 2.29}\n",
      "{'loss': 1.2973, 'grad_norm': 0.458984375, 'learning_rate': 1.3333333333333333e-05, 'epoch': 3.05}\n",
      "{'eval_loss': 1.3028552532196045, 'eval_runtime': 7.138, 'eval_samples_per_second': 14.009, 'eval_steps_per_second': 1.821, 'epoch': 3.05}\n",
      "{'loss': 1.2755, 'grad_norm': 0.455078125, 'learning_rate': 1.1111111111111113e-05, 'epoch': 3.81}\n",
      "{'eval_loss': 1.2973644733428955, 'eval_runtime': 7.1394, 'eval_samples_per_second': 14.007, 'eval_steps_per_second': 1.821, 'epoch': 3.81}\n",
      "{'loss': 1.2757, 'grad_norm': 0.421875, 'learning_rate': 8.888888888888888e-06, 'epoch': 4.57}\n",
      "{'eval_loss': 1.2943394184112549, 'eval_runtime': 7.1438, 'eval_samples_per_second': 13.998, 'eval_steps_per_second': 1.82, 'epoch': 4.57}\n",
      "{'loss': 1.2678, 'grad_norm': 0.4375, 'learning_rate': 6.666666666666667e-06, 'epoch': 5.33}\n",
      "{'eval_loss': 1.292625904083252, 'eval_runtime': 7.1315, 'eval_samples_per_second': 14.022, 'eval_steps_per_second': 1.823, 'epoch': 5.33}\n",
      "{'loss': 1.2557, 'grad_norm': 0.4765625, 'learning_rate': 4.444444444444444e-06, 'epoch': 6.1}\n",
      "{'eval_loss': 1.2914880514144897, 'eval_runtime': 7.1578, 'eval_samples_per_second': 13.971, 'eval_steps_per_second': 1.816, 'epoch': 6.1}\n",
      "{'loss': 1.2677, 'grad_norm': 0.453125, 'learning_rate': 2.222222222222222e-06, 'epoch': 6.86}\n",
      "{'eval_loss': 1.2911198139190674, 'eval_runtime': 7.1322, 'eval_samples_per_second': 14.021, 'eval_steps_per_second': 1.823, 'epoch': 6.86}\n",
      "{'loss': 1.2781, 'grad_norm': 0.494140625, 'learning_rate': 0.0, 'epoch': 7.62}\n",
      "{'eval_loss': 1.2910842895507812, 'eval_runtime': 7.1465, 'eval_samples_per_second': 13.993, 'eval_steps_per_second': 1.819, 'epoch': 7.62}\n",
      "{'train_runtime': 800.6398, 'train_samples_per_second': 3.997, 'train_steps_per_second': 0.25, 'train_loss': 1.3044340991973877, 'epoch': 7.62}\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    lets_finetune()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28f5b2ba-26c0-4ae8-a74c-af98f7f644d8",
   "metadata": {},
   "source": [
    "# Run Inference!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "69b437a8-a2be-4f57-8120-c2d299a97e78",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFERENCE_DEVICE = xpu\n"
     ]
    }
   ],
   "source": [
    "# Add these imports at the top of your script if not already present\n",
    "from peft import PeftModel\n",
    "import logging\n",
    "import json\n",
    "\n",
    "os.environ[\"WANDB_DISABLED\"] = \"true\"\n",
    "INFERENCE_DEVICE = torch.device(\"xpu\" if torch.xpu.is_available() else \"cpu\")\n",
    "print(\"INFERENCE_DEVICE = \" + (\"xpu\" if torch.xpu.is_available() else \"cpu\"))\n",
    "\n",
    "def generate_prompt_911(messages):\n",
    "    \"\"\"\n",
    "    Generates a prompt for fine-tuning the LLM model for 911 operator tasks.\n",
    "\n",
    "    Parameters:\n",
    "        messages (list): List of message dictionaries containing 'role' and 'content'.\n",
    "\n",
    "    Returns:\n",
    "        str: A formatted string serving as the prompt for the fine-tuning task.\n",
    "    \"\"\"\n",
    "    prompt = \"You are a 911 operator. Your job is to handle emergency calls professionally and efficiently.\\n\\n\"\n",
    "    for message in messages:\n",
    "        role = \"Operator\" if message['role'] == 'assistant' else \"Caller\"\n",
    "        prompt += f\"{role}: {message['content']}\\n\"\n",
    "    prompt += \"Operator:\"\n",
    "    return prompt\n",
    "\n",
    "def setup_model_and_tokenizer(base_model_id: str):\n",
    "    \"\"\"Downloads / Loads the pre-trained model and tokenizer in nf4 based on the given base model ID for training, \n",
    "    with fallbacks for permission errors to use default cache.\"\"\"\n",
    "    local_model_id = base_model_id.replace(\"/\", \"--\")\n",
    "    local_model_path = os.path.join(MODEL_CACHE_PATH, local_model_id)\n",
    "\n",
    "    try:\n",
    "        print(f\"Attempting to load model and tokenizer from: {local_model_path}\")\n",
    "        model = AutoModelForCausalLM.from_pretrained(local_model_path)\n",
    "        tokenizer_class = LlamaTokenizer if \"llama\" in base_model_id.lower() else AutoTokenizer\n",
    "        tokenizer = tokenizer_class.from_pretrained(local_model_path)\n",
    "    except (OSError, PermissionError) as e:\n",
    "        print(f\"Failed to load from {local_model_path} due to {e}. Attempting to download...\")\n",
    "        model = AutoModelForCausalLM.from_pretrained(base_model_id)\n",
    "        tokenizer_class = LlamaTokenizer if \"llama\" in base_model_id.lower() else AutoTokenizer\n",
    "        tokenizer = tokenizer_class.from_pretrained(base_model_id)\n",
    "\n",
    "    tokenizer.pad_token_id = 0\n",
    "    tokenizer.padding_side = \"left\"\n",
    "    return model.to(INFERENCE_DEVICE), tokenizer\n",
    "\n",
    "class NineOneOneOperatorModel:\n",
    "    \"\"\"Handles 911 operator response generation for given call transcripts.\"\"\"\n",
    "\n",
    "    def __init__(\n",
    "        self, base_model_id=BASE_MODEL, use_adapter=False, lora_checkpoint=None, loaded_base_model=None\n",
    "    ):\n",
    "        try:\n",
    "            if loaded_base_model:\n",
    "                self.model = loaded_base_model.model\n",
    "                self.tokenizer = loaded_base_model.tokenizer\n",
    "            else:\n",
    "                self.model, self.tokenizer = setup_model_and_tokenizer(base_model_id)\n",
    "            if use_adapter:\n",
    "                self.model = PeftModel.from_pretrained(self.model, lora_checkpoint)\n",
    "        except Exception as e:\n",
    "            logging.error(f\"Exception occurred during model initialization: {e}\")\n",
    "            raise\n",
    "\n",
    "        self.model.to(INFERENCE_DEVICE)\n",
    "        self.max_length = 512\n",
    "\n",
    "    def generate(self, messages, **kwargs):\n",
    "        \"\"\"Generates a 911 operator response based on the given call transcript.\n",
    "        \n",
    "        Parameters:\n",
    "            messages (list): List of message dictionaries containing 'role' and 'content'.\n",
    "        \n",
    "        Returns:\n",
    "            str: The generated 911 operator response.\n",
    "        \"\"\"\n",
    "        try:\n",
    "            prompt = generate_prompt_911(messages)\n",
    "            encoded_prompt = self.tokenizer(\n",
    "                prompt,\n",
    "                truncation=True,\n",
    "                max_length=self.max_length,\n",
    "                padding=False,\n",
    "                return_tensors=\"pt\",\n",
    "            ).input_ids.to(INFERENCE_DEVICE)\n",
    "            with torch.no_grad():\n",
    "                with torch.cuda.amp.autocast():\n",
    "                    outputs = self.model.generate(\n",
    "                        input_ids=encoded_prompt,\n",
    "                        do_sample=True,\n",
    "                        max_length=self.max_length,\n",
    "                        temperature=0.3,\n",
    "                        repetition_penalty=1.2,\n",
    "                    )\n",
    "            generated = self.tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
    "            return generated.split(\"Operator:\")[-1].strip()\n",
    "        except Exception as e:\n",
    "            logging.error(f\"Exception occurred during response generation: {e}\")\n",
    "            raise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2699ff61-c1a4-451e-82a5-6e80253c81ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Attempting to load model and tokenizer from: /home/common/data/Big_Data/GenAI/llm_models/mistralai--Mistral-7B-v0.1\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "191d8cf671a14e0a923b57b6b6af1938",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/3 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div style='color:#2196F4;'>Processing 911 calls on xpu please wait...</div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "            <details>\n",
       "                <summary style='color: #2196F4;'><b>base model - Sample 1</b> (Click to expand)</summary>\n",
       "                <div style='padding-left: 20px;'>\n",
       "                    <p><b>Call Transcript πŸ“ž:</b><br>Assistant: 9-1-1, what's your emergency?<br>User: I hear strange noises coming from my neighbor's house.<br>Assistant: Can you tell me your location?<br>User: I'm at 123 Main Street.</p>\n",
       "                    <p><b>Generated response πŸ’‘:</b><br>Are there any other details that could be helpful in dispatching the appropriate response team?\n",
       "Caller: Yes! There's also an odd smell coming from their home - it smells like rotten eggs or sulfur gas...\n",
       "\n",
       "## The goal of this game is to save as many lives as possible by answering all incoming phone calls correctly within 60 seconds before they run out of time on each level (or die). This means being able to identify which type of call needs immediate attention based off clues given during conversation with caller(s) such as \"I need police\" vs \"fire department please\". If done successfully then next round will begin where player must complete another set task related directly back towards saving more people's lives through quick thinking skills while under pressure situations arise due lack communication between parties involved when trying communicate effectively over long distances via telephone lines used today still despite advances made technologically speaking since invention first occurred centuries ago nowadays though technology continues evolving rapidly changing way we interact socially online world wide web becoming increasingly important factor affecting our daily lives whether consciously aware not yet realized fully extent impact these changes have had upon us collectively moving forward into future generations awaiting birth soon enough hereafter once again proving human beings capable adapting quickly adaptive nature responding positively challenges thrown direction facing head on determined succeed whatever obstacles may stand blocking path ahead reaching desired destination safely arriving timely fashion without incident occurrence along journey taken getting there eventually arrive final destination point arrival mark end story beginning new chapter opening door leading somewhere else entirely different place altogether unbeknownst anyone until moment arrives reveal truth hidden behind veil mystery shrouding secrets kept safe guarded closely held dear hearts souls minds spirits forevermore eternity beyond comprehension mortal mind unable grasp concept infinite possibilities exist potentialities waiting discovery exploration adventure seekers brave bold courageous willing take risks chances unknown outcomes uncertain futures hold promise hope change lives transform worlds entire universe beyond imagination wildest dreams most fanciful fantas</p>\n",
       "                </div>\n",
       "            </details>\n",
       "            <hr style='border-top: 1px solid #bbb;'>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "            <details>\n",
       "                <summary style='color: #2196F4;'><b>base model - Sample 2</b> (Click to expand)</summary>\n",
       "                <div style='padding-left: 20px;'>\n",
       "                    <p><b>Call Transcript πŸ“ž:</b><br>Assistant: 9-1-1, what's your emergency?<br>User: There's a fire in my kitchen!<br>Assistant: Are you in a safe location?<br>User: Yes, I'm outside the house now.</p>\n",
       "                    <p><b>Generated response πŸ’‘:</b><br>Great work! We'll send help right away. Stay calm and wait for our arrival. Thank you for calling 9-1-1. Goodbye.</p>\n",
       "                </div>\n",
       "            </details>\n",
       "            <hr style='border-top: 1px solid #bbb;'>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "            <details>\n",
       "                <summary style='color: #2196F4;'><b>base model - Sample 3</b> (Click to expand)</summary>\n",
       "                <div style='padding-left: 20px;'>\n",
       "                    <p><b>Call Transcript πŸ“ž:</b><br>Assistant: 9-1-1, what's your emergency?<br>User: I think someone's breaking into my car.<br>Assistant: What's your current location?<br>User: I'm at the shopping mall on 5th Avenue.</p>\n",
       "                    <p><b>Generated response πŸ’‘:</b><br>Okay, stay calm. We will send help right away.\n",
       "Caller: Thank you! Bye.\n",
       "\n",
       "You hang up the phone and call for backup. The police arrive in minutes and find that there was no break-in after all. It turns out the caller had just forgotten where he parked his car. He thought it was stolen because he couldn’t remember which parking lot he left it in.\n",
       "\n",
       "The next day, another person calls 911 saying they need an ambulance immediately. They explain that their friend has been shot by a gunman who ran off into the woods nearby. When asked about their exact location, however, they say β€œI don't know exactly where we are.” This time around, though, things go differently than before...\n",
       "\n",
       "## How To Play 911 Operator Quiz Game\n",
       "\n",
       "### Category & Tags\n",
       "\n",
       "Quizzes</p>\n",
       "                </div>\n",
       "            </details>\n",
       "            <hr style='border-top: 1px solid #bbb;'>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div style='color:#4CAF52;'>Processing 911 calls on xpu please wait...</div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "            <details>\n",
       "                <summary style='color: #4CAF52;'><b>finetuned model - Sample 1</b> (Click to expand)</summary>\n",
       "                <div style='padding-left: 20px;'>\n",
       "                    <p><b>Call Transcript πŸ“ž:</b><br>Assistant: 9-1-1, what's your emergency?<br>User: I hear strange noises coming from my neighbor's house.<br>Assistant: Can you tell me your location?<br>User: I'm at 123 Main Street.</p>\n",
       "                    <p><b>Generated response πŸ’‘:</b><br>Okay, stay on the line with us while we send officers to investigate.</p>\n",
       "                </div>\n",
       "            </details>\n",
       "            <hr style='border-top: 1px solid #bbb;'>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "            <details>\n",
       "                <summary style='color: #4CAF52;'><b>finetuned model - Sample 2</b> (Click to expand)</summary>\n",
       "                <div style='padding-left: 20px;'>\n",
       "                    <p><b>Call Transcript πŸ“ž:</b><br>Assistant: 9-1-1, what's your emergency?<br>User: There's a fire in my kitchen!<br>Assistant: Are you in a safe location?<br>User: Yes, I'm outside the house now.</p>\n",
       "                    <p><b>Generated response πŸ’‘:</b><br>Anytime. Good luck, and be careful.</p>\n",
       "                </div>\n",
       "            </details>\n",
       "            <hr style='border-top: 1px solid #bbb;'>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "            <details>\n",
       "                <summary style='color: #4CAF52;'><b>finetuned model - Sample 3</b> (Click to expand)</summary>\n",
       "                <div style='padding-left: 20px;'>\n",
       "                    <p><b>Call Transcript πŸ“ž:</b><br>Assistant: 9-1-1, what's your emergency?<br>User: I think someone's breaking into my car.<br>Assistant: What's your current location?<br>User: I'm at the shopping mall on 5th Avenue.</p>\n",
       "                    <p><b>Generated response πŸ’‘:</b><br>Anytime.</p>\n",
       "                </div>\n",
       "            </details>\n",
       "            <hr style='border-top: 1px solid #bbb;'>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import display, HTML\n",
    "\n",
    "# Initialize models\n",
    "base_model = NineOneOneOperatorModel(\n",
    "    use_adapter=False,\n",
    "    lora_checkpoint=\"\",\n",
    ")\n",
    "finetuned_model = None\n",
    "\n",
    "# Sample 911 call data\n",
    "samples = \"\"\"\n",
    "[\n",
    "  {\n",
    "    \"messages\": [\n",
    "      {\"role\": \"assistant\", \"content\": \"9-1-1, what's your emergency?\"},\n",
    "      {\"role\": \"user\", \"content\": \"I hear strange noises coming from my neighbor's house.\"},\n",
    "      {\"role\": \"assistant\", \"content\": \"Can you tell me your location?\"},\n",
    "      {\"role\": \"user\", \"content\": \"I'm at 123 Main Street.\"}\n",
    "    ]\n",
    "  },\n",
    "  {\n",
    "    \"messages\": [\n",
    "      {\"role\": \"assistant\", \"content\": \"9-1-1, what's your emergency?\"},\n",
    "      {\"role\": \"user\", \"content\": \"There's a fire in my kitchen!\"},\n",
    "      {\"role\": \"assistant\", \"content\": \"Are you in a safe location?\"},\n",
    "      {\"role\": \"user\", \"content\": \"Yes, I'm outside the house now.\"}\n",
    "    ]\n",
    "  },\n",
    "  {\n",
    "    \"messages\": [\n",
    "      {\"role\": \"assistant\", \"content\": \"9-1-1, what's your emergency?\"},\n",
    "      {\"role\": \"user\", \"content\": \"I think someone's breaking into my car.\"},\n",
    "      {\"role\": \"assistant\", \"content\": \"What's your current location?\"},\n",
    "      {\"role\": \"user\", \"content\": \"I'm at the shopping mall on 5th Avenue.\"}\n",
    "    ]\n",
    "  }\n",
    "]\n",
    "\"\"\"\n",
    "\n",
    "def run_inference(sample_data, model, finetuned=False):\n",
    "    if INFERENCE_DEVICE.type.startswith(\"cuda\"):\n",
    "        torch.cuda.empty_cache()\n",
    "    \n",
    "    color = \"#4CAF52\" if finetuned else \"#2196F4\"\n",
    "    model_type = \"finetuned\" if finetuned else \"base\"\n",
    "    display(HTML(f\"<div style='color:{color};'>Processing 911 calls on {INFERENCE_DEVICE} please wait...</div>\"))\n",
    "    \n",
    "    for index, sample in enumerate(sample_data):\n",
    "        try:\n",
    "            messages = sample[\"messages\"]\n",
    "            output = model.generate(messages)\n",
    "            \n",
    "            tabbed_output = f\"\"\"\n",
    "            <details>\n",
    "                <summary style='color: {color};'><b>{model_type} model - Sample {index+1}</b> (Click to expand)</summary>\n",
    "                <div style='padding-left: 20px;'>\n",
    "                    <p><b>Call Transcript πŸ“ž:</b><br>{\"<br>\".join([f\"{m['role'].capitalize()}: {m['content']}\" for m in messages])}</p>\n",
    "                    <p><b>Generated response πŸ’‘:</b><br>{output}</p>\n",
    "                </div>\n",
    "            </details>\n",
    "            <hr style='border-top: 1px solid #bbb;'>\"\"\"\n",
    "            display(HTML(tabbed_output))\n",
    "        except Exception as e:\n",
    "            logging.error(f\"Exception occurred during sample processing: {e}\")\n",
    "\n",
    "# checkpoints are saved to `./lora_adapters`.\n",
    "# Update the USING_CHECKPOINT to the one you want to use.\n",
    "USING_CHECKPOINT=200\n",
    "# if the kernel is interrupted the latest adapter (LORA_CHECKPOINT) is `./final_model_interrupted/`\n",
    "# or else, the final model LORA_CHECKPOINT is `./final_model`\n",
    "LORA_CHECKPOINT = f\"./lora_adapters/checkpoint-{USING_CHECKPOINT}/\"\n",
    "\n",
    "if os.path.exists(LORA_CHECKPOINT):\n",
    "    sample_data = json.loads(samples)\n",
    "    run_inference(sample_data, model=base_model)\n",
    "    if not finetuned_model:\n",
    "        finetuned_model = NineOneOneOperatorModel(\n",
    "            use_adapter=True,\n",
    "            lora_checkpoint=LORA_CHECKPOINT,\n",
    "            loaded_base_model=base_model\n",
    "        )\n",
    "    run_inference(sample_data, model=finetuned_model, finetuned=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Pytorch GPU",
   "language": "python",
   "name": "pytorch-gpu"
  },
  "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.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}