| WEBVTT | |
| X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:144533 | |
| 1 | |
| 00:00:02.002 --> 00:00:05.038 | |
| The unpacking | |
| algorithm is simply the other way around. | |
| 2 | |
| 00:00:05.372 --> 00:00:08.141 | |
| So if you pack the tensor | |
| 3 | |
| 00:00:08.141 --> 00:00:10.243 | |
| as follows, you would like to unpack them | |
| 4 | |
| 00:00:10.243 --> 00:00:13.847 | |
| to extract the real values | |
| of the two bit weights. | |
| 5 | |
| 00:00:13.947 --> 00:00:16.750 | |
| And most importantly, | |
| you want to unpack these tensors | |
| 6 | |
| 00:00:16.750 --> 00:00:20.520 | |
| so that you can also perform | |
| some operations on top of these tensors. | |
| 7 | |
| 00:00:20.620 --> 00:00:20.954 | |
| Yeah. | |
| 8 | |
| 00:00:20.954 --> 00:00:24.924 | |
| So if you have for example packages tensor | |
| then you would unpack it like this. | |
| 9 | |
| 00:00:24.924 --> 00:00:29.924 | |
| So 010000 0 or 1 one and then one zero. | |
| 10 | |
| 00:00:30.897 --> 00:00:33.533 | |
| So let's also try to do it step by step. | |
| 11 | |
| 00:00:33.533 --> 00:00:37.270 | |
| So unpacked weights | |
| the signature would be the same as well. | |
| 12 | |
| 00:00:37.771 --> 00:00:41.007 | |
| You take in packed uint8 tensor | |
| number of bits. | |
| 13 | |
| 00:00:41.174 --> 00:00:45.678 | |
| So to get the expected number of values | |
| you can just use this formula. | |
| 14 | |
| 00:00:45.979 --> 00:00:46.179 | |
| Yeah. | |
| 15 | |
| 00:00:46.179 --> 00:00:49.716 | |
| It's simply | |
| the number of packed tensors multiplied | |
| 16 | |
| 00:00:49.716 --> 00:00:52.986 | |
| by eight divided by the number of bits. | |
| 17 | |
| 00:00:53.453 --> 00:00:57.223 | |
| NUM steps is also going to be the same | |
| for each packed tensor. | |
| 18 | |
| 00:00:57.724 --> 00:01:01.861 | |
| We're expecting to extract | |
| eight divided by number of bits | |
| 19 | |
| 00:01:02.695 --> 00:01:03.363 | |
| tensors. | |
| 20 | |
| 00:01:03.997 --> 00:01:06.599 | |
| Let's initialize | |
| our unpack tensor. | |
| 21 | |
| 00:01:06.699 --> 00:01:10.403 | |
| So yeah by just initializing | |
| some torch zeros. | |
| 22 | |
| 00:01:10.837 --> 00:01:13.073 | |
| The values for you the eight. | |
| 23 | |
| 00:01:13.073 --> 00:01:15.775 | |
| And let's keep track | |
| 24 | |
| 00:01:15.775 --> 00:01:18.511 | |
| of the index of the unpacked tensor. | |
| 25 | |
| 00:01:18.511 --> 00:01:22.382 | |
| So yeah we're going to go for a simple | |
| and naive approach. | |
| 26 | |
| 00:01:22.382 --> 00:01:22.682 | |
| Yeah. | |
| 27 | |
| 00:01:22.682 --> 00:01:27.387 | |
| We're just going to loop over the packed | |
| weights one by one | |
| 28 | |
| 00:01:28.054 --> 00:01:30.957 | |
| and extract | |
| 29 | |
| 00:01:30.957 --> 00:01:33.960 | |
| for each pack tensor number of steps. | |
| 30 | |
| 00:01:34.360 --> 00:01:37.130 | |
| So here for two bits four. | |
| 31 | |
| 00:01:37.130 --> 00:01:40.733 | |
| So again if we consider this example okay. | |
| 32 | |
| 00:01:40.733 --> 00:01:42.268 | |
| So this is our packed weight. | |
| 33 | |
| 00:01:42.268 --> 00:01:45.738 | |
| We want to extract | |
| step by step this value in two bit. | |
| 34 | |
| 00:01:46.005 --> 00:01:48.374 | |
| And this one and so on. | |
| 35 | |
| 00:01:48.374 --> 00:01:51.377 | |
| So let's consider the first iteration. | |
| 36 | |
| 00:01:51.411 --> 00:01:53.046 | |
| So for the first packed weight. | |
| 37 | |
| 00:01:53.046 --> 00:01:56.583 | |
| So this is in our simple case | |
| this is one value right. | |
| 38 | |
| 00:01:56.649 --> 00:02:01.649 | |
| So in the first iteration | |
| you uint8 tensor of I is this value. | |
| 39 | |
| 00:02:03.256 --> 00:02:04.524 | |
| So if we want to extract | |
| 40 | |
| 00:02:04.524 --> 00:02:07.527 | |
| only these first two bits, | |
| 41 | |
| 00:02:08.328 --> 00:02:11.197 | |
| in an iterative manner, | |
| we're going to shift it | |
| 42 | |
| 00:02:11.197 --> 00:02:14.033 | |
| by bits times j. | |
| 43 | |
| 00:02:14.033 --> 00:02:16.769 | |
| So j starts from zero. | |
| 44 | |
| 00:02:16.769 --> 00:02:19.172 | |
| And then it goes until three. | |
| 45 | |
| 00:02:19.172 --> 00:02:21.374 | |
| So for the first iteration | |
| this is going to be zero. | |
| 46 | |
| 00:02:21.374 --> 00:02:23.143 | |
| We're going to do nothing. | |
| 47 | |
| 00:02:23.143 --> 00:02:26.146 | |
| But it's fine because we just want to take | |
| the first two bits. | |
| 48 | |
| 00:02:26.746 --> 00:02:30.517 | |
| We're going to use the same bitwise | |
| or operation | |
| 49 | |
| 00:02:30.950 --> 00:02:33.553 | |
| on the unpacked tensor. | |
| 50 | |
| 00:02:33.553 --> 00:02:36.556 | |
| So let's also write down. | |
| 51 | |
| 00:02:37.290 --> 00:02:39.859 | |
| Here. | |
| 52 | |
| 00:02:39.859 --> 00:02:41.895 | |
| The stages of the unpacked tensor. | |
| 53 | |
| 00:02:41.895 --> 00:02:44.898 | |
| So we have num values here | |
| 54 | |
| 00:02:45.365 --> 00:02:47.233 | |
| is four. | |
| 55 | |
| 00:02:47.233 --> 00:02:49.769 | |
| So we have four values in four bit. | |
| 56 | |
| 00:02:49.769 --> 00:02:53.506 | |
| For the first index | |
| we're going to make bitwise or operation | |
| 57 | |
| 00:02:53.506 --> 00:02:58.506 | |
| between these full zeros | |
| and this tensor shifted by zero. | |
| 58 | |
| 00:02:58.578 --> 00:03:00.180 | |
| So basically just this tensor. | |
| 59 | |
| 00:03:01.281 --> 00:03:03.383 | |
| And in the first iteration | |
| 60 | |
| 00:03:03.383 --> 00:03:06.719 | |
| it's going to be | |
| an exact replication of the packed tensor. | |
| 61 | |
| 00:03:07.554 --> 00:03:11.324 | |
| So you may be wondering | |
| "what do we do to remove | |
| 62 | |
| 00:03:11.324 --> 00:03:14.327 | |
| all these bits that are before zero one?" | |
| 63 | |
| 00:03:14.727 --> 00:03:15.929 | |
| But don't worry, | |
| 64 | |
| 00:03:15.929 --> 00:03:18.264 | |
| you're going to see that in a few moments. | |
| 65 | |
| 00:03:18.264 --> 00:03:20.366 | |
| So this is on the first iteration. | |
| 66 | |
| 00:03:20.366 --> 00:03:21.301 | |
| Of course. | |
| 67 | |
| 00:03:21.301 --> 00:03:24.404 | |
| Don't forget to increment unpacked index. | |
| 68 | |
| 00:03:25.171 --> 00:03:27.874 | |
| And let's try to see | |
| what happens on the next iteration. | |
| 69 | |
| 00:03:27.874 --> 00:03:31.177 | |
| So in the next iteration | |
| we want to consider these two bits | |
| 70 | |
| 00:03:31.578 --> 00:03:33.746 | |
| and put them here, right. | |
| 71 | |
| 00:03:33.746 --> 00:03:37.917 | |
| So now unpacked index is going to be one | |
| J is going to be one. | |
| 72 | |
| 00:03:38.818 --> 00:03:41.487 | |
| And you uint | |
| tensor is still going to be zero. | |
| 73 | |
| 00:03:41.487 --> 00:03:43.756 | |
| Because we only have | |
| a single packed tensor. | |
| 74 | |
| 00:03:43.756 --> 00:03:47.493 | |
| So this time instead of shifting by zero | |
| we're shifting by | |
| 75 | |
| 00:03:47.994 --> 00:03:51.331 | |
| two bits | |
| bits times j because j is equal to one. | |
| 76 | |
| 00:03:51.397 --> 00:03:53.633 | |
| So in the second iteration. | |
| 77 | |
| 00:03:53.633 --> 00:03:55.802 | |
| So this is first iteration. | |
| 78 | |
| 00:03:55.802 --> 00:03:57.337 | |
| Second iteration. | |
| 79 | |
| 00:03:57.337 --> 00:04:01.774 | |
| In the second iteration we're going | |
| to shift that by two bits on the right. | |
| 80 | |
| 00:04:03.676 --> 00:04:06.679 | |
| So, the shifted | |
| 81 | |
| 00:04:07.347 --> 00:04:09.616 | |
| tensor would look like this. | |
| 82 | |
| 00:04:09.616 --> 00:04:12.085 | |
| So if you shift something on the right | |
| 83 | |
| 00:04:12.085 --> 00:04:15.088 | |
| the shifted values would be equal to zero. | |
| 84 | |
| 00:04:15.288 --> 00:04:18.024 | |
| And then you're going | |
| to perform a bitwise or. | |
| 85 | |
| 00:04:18.024 --> 00:04:21.027 | |
| So since this is full zeros | |
| 86 | |
| 00:04:21.527 --> 00:04:24.530 | |
| the unpack tensor would look like this. | |
| 87 | |
| 00:04:24.864 --> 00:04:27.033 | |
| And then third iteration. | |
| 88 | |
| 00:04:27.033 --> 00:04:29.002 | |
| So j is going to be equal to two. | |
| 89 | |
| 00:04:29.002 --> 00:04:32.572 | |
| So we're going to shift this by four. | |
| 90 | |
| 00:04:33.072 --> 00:04:36.075 | |
| All right. | |
| So it's going to look like this. | |
| 91 | |
| 00:04:36.209 --> 00:04:39.779 | |
| And we're going to do again | |
| bitwise or between | |
| 92 | |
| 00:04:40.346 --> 00:04:43.349 | |
| this value and our shifted value. | |
| 93 | |
| 00:04:44.183 --> 00:04:46.119 | |
| Perfect. And then last iteration. | |
| 94 | |
| 00:04:46.119 --> 00:04:48.321 | |
| All right. We're almost there. | |
| 95 | |
| 00:04:48.321 --> 00:04:50.023 | |
| There is just one thing. | |
| 96 | |
| 00:04:50.023 --> 00:04:55.023 | |
| So you may be wondering, | |
| "we're not having exactly the same results | |
| 97 | |
| 00:04:55.061 --> 00:04:59.399 | |
| as our target tensor", | |
| which, again, would look like this, right? | |
| 98 | |
| 00:05:01.634 --> 00:05:02.969 | |
| But now we have | |
| 99 | |
| 00:05:02.969 --> 00:05:07.969 | |
| these bits that we want to remove | |
| from our, unpacked tensors. | |
| 100 | |
| 00:05:08.408 --> 00:05:11.444 | |
| So to do that, we're just going to perform | |
| a simple trick. | |
| 101 | |
| 00:05:11.644 --> 00:05:16.182 | |
| So, basically the idea is | |
| that we're just going to create a mask | |
| 102 | |
| 00:05:16.182 --> 00:05:19.185 | |
| and use that mask to perform | |
| a bitwise operation. | |
| 103 | |
| 00:05:19.519 --> 00:05:21.788 | |
| That will always give us here | |
| 104 | |
| 00:05:21.788 --> 00:05:23.222 | |
| zero. | |
| 105 | |
| 00:05:23.222 --> 00:05:26.392 | |
| And always make sure that these values | |
| stay the same. | |
| 106 | |
| 00:05:26.826 --> 00:05:30.963 | |
| So if we consider this mask | |
| and let's say this value, | |
| 107 | |
| 00:05:31.364 --> 00:05:34.367 | |
| if we perform a bitwise and operation | |
| 108 | |
| 00:05:34.901 --> 00:05:37.503 | |
| between these two values, | |
| 109 | |
| 00:05:37.503 --> 00:05:42.442 | |
| so one and 000100 | |
| 110 | |
| 00:05:43.142 --> 00:05:46.145 | |
| and so on zero everywhere. | |
| 111 | |
| 00:05:46.512 --> 00:05:49.515 | |
| Except for the last two bits | |
| 112 | |
| 00:05:50.149 --> 00:05:54.420 | |
| where you have 010 and 10111. | |
| 113 | |
| 00:05:55.555 --> 00:05:59.325 | |
| So all the bits before these two bits | |
| 114 | |
| 00:05:59.325 --> 00:06:02.328 | |
| that we want to keep | |
| were correctly masked out a zero. | |
| 115 | |
| 00:06:02.628 --> 00:06:05.732 | |
| And the two bits here are preserved. | |
| 116 | |
| 00:06:05.765 --> 00:06:07.934 | |
| So that's exactly what we want to achieve. | |
| 117 | |
| 00:06:07.934 --> 00:06:10.970 | |
| You can try that out on the other values. | |
| 118 | |
| 00:06:10.970 --> 00:06:12.905 | |
| That's just to double check. | |
| 119 | |
| 00:06:12.905 --> 00:06:17.076 | |
| But this mask should be sufficient | |
| to remove all the bits | |
| 120 | |
| 00:06:17.076 --> 00:06:22.076 | |
| before these, all of these two bits | |
| that we are interested in keeping. | |
| 121 | |
| 00:06:22.648 --> 00:06:26.152 | |
| And there is a general rule, where | |
| you can compute the value of this mask. | |
| 122 | |
| 00:06:26.819 --> 00:06:29.789 | |
| so this mask is simply three. | |
| 123 | |
| 00:06:30.156 --> 00:06:32.525 | |
| So three in case of two bits is | |
| 124 | |
| 00:06:32.525 --> 00:06:36.429 | |
| just two to the power | |
| of two number of bits minus one, | |
| 125 | |
| 00:06:37.063 --> 00:06:39.766 | |
| which corresponds | |
| to the maximum value you can, | |
| 126 | |
| 00:06:40.733 --> 00:06:43.703 | |
| reach within this number of bits. | |
| 127 | |
| 00:06:43.703 --> 00:06:47.273 | |
| So if you encode in binary in two bits, | |
| you can't go above three | |
| 128 | |
| 00:06:47.740 --> 00:06:50.476 | |
| because you can only encode zero, one, | |
| two and three. | |
| 129 | |
| 00:06:50.476 --> 00:06:52.145 | |
| And this formula of course is extensible. | |
| 130 | |
| 00:06:52.145 --> 00:06:54.180 | |
| So you can also try it with four eight | |
| 131 | |
| 00:06:54.180 --> 00:06:56.416 | |
| and you can the same logic. | |
| 132 | |
| 00:06:56.416 --> 00:07:00.953 | |
| So we can perform the masking operation | |
| at the end of everything. | |
| 133 | |
| 00:07:01.421 --> 00:07:05.324 | |
| So we're just going to perform | |
| an and operation | |
| 134 | |
| 00:07:06.292 --> 00:07:09.429 | |
| to mask out those values as explained. | |
| 135 | |
| 00:07:09.929 --> 00:07:12.932 | |
| All right. | |
| 136 | |
| 00:07:13.199 --> 00:07:16.202 | |
| So let's try that out on | |
| 137 | |
| 00:07:17.437 --> 00:07:20.106 | |
| let's say this tensor. | |
| 138 | |
| 00:07:20.106 --> 00:07:23.009 | |
| So this is the packed version | |
| of the tensor. | |
| 139 | |
| 00:07:23.009 --> 00:07:26.045 | |
| if we unpack it we should retrieve exactly | |
| 140 | |
| 00:07:26.045 --> 00:07:29.048 | |
| this tensor. | |
| 141 | |
| 00:07:32.752 --> 00:07:33.286 | |
| Perfect. | |
| 142 | |
| 00:07:33.286 --> 00:07:36.088 | |
| It should be the same. Yeah. | |
| 143 | |
| 00:07:36.088 --> 00:07:39.392 | |
| So yeah, if you compare both tensors, | |
| they're exactly the same. | |
| 144 | |
| 00:07:40.259 --> 00:07:43.095 | |
| 1032 and 3333. | |
| 145 | |
| 00:07:45.331 --> 00:07:46.666 | |
| Perfect. | |
| 146 | |
| 00:07:46.666 --> 00:07:48.868 | |
| So. Yeah, we just did it now for two bits. | |
| 147 | |
| 00:07:48.868 --> 00:07:52.939 | |
| We also, applied a very naive approach | |
| where we, | |
| 148 | |
| 00:07:53.239 --> 00:07:56.042 | |
| you know, did two, two, four loops. | |
| 149 | |
| 00:07:56.042 --> 00:08:00.713 | |
| we also didn't consider the case | |
| where the tensor is, has multiple shapes. | |
| 150 | |
| 00:08:01.047 --> 00:08:03.182 | |
| Here we just considered a simple vector. | |
| 151 | |
| 00:08:03.182 --> 00:08:06.819 | |
| But yeah, feel free to enhance | |
| this algorithm. | |
| 152 | |
| 00:08:06.819 --> 00:08:09.121 | |
| Make it faster. | |
| 153 | |
| 00:08:09.121 --> 00:08:13.092 | |
| Try to also extend this algorithm | |
| 154 | |
| 00:08:13.092 --> 00:08:17.463 | |
| so that it works on different shapes, | |
| arbitrary shapes as well. | |
| 155 | |
| 00:08:17.730 --> 00:08:20.099 | |
| So yeah, feel free to battle test | |
| everything. | |
| 156 | |
| 00:08:20.099 --> 00:08:24.904 | |
| Make sure that you have understood all | |
| the internals that I have explained here. | |
| 157 | |
| 00:08:25.137 --> 00:08:28.140 | |
| To wrap up the lesson, | |
| we're just going to see other challenges | |
| 158 | |
| 00:08:28.140 --> 00:08:32.044 | |
| when you quantize large | |
| models, such as large language models. | |
| 159 | |
| 00:08:32.645 --> 00:08:36.382 | |
| And yeah, we're going to wrap up | |
| the whole course with these explanations. | |
| 160 | |
| 00:08:36.816 --> 00:08:38.184 | |
| So, yeah let's move on to the slides. | |