Majid Al-RaimiK-means weight quantization: codebook, storage and centroid fine-tuningتكميم الأوزان بـK-means: دفتر الترميز والتخزين وضبط المراكز

COE 592Lecture 5.1المحاضرة 5.1Part 05الجزء 05

K-means weight quantization: codebook, storage and centroid fine-tuningتكميم الأوزان بـK-means: دفتر الترميز والتخزين وضبط المراكز

Cluster weights with K-means, store 2-bit cluster indexes plus a 32-bit codebook, compute the compression ratio, and fine-tune centroids by grouping gradients by cluster index and summing them.جمّع الأوزان بـK-means، وخزّن فهارس عناقيد من 2 بت مع دفتر ترميز (codebook) من 32 بت، واحسب نسبة الضغط، واضبط المراكز (centroids) بتجميع التدرجات حسب فهرس العنقود وجمعها.

Conceptsالمفاهيم
4
Slidesالشرائح
26-29
Readingمدة القراءة
24 min24 دقيقة
Understoodما فهمته
0/4 conceptsمفاهيم

Why this part mattersلماذا يهمّ هذا الجزء

A trained network stores every weight as a 32-bit float, yet many weights sit so close together that the network would barely notice if they shared one value. This part turns that observation into a working compression method: cluster the weights, keep only a short index per weight and a tiny table of shared values, then retrain the table so the network recovers its accuracy.

Everything here follows Figure 3 of Deep Compression (Han, Mao and Dally, ICLR 2016), the same 4 × 4 example the slides use. For the midterm, expect three kinds of question: the storage bill for given NN and MM, the fine-tuned centroids for a given gradient matrix and learning rate, and why the index map does not change during fine-tuning. For embedded work, this is the method that lets a model ship at a fraction of its size while the arithmetic stays in floating point.

الشبكة المدرَّبة تخزّن كل وزن كعدد عشري بحجم 32-bit، ومع ذلك فكثير من الأوزان قريبة جدًا من بعضها لدرجة أن الشبكة بالكاد ستلاحظ الفرق لو تشاركت قيمة واحدة. هذا الجزء يحوّل هذه الملاحظة إلى طريقة ضغط عملية: جمّع الأوزان في مجموعات، واحتفظ فقط بفهرس قصير لكل وزن وجدول صغير جدًا من القيم المشتركة، ثم أعد تدريب هذا الجدول حتى تستعيد الشبكة دقتها.

كل ما هنا يتبع الشكل 3 من ورقة Deep Compression (Han, Mao and Dally, ICLR 2016)، وهو نفس مثال 4 × 4 الذي تستخدمه الشرائح. في الاختبار النصفي توقّع ثلاثة أنواع من الأسئلة: حساب التخزين لقيم معطاة من NN و MM، والمراكز بعد الـ fine-tuning لمصفوفة gradients ومعدل تعلم معطيين، ولماذا لا تتغير خريطة الفهارس أثناء الـ fine-tuning. وفي الأنظمة المدمجة، هذه هي الطريقة التي تسمح بشحن النموذج بجزء صغير من حجمه بينما تبقى العمليات الحسابية بالفاصلة العائمة (floating point).

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Cluster a small weight matrix with K-means and explain why each centroid is the mean of its cluster.تجميع مصفوفة أوزان صغيرة باستخدام K-means، وشرح لماذا يكون كل مركز (centroid) هو متوسط عناصر مجموعته.
  2. Turn a clustering into an index matrix plus a codebook, rebuild the weights and read the quantization error.تحويل نتيجة التجميع إلى مصفوفة فهارس (index matrix) زائد دفتر رموز (codebook)، ثم إعادة بناء الأوزان وقراءة خطأ التكميم (quantization error).
  3. Compute storage before and after for any N and M, and explain why the ratio approaches 32/N only when M is much larger than 2^N.حساب حجم التخزين قبل وبعد لأي N و M، وشرح لماذا تقترب النسبة من 32/N فقط عندما تكون M أكبر بكثير من 2^N.
  4. Derive the centroid gradient as the sum of the gradients of every weight that shares it.اشتقاق gradient المركز على أنه مجموع gradients كل الأوزان التي تشترك فيه.
  5. Fine-tune a codebook by hand: group by index, reduce by sum, multiply by the learning rate and subtract.ضبط دفتر الرموز (fine-tuning) يدويًا: جمّع حسب الفهرس، اختزل بالجمع، اضرب في معدل التعلم (learning rate) ثم اطرح.

Look at the 16 weights of the example layer. Four of them are 2.09, 2.12, 1.92 and 1.87. Four more are -0.98, -1.08, -0.91 and -1.03. Three hover around 1.5, and five sit near zero. The matrix holds sixteen numbers, but really only four ideas.

انظر إلى الأوزان الـ 16 في طبقة المثال. أربعة منها هي 2.09 و 2.12 و 1.92 و 1.87. وأربعة أخرى هي -0.98 و -1.08 و -0.91 و -1.03. ثلاثة تحوم حول 1.5، وخمسة تقع قرب الصفر. المصفوفة تحمل ستة عشر رقمًا، لكنها في الحقيقة تحمل أربع أفكار فقط.

From an observation to an algorithmمن ملاحظة إلى خوارزمية

Weight sharing makes that observation official: all weights in one group are replaced by one shared value. The question is how to choose the groups and the values. K-means-based weight quantization answers with one-dimensional K-means. You fix the number of clusters kk in advance, then minimise the within-cluster sum of squares, the total squared distance from each weight to the value that will replace it.

Weight sharing (مشاركة الأوزان) تجعل هذه الملاحظة رسمية: كل الأوزان في مجموعة واحدة تُستبدل بقيمة مشتركة واحدة. السؤال هو كيف نختار المجموعات والقيم. K-means-based weight quantization تجيب باستخدام K-means أحادي البعد. تحدد عدد المجموعات kk مسبقًا، ثم تقلّل مجموع المربعات داخل المجموعات (within-cluster sum of squares)، أي مجموع مربعات المسافات من كل وزن إلى القيمة التي ستحل محله.

arg⁡min⁡C ∑i=1k ∑w∈ci∣w−ci∣2\arg\min_{C}\ \sum_{i=1}^{k}\ \sum_{w \in c_i} \lvert w - c_i \rvert^{2}
Deep Compression, Eq. 2: partition the n weights into k clusters that minimise the within-cluster sum of squaresDeep Compression، المعادلة 2: قسّم الأوزان n إلى k عنقودًا بحيث يُقلَّل مجموع المربعات داخل العناقيد

The standard way to minimise it is Lloyd's algorithm. Start with kk guesses for the shared values. Then repeat two steps until nothing changes. First, assign every weight to its nearest guess. Second, move every guess to the mean of the weights assigned to it. Neither step can raise the objective, so the loop always stops, though possibly at a local rather than global minimum, which is why the starting guesses matter.

الطريقة المعيارية لتقليله هي خوارزمية Lloyd. ابدأ بـ kk تخمينات للقيم المشتركة. ثم كرّر خطوتين حتى لا يتغير شيء. أولًا، أسند كل وزن إلى أقرب تخمين. ثانيًا، انقل كل تخمين إلى متوسط الأوزان المسندة إليه. لا يمكن لأي من الخطوتين أن ترفع قيمة الهدف، لذلك تتوقف الحلقة دائمًا، لكن ربما عند قيمة صغرى محلية (local minimum) لا عامة (global)، ولهذا تهمّ التخمينات الابتدائية.

Each of the 16 FP32 weights snaps to the nearest of four shared values: -1.00, 0.00, 1.50 and 2.00كل وزن من الأوزان الـ 16 بصيغة FP32 ينجذب إلى أقرب قيمة من أربع قيم مشتركة: -1.00 و 0.00 و 1.50 و 2.00

The representative value of a cluster is its Centroid. Because the second Lloyd step sets it to the mean, you can check the slide by hand. Every centroid on slide 27 is the exact mean of its members:

القيمة الممثِّلة للمجموعة هي centroid (المركز). ولأن خطوة Lloyd الثانية تضعه عند المتوسط، يمكنك التحقق من الشريحة يدويًا. كل مركز في الشريحة 27 هو المتوسط الدقيق لأعضائه:

IndexالفهرسSlide colourاللون في الشريحةCentroidالمركز (centroid)Member weightsالأوزان الأعضاءMeanالمتوسط
33Blueأزرق2.002.002.09, 2.12, 1.92, 1.872.09, 2.12, 1.92, 1.878.00 / 4 = 2.008.00 / 4 = 2.00
22Pinkوردي1.501.501.48, 1.53, 1.491.48, 1.53, 1.494.50 / 3 = 1.504.50 / 3 = 1.50
11Orangeبرتقالي0.000.000.09, 0.05, -0.14, 0, 00.09, 0.05, -0.14, 0, 00.00 / 5 = 0.000.00 / 5 = 0.00
00Greenأخضر-1.00-1.00-0.98, -1.08, -0.91, -1.03-0.98, -1.08, -0.91, -1.03-4.00 / 4 = -1.00-4.00 / 4 = -1.00
The k = 4 clustering of slide 26, with the index each colour receives on slide 27تجميع k = 4 من الشريحة 26، مع الفهرس الذي يأخذه كل لون في الشريحة 27

A useful consequence: since a centroid is a mean, the errors inside each cluster cancel. In the blue cluster, 0.09 + 0.12 - 0.08 - 0.13 = 0. That is a quick consistency check in an exam when you are handed a clustering and asked whether it is a converged K-means solution.

نتيجة مفيدة: بما أن المركز متوسط، فإن الأخطاء داخل كل مجموعة يلغي بعضها بعضًا. في المجموعة الزرقاء، 0.09 + 0.12 - 0.08 - 0.13 = 0. هذا فحص سريع للاتساق في الاختبار عندما تُعطى تجميعًا ويُسأل هل هو حل K-means متقارب (converged).

Where the starting guesses come fromمن أين تأتي التخمينات الابتدائية

Han et al. compared three initialisations: random (Forgy), density-based and linear. Linear spaces the starting centroids evenly between the smallest and largest weight. It won, because large-magnitude weights are rare but important, and the other two methods crowd the centroids into the dense middle of the distribution, leaving the few large weights badly represented. For the example, linear initialisation between -1.08 and 2.12 converges in one update to exactly the slide's four centroids.

قارن Han et al. بين ثلاث طرق للتهيئة (initialisation): العشوائية (Forgy)، والمعتمدة على الكثافة (density-based)، والخطية (linear). الطريقة الخطية توزّع المراكز الابتدائية بالتساوي بين أصغر وزن وأكبر وزن. وقد فازت، لأن الأوزان الكبيرة في القيمة المطلقة نادرة لكنها مهمة، والطريقتان الأخريان تحشران المراكز في الوسط الكثيف من التوزيع، فتبقى الأوزان الكبيرة القليلة ممثَّلة بشكل سيئ. في المثال، التهيئة الخطية بين -1.08 و 2.12 تتقارب في تحديث واحد إلى المراكز الأربعة نفسها الموجودة في الشريحة بالضبط.

Recall

State the two alternating steps of Lloyd's algorithm, and explain why the pink centroid is 1.50.اذكر الخطوتين المتناوبتين في خوارزمية Lloyd، واشرح لماذا يساوي المركز الوردي 1.50.

Assign each weight to its nearest centroid, then set each centroid to the mean of its assigned weights. The pink cluster holds 1.48, 1.53 and 1.49, whose mean is 4.50 / 3 = 1.50.

أسند كل وزن إلى أقرب مركز، ثم اجعل كل مركز يساوي متوسط الأوزان المسندة إليه. المجموعة الوردية تضم 1.48 و 1.53 و 1.49، ومتوسطها 4.50 / 3 = 1.50.

Once the clustering is fixed, the original matrix can be thrown away. In its place you store two things: a 4 × 4 matrix of small integers saying which cluster each weight belongs to, and a list of four floats saying what each cluster's value is.

بمجرد تثبيت التجميع يمكن التخلص من المصفوفة الأصلية. وتخزّن مكانها شيئين: مصفوفة 4 × 4 من أعداد صحيحة صغيرة تقول إلى أي مجموعة ينتمي كل وزن، وقائمة من أربعة أعداد عشرية تقول ما قيمة كل مجموعة.

Two tables replace oneجدولان بدلًا من جدول واحد

The integer matrix is the map of Cluster index values. With k=4k = 4 clusters an index needs log⁡24=2\log_2 4 = 2 bits, so slide 27 labels it 2-bit int. Its first row is 3, 0, 2, 1. The list of floats is the Codebook: 3: 2.00, 2: 1.50, 1: 0.00, 0: -1.00. Notice the indexes are numbered in increasing centroid order, so index 0 is the most negative value.

مصفوفة الأعداد الصحيحة هي خريطة قيم cluster index (فهرس المجموعة). مع k=4k = 4 مجموعات يحتاج الفهرس إلى log⁡24=2\log_2 4 = 2 بت، ولذلك تسمّيها الشريحة 27 باسم 2-bit int. صفها الأول هو 3, 0, 2, 1. أما قائمة الأعداد العشرية فهي codebook (دفتر الرموز): 3: 2.00 و 2: 1.50 و 1: 0.00 و 0: -1.00. لاحظ أن الفهارس مرقّمة بترتيب تصاعدي للمراكز، فالفهرس 0 هو القيمة الأكثر سالبية.

To use the layer you rebuild each weight by looking up its index in the codebook. Index 3 becomes 2.00, index 0 becomes -1.00, and so on. The rebuilt matrix is:

لاستخدام الطبقة تعيد بناء كل وزن بالبحث عن فهرسه في دفتر الرموز. الفهرس 3 يصبح 2.00، والفهرس 0 يصبح -1.00، وهكذا. المصفوفة المعاد بناؤها هي:

W^=[2−11.5000−12−120−1201.51.5],W^ij=CIij\hat W = \begin{bmatrix} 2 & -1 & 1.5 & 0 \\ 0 & 0 & -1 & 2 \\ -1 & 2 & 0 & -1 \\ 2 & 0 & 1.5 & 1.5 \end{bmatrix},\qquad \hat W_{ij} = C_{I_{ij}}
Reconstructed weights: each entry is the codebook value at that position's indexالأوزان المعاد بناؤها: كل عنصر هو قيمة دفتر الرموز عند فهرس ذلك الموضع

The rebuilt weights are still 32-bit floats, which is why slide 27 labels them that way. K-means saves storage, not arithmetic: at inference the indexes are decoded through the codebook and the layer runs in floating point, the Decode (lookup) at inference step a later part covers.

الأوزان المعاد بناؤها ما زالت أعدادًا عشرية بحجم 32-bit، ولهذا تسمّيها الشريحة 27 بهذا الاسم. K-means يوفّر في التخزين لا في الحساب: عند الاستدلال (inference) تُفك الفهارس عبر دفتر الرموز وتعمل الطبقة بالفاصلة العائمة، وهي خطوة decode (lookup) التي يشرحها جزء لاحق.

What is lost: the quantization errorما الذي نخسره: خطأ التكميم

Subtracting gives the Quantization error at each position, original minus reconstructed:

الطرح يعطي quantization error (خطأ التكميم) عند كل موضع، أي الأصلي ناقص المعاد بناؤه:

W−W^=[0.090.02−0.020.090.05−0.14−0.080.120.09−0.080−0.03−0.1300.03−0.01]W - \hat W = \begin{bmatrix} 0.09 & 0.02 & -0.02 & 0.09 \\ 0.05 & -0.14 & -0.08 & 0.12 \\ 0.09 & -0.08 & 0 & -0.03 \\ -0.13 & 0 & 0.03 & -0.01 \end{bmatrix}
Quantization error matrix from slide 27مصفوفة خطأ التكميم من الشريحة 27

The largest error in magnitude is -0.14, the weight -0.14 forced to 0.00. These small errors are what the network pays for compression, and what the fine-tuning in the next two concepts tries to win back.

أكبر خطأ في القيمة المطلقة هو -0.14، وهو الوزن -0.14 الذي أُجبر على أن يصبح 0.00. هذه الأخطاء الصغيرة هي الثمن الذي تدفعه الشبكة مقابل الضغط، وهي ما يحاول الـ fine-tuning في المفهومين التاليين استعادته.

The storage billفاتورة التخزين

Count bits. The original layer is 16 floats of 32 bits: 32 × 16 = 512 bit = 64 B. The quantized layer stores 16 indexes of 2 bits, 32 bit = 4 B, plus 4 centroids of 32 bits, 128 bit = 16 B. Total 20 B, so the layer is 64 / 20 = 3.2× smaller. That is the Compression ratio on the slide.

عُدّ البتات. الطبقة الأصلية هي 16 عددًا عشريًا كل منها 32 بت: 32 × 16 = 512 bit = 64 B. الطبقة المكمَّمة تخزّن 16 فهرسًا كل منها 2 بت، أي 32 bit = 4 B، زائد 4 مراكز كل منها 32 بت، أي 128 bit = 16 B. المجموع 20 B، فالطبقة أصغر بمقدار 64 / 20 = 3.2×. هذه هي compression ratio (نسبة الضغط) في الشريحة.

64 B of FP32 weights against 4 B of indexes plus 16 B of codebook: the codebook is most of the 20 B64 B من أوزان FP32 مقابل 4 B من الفهارس زائد 16 B من دفتر الرموز: دفتر الرموز هو معظم الـ 20 B

Now generalise. Take NN-bit indexes, so k=2Nk = 2^N clusters, and MM parameters. The original costs 32M32M bits. The indexes cost NMNM bits and the codebook 32⋅2N=2N+532 \cdot 2^N = 2^{N+5} bits.

الآن عمّم. خذ فهارس من NN بت، أي k=2Nk = 2^N مجموعة، و MM معاملًا. الأصل يكلّف 32M32M بت. الفهارس تكلّف NMNM بت، ودفتر الرموز 32⋅2N=2N+532 \cdot 2^N = 2^{N+5} بت.

r=32MNM+2N+5  →  M≫2N    32Nr = \frac{32M}{NM + 2^{N+5}} \;\xrightarrow{\;M \gg 2^N\;}\; \frac{32}{N}
Compression ratio of N-bit K-means quantization; Han et al. write it as nb / (n log2 k + kb)نسبة الضغط لتكميم K-means بعدد N من البتات؛ ويكتبها Han et al. على الصورة nb / (n log2 k + kb)

The codebook term does not grow with MM. In a real layer with M = 1,000,000 and N = 4, the codebook is 16 × 32 = 512 bit against 4,000,000 bit of indexes, about 0.013% of the total. That is why the slide crosses it out.

حدّ دفتر الرموز لا يكبر مع MM. في طبقة حقيقية فيها M = 1,000,000 و N = 4، يكون دفتر الرموز 16 × 32 = 512 bit مقابل 4,000,000 bit من الفهارس، أي نحو 0.013% من المجموع. ولهذا تشطبه الشريحة.

Worked exampleمثال محلول

A million weights at 4 bitsمليون وزن بأربعة بتات

  1. Originalالأصل

    32 × 1,000,000 = 32,000,000 bit (4 MB).
    32 × 1,000,000 = 32,000,000 bit (4 MB).
  2. Indexesالفهارس

    4 × 1,000,000 = 4,000,000 bit.
    4 × 1,000,000 = 4,000,000 bit.
  3. Codebookدفتر الرموز

    2^4 = 16 centroids × 32 bits = 512 bit.
    2^4 = 16 مركزًا × 32 بت = 512 bit.
  4. Resultالنتيجة

    32,000,000 / 4,000,512 ≈ 7.999 ≈ 8.0×, essentially 32/N = 8.
    32,000,000 / 4,000,512 ≈ 7.999 ≈ 8.0×، وهي عمليًا 32/N = 8.

The 4 × 4 example misses 32/2 = 16× by a wide margin because its assumption M≫2NM \gg 2^N fails: with only 16 weights, the 16 B codebook is 80% of the 20 B total. Push it further and the method backfires. At N = 4 on these 16 weights, the indexes cost 64 bit but the codebook costs 512 bit, 72 B in all, larger than the 64 B original.

مثال 4 × 4 يبتعد كثيرًا عن 32/2 = 16× لأن افتراضه M≫2NM \gg 2^N لا يتحقق: مع 16 وزنًا فقط، يمثّل دفتر الرموز البالغ 16 B نسبة 80% من المجموع 20 B. وإذا ذهبت أبعد تنقلب الطريقة ضدك. عند N = 4 على هذه الأوزان الـ 16، تكلّف الفهارس 64 bit لكن دفتر الرموز يكلّف 512 bit، أي 72 B إجمالًا، وهذا أكبر من الأصل البالغ 64 B.

ItemالعنصرFormulaالصيغةSlide: N = 2, M = 16الشريحة: N = 2، M = 16Layer: N = 4, M = 10^6طبقة: N = 4، M = 10^6
Original weightsالأوزان الأصلية32 bit x M32 bit x M32 x 16 = 512 bit = 64 B32 x 16 = 512 bit = 64 B32,000,000 bit32,000,000 bit
IndexesالفهارسN bit x MN bit x M2 x 16 = 32 bit = 4 B2 x 16 = 32 bit = 4 B4,000,000 bit4,000,000 bit
Codebookدفتر الرموز (codebook)32 bit x 2^N = 2^(N+5) bit32 bit x 2^N = 2^(N+5) bit32 x 4 = 128 bit = 16 B32 x 4 = 128 bit = 16 B32 x 16 = 512 bit32 x 16 = 512 bit
Ratioالنسبة32M / (NM + 2^(N+5))32M / (NM + 2^(N+5))64 / 20 = 3.2x64 / 20 = 3.2x≈ 8.0x (32/N = 8)≈ 8.0x (32/N = 8)
The same three formulas applied to the slide example and to a realistic layerالصيغ الثلاث نفسها مطبّقة على مثال الشريحة وعلى طبقة واقعية

The simulator runs K-means on the slide's weights. At k = 4 it reproduces slide 27 exactly. Switch to k = 2: the ratio rises to 6.4× but the errors grow to about 0.6. Switch to k = 8: the errors barely improve, three centroids own no weight at all, and the ratio falls to 1.68× because the codebook doubles.

المحاكي يشغّل K-means على أوزان الشريحة. عند k = 4 يعيد إنتاج الشريحة 27 بالضبط. انتقل إلى k = 2: ترتفع النسبة إلى 6.4× لكن الأخطاء تكبر إلى نحو 0.6. انتقل إلى k = 8: الأخطاء بالكاد تتحسن، وثلاثة مراكز لا يملك أي منها أي وزن، وتنخفض النسبة إلى 1.68× لأن دفتر الرموز يتضاعف.

SimulatorمحاكيK-means on the 16 slide weights: indexes, codebook and storageK-means على أوزان الشريحة الستة عشر: الفهارس ودفتر الرموز والتخزين
Weights, coloured by clusterالأوزان ملوّنة حسب العنقود32-bit float
Cluster indexفهرس العنقود2-bit uint
Codebookدفتر الرموز4 x 32 bit
  • 3:2.00x4
  • 2:1.50x3
  • 1:0.00x5
  • 0:-1.00x4
Reconstructed weightsالأوزان المعاد بناؤهاcodebook[index]
Quantization errorخطأ التكميمweight - reconstructed
Indexes4B2 bit x 16 = 32 bit
Codebook16B32 bit x 4 = 128 bit
Total20Boriginal is 64 Bالأصل 64 B
Ratio3.20xsmaller1M weights: 16.00x
Lloyd iterations from linear initialisation: 1. Mean absolute error 0.061. Every centroid is the mean of the weights that point to it, so within each cluster the errors cancel (exactly at k = 4, up to display rounding otherwise). With only 16 weights the codebook is a large share of the bill, which is why the ratio here sits far below 32/2 = 16.0x.عدد تكرارات Lloyd بدءا من التهيئة الخطية: 1. متوسط الخطأ المطلق 0.061. كل مركز هو متوسط الأوزان التي تشير إليه، لذلك تتلاشى الأخطاء داخل كل عنقود (تماما عند k = 4، وفي غيرها بقدر تقريب العرض). مع 16 وزنا فقط يشغل دفتر الرموز حصة كبيرة من الفاتورة، ولهذا تبقى النسبة هنا أقل بكثير من 32/2 = 16.0x.

Centroids start evenly spaced between the smallest and largest weight (the linear initialisation Han et al. found best) and are rounded to two decimals for display. At k = 4 the result is exactly the slide: codebook -1.00, 0.00, 1.50, 2.00, 20 B, 3.2x.تبدأ المراكز موزعة بالتساوي بين أصغر وزن وأكبر وزن (وهي التهيئة الخطية التي وجدها Han et al. الأفضل)، وتُقرَّب إلى منزلتين عشريتين للعرض. عند k = 4 تطابق النتيجة الشريحة تماما: دفتر الرموز -1.00, 0.00, 1.50, 2.00، و20 B، و3.2x.

Quick checkاختبار سريع

A layer with one million weights is quantized with 4-bit K-means and an FP32 codebook. About how much smaller does it become?طبقة فيها مليون وزن تُكمَّم باستخدام K-means بـ 4 بت مع دفتر رموز (codebook) بصيغة FP32. بكم مرة تقريبًا يصغر حجمها؟

Recall

Why does the 4 × 4 example reach only 3.2× when 32/N predicts 16×?لماذا يصل مثال 4 × 4 إلى 3.2× فقط بينما تتوقع 32/N قيمة 16×؟

The formula 32/N assumes the codebook is negligible (M ≫ 2^N). Here the codebook is 16 B out of 20 B. Without it, 64 / 4 = 16×; with it, 64 / 20 = 3.2×.

الصيغة 32/N تفترض أن دفتر الرموز مهمل (M ≫ 2^N). هنا دفتر الرموز 16 B من أصل 20 B. بدونه تكون النسبة 64 / 4 = 16×، ومعه 64 / 20 = 3.2×.

Quantizing a trained network nudges every weight by its quantization error, and the accuracy usually drops a little. The fix is to keep training. But what exactly is there to train, now that the weights no longer exist as sixteen independent numbers?

تكميم شبكة مدرَّبة يزيح كل وزن بمقدار خطأ التكميم الخاص به، وعادة تنخفض الدقة قليلًا. والحل هو مواصلة التدريب. لكن ما الذي يمكن تدريبه بالضبط، بعد أن لم تعد الأوزان موجودة كستة عشر رقمًا مستقلًا؟

Only the codebook is free to moveدفتر الرموز وحده حرّ في الحركة

After quantization the layer is described by the index map and the codebook. The index map is a grid of integers: there is no meaningful small step from index 2 to index 2.01, so it has no gradient. The four centroids are ordinary floats, and the forward pass uses them through W^ij=CIij\hat W_{ij} = C_{I_{ij}}. So Centroid fine-tuning trains the codebook and keeps every index exactly where K-means put it. The Deep Compression paper describes this as a level of indirection in both the forward and backward pass.

بعد التكميم توصف الطبقة بخريطة الفهارس ودفتر الرموز. خريطة الفهارس شبكة من الأعداد الصحيحة: لا توجد خطوة صغيرة ذات معنى من الفهرس 2 إلى الفهرس 2.01، لذلك ليس لها gradient. أما المراكز الأربعة فهي أعداد عشرية عادية، والتمرير الأمامي (forward pass) يستخدمها عبر W^ij=CIij\hat W_{ij} = C_{I_{ij}}. ولذلك فإن centroid fine-tuning يدرّب دفتر الرموز ويُبقي كل فهرس بالضبط حيث وضعه K-means. وتصف ورقة Deep Compression ذلك بأنه مستوى من الإحالة غير المباشرة (indirection) في التمرير الأمامي والخلفي معًا.

Backpropagation still produces one gradient per weightالـ backpropagation ما زال ينتج gradient واحدًا لكل وزن

The forward pass runs with the reconstructed weights, so ordinary backpropagation returns the usual 4 × 4 matrix ∂L/∂Wij\partial L / \partial W_{ij}, one number per position. Slide 28 shows it: first row -0.03, -0.01, 0.03, 0.02, and the largest entry is 0.12 at row 2, column 4. These numbers are gradients for positions, but the parameters are centroids. The chain rule connects the two.

التمرير الأمامي يعمل بالأوزان المعاد بناؤها، لذلك يعيد الـ backpropagation العادي مصفوفة 4 × 4 المعتادة ∂L/∂Wij\partial L / \partial W_{ij}، رقمًا واحدًا لكل موضع. تعرضها الشريحة 28: الصف الأول -0.03, -0.01, 0.03, 0.02، وأكبر عنصر هو 0.12 في الصف 2 والعمود 4. هذه الأرقام gradients للمواضع، لكن المعاملات هي المراكز. وقاعدة السلسلة (chain rule) تربط بين الاثنين.

Centroid CkC_k appears in every position whose index is kk, and nowhere else. So ∂Wij/∂Ck\partial W_{ij} / \partial C_k is 1 when Iij=kI_{ij} = k and 0 otherwise. Summing over all positions:

المركز CkC_k يظهر في كل موضع فهرسه kk، ولا يظهر في أي مكان آخر. لذلك فإن ∂Wij/∂Ck\partial W_{ij} / \partial C_k تساوي 1 عندما Iij=kI_{ij} = k و 0 في غير ذلك. وبالجمع على كل المواضع:

∂L∂Ck=∑i,j∂L∂Wij ∂Wij∂Ck=∑i,j∂L∂Wij  1(Iij=k)\frac{\partial L}{\partial C_k} = \sum_{i,j} \frac{\partial L}{\partial W_{ij}}\,\frac{\partial W_{ij}}{\partial C_k} = \sum_{i,j} \frac{\partial L}{\partial W_{ij}}\;\mathbb{1}(I_{ij} = k)
Deep Compression, Eq. 3: the centroid gradient is the sum of the gradients of all weights that use itDeep Compression، المعادلة 3: تدرّج المركز هو مجموع تدرجات كل الأوزان التي تستخدمه

This is the same rule that governs any shared parameter, such as one convolution filter used at every image position: a parameter used in several places receives the sum of the gradients from all of them.

هذه هي نفس القاعدة التي تحكم أي معامل مشترك، مثل مرشّح convolution واحد يُستخدم في كل موضع من الصورة: المعامل المستخدم في عدة أماكن يتلقى مجموع الـ gradients من كل تلك الأماكن.

Recall

Why is the index map not trained during fine-tuning? Give two reasons.لماذا لا تُدرَّب خريطة الفهارس أثناء الـ fine-tuning؟ اذكر سببين.

Indexes are discrete integers, so they have no gradient: a tiny change has no meaning. And keeping them fixed keeps the stored compressed format (the 2-bit map and its storage cost) valid throughout training; changing them would amount to re-clustering.

الفهارس أعداد صحيحة منفصلة (discrete)، لذلك ليس لها gradient: التغيير الصغير جدًا لا معنى له. كما أن تثبيتها يُبقي الصيغة المضغوطة المخزّنة (خريطة 2-bit وكلفة تخزينها) صالحة طوال التدريب؛ وتغييرها يعادل إعادة التجميع.

Slide 29 turns the formula into three mechanical moves you can do with a pencil: group the gradients by colour, reduce each group to one number, then take a gradient-descent step on each centroid.

الشريحة 29 تحوّل الصيغة إلى ثلاث حركات آلية يمكنك تنفيذها بالقلم: جمّع الـ gradients حسب اللون، واختزل كل مجموعة إلى رقم واحد، ثم خذ خطوة gradient descent على كل مركز.

Group, reduce, updateجمّع، اختزل، حدّث

  1. Group by index. Lay the gradient matrix over the index map. Every gradient joins the group of the index at the same position. Read in row-major order, blue (index 3) collects -0.03, 0.12, 0.02, -0.07.التجميع حسب الفهرس (group by index). ضع مصفوفة الـ gradients فوق خريطة الفهارس. كل gradient ينضم إلى مجموعة الفهرس الموجود في الموضع نفسه. بالقراءة صفًا بعد صف (row-major)، يجمع الأزرق (الفهرس 3) القيم -0.03, 0.12, 0.02, -0.07.
  2. Reduce by sum. Add up each group. The result is ∂L/∂Ck\partial L / \partial C_k.الاختزال بالجمع (reduce by sum). اجمع كل مجموعة. الناتج هو ∂L/∂Ck\partial L / \partial C_k.
  3. Update. Multiply by the learning rate and subtract from the old centroid.التحديث (update). اضرب في معدل التعلم واطرح الناتج من المركز القديم.
Ck←Ck−lr⋅∑Iij=k∂L∂WijC_k \leftarrow C_k - \text{lr}\cdot \sum_{I_{ij}=k} \frac{\partial L}{\partial W_{ij}}
Centroid updateتحديث المركز (centroid)
Sixteen gradients flow into four sums, and each sum moves one centroid: 2.00 to 1.96, 1.50 to 1.48, 0.00 to -0.04, -1.00 to -0.97ستة عشر gradient تتدفق إلى أربعة مجاميع، وكل مجموع يحرّك مركزًا واحدًا: 2.00 إلى 1.96، و 1.50 إلى 1.48، و 0.00 إلى -0.04، و -1.00 إلى -0.97

Worked exampleمثال محلول

Slide 29 by handالشريحة 29 يدويًا

  1. Group and reduceجمّع واختزل

    Cluster 3: -0.03 + 0.12 + 0.02 - 0.07 = 0.04. Cluster 2: 0.03 + 0.01 - 0.02 = 0.02. Cluster 1: 0.02 - 0.01 + 0.01 + 0.04 - 0.02 = 0.04. Cluster 0: -0.01 - 0.02 - 0.01 + 0.01 = -0.03.
    المجموعة 3: -0.03 + 0.12 + 0.02 - 0.07 = 0.04. المجموعة 2: 0.03 + 0.01 - 0.02 = 0.02. المجموعة 1: 0.02 - 0.01 + 0.01 + 0.04 - 0.02 = 0.04. المجموعة 0: -0.01 - 0.02 - 0.01 + 0.01 = -0.03.
  2. Check the countsتحقق من الأعداد

    The groups hold 4 + 3 + 5 + 4 = 16 gradients, one per weight, and the sizes match the clusters of the first concept.
    المجموعات تضم 4 + 3 + 5 + 4 = 16 gradient، واحدًا لكل وزن، وأحجامها تطابق مجموعات المفهوم الأول.
  3. Update with lr = 1حدّث مع lr = 1

    2.00 - 0.04 = 1.96, 1.50 - 0.02 = 1.48, 0.00 - 0.04 = -0.04, -1.00 - (-0.03) = -0.97.
    2.00 - 0.04 = 1.96، 1.50 - 0.02 = 1.48، 0.00 - 0.04 = -0.04، -1.00 - (-0.03) = -0.97.
  4. Resultالنتيجة

    Fine-tuned codebook 3: 1.96, 2: 1.48, 1: -0.04, 0: -0.97. The index map is unchanged, so every blue weight is now 1.96.
    دفتر الرموز بعد الضبط: 3: 1.96 و 2: 1.48 و 1: -0.04 و 0: -0.97. خريطة الفهارس لم تتغير، لذلك كل وزن أزرق أصبح الآن 1.96.
Index (colour)الفهرس (اللون)Grouped gradientsالـ gradients المجمّعةSumالمجموعNew centroidالمركز الجديد
3 (blue)3 (أزرق)-0.03 + 0.12 + 0.02 - 0.07-0.03 + 0.12 + 0.02 - 0.070.040.042.00 - 0.04 = 1.962.00 - 0.04 = 1.96
2 (pink)2 (وردي)0.03 + 0.01 - 0.020.03 + 0.01 - 0.020.020.021.50 - 0.02 = 1.481.50 - 0.02 = 1.48
1 (orange)1 (برتقالي)0.02 - 0.01 + 0.01 + 0.04 - 0.020.02 - 0.01 + 0.01 + 0.04 - 0.020.040.040.00 - 0.04 = -0.040.00 - 0.04 = -0.04
0 (green)0 (أخضر)-0.01 - 0.02 - 0.01 + 0.01-0.01 - 0.02 - 0.01 + 0.01-0.03-0.03-1.00 + 0.03 = -0.97-1.00 + 0.03 = -0.97
Slide 29 grouped and reduced, then updated with lr = 1الشريحة 29 بعد التجميع والاختزال، ثم التحديث مع lr = 1

Watch the signs. A positive summed gradient means the loss rises as the centroid rises, so the centroid moves down: blue falls from 2.00 to 1.96. Green's sum is negative, so it moves up, from -1.00 to -0.97. Step through it with a different learning rate below.

انتبه للإشارات. الـ gradient المجموع الموجب يعني أن الخسارة ترتفع عندما يرتفع المركز، لذلك يتحرك المركز إلى الأسفل: الأزرق ينخفض من 2.00 إلى 1.96. مجموع الأخضر سالب، لذلك يتحرك إلى الأعلى، من -1.00 إلى -0.97. جرّب الخطوات بمعدل تعلم مختلف في الأسفل.

SimulatorمحاكيCentroid fine-tuning: group, reduce, then step against the gradientالضبط الدقيق للمراكز: جمّع، اختزل، ثم خذ خطوة عكس التدرّج
  1. 1. Gradient1. التدرّج
  2. 2. Group by index2. التجميع حسب الفهرس
  3. 3. Reduce (sum)3. الاختزال (الجمع)
  4. 4. Centroid - lr x sum4. المركز - lr x المجموع
Gradient dL/dW, coloured by indexالتدرّج dL/dW ملوّنا حسب الفهرسfrom backprop
Cluster index (frozen)فهرس العنقود (مجمّد)2-bit uint
  • index 3-0.03 + 0.12 + 0.02 - 0.07= 0.042.00 - 1 x 0.04 = 1.96
  • index 20.03 + 0.01 - 0.02= 0.021.50 - 1 x 0.02 = 1.48
  • index 10.02 - 0.01 + 0.01 + 0.04 - 0.02= 0.040.00 - 1 x 0.04 = -0.04
  • index 0-0.01 - 0.02 - 0.01 + 0.01= -0.03-1.00 - 1 x -0.03 = -0.97
Backpropagation gives one gradient per weight position, the 4 x 4 matrix on the left. The index map beside it is frozen.يعطي الانتشار العكسي backpropagation تدرّجا واحدا لكل موضع وزن، وهو المصفوفة 4 x 4 على اليسار. أما خريطة الفهارس بجانبها فمجمّدة.

Gradients and centroids are the slide 28 and 29 values from Han et al., Figure 3. The slide prints x lr without a value; its fine-tuned centroids only follow with lr = 1. Try lr = 0.1 to see the realistic, much smaller step.التدرّجات والمراكز هي قيم الشريحتين 28 و29 المأخوذة من Han et al.، الشكل 3. تكتب الشريحة x lr دون قيمة، ولا تتحقق مراكزها بعد الضبط الدقيق إلا مع lr = 1. جرّب lr = 0.1 لترى الخطوة الواقعية الأصغر بكثير.

Worked exampleمثال محلول

The same gradients with lr = 0.5نفس الـ gradients مع lr = 0.5

  1. Scale the sumsاضرب المجاميع في معدل التعلم

    0.5 × 0.04 = 0.02, 0.5 × 0.02 = 0.01, 0.5 × 0.04 = 0.02, 0.5 × (-0.03) = -0.015.
    0.5 × 0.04 = 0.02، 0.5 × 0.02 = 0.01، 0.5 × 0.04 = 0.02، 0.5 × (-0.03) = -0.015.
  2. Resultالنتيجة

    2.00 - 0.02 = 1.98, 1.50 - 0.01 = 1.49, 0.00 - 0.02 = -0.02, -1.00 + 0.015 = -0.985.
    2.00 - 0.02 = 1.98، 1.50 - 0.01 = 1.49، 0.00 - 0.02 = -0.02، -1.00 + 0.015 = -0.985.

Quick checkاختبار سريع

Centroid 0 is -1.00 and its gradients sum to -0.03. With lr = 0.5, what is the fine-tuned centroid?المركز 0 قيمته -1.00 ومجموع gradients الخاصة به -0.03. مع lr = 0.5، ما قيمة المركز بعد الـ fine-tuning؟

Quick checkاختبار سريع

During centroid fine-tuning, what happens to the 2-bit index matrix?أثناء الـ fine-tuning للمراكز، ماذا يحدث لمصفوفة الفهارس ذات الـ 2 بت؟

Recall

Without looking, reduce the slide 29 gradients for cluster 1 (orange) and give its new centroid at lr = 1.دون أن تنظر، اختزل gradients الشريحة 29 للمجموعة 1 (البرتقالية) وأعطِ مركزها الجديد عند lr = 1.

0.02 - 0.01 + 0.01 + 0.04 - 0.02 = 0.04, so 0.00 - 1 × 0.04 = -0.04.

0.02 - 0.01 + 0.01 + 0.04 - 0.02 = 0.04، إذن 0.00 - 1 × 0.04 = -0.04.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • K-means (Lloyd's algorithm) alternates two steps: assign each weight to its nearest centroid, then move each centroid to the mean of its members.خوارزمية K-means (خوارزمية Lloyd) تتناوب بين خطوتين: أسند كل وزن إلى أقرب مركز، ثم انقل كل مركز إلى متوسط أعضائه.
  • On the slide example, k = 4 gives centroids -1.00, 0.00, 1.50 and 2.00, each the exact mean of its 4, 5, 3 and 4 weights.في مثال الشريحة، k = 4 يعطي المراكز -1.00 و 0.00 و 1.50 و 2.00، وكل منها هو المتوسط الدقيق لأوزانه البالغ عددها 4 و 5 و 3 و 4 على الترتيب.
  • Store an N-bit index per weight plus a codebook of 2^N FP32 centroids. Reconstructed weight = codebook[index].خزّن فهرسًا من N بت لكل وزن، زائد دفتر رموز (codebook) فيه 2^N مركزًا بصيغة FP32. الوزن المُعاد بناؤه = codebook[index].
  • Storage: 32M bits before, NM + 32 x 2^N bits after. The example drops 64 B to 4 B + 16 B = 20 B, a 3.2x saving.التخزين: 32M بت قبل، و NM + 32 x 2^N بت بعد. في المثال ينخفض الحجم من 64 B إلى 4 B + 16 B = 20 B، أي توفير بمقدار 3.2x.
  • When M is much larger than 2^N the codebook is negligible and the ratio tends to 32/N (8.0x for N = 4 and a million weights).عندما تكون M أكبر بكثير من 2^N يصبح دفتر الرموز مهملًا وتقترب النسبة من 32/N (أي 8.0x عند N = 4 ومليون وزن).
  • The index map is frozen during fine-tuning. Only the centroids train, with dL/dC_k = sum of dL/dW over the weights whose index is k.خريطة الفهارس مجمّدة أثناء الـ fine-tuning. المراكز وحدها تتدرّب، مع dL/dC_k = مجموع dL/dW على كل الأوزان التي فهرسها k.
  • Slide 29: sums 0.04, 0.02, 0.04, -0.03 give fine-tuned centroids 1.96, 1.48, -0.04, -0.97, which implies lr = 1.الشريحة 29: المجاميع 0.04 و 0.02 و 0.04 و -0.03 تعطي مراكز بعد الضبط 1.96 و 1.48 و -0.04 و -0.97، وهذا يعني أن lr = 1.

Sourcesالمصادر