Lecture 05-1 · Quantization I

المحاضرة 05-1 · التكميم (Quantization I)

Deep Compression: bits, Huffman, results

الـ Deep Compression: البتات، وHuffman، والنتائج

Slides 33–43الشرائح 33–43 P2 SHOULD

Deep Compression chains three stages: pruning cuts the number of weights, K-means quantization cuts the bits per weight, and Huffman coding cuts the bits per stored symbol. Each stage keeps accuracy, and the savings multiply to 35x on AlexNet and 49x on VGG.

الـ Deep Compression يسلسل ثلاث مراحل: الـ pruning يقلّل عدد الأوزان، وK-means quantization يقلّل البتات لكل وزن، وHuffman coding يقلّل البتات لكل رمز مخزَّن. كل مرحلة تحافظ على الدقة، والتوفيرات تتضاعف لتصل إلى 35x على AlexNet و49x على VGG.

What quantization does to the weight histogram

ماذا يفعل التكميم بالمدرّج التكراري (histogram) للأوزان

Slides 33 to 35 show one AlexNet layer's weights three times. Before quantization the histogram is continuous and bimodal, with a gap around zero: this layer was already pruned, so the small-magnitude weights are gone. After quantization it becomes a set of discrete spikes, one per centroid (about 16 here). After quantization + retraining the spikes are still discrete, just nudged to slightly better positions by centroid fine-tuning. Note the y-axis jumps from 103 to 104: the same weights now pile into a handful of values.

تعرض الشرائح من 33 إلى 35 أوزان طبقة واحدة من AlexNet ثلاث مرات. قبل التكميم يكون المدرّج التكراري (histogram) continuous وثنائي القمة، مع فجوة حول الصفر: هذه الطبقة مقلَّمة مسبقاً، فالأوزان الصغيرة أُزيلت. بعد التكميم يصبح مجموعة من القمم المنفصلة (discrete)، واحدة لكل centroid (نحو 16 هنا). بعد التكميم وإعادة التدريب تبقى القمم منفصلة، لكنها تُزاح قليلاً إلى مواضع أفضل بفضل ضبط الـ centroids. لاحظ أن المحور الرأسي يقفز من 103 إلى 104: نفس الأوزان تتكدّس الآن في عدد قليل من القيم.

How many bits? Conv about 4, FC about 2

كم بتاً نحتاج؟ Conv نحو 4، وFC نحو 2

Slide 36 sweeps the bits per effective (non-zero) weight separately for all conv layers and all FC layers of AlexNet. Accuracy holds down to 4 bits for conv layers and 2 bits for FC layers, then collapses (conv at 2 bits and FC at 1 bit are near 0 percent). The pruned + quantized curves sit on top of the quantized-only curves, so pruning first does not make the network harder to quantize.

تمسح الشريحة 36 عدد البتات لكل وزن فعّال (غير صفري) بشكل منفصل لكل طبقات conv ولكل طبقات FC في AlexNet. تصمد الدقة حتى 4 bits لطبقات conv و2 bits لطبقات FC، ثم تنهار (طبقات conv عند 2 bits وطبقات FC عند 1 bit قرب 0%). ومنحنيات pruned + quantized تنطبق على منحنيات quantized only، أي أن الـ pruning أولاً لا يجعل التكميم أصعب.

Why the difference? Conv layers have few weights, each reused at every spatial position, so each weight carries more information. FC layers are huge and highly redundant, so they tolerate coarser values.

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

Huffman coding: variable-length codes

الـ Huffman coding: رموز متغيرة الطول

After quantization each weight is an index into a small codebook, but the indices are not equally common. Slide 37's histogram of 32 effective weights (5-bit indices) is strongly bimodal. Huffman coding gives frequent symbols fewer bits and infrequent symbols more bits. It is applied to both the weights (the cluster indices) and the sparse position index left by pruning. It is lossless, so accuracy is unchanged.

بعد التكميم يصبح كل وزن فهرساً في codebook صغير، لكن الفهارس ليست متساوية التكرار. مدرّج الشريحة 37 لـ 32 وزناً فعّالاً (فهارس 5-bit) ثنائي القمة بوضوح. الـ Huffman coding يعطي الرموز المتكررة بتات أقل، والرموز النادرة بتات أكثر. ويُطبَّق على الأوزان (فهارس المجموعات) وعلى فهرس المواضع المتناثر الذي يتركه الـ pruning. وهو lossless، فلا تتغير الدقة.

Workedمثال محلول
Four cluster indices with frequencies 50%, 25%, 12.5%, 12.5%

Fixed-length      every index = 2 bit               average = 2 bit
Huffman codes     A: 0     B: 10    C: 110    D: 111
average bits    = 0.5 x 1 + 0.25 x 2 + 0.125 x 3 + 0.125 x 3
                = 0.5 + 0.5 + 0.375 + 0.375 = 1.75 bit

saving          = 2 / 1.75 = 1.14x on top of quantization, with zero error

The more skewed the histogram, the bigger the Huffman gain. A flat histogram gains nothing.

كلما كان المدرّج أكثر انحرافاً كان مكسب Huffman أكبر. والمدرّج المسطّح لا يكسب شيئاً.

The full pipeline

السلسلة الكاملة (pipeline)

original network PRUNING Train connectivity Prune connections Train weights fewer weights QUANTIZATION Cluster the weights Generate codebook Quantize with codebook Retrain codebook fewer bits per weight HUFFMAN Encode weights Encode index fewer bits per symbol 9-13x 27-31x 35-49x same acc. same acc. same acc.
Slide 38. The ratios on the arrows are cumulative from the original size, not per stage. The loops (prune then retrain, quantize then retrain codebook) are what keep accuracy unchanged.
الشريحة 38. النسب على الأسهم تراكمية من الحجم الأصلي، وليست لكل مرحلة على حدة. والحلقات (prune ثم إعادة تدريب، وquantize ثم إعادة تدريب الـ codebook) هي ما يُبقي الدقة دون تغيير.

Read the ratios as running totals. The low ends of the ranges are AlexNet and the high ends are VGG-16. Dividing consecutive totals gives each stage's own contribution:

اقرأ النسب كمجاميع تراكمية. الطرف الأدنى من كل مدى هو AlexNet والطرف الأعلى هو VGG-16. وبقسمة المجاميع المتتالية تحصل على إسهام كل مرحلة وحدها:

Workedمثال محلول
AlexNet   pruning 9x  ->  + quantization 27x  ->  + Huffman 35x
          quantization alone adds 27 / 9  = 3.0x
          Huffman alone adds      35 / 27 = 1.3x
          check  9 x 3.0 x 1.3 = 35.1

VGG-16    13x -> 31x -> 49x
          quantization adds 31 / 13 = 2.4x,  Huffman adds 49 / 31 = 1.6x

AlexNet size   240 MB / 35 = 6.9 MB

Ratios multiply; they never add. 9x pruning plus 3x quantization is 27x, not 12x.

النسب تتضاعف ولا تُجمع. 9x من الـ pruning مع 3x من التكميم تعطي 27x لا 12x.

Results across networks

النتائج عبر الشبكات

NetworkالشبكةOriginalالأصليCompressedبعد الضغطRatioالنسبةOrig. acc.الدقة الأصليةComp. acc.الدقة بعد الضغط
LeNet-3001070 KB27 KB40x98.36%98.42%
LeNet-51720 KB44 KB39x99.20%99.26%
AlexNet240 MB6.9 MB35x80.27%80.30%
VGGNet550 MB11.3 MB49x88.68%89.09%
GoogleNet28 MB2.8 MB10x88.90%88.92%
ResNet-1844.6 MB4.0 MB11x89.24%89.28%

يلخّص الجدول نتائج الشريحة 39. في كل الشبكات لم تنخفض الدقة، بل ارتفعت قليلاً. الشبكات الكبيرة الغنية بطبقات FC (AlexNet وVGG) تنضغط بين 35x و49x، بينما الشبكات المصممة لتكون مدمجة أصلاً (GoogleNet وResNet-18) تنضغط نحو 10x فقط، لأن فيها تكراراً أقل.

Accuracy never drops; it even rises slightly. FC-heavy giants (AlexNet, VGG) compress 35x to 49x, while networks that were already compact by design (GoogleNet, ResNet-18) compress only about 10x because there is less redundancy left.

الدقة لا تنخفض أبداً، بل ترتفع قليلاً، والشبكات المدمجة أصلاً تنضغط أقل لأن التكرار المتبقي فيها أقل.

SqueezeNet: compact by design, then compressed

الـ SqueezeNet: مدمج بالتصميم، ثم مضغوط

SqueezeNet (Iandola et al., 2016) reaches AlexNet-level accuracy with 50x fewer parameters by building the network from fire modules: a 1 x 1 squeeze layer cuts the channels (64 to 16), then a 1 x 1 expand and a 3 x 3 expand (64 channels each) run in parallel and are concatenated to 128 channels.

الـ SqueezeNet (Iandola et al., 2016) يصل إلى دقة بمستوى AlexNet بعدد معاملات أقل بـ 50x، ببناء الشبكة من fire modules: طبقة 1 x 1 squeeze تقلّص القنوات (من 64 إلى 16)، ثم تعمل طبقتا 1 x 1 expand و3 x 3 expand (64 قناة لكل منهما) بالتوازي وتُدمجان (concat) إلى 128 قناة.

Input 1 x 1 conv squeeze 1 x 1 conv expand 3 x 3 conv expand Concat 64 16 64 64 128 out squeeze: 64 x 16 = 1,024 1x1 expand: 16 x 64 = 1,024 3x3 expand: 16 x 64 x 9 = 9,216 weights only, bias ignored
Slide 40's fire module. The squeeze to 16 channels is what makes the expensive 3 x 3 filters cheap: they see only 16 input channels.
الـ fire module في الشريحة 40. التقليص (squeeze) إلى 16 قناة هو ما يجعل مرشحات 3 x 3 المكلفة رخيصة: فهي لا ترى إلا 16 قناة دخل.
Workedمثال محلول
Fire module (64 in, 128 out), weights only
  squeeze   1x1, 64 -> 16      64 x 16       =  1,024
  expand    1x1, 16 -> 64      16 x 64       =  1,024
  expand    3x3, 16 -> 64      16 x 64 x 9   =  9,216
  total                                      = 11,264

Plain 3x3 conv, 64 -> 128      64 x 128 x 9  = 73,728
ratio  73,728 / 11,264 = 6.5x fewer weights
NetworkالشبكةApproachالطريقةSizeالحجمRatio vs AlexNetالنسبة مقارنة بـ AlexNetTop-1Top-5
AlexNetnoneبلا ضغط240 MB1x57.2%80.3%
AlexNetSVD48 MB5x56.0%79.4%
AlexNetDeep Compression6.9 MB35x57.2%80.3%
SqueezeNetnoneبلا ضغط4.8 MB50x57.5%80.3%
SqueezeNetDeep Compression0.47 MB510x57.5%80.3%

نتائج الشريحة 41: الـ SVD يعطي 5x فقط ويخسر دقة (56.0% مقابل 57.2%). الـ Deep Compression على AlexNet يعطي 35x بلا خسارة. والـ SqueezeNet بتصميمه وحده أصغر بـ 50x، ومع Deep Compression يصل إلى 0.47 MB، أي 510x أصغر من AlexNet بدقة top-1 قدرها 57.5%. وإسهام Deep Compression على SqueezeNet وحده هو 4.8 / 0.47 ≈ 10x.

The 510x is relative to AlexNet's 240 MB. Deep Compression's own contribution on SqueezeNet is 4.8 / 0.47 ≈ 10x, consistent with the roughly 10x seen on other compact nets: good architecture design and compression stack multiplicatively (50 x 10 ≈ 510).

النسبة 510x محسوبة نسبة إلى 240 MB لـ AlexNet. والتصميم الجيد للبنية والضغط يتضاعفان معاً (50 x 10 ≈ 510).

At runtime: decode, then compute in float

وقت التشغيل: فك الترميز (decode) ثم الحساب بالأعداد العشرية

Slide 42 closes the K-means story. In storage the model is quantized indices plus a float codebook. At inference a decode step looks up each index in the codebook (a lookup table) and produces float weights; the conv, bias add and ReLU all run in floating point. So K-means-based quantization only saves storage: all computation and memory access during compute are still floating point. Getting integer arithmetic needs linear quantization, the next topic.

الشريحة 42 تختم قصة K-means. في التخزين يكون النموذج فهارس مكمّمة مع codebook عشري. وقت الاستدلال تبحث خطوة decode عن كل فهرس في الـ codebook (جدول بحث، lookup table) فتنتج أوزاناً عشرية، ثم يعمل الـ conv وإضافة الـ bias والـ ReLU كلها بالحساب العشري. لذلك فإن K-means-based quantization يوفّر التخزين فقط: كل الحساب والوصول إلى الذاكرة أثناءه يبقيان عشريين. وللحصول على حساب بالأعداد الصحيحة نحتاج linear quantization، وهو الموضوع التالي.

Trapفخ

Compression ratios compound multiplicatively. The 9x, 27x, 35x on slide 38 are cumulative. Never add stage ratios, and never read 27x as "quantization alone".

نسب الضغط تتراكم بالضرب. القيم 9x و27x و35x في الشريحة 38 تراكمية. لا تجمع نسب المراحل أبداً، ولا تقرأ 27x على أنها "التكميم وحده".

Deep Compression does not speed up arithmetic. It shrinks the model file (and DRAM traffic for weights); weights are decoded to float and every MAC is a float MAC.

الـ Deep Compression لا يسرّع الحساب. هو يصغّر ملف النموذج (وحركة DRAM للأوزان)، والأوزان تُفك إلى أعداد عشرية، وكل MAC هو MAC عشري.

Huffman is lossless and adds no accuracy risk. The accuracy-sensitive stages are pruning and quantization; that is why only those two have retraining loops.

الـ Huffman بلا فقد (lossless) ولا يضيف أي خطر على الدقة. المرحلتان الحساستان للدقة هما الـ pruning والتكميم، ولهذا لهما وحدهما حلقات إعادة تدريب.

510x is versus AlexNet, not versus SqueezeNet.

النسبة 510x مقارنة بـ AlexNet، لا بـ SqueezeNet.

Memory hookمفتاح للحفظ

Prune the count, quantize the bits, Huffman the code: 9, 27, 35 for AlexNet. Conv needs 4 bits, FC needs 2.

قلّم العدد، كمّم البتات، رمّز بـ Huffman: 9 ثم 27 ثم 35 لـ AlexNet. الـ conv يحتاج 4 bits، والـ FC يحتاج 2.

Check yourself

اختبر نفسك

In Deep Compression on AlexNet, pruning gives 9x and pruning + quantization gives 27x. How much does quantization contribute on its own?

في Deep Compression على AlexNet، يعطي الـ pruning 9x، ويعطي الـ pruning مع التكميم 27x. كم يسهم التكميم وحده؟

B. Ratios are cumulative and multiply: 27 / 9 = 3. A subtracts (27 - 9) as if ratios added. C reads the cumulative total as the stage's own ratio. D adds 9 + 27.

B. النسب تراكمية وتتضاعف: 27 / 9 = 3. الخيار A يطرح (27 - 9) كأن النسب تُجمع. والخيار C يقرأ المجموع التراكمي كأنه نسبة المرحلة وحدها. والخيار D يجمع 9 + 27.

According to slide 36, roughly how many bits per effective weight do AlexNet's conv and FC layers need before accuracy collapses?

وفق الشريحة 36، كم بتاً تقريباً لكل وزن فعّال تحتاج طبقات conv وطبقات FC في AlexNet قبل أن تنهار الدقة؟

A. The red circles on slide 36 mark 4 bits for conv and 2 bits for FC. B swaps them; FC layers are the more redundant ones, so they tolerate fewer bits. C is far more than needed, and D collapses to near 0 percent accuracy.

A. الدوائر الحمراء في الشريحة 36 تحدد 4 bits للـ conv و2 bits للـ FC. الخيار B يعكسهما، وطبقات FC هي الأكثر تكراراً فتتحمّل بتات أقل. والخيار C أكثر بكثير من اللازم، والخيار D ينهار إلى دقة قرب 0 بالمئة.

Four quantized indices occur with frequencies 50%, 25%, 12.5% and 12.5%. Huffman assigns codes of length 1, 2, 3 and 3. What is the average code length?

أربعة فهارس مكمّمة تتكرر بنسب 50% و25% و12.5% و12.5%. يعطيها Huffman رموزاً بأطوال 1 و2 و3 و3. ما متوسط طول الرمز؟

D. 0.5 x 1 + 0.25 x 2 + 0.125 x 3 + 0.125 x 3 = 0.5 + 0.5 + 0.375 + 0.375 = 1.75. A is the fixed-length cost. B is the unweighted mean of the code lengths (9 / 4). Frequent symbols get short codes, so the weighted average drops below 2.

D. 0.5 x 1 + 0.25 x 2 + 0.125 x 3 + 0.125 x 3 = 0.5 + 0.5 + 0.375 + 0.375 = 1.75. الخيار A هو كلفة الطول الثابت. والخيار B هو المتوسط غير الموزون لأطوال الرموز (9 / 4). الرموز المتكررة تأخذ رموزاً قصيرة، فينخفض المتوسط الموزون تحت 2.

SqueezeNet with Deep Compression is 0.47 MB. Which statement is correct?

حجم SqueezeNet مع Deep Compression هو 0.47 MB. أي عبارة صحيحة؟

C. 240 / 0.47 ≈ 510, and top-1 stays at 57.5 percent (top-5 80.3 percent). A is wrong because Deep Compression's own factor on SqueezeNet is 4.8 / 0.47 ≈ 10x. B invents an accuracy loss. D is uncompressed SqueezeNet (4.8 MB).

C. 240 / 0.47 ≈ 510، ودقة top-1 تبقى 57.5 بالمئة (وtop-5 80.3 بالمئة). الخيار A خطأ لأن عامل Deep Compression نفسه على SqueezeNet هو 4.8 / 0.47 ≈ 10x. والخيار B يختلق خسارة في الدقة. والخيار D هو SqueezeNet غير المضغوط (4.8 MB).

At inference, a Deep-Compressed layer's conv is computed in which number format?

وقت الاستدلال، بأي صيغة عددية يُحسب الـ conv في طبقة مضغوطة بـ Deep Compression؟

B. Slide 42: quantized weights (uint) pass through a decode step to float weights, and the conv, bias add and ReLU all run in float. The indices (A) are only pointers, Huffman codes (C) are decompressed before use, and XNOR (D) belongs to binary networks.

B. الشريحة 42: الأوزان المكمّمة (uint) تمر بخطوة decode إلى أوزان عشرية، ثم يعمل الـ conv وإضافة الـ bias والـ ReLU كلها بالحساب العشري. الفهارس (A) مجرد مؤشرات، ورموز Huffman (C) تُفك قبل الاستخدام، والـ XNOR (D) يخص الشبكات الثنائية.