Lecture 04-1 · Pruning and Sparsity I
المحاضرة 04-1 · التقليم والتناثر (Pruning and Sparsity I)
Prune, fine-tune, iterate: what it buys
قلّم، أعد الضبط (fine-tune)، كرّر: ماذا نكسب
P2 SHOULDCutting weights alone is free only to about 50 percent and costs about 4 points at 80; retraining the survivors pushes that to 80 without loss, and iterating pushes it to 90. Fewer parameters is not the same as fewer FLOPs, and neither is speed without hardware support.
قطع الأوزان وحده مجاني فقط حتى نحو 50% ويكلّف نحو 4 نقاط عند 80%؛ وإعادة تدريب الأوزان الباقية تجعله يصل إلى 80% بلا خسارة، والتكرار يدفعه إلى 90%. عدد معاملات أقل لا يعني FLOPs أقل بالضرورة، ولا أيّ منهما يعني سرعة بدون دعم من العتاد.
The recipe: train, prune, retrain
الوصفة: درّب، قلّم، أعد التدريب
Slides 7 to 10 build up Han et al.'s method (NeurIPS 2015) one box at a time:
الشرائح من 7 إلى 10 تبني طريقة Han et al. (NeurIPS 2015) مربعاً بعد مربع:
- Train connectivity. Train the dense network normally. This learns not only the weights but which connections matter.
Train connectivity. درّب الشبكة الكثيفة بشكل عادي. هذا لا يتعلم الأوزان فقط، بل يتعلم أي الاتصالات مهمة.
- Prune connections. Remove every weight whose magnitude is below a threshold. They are set to zero and masked so they stay zero.
Prune connections. احذف كل وزن مقداره أقل من عتبة (threshold). تُجعل صفراً وتُغطّى بقناع (mask) حتى تبقى صفراً.
- Train weights. Retrain (fine-tune) the surviving weights with the mask fixed, so they compensate for what was removed.
Train weights. أعد تدريب الأوزان الباقية (fine-tune) مع تثبيت القناع، حتى تعوّض ما حُذف.
- Iterate. Slide 10 adds a loop arrow from "Train Weights" back to "Prune Connections": prune a bit more, retrain, repeat, raising sparsity gradually.
التكرار. تضيف الشريحة 10 سهماً راجعاً من "Train Weights" إلى "Prune Connections": احذف قليلاً أكثر، أعد التدريب، وكرّر، فترتفع sparsity تدريجياً.
يساراً: المربعات الثلاثة من الشرائح 7 إلى 10 مع حلقة التكرار التي تضيفها الشريحة 10. يميناً: إعادة رسم لمنحنى الشريحة. كل خطوة تدفع "الجرف" (cliff) أبعد إلى اليمين: النموذج نفسه والمعيار نفسه (magnitude)، والفرق الوحيد هو إعادة التدريب.
Reading the three curves
قراءة المنحنيات الثلاثة
| Methodالطريقة | Where accuracy holdsأين تصمد الدقة | Where it breaksأين تنهار | Max ratio at no lossأقصى نسبة بلا خسارة | Reductionالتخفيض |
|---|---|---|---|---|
| Prune only (slide 8)تقليم فقط (الشريحة 8) | about 50% (−0.1%)نحو 50% (−0.1%) | about −1% at 67%, about −2% at 75%, about −4% at 80%نحو −1% عند 67%، ونحو −2% عند 75%، ونحو −4% عند 80% | ~50% | 2x |
| Prune + finetune (slide 9)تقليم + إعادة ضبط (الشريحة 9) | flat, even slightly positive, to ~80%ثابتة، بل موجبة قليلاً، حتى ~80% | about −2% near 90%نحو −2% قرب 90% | ~80% | 5x |
| Iterative prune + finetune (slide 10)تقليم + إعادة ضبط تكراري (الشريحة 10) | flat to ~90%ثابتة حتى ~90% | steep drop past ~92%هبوط حاد بعد ~92% | ~90% | ~9x to 10x~9x إلى 10x |
يلخّص الجدول المنحنيات الثلاثة: الحذف وحده يصمد حتى حوالي 50% ثم يخسر حوالي 1% عند 67% وحوالي 4% عند 80%. الحذف مع fine-tune يبقى مستوياً (بل يتحسن قليلاً) حتى حوالي 80%. والتكرار يبقى مستوياً حتى حوالي 90%. العمود الأخير يحوّل النسبة إلى reduction factor: 50% تعني 2x، و80% تعني 5x، و90% تعني 10x.
The improvement from iterating is the lecture's "5x to 9x" for AlexNet: one-shot prune and retrain gets about 80 percent (5x) without loss, iterative gets about 89 percent (9x, the 61M to 6.7M of slide 11).
التحسّن الذي يجلبه التكرار هو "من 5x إلى 9x" لـ AlexNet في المحاضرة: الحذف مرة واحدة مع إعادة التدريب يصل إلى حوالي 80% (5x) بلا خسارة، والتكرار يصل إلى حوالي 89% (9x، أي 61M إلى 6.7M في الشريحة 11).
The weight histograms
المدرّجات التكرارية للأوزان
The small insets on slides 7 to 9 tell the story of the weights themselves:
الرسوم الصغيرة داخل الشرائح 7 إلى 9 تحكي قصة الأوزان نفسها:
Trained
بعد التدريب
One bell curve centred on 0, most weights in roughly ±0.05.
منحنى جرسي واحد متمركز عند 0، ومعظم الأوزان في حدود ±0.05 تقريباً.
Pruned
بعد التقليم
The same curve with a hole cut out around 0: the small-magnitude weights are gone.
المنحنى نفسه مع فجوة مقطوعة حول 0: الأوزان صغيرة المقدار حُذفت.
Retrained
بعد إعادة التدريب
Bimodal: two smooth humps on either side of 0, spread wider. The survivors moved outward to carry the removed weights' work.
ثنائي القمة (bimodal): سنامان ناعمان على جانبي 0 وأكثر انتشاراً. الأوزان الباقية تحركت للخارج لتحمل عمل الأوزان المحذوفة.
This is also why iterating helps: after retraining, the distribution has changed, so the next magnitude cut removes a different, now-least-important set instead of guessing everything at once.
وهذا أيضاً سبب فائدة التكرار: بعد إعادة التدريب يتغير التوزيع، فيحذف القطع التالي حسب magnitude مجموعة مختلفة هي الآن الأقل أهمية، بدلاً من تخمين كل شيء دفعة واحدة.
Reduction factor = 1 / (1 − pruning ratio)
معامل التخفيض = 1 / (1 − نسبة التقليم)
Pruning ratio r is the fraction removed (sparsity). The model keeps 1 − r of its weights, so it is 1 / (1 − r) times smaller. The function is very nonlinear near 100 percent:
pruning ratio أي r هو النسبة المحذوفة (sparsity). يُبقي النموذج 1 − r من أوزانه، فيصغر بمقدار 1 / (1 − r) مرة. هذه الدالة غير خطية بشدة قرب 100%:
| Pruning ratio rنسبة التقليم r | Kept 1 − rالمتبقي 1 − r | Reduction 1/(1 − r)التخفيض 1/(1 − r) |
|---|---|---|
| 50% | 0.50 | 2x |
| 80% | 0.20 | 5x |
| 89% | 0.11 | 9.1x |
| 90% | 0.10 | 10x |
| 95% | 0.05 | 20x |
| 99% | 0.01 | 100x |
يحوّل الجدول نسبة الحذف إلى عامل التصغير. ما يجب أن تلاحظه: من 50% إلى 80% ينتقل العامل من 2x إلى 5x فقط، بينما من 90% إلى 95% يتضاعف من 10x إلى 20x. كل نقطة مئوية قرب النهاية تساوي أكثر بكثير، ولهذا يهمّ دفع "الجرف" من 80% إلى 90%.
Slide 11: parameters vs MACs
الشريحة 11: المعاملات مقابل MACs
| Networkالشبكة | Params beforeالمعاملات قبل | Params afterالمعاملات بعد | Param reductionتخفيض المعاملات | MAC reductionتخفيض MACs |
|---|---|---|---|---|
| AlexNet | 61 M | 6.7 M | 9x | 3x |
| VGG-16 | 138 M | 10.3 M | 12x | 5x |
| GoogleNet | 7 M | 2.0 M | 3.5x | 5x |
| ResNet50 | 26 M | 7.47 M | 3.4x | 6.3x |
| SqueezeNet | 1 M | 0.38 M | 3.2x | 3.5x |
جدول الشريحة 11: عدد المعاملات قبل الحذف وبعده، ومعامل التقليل في المعاملات وفي MACs. ما يجب أن تلاحظه: في AlexNet وVGG-16 تقليل المعاملات (9x و12x) أكبر بكثير من تقليل MACs (3x و5x)، بينما في GoogleNet وResNet50 يحدث العكس. الرقمان مستقلان، وما يربطهما هو مكان الحذف.
المعاملات وMACs تتركز في طبقات مختلفة. في AlexNet تحمل طبقات FC حوالي 96.2% من المعاملات، بينما تحمل طبقات conv حوالي 92% من MACs. الحذف يزيل معظم الأوزان من طبقات FC حيث يوجد القليل من الحساب، فيبالغ تقليل المعاملات (9x) في تصوير تقليل الحساب (3x).
Why FLOPs fall less than parameters: a MAC is skipped only if the weight or the input activation is zero, and the MACs are spread unevenly over layers. Han et al. report, per layer:
لماذا تنخفض FLOPs أقل من المعاملات: عملية MAC تُتخطى فقط إذا كان الوزن أو التفعيل الداخل صفراً، وMACs موزعة بشكل غير متساوٍ على الطبقات. يذكر Han et al. لكل طبقة:
FLOP% ≈ weight density × input activation density
a conv layer keeps 40 % of its weights, and 50 % of its inputs are nonzero (ReLU) FLOP% ~ 0.40 x 0.50 = 0.20 -> 20 % of the dense MACs remain an FC layer keeps 10 % of its weights, but it holds only a few % of all MACs -> big parameter win, tiny compute win
Whole-network compute reduction is then the MAC-weighted average over layers, and since the conv layers (92 percent of MACs) were pruned lightly, AlexNet only gets 3x.
تقليل الحساب للشبكة كلها هو المتوسط الموزون بـ MACs على الطبقات، وبما أن طبقات conv (92% من MACs) حُذف منها القليل، فإن AlexNet يحصل على 3x فقط.
The reverse case is ResNet50 (3.4x params, 6.3x MACs): it has almost no big FC layer, its parameters and MACs both live in conv layers, and pruning the early high-resolution layers removes many MACs per weight.
الحالة المعاكسة هي ResNet50 (3.4x في المعاملات، 6.3x في MACs): ليس فيه تقريباً طبقة FC كبيرة، فمعاملاته وMACs كلاهما في طبقات conv، وحذف أوزان الطبقات الأولى عالية الدقة المكانية يزيل الكثير من MACs لكل وزن.
It is not just CNNs: NeuralTalk
ليس في CNNs فقط: NeuralTalk
Slide 12 prunes the NeuralTalk LSTM (image captioning). At 90 percent pruned the captions are essentially the same as the baseline: "a basketball player in a white uniform is playing with a ball" becomes "... with a basketball"; "a brown dog is running through a grassy field" becomes "... grassy area". BLEU stays flat. At 95 percent it starts to break: the soccer caption turns into "a man in a red shirt and black and white black shirt is running through a field".
تحذف الشريحة 12 من NeuralTalk LSTM (توليد أوصاف الصور). عند حذف 90% تبقى الأوصاف مطابقة تقريباً للأصل: "... playing with a ball" تصبح "... with a basketball"، و"... grassy field" تصبح "... grassy area". ويبقى BLEU مستوياً. وعند 95% يبدأ الانهيار: يتحول وصف مباراة كرة القدم إلى جملة مكررة ومشوشة عن القمصان.
The point: recurrent and FC-heavy models are highly prunable too, and the knee near 90 to 95 percent shows up again.
الفكرة: النماذج التكرارية (recurrent) والنماذج الثقيلة بطبقات FC قابلة للحذف بشدة أيضاً، والانكسار قرب 90% إلى 95% يظهر مرة أخرى.
Slide 13: sparsity is speed only with hardware support
الشريحة 13: التناثر لا يصبح سرعة إلا بدعم من العتاد
A pruned weight matrix stored densely still has the same shape; the zeros are still loaded and multiplied. The savings above become real only if the hardware or kernel skips zeros. Slide 13 lists the designs that do:
مصفوفة الأوزان بعد الحذف إذا خُزّنت بشكل كثيف تبقى بالشكل نفسه، والأصفار ما زالت تُحمَّل وتُضرب. التوفير أعلاه يصبح حقيقياً فقط إذا كان العتاد أو الـ kernel يتخطى الأصفار. تسرد الشريحة 13 التصاميم التي تفعل ذلك:
Accelerators
المسرّعات (accelerators)
EIE (ISCA 2016): sparse, compressed FC weights with relative indices and 4-bit shared weights. ESE (FPGA 2017): sparse LSTM. SpArch (HPCA 2020): sparse matrix multiply. SpAtten (HPCA 2021): sparse attention.
EIE (ISCA 2016): أوزان FC متفرقة ومضغوطة مع فهارس نسبية وأوزان مشتركة بـ 4-bit. ESE (FPGA 2017): LSTM متفرق. SpArch (HPCA 2020): ضرب مصفوفات متفرقة. SpAtten (HPCA 2021): attention متفرق.
Commodity GPU
وحدة GPU تجارية عادية
NVIDIA A100, 2:4 sparsity: in every group of 4 weights at most 2 are nonzero. Sparse tensor cores give 2x peak and 1.5x measured on BERT.
NVIDIA A100 بنمط 2:4: في كل مجموعة من 4 أوزان، اثنان على الأكثر غير صفريين. تعطي sparse tensor cores سرعة 2x في الذروة و1.5x مقيسة على BERT.
Note the A100 numbers: 2:4 is exactly 50 percent sparsity, and 50 percent sparsity has a 2x ceiling. Measured speed (1.5x) is below peak because not every part of a real model is a sparse matmul.
لاحظ أرقام A100: نمط 2:4 هو sparsity بنسبة 50% بالضبط، و50% سقفها 2x. السرعة المقيسة (1.5x) أقل من الذروة لأن ليس كل جزء من النموذج الحقيقي عملية ضرب مصفوفات متفرقة.
- Parameter reduction is not compute reduction. AlexNet: 9x fewer parameters, 3x fewer MACs. ResNet50 goes the other way (3.4x vs 6.3x). Never quote one as the other.
تقليل المعاملات ليس تقليل الحساب. AlexNet: معاملات أقل بـ 9x وMACs أقل بـ 3x. وResNet50 بالعكس (3.4x مقابل 6.3x). لا تذكر أحدهما على أنه الآخر.
- 90 percent pruned is 10x, not 9x. 1 / (1 − 0.9) = 10. AlexNet's 9x comes from 89 percent (61 / 6.7).
حذف 90% يعني 10x وليس 9x. 1 / (1 − 0.9) = 10. والـ 9x في AlexNet تأتي من 89% (61 / 6.7).
- Which curve reaches what. Prune only: ~50 percent. Prune + finetune: ~80 percent. Iterative: ~90 percent. All at no accuracy loss.
أي منحنى يصل إلى أين. الحذف وحده: حوالي 50%. الحذف مع fine-tune: حوالي 80%. التكرار: حوالي 90%. كلها بلا خسارة في الدقة.
- Sparse is not automatically fast. Without sparse kernels or hardware (EIE, 2:4 tensor cores), zeros are multiplied like any other number.
المتفرق ليس سريعاً تلقائياً. بدون kernels أو عتاد يدعم التفرّق (EIE، 2:4 tensor cores) تُضرب الأصفار مثل أي رقم آخر.
Check yourself
اختبر نفسك
A model is pruned with a pruning ratio of 90 percent. By what factor does its weight count shrink?
نموذج حُذف منه بنسبة pruning ratio قدرها 90%. بأي عامل يصغر عدد أوزانه؟
B. 1 / (1 − 0.9) = 1 / 0.1 = 10. A is AlexNet's slide number, which corresponds to about 89 percent. C reads the percentage as a factor. D is 1 / 0.9, the reciprocal of the fraction removed instead of the fraction kept.
الجواب B. 1 / (1 − 0.9) = 1 / 0.1 = 10. الخيار A هو رقم AlexNet في الشريحة، وهو يقابل حوالي 89%. والخيار C يقرأ النسبة المئوية كعامل. والخيار D هو 1 / 0.9، أي مقلوب النسبة المحذوفة بدلاً من النسبة المُبقاة.
AlexNet's parameters fall 9x after pruning but its MACs only fall 3x. Why?
تنخفض معاملات AlexNet بمقدار 9x بعد الحذف، لكن MACs تنخفض 3x فقط. لماذا؟
C. FC layers hold about 96 percent of AlexNet's parameters but conv layers hold about 92 percent of its MACs. Cutting FC weights hard shrinks the model far more than the compute. A confuses counted MACs with executed work on dense hardware; the table counts MACs on nonzero weights. B is false: fine-tuning keeps the mask fixed. D is invented.
الجواب C. طبقات FC تحمل حوالي 96% من معاملات AlexNet بينما طبقات conv تحمل حوالي 92% من MACs. قطع أوزان FC بشدة يصغّر النموذج أكثر بكثير من الحساب. الخيار A يخلط بين MACs المحسوبة والعمل المنفّذ على عتاد كثيف، والجدول يعدّ MACs على الأوزان غير الصفرية. والخيار B خطأ لأن fine-tuning يثبّت القناع. والخيار D مختلق.
On slides 8 to 10, which method keeps AlexNet at no accuracy loss up to about 90 percent of parameters pruned?
في الشرائح من 8 إلى 10، أي طريقة تُبقي AlexNet بلا خسارة في الدقة حتى حذف حوالي 90% من المعاملات؟
A. The iterative curve stays on the zero line until about 90 percent. Pruning only (B) is already about −4 percent at 80 percent. One-shot prune plus finetune (C) holds to about 80 percent. D is not on the slide at all.
الجواب A. منحنى التكرار يبقى على خط الصفر حتى حوالي 90%. الحذف وحده (B) يصل إلى حوالي −4% عند 80%. والحذف مرة واحدة مع fine-tune (C) يصمد حتى حوالي 80%. والخيار D غير موجود في الشريحة.
After pruning and retraining, the histogram of surviving weights becomes:
بعد الحذف وإعادة التدريب، يصبح المدرج التكراري للأوزان الباقية:
D. Retraining lets the survivors move. They drift outward to compensate, smoothing the hard-edged gap into two humps. A describes the original trained weights more than the retrained ones. C is the state right after pruning, before retraining.
الجواب D. إعادة التدريب تسمح للأوزان الباقية بالتحرك، فتنجرف للخارج لتعوّض، وتتحول الفجوة الحادة إلى سنامين. الخيار A يصف الأوزان الأصلية أكثر من المعاد تدريبها. والخيار C هو الحالة بعد الحذف مباشرة وقبل إعادة التدريب.
The A100's 2:4 structured sparsity reports 2x peak but 1.5x measured on BERT. What sparsity does 2:4 impose?
نمط 2:4 في A100 يعطي 2x في الذروة و1.5x مقيسة على BERT. ما نسبة sparsity التي يفرضها 2:4؟
B. At most 2 nonzeros in every 4 weights: 2 of 4 are zero, 50 percent sparsity, density 0.5, so the compute ceiling is 1 / 0.5 = 2x. The measured 1.5x is lower because non-matmul work does not speed up.
الجواب B. على الأكثر قيمتان غير صفريتين في كل 4 أوزان: 2 من 4 أصفار، أي sparsity 50% وdensity 0.5، فيكون سقف الحساب 1 / 0.5 = 2x. والقيمة المقيسة 1.5x أقل لأن العمل غير المتعلق بضرب المصفوفات لا يتسارع.