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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:144533

1
00:00:02.035 --> 00:00:02.936
In this lesson,

2
00:00:02.936 --> 00:00:05.872
you will leverage the tools
that you have just built in order

3
00:00:05.872 --> 00:00:09.976
to create your own quantizer to quantize
any model in eight-bit precision.

4
00:00:10.176 --> 00:00:14.080
This quantizer is modality agnostic,
meaning you can apply it on

5
00:00:14.080 --> 00:00:17.817
vision,
audio texts, and even multimodal models.

6
00:00:17.984 --> 00:00:20.987
Let's get started
and quantize some models.

7
00:00:23.089 --> 00:00:23.723
In this lesson,

8
00:00:23.723 --> 00:00:28.194
we will learn together about how to make
our own quantizer to quantize any model

9
00:00:28.194 --> 00:00:31.197
in eight-bit precision
using the per channel linear

10
00:00:31.197 --> 00:00:34.401
quantization scheme that you have seen
in the lesson previously.

11
00:00:34.634 --> 00:00:37.904
So for that, we'll break down our project
into multiple sub steps.

12
00:00:37.971 --> 00:00:42.971
So we'll first deep dive into creating a
W8A16 linear layer class.

13
00:00:44.511 --> 00:00:48.381
And "W8" stands for eight-bit weights
and "A16"

14
00:00:48.715 --> 00:00:51.384
simply stands for 16 bits activations.

15
00:00:51.384 --> 00:00:54.487
We use this class to store
eight-bit weights and scales,

16
00:00:54.487 --> 00:00:57.557
as you have seen previously
on the previous lesson,

17
00:00:57.791 --> 00:01:01.628
and then we will see how we can replace
all instances

18
00:01:01.628 --> 00:01:04.964
of torch nn layers
with that new class.

19
00:01:04.964 --> 00:01:08.668
And then we will build a quantizer
to quantize our model end to end.

20
00:01:08.668 --> 00:01:12.072
And we will test our quantizer
on many scenarios.

21
00:01:12.072 --> 00:01:16.109
And we will study the impact of the eight
bit quantization on different models.

22
00:01:16.643 --> 00:01:19.979
So let's start with the first subtask
that we have defined before.

23
00:01:20.380 --> 00:01:24.350
So building the W8A16
linear layer class.

24
00:01:24.717 --> 00:01:29.422
So for this task we'll also break it down
into different multiple subtasks.

25
00:01:29.622 --> 00:01:33.293
First of all you will build a forward
method called W8A16 forward

26
00:01:33.426 --> 00:01:37.597
that will take as input eight-bit weights,

27
00:01:38.765 --> 00:01:41.367
16 bit inputs,

28
00:01:41.367 --> 00:01:44.337
scales, and optional bias.

29
00:01:44.337 --> 00:01:46.706
Once you have built this method,
the idea is to call

30
00:01:46.706 --> 00:01:49.809
that method inside the linear
layers forward pass

31
00:01:50.844 --> 00:01:54.848
and pass
the 8 bit weights of the linear layer,

32
00:01:55.014 --> 00:01:59.752
input, the scales that are stored inside
the layer, and the optional bias as well.

33
00:02:00.487 --> 00:02:01.921
So let's get started.

34
00:02:01.921 --> 00:02:05.925
So what the W8A16 forward
method will do under the hood

35
00:02:06.126 --> 00:02:09.229
is to first cast the eight bit weights

36
00:02:09.229 --> 00:02:12.232
into the same data type as the input.

37
00:02:12.465 --> 00:02:16.336
So for example, in the case
the input is in float16 or b float16,

38
00:02:16.803 --> 00:02:19.939
cast the weights into that precision
while keeping

39
00:02:20.406 --> 00:02:23.409
the weights into the same range as before.

40
00:02:23.676 --> 00:02:27.580
So between -128 and 127,

41
00:02:27.747 --> 00:02:31.451
we'll just cast the data type of those weights in,

42
00:02:31.818 --> 00:02:35.755
half precision so that it will match the data
type of the input.

43
00:02:35.922 --> 00:02:38.892
Then we will perform, the linear
operation.

44
00:02:38.892 --> 00:02:40.193
So classic matrix

45
00:02:40.193 --> 00:02:43.830
multiplication
between the input and the casted weights.

46
00:02:44.030 --> 00:02:46.900
We will multiply these results together
with the scales

47
00:02:46.900 --> 00:02:49.903
of the model and optionally add the bias

48
00:02:50.103 --> 00:02:51.804
once this is done.

49
00:02:51.804 --> 00:02:52.972
So let's get started.

50
00:02:52.972 --> 00:02:57.972
So let's first import those modules that
we will use for implementing our method.

51
00:02:58.878 --> 00:03:03.878
And I'm also I'm also defining here
some random inputs a random int8 matrix,

52
00:03:04.584 --> 00:03:07.587
random hidden states, random scales,
and random bias.

53
00:03:07.854 --> 00:03:10.590
So, typically the workflow
would be something as follows.

54
00:03:10.590 --> 00:03:13.493
So we first cast the weights

55
00:03:13.493 --> 00:03:16.229
into the same data
type as the hidden states.

56
00:03:16.229 --> 00:03:21.100
Then on top of that,
you will perform the matrix multiplication

57
00:03:21.501 --> 00:03:24.637
by calling f.linear from PyTorch.

58
00:03:26.739 --> 00:03:27.974
All right.

59
00:03:27.974 --> 00:03:31.244
And then we'll multiply that
with the input scales

60
00:03:32.679 --> 00:03:35.682
and optionally add a bias term

61
00:03:35.949 --> 00:03:38.952
at the end of the operation.

62
00:03:41.054 --> 00:03:42.455
Perfect.

63
00:03:42.455 --> 00:03:45.491
And notice also for the weight matrix.

64
00:03:45.825 --> 00:03:50.129
So it has the shape output dimension
input dimension.

65
00:03:50.363 --> 00:03:53.633
When you perform the matrix multiplication
between the weight matrix

66
00:03:53.633 --> 00:03:58.633
and the input hidden states, you will have
a vector of batch size output dimension.

67
00:03:58.805 --> 00:04:00.106
So 132.

68
00:04:00.106 --> 00:04:03.009
So it's important that the scales have this

69
00:04:03.009 --> 00:04:06.479
the same shape
as the output shape of your weight matrix.

70
00:04:06.512 --> 00:04:09.849
And same comment for the bias
so that you can broadcast the operations

71
00:04:09.849 --> 00:04:14.849
between the output from here
and the scales and the whole output here

72
00:04:15.154 --> 00:04:16.456
and the bias. Perfect.

73
00:04:16.456 --> 00:04:19.459
So let's wrap everything
in a single method.

74
00:04:20.827 --> 00:04:22.228
Perfect.

75
00:04:22.228 --> 00:04:24.497
And let's also quickly try it out.

76
00:04:25.698 --> 00:04:27.734
With and without bias.

77
00:04:27.734 --> 00:04:29.969
Great. So it seems to work fine.

78
00:04:29.969 --> 00:04:32.972
So I guess we can move forward
with the next building block

79
00:04:33.039 --> 00:04:36.576
that will leverage the method
that we have just created. To continue

80
00:04:36.576 --> 00:04:38.511
building our linear layer class,

81
00:04:38.511 --> 00:04:42.382
we'll start implementing
the init method of that class.

82
00:04:42.715 --> 00:04:46.753
So recall for this linear layer
we need to store the int8 weights,

83
00:04:47.420 --> 00:04:49.956
the scales and the bias.

84
00:04:49.956 --> 00:04:53.092
Let's first start by implementing
the skeleton of the init method.

85
00:04:53.693 --> 00:04:56.596
So it has to kind of match,

86
00:04:56.596 --> 00:05:01.167
the signature of the init
method of a torch in our layer.

87
00:05:01.167 --> 00:05:05.104
So it has to contain
input features, output features

88
00:05:05.138 --> 00:05:08.808
in order to correctly initialize
the input matrix.

89
00:05:09.175 --> 00:05:13.479
The weights matrix bias, whether
the linear layer has a bias term or not,

90
00:05:13.913 --> 00:05:17.250
and data type which would correspond
to the data type of the bias.

91
00:05:17.550 --> 00:05:22.455
Because our weight matrix
will have a torch that int8 as data type.

92
00:05:22.755 --> 00:05:23.456
So here

93
00:05:23.456 --> 00:05:27.527
we're going to define our int8
weights, together with the scales

94
00:05:27.527 --> 00:05:29.395
that are going to be stored
in the linear layer.

95
00:05:29.395 --> 00:05:32.565
So if you have done any PyTorch
before this lab,

96
00:05:32.565 --> 00:05:36.035
you might be directly trying your hands on
doing something like this

97
00:05:36.035 --> 00:05:39.972
to create your int8 weights,
assigning the new attributes int8 weights,

98
00:05:40.807 --> 00:05:43.242
being a parameter.

99
00:05:43.242 --> 00:05:45.144
And then maybe do something like this.

100
00:05:45.144 --> 00:05:50.016
So the issue with this, with this approach
is that when you create

101
00:05:50.049 --> 00:05:53.052
an nn. parameter, PyTorch expects

102
00:05:53.086 --> 00:05:56.356
that parameter where it's able to compute
gradients on it.

103
00:05:56.589 --> 00:05:59.859
The issue is that with PyTorch,
you can't explicitly compute gradients

104
00:06:00.526 --> 00:06:04.197
on int8 tensors, yet,
so you should get an error

105
00:06:04.197 --> 00:06:07.200
if you try just to initialize
a dummy layer

106
00:06:07.667 --> 00:06:08.835
with this approach.

107
00:06:08.835 --> 00:06:11.904
So if you try that out, you get an error

108
00:06:12.372 --> 00:06:16.242
saying "only tensors of floating point
and complexity can require gradients."

109
00:06:16.809 --> 00:06:19.812
So, the right approach to store int8
weights

110
00:06:20.046 --> 00:06:24.317
is instead of saving attributes as being

111
00:06:24.317 --> 00:06:28.421
an endless parameter, is to call
this method called register buffer.

112
00:06:28.721 --> 00:06:32.291
That way instead of storing a parameter,
we just store a buffer, meaning

113
00:06:32.291 --> 00:06:35.261
we don't need to compute
gradients on the tensor,

114
00:06:35.261 --> 00:06:38.264
and you can initialize it
with whatever dtype that you want.

115
00:06:38.364 --> 00:06:42.435
So if you try that out and
initialize, it just works.

116
00:06:43.736 --> 00:06:46.272
So let's
continue designing our linear layer.

117
00:06:46.272 --> 00:06:48.875
So we have our int8 weights.

118
00:06:48.875 --> 00:06:51.878
And then we'll do the same thing
for scales

119
00:06:52.512 --> 00:06:55.381
as well by initializing
with the correct shape.

120
00:06:55.381 --> 00:06:58.518
And we're also going to call register
buffer on scales

121
00:06:58.518 --> 00:07:02.021
because again, here, we're just expecting
to do simple inference.

122
00:07:02.054 --> 00:07:04.157
We're not interested in doing training.

123
00:07:04.157 --> 00:07:07.460
So just calling registered
buffer is sufficient.

124
00:07:07.860 --> 00:07:09.829
And then we're going to store
an optional bias.

125
00:07:09.829 --> 00:07:14.500
So if bias is set to true we're just
starting a new buffer called bias.

126
00:07:15.067 --> 00:07:17.069
Otherwise we'll set it to none.

127
00:07:17.069 --> 00:07:21.507
So let's quickly try that out and create
a dummy instance of a linear layer

128
00:07:21.974 --> 00:07:24.977
and see if our attributes
have been correctly saved.

129
00:07:25.611 --> 00:07:27.447
Perfect. So yeah.

130
00:07:27.447 --> 00:07:31.350
So all the expected attributes have,
the expected shape.

131
00:07:31.784 --> 00:07:35.688
So output shape input
shape output output shape or the scales.

132
00:07:36.255 --> 00:07:39.325
I guess we can move forward with the next task,

133
00:07:39.759 --> 00:07:42.895
which is building the
forward pass of that class.

134
00:07:43.863 --> 00:07:46.666
So we're going to copy

135
00:07:46.666 --> 00:07:48.134
what we did here.

136
00:07:48.134 --> 00:07:52.839
And we're going to call the method that
we have defined in the first sub task.

137
00:07:52.972 --> 00:07:56.709
And we're just simply going to call it on
self.int8

138
00:07:56.709 --> 00:07:59.712
weights,
self.skills, and self.bias.

139
00:07:59.779 --> 00:08:02.048
And this method will do everything
under the hood for us.

140
00:08:02.048 --> 00:08:06.118
And we'll take care of casting the weights
into the correct dtype and multiplying

141
00:08:06.118 --> 00:08:09.555
everything with the scales and optionally
add the whole results with bias.

142
00:08:09.789 --> 00:08:11.657
All right. So let's create a new module.

143
00:08:11.657 --> 00:08:14.660
Some dummy hidden states with the shape
batch size,

144
00:08:14.827 --> 00:08:17.897
sequence length, hidden shape,
which should match the input

145
00:08:17.897 --> 00:08:21.033
hidden states shape
that we have, passed here.

146
00:08:24.337 --> 00:08:25.338
Perfect.

147
00:08:25.338 --> 00:08:27.707
So we still have batch
size, sequence length.

148
00:08:27.707 --> 00:08:30.042
And here, instead of input
shape, we have output shape.

149
00:08:31.677 --> 00:08:34.180
It's also check data type.

150
00:08:34.180 --> 00:08:36.215
So the dtype is correct Float32.

151
00:08:36.215 --> 00:08:38.351
Because we have initialized
a random tensor

152
00:08:38.351 --> 00:08:42.154
and by default PyTorch initialize
everything in torch, not Float32.

153
00:08:42.321 --> 00:08:43.089
Great.

154
00:08:43.089 --> 00:08:46.559
Now that we have a forward pass
that is working a linear layer class

155
00:08:46.559 --> 00:08:51.559
that has all the needed attributes,
we need to build, quantize method

156
00:08:52.532 --> 00:08:56.435
in order to perform
the linear quantization algorithm

157
00:08:56.435 --> 00:09:00.139
that you have seen in the previous lesson,
so that the weights gets correctly,

158
00:09:00.840 --> 00:09:01.741
quantized.

159
00:09:01.741 --> 00:09:03.543
Because right now everything is random.

160
00:09:03.543 --> 00:09:07.413
So you need to replace all the layers
with this, linear layer,

161
00:09:07.647 --> 00:09:10.316
you'll get gibberish output most likely.

162
00:09:10.316 --> 00:09:12.385
So just to give you more idea,

163
00:09:12.385 --> 00:09:15.821
once we have defined that quantize method,
the workflow will be the following.

164
00:09:15.821 --> 00:09:19.825
So, you have your base model
that is let's say in half precision.

165
00:09:20.092 --> 00:09:23.095
So either Fp16 or Vf16.

166
00:09:23.329 --> 00:09:26.999
Will loop over all the linear
layer classes, replace them

167
00:09:27.300 --> 00:09:30.836
with our new linear class,
and then call quantize

168
00:09:31.404 --> 00:09:36.008
by passing the old weights in order
to quantize the old weights into int8.

169
00:09:36.108 --> 00:09:38.945
So let's redefine our class again.

170
00:09:38.945 --> 00:09:41.247
And here start
thinking about the quantize method.

171
00:09:41.247 --> 00:09:46.247
So as I said the quantize method
will take the original weights as input.

172
00:09:46.752 --> 00:09:49.922
It will quantize the weights in Int8
precision,

173
00:09:50.690 --> 00:09:55.690
get the scales of the quantization,
and then manually assign int8

174
00:09:55.861 --> 00:09:59.599
weights and scales to the computed
quantized weights and scales.

175
00:10:00.032 --> 00:10:02.234
So let's do that step by step.

176
00:10:02.234 --> 00:10:07.234
So first of all, I would recommend
to upcast the weights in FP32

177
00:10:08.140 --> 00:10:09.408
for stability.

178
00:10:09.408 --> 00:10:13.546
So we'll get the weights in Fp32 first
and then we will use

179
00:10:13.679 --> 00:10:17.183
this simple formula
that you have seen in the previous lesson.

180
00:10:17.483 --> 00:10:21.554
So we'll first
get the absolute values of the weights.

181
00:10:22.054 --> 00:10:26.025
Get the maximum on the last dimension
and divide it by

182
00:10:26.292 --> 00:10:29.295
yeah 127 in order to get the scales.

183
00:10:29.295 --> 00:10:32.999
So we're going to assign
that to scales variable.

184
00:10:33.399 --> 00:10:37.036
And make sure that scales
has the same datatype as the input weights

185
00:10:38.004 --> 00:10:41.007
by calling two weights the dtype.

186
00:10:41.007 --> 00:10:44.477
And to get the int8 weights,
we'll just apply the formula

187
00:10:44.477 --> 00:10:47.747
that you have seen on the previous lesson
on linear quantization.

188
00:10:48.547 --> 00:10:51.017
So this is the per channel,

189
00:10:51.017 --> 00:10:53.552
linear quantization

190
00:10:53.552 --> 00:10:55.454
as you're getting the maximum

191
00:10:55.454 --> 00:10:58.424
on each element of the last dimension.

192
00:10:58.724 --> 00:11:01.060
So yeah basically
this is how you get the int8 weights.

193
00:11:01.060 --> 00:11:04.163
Again, it's based on the previous lesson
unrolled.

194
00:11:04.463 --> 00:11:07.233
We're just simply assigning self

195
00:11:07.233 --> 00:11:10.236
dot int8 weights and scales

196
00:11:10.569 --> 00:11:13.572
with these tensors.

197
00:11:14.774 --> 00:11:15.775
Perfect.

198
00:11:15.775 --> 00:11:18.778
And the forward pass will stay the same.

199
00:11:21.480 --> 00:11:23.683
Perfect.

200
00:11:23.683 --> 00:11:23.949
Okay.

201
00:11:23.949 --> 00:11:26.419
So let's let's try that out.

202
00:11:26.419 --> 00:11:28.454
So let's first initialize
the dummy module.

203
00:11:28.454 --> 00:11:31.724
Maybe print
the int8 weights before quantizing

204
00:11:32.024 --> 00:11:35.027
and we'll compare the results
afterwards as well.

205
00:11:35.061 --> 00:11:38.064
I might just take a smaller.

206
00:11:38.564 --> 00:11:39.965
Yeah.

207
00:11:39.965 --> 00:11:40.599
All right.

208
00:11:40.599 --> 00:11:43.302
Let's also pass some dummy random

209
00:11:44.303 --> 00:11:45.705
original weights.

210
00:11:45.705 --> 00:11:49.775
So this random matrix will act
as the original weights

211
00:11:49.775 --> 00:11:52.278
that we're going to use
to quantize your module.

212
00:11:52.278 --> 00:11:54.914
So let's call module.quantize.

213
00:11:54.914 --> 00:11:57.016
Perfect.

214
00:11:57.016 --> 00:12:00.419
So as you can see the weights tensor
are completely different.

215
00:12:00.886 --> 00:12:04.156
And because we quantized the module
with the correct weights.

216
00:12:04.890 --> 00:12:07.426
And also

217
00:12:07.426 --> 00:12:09.028
the int8 weights

218
00:12:09.028 --> 00:12:13.666
are now between -128 and 127.

219
00:12:14.033 --> 00:12:17.670
So those values did not exist before
because the module has been

220
00:12:17.670 --> 00:12:19.305
initialized randomly.

221
00:12:19.305 --> 00:12:22.608
And since here we're performing
abs max quantitzation.

222
00:12:22.875 --> 00:12:26.445
We always have these values
in the quantized int8 weights.

223
00:12:26.812 --> 00:12:27.046
Yeah.

224
00:12:27.046 --> 00:12:31.050
We can also inspect the scales
of our quantized module

225
00:12:32.118 --> 00:12:33.152
which look like this.

226
00:12:33.152 --> 00:12:36.155
I want you to quickly inspect
also the shape of scales.

227
00:12:36.722 --> 00:12:41.193
So you have a tensor of size four
which is the expected output shape.

228
00:12:42.194 --> 00:12:45.197
So we're going to do the same for

229
00:12:45.531 --> 00:12:46.432
int8 weights.

230
00:12:46.432 --> 00:12:48.167
So four eight.

231
00:12:48.167 --> 00:12:52.538
So if we directly multiply the two tensors
it won't work.

232
00:12:52.571 --> 00:12:55.641
So you have to add a new dimension
here in scales.

233
00:12:57.977 --> 00:12:58.844
All right.

234
00:12:58.844 --> 00:13:02.281
So let's compare
that against our original weights.

235
00:13:02.715 --> 00:13:05.518
Yeah. So as you can see,

236
00:13:05.518 --> 00:13:08.821
if you quickly look into it, the weights
look pretty close.

237
00:13:08.954 --> 00:13:12.124
We can have also a better idea
by computing the quantization error

238
00:13:12.725 --> 00:13:14.994
that can be done through this formula.

239
00:13:14.994 --> 00:13:16.695
So we just,

240
00:13:16.695 --> 00:13:18.998
subtracting both tensors.

241
00:13:18.998 --> 00:13:23.335
So the quantized weights
the original weights absolute value.

242
00:13:23.702 --> 00:13:27.473
So this is the average quantization error
for each element

243
00:13:27.473 --> 00:13:32.077
between the original weights
and the dequantized weights.

244
00:13:32.211 --> 00:13:32.745
Perfect.