Lecture 04-1 · Pruning and Sparsity I
المحاضرة 04-1 · التقليم والتناثر (Pruning and Sparsity I)
Fine-grained, N:M and channel pruning
تقليم fine-grained وN:M وتقليم القنوات (channel pruning)
P1 MUSTThree points on the granularity spectrum, three different bills. Fine-grained compresses most and speeds up least, 2:4 is the GPU-friendly middle, channel pruning gives a smaller dense network that runs faster everywhere.
ثلاث نقاط على طيف granularity، ولكل منها ثمن مختلف. Fine-grained يضغط أكثر ويُسرّع أقل، و2:4 هو الحل الوسط المناسب لـ GPU، وchannel pruning يعطيك شبكة كثيفة أصغر تعمل أسرع على أي عتاد.
Expect a compare-and-contrast (compression versus speedup for each granularity), one storage calculation for 2:4 (64 bits to 36 bits), a definition of N:M with a sparsity percentage, and a channel-pruning parameter count where the shrinkage compounds across two layers.
توقّع سؤال مقارنة (الضغط مقابل التسريع لكل granularity)، وحساب تخزين واحد لـ 2:4 (من 64 بت إلى 36 بت)، وتعريف N:M مع نسبة sparsity، وحساب عدد parameters في channel pruning حيث يتراكم الانكماش عبر طبقتين.
Fine-grained pruning, slides 19 and 20
التقليم الدقيق (fine-grained)، الشريحتان 19 و20
Fine-grained (unstructured) pruning may zero any individual weight. The indices of the surviving weights are completely flexible, so the pruner can pick exactly the redundant weights wherever they sit. That flexibility is why it reaches the largest compression ratio.
Fine-grained pruning (أي غير المُهيكل) يمكنه تصفير أي وزن بمفرده. مواقع الأوزان الباقية مرنة تماماً، فيستطيع المُقلِّم اختيار الأوزان الزائدة بالضبط أينما كانت. وهذه المرونة هي سبب وصوله إلى أعلى نسبة ضغط.
The cost is on the hardware side. Every surviving weight needs its own index (a row and column, or a relative offset), and the nonzeros are scattered, so a GPU cannot turn the zeros into skipped work easily. The slide's red line: it can deliver speedup on some custom hardware (EIE later in the course), but not on a GPU (easily).
الثمن يُدفع في العتاد. كل وزن باقٍ يحتاج فهرساً خاصاً به (صف وعمود، أو إزاحة نسبية)، والقيم غير الصفرية مبعثرة، فلا يستطيع GPU تحويل الأصفار إلى عمل محذوف بسهولة. السطر الأحمر في الشريحة: يمكنه تحقيق تسريع على عتاد مخصص (مثل EIE لاحقاً في المقرر)، لكن ليس على GPU بسهولة.
| Networkالشبكة | Beforeقبل | Afterبعد | Reductionالتخفيض |
|---|---|---|---|
| AlexNet | 61 M | 6.7 M | 9x |
| VGG-16 | 138 M | 10.3 M | 12x on slide (138 / 10.3 = 13.4, paper says 13x)12x في الشريحة (138 / 10.3 = 13.4، والورقة تقول 13x) |
| GoogleNet | 7 M | 2.0 M | 3.5x |
| ResNet50 | 26 M | 7.47 M | 3.4x |
يعرض الجدول نتائج fine-grained pruning من الشريحة 20: AlexNet من 61 M إلى 6.7 M أي 9x، وVGG-16 من 138 M إلى 10.3 M وتطبع الشريحة 12x بينما القسمة تعطي 138 / 10.3 = 13.4 والورقة الأصلية تقول 13x. والذي يجب أن تلاحظه: الشبكات الضخمة ذات طبقات FC الكبيرة (AlexNet وVGG) تنضغط بنحو 10x، أما الشبكات المصممة بكفاءة أصلاً (GoogleNet وResNet50) فلا تتجاوز 3.5x تقريباً.
The pattern: networks with huge fully-connected layers (AlexNet, VGG) have the most redundancy and shrink about 10x; already-compact designs (GoogleNet, ResNet50) give about 3.5x.
النمط: الشبكات ذات طبقات fully-connected الضخمة فيها أكبر قدر من التكرار وتنكمش بنحو 10x، أما التصاميم المضغوطة أصلاً فتعطي نحو 3.5x.
Pattern-based pruning: N:M sparsity, slides 21 and 22
التقليم المبني على أنماط: تناثر N:M، الشريحتان 21 و22
N:M sparsity constrains the pattern inside every group of M contiguous weights. Under NVIDIA's convention (the one the hardware uses), each group keeps at most N nonzeros. The classic case is 2:4: in every four consecutive weights, two survive and two are zero, which is 50 percent sparsity.
N:M sparsity يقيّد النمط داخل كل مجموعة من M أوزان متتالية. وحسب اصطلاح NVIDIA (وهو الذي يستخدمه العتاد) تحتفظ كل مجموعة بـ N قيم غير صفرية على الأكثر. والحالة الكلاسيكية هي 2:4: في كل أربعة أوزان متتالية يبقى اثنان ويُصفَّر اثنان، أي sparsity بنسبة 50%.
The storage arithmetic
حساب التخزين
one group of M = 4 weights, 2:4 sparsity dense FP16 4 weights x 16 bits = 64 bits 2:4 compressed FP16 values: 2 kept x 16 bits = 32 bits indices: 2 kept x 2 bits = 4 bits (log2 4 = 2 bits picks a slot) total = 36 bits ratio 64 / 36 = 1.78x savings 1 - 36/64 = 43.75% (about 44%) same group in INT8 dense 4 x 8 = 32 bits 2:4 2 x 8 + 2 x 2 = 20 bits ratio 32 / 20 = 1.6x, savings 37.5%
The memory saving is less than 2x because the indices are metadata. The math saving is about 2x: Ampere Sparse Tensor Cores read the 2-bit indices, fetch only the matching activations, and skip the zero multiplies.
التوفير في الذاكرة أقل من 2x لأن الفهارس بيانات وصفية (metadata) إضافية. أما التوفير في الحساب فهو نحو 2x: فوحدات Sparse Tensor Cores في معمارية Ampere تقرأ الفهارس ذات البتين، وتجلب فقط activations المقابلة، وتتخطى عمليات الضرب في الأصفار.
Does 2:4 cost accuracy? Slide 22
هل يكلّف 2:4 شيئاً من الدقة؟ الشريحة 22
| Networkالشبكة | Data setمجموعة البيانات | Metricالمقياس | Dense FP16FP16 كثيف | Sparse FP16FP16 متناثر |
|---|---|---|---|---|
| ResNet-50 | ImageNet | Top-1 | 76.1 | 76.2 |
| ResNeXt-101_32x8d | ImageNet | Top-1 | 79.3 | 79.3 |
| Xception | ImageNet | Top-1 | 79.2 | 79.2 |
| SSD-RN50 | COCO2017 | bbAP | 24.8 | 24.8 |
| MaskRCNN-RN50 | COCO2017 | bbAP | 37.9 | 37.9 |
| FairSeq Transformer | EN-DE WMT'14 | BLEU | 28.2 | 28.5 |
| BERT-Large | SQuAD v1.1 | F1 | 91.9 | 91.9 |
يعرض الجدول سبع مهام (تصنيف صور، كشف أجسام، ترجمة، أسئلة وأجوبة) بنسخة كثيفة FP16 ونسخة 2:4 بعد وصفة NVIDIA (تقليم ثم إعادة تدريب). والذي يجب أن تلاحظه: لا توجد خسارة في أي صف؛ خمسة صفوف متطابقة، وResNet-50 ارتفع من 76.1 إلى 76.2، وFairSeq من 28.2 إلى 28.5.
Seven tasks across vision, detection, translation and question answering: with NVIDIA's prune-then-retrain recipe, no task lost accuracy, and two gained slightly. That is the point of the slide: 2:4 is structured enough for hardware and loose enough to keep accuracy.
سبع مهام في الرؤية والكشف والترجمة والإجابة عن الأسئلة: مع وصفة NVIDIA (تقليم ثم إعادة تدريب) لم تخسر أي مهمة من دقتها، وتحسّنت مهمتان قليلاً. وهذه هي فكرة الشريحة: 2:4 مُهيكل بما يكفي للعتاد، ومرن بما يكفي للحفاظ على الدقة.
Read the N in N:M carefully. The slide says "in each contiguous M elements, N of them is pruned". NVIDIA and the literature say N is the number kept (at most N nonzeros). For 2:4 it does not matter, both readings give 50 percent. For 1:4 it flips: NVIDIA reading means 3 of 4 zero, 75 percent sparsity; slide reading means 1 of 4 zero, 25 percent.
اقرأ N في N:M بعناية. تقول الشريحة إنه في كل M عناصر متتالية يُقلَّم N منها. أما NVIDIA والأدبيات فتعتبر N هو عدد ما يبقى (N قيم غير صفرية على الأكثر). في 2:4 لا فرق، فالقراءتان تعطيان 50%. لكن في 1:4 تنقلب النتيجة: قراءة NVIDIA تعني أن ثلاثة من أربعة أصفار، أي sparsity بنسبة 75%، وقراءة الشريحة تعني صفراً واحداً من أربعة، أي 25%.
On the exam, state the convention in one phrase ("N kept per M") before giving the number.
في الامتحان، اذكر الاصطلاح في عبارة قصيرة ("N kept per M") قبل أن تكتب الرقم.
Channel pruning, slides 23 to 25
تقليم القنوات (channel pruning)، الشرائح 23 إلى 25
Channel pruning removes whole output channels (whole filters) of a convolution. What is left is a smaller dense network: fewer channels, ordinary dense tensors, no indices. Any hardware and any library runs it faster, so the speedup is direct. The con: removing whole channels is coarse, so the compression ratio is smaller than fine-grained at the same accuracy.
Channel pruning يحذف قنوات إخراج كاملة (مرشحات كاملة) من طبقة الالتفاف. والناتج شبكة كثيفة أصغر: قنوات أقل، وtensors كثيفة عادية، بلا فهارس. أي عتاد وأي مكتبة تشغّلها أسرع، فالتسريع مباشر. والعيب: حذف قنوات كاملة عملية خشنة، فنسبة الضغط أقل من fine-grained عند الدقة نفسها.
two 3x3 conv layers, biases ignored, same spatial size A: c_in 64, c_out 128 B: c_in 128, c_out 256 before A: 3 x 3 x 64 x 128 = 73,728 B: 3 x 3 x 128 x 256 = 294,912 total = 368,640 prune 50% of A's outputs (128 -> 64) prune 25% of B's outputs (256 -> 192) after A: 3 x 3 x 64 x 64 = 36,864 kept 0.5 B: 3 x 3 x 64 x 192 = 110,592 kept 0.5 x 0.75 = 0.375 total = 147,456 overall 147,456 / 368,640 = 0.40 -> 2.5x fewer params MACs scale by the same factors (MACs = params x H_out x W_out per layer) the naive answer "B keeps 75%" is wrong: B also lost half its inputs.
Uniform shrink versus per-layer ratios, slides 24 and 25
التصغير الموحّد مقابل نسب لكل طبقة، الشريحتان 24 و25
Slide 24 compares a uniform shrink (sparsity 0.3 in every layer) against channel pruning with per-layer ratios (0.5, 0.3, 0.7, 0.2, 0.3). The "<" between them means the per-layer version is better: some layers are redundant and can lose 70 percent, others are sensitive and should lose only 20 percent. How to find those ratios is a later topic (AMC, NetAdapt, sensitivity analysis).
تقارن الشريحة 24 بين uniform shrink (نسبة 0.3 في كل طبقة) وبين channel pruning بنسب مختلفة لكل طبقة (0.5, 0.3, 0.7, 0.2, 0.3). وعلامة "<" بينهما تعني أن النسخة متعددة النسب أفضل: بعض الطبقات فيها تكرار ويمكنها خسارة 70%، وبعضها حساس فلا يُحذف منه إلا 20%. وطريقة إيجاد هذه النسب موضوع لاحق (AMC وNetAdapt وsensitivity analysis).
| MobileNet on Pixel 1 (slide 25)MobileNet على Pixel 1 (الشريحة 25) | ImageNet top-1ImageNet top-1 | Latencyزمن الاستجابة |
|---|---|---|
| Uniform scaling, 0.75 MobileNetتحجيم موحّد، 0.75 MobileNet | 68.4% | 72.3 ms |
| Channel pruning with AMC per-layer ratiosتقليم القنوات بنسب AMC لكل طبقة | 70.5% | 68.3 ms |
يقارن الجدول نقطتين من رسم الشريحة 25 على هاتف Pixel 1: MobileNet مصغَّرة بشكل موحّد (0.75) تعطي 68.4% عند 72.3 ms، والنسخة المقلَّمة بنسب AMC تعطي 70.5% عند 68.3 ms. والذي يجب أن تلاحظه: AMC أفضل في المحورين معاً، أدق بـ 2.1 نقطة وأسرع بـ 4 ms.
AMC wins on both axes at once: 2.1 points more accurate and 4 ms faster (72.3 / 68.3 is about 1.06x). On the plot the whole AMC curve sits above and to the left of the uniform line.
AMC يتفوق في المحورين في الوقت نفسه: أدق بـ 2.1 نقطة وأسرع بـ 4 ms (72.3 / 68.3 ≈ 1.06x). وفي الرسم يقع منحنى AMC كله فوق خط uniform scaling وإلى يساره.
Fine-grained
Fine-grained (دقيق الحبيبات)
Any weight. Largest compression (AlexNet 9x). Needs per-weight indices; speedup only on custom hardware.
أي وزن. أكبر ضغط (AlexNet 9x). يحتاج فهرساً لكل وزن، والتسريع على عتاد مخصص فقط.
N:M (2:4)
N:M (2:4)
At most N nonzeros per M. 50 percent, 36 of 64 bits stored, about 2x math on Ampere Sparse Tensor Cores, accuracy kept.
N قيم غير صفرية على الأكثر في كل M. نسبة 50%، وتخزين 36 من 64 بت، ونحو 2x في الحساب على Ampere، مع الحفاظ على الدقة.
Channel
القنوات (channel)
Whole filters. Smaller dense net, direct speedup anywhere, smaller compression ratio. Sparsity compounds across layers.
مرشحات كاملة. شبكة كثيفة أصغر، وتسريع مباشر على أي عتاد، ونسبة ضغط أقل. والـ sparsity تتراكم عبر الطبقات.
Channel sparsity compounds. A channel sparsity of s in every layer does not cut parameters by s. A middle layer keeps (1 - s) of its outputs and (1 - s) of its inputs, so it keeps (1 - s)2 of its weights and MACs. Uniform 0.3 means each middle layer keeps 0.49, about 2x fewer weights, not 1.43x.
الـ sparsity في القنوات تتراكم. نسبة s في كل طبقة لا تخفّض parameters بمقدار s فقط. فالطبقة الوسطى تحتفظ بـ (1 - s) من مخرجاتها و(1 - s) من مدخلاتها، أي تحتفظ بـ (1 - s)2 من أوزانها ومن MACs. فنسبة موحدة 0.3 تعني أن كل طبقة وسطى تحتفظ بـ 0.49، أي أوزان أقل بنحو 2x، لا 1.43x.
The first layer is the exception: its input is the image (3 channels), which is never pruned, so it shrinks only by its own output ratio.
الطبقة الأولى استثناء: مدخلها هو الصورة (3 قنوات) ولا تُقلَّم أبداً، فلا تنكمش إلا بنسبة مخرجاتها هي.
"Fine compresses, channel accelerates, 2:4 does both halfway." And for storage: "two values, two crumbs", two 16-bit values plus two 2-bit indices, 36 bits.
"Fine compresses, channel accelerates, 2:4 does both halfway." أي: fine-grained يضغط، وchannel يُسرّع، و2:4 يفعل الاثنين بنصف القوة. وللتخزين: قيمتان من 16 بت مع فهرسين من بتين، المجموع 36 بت.
Check yourself
اختبر نفسك
A group of four FP16 weights is stored in 2:4 compressed form. How many bits does the group occupy?
مجموعة من أربعة أوزان FP16 مخزنة بصيغة 2:4 المضغوطة. كم بتاً تشغل المجموعة؟
Two kept values at 16 bits each give 32 bits, plus one 2-bit index per kept value (log2 4 = 2) gives 4 bits: 36 bits. Option A forgets the index metadata. Option C would be 2 x 16 + 2 x 4, using 4-bit indices that a group of four does not need. The ratio is 64 / 36 = 1.78x.
قيمتان محفوظتان بـ 16 بت لكل منهما تعطيان 32 بت، ومعهما فهرس من بتين لكل قيمة (log2 4 = 2) أي 4 بت: المجموع 36 بت. الخيار A ينسى الفهارس. والخيار C يستخدم فهارس من 4 بت لا تحتاجها مجموعة من أربعة. والنسبة 64 / 36 = 1.78x.
Under NVIDIA's convention (N = maximum number of nonzeros per M), what is the sparsity of a 1:4 pattern?
حسب اصطلاح NVIDIA (N = أقصى عدد من القيم غير الصفرية في كل M)، ما نسبة sparsity في نمط 1:4؟
One nonzero kept in every four means three zeros out of four: 75 percent. Option A is the answer you get from the slide's wording ("N of them is pruned"), which is why you should state the convention. Option B is true only for 2:4, where both readings agree.
قيمة واحدة غير صفرية في كل أربعة تعني ثلاثة أصفار من أربعة: 75%. والخيار A هو ما تحصل عليه من صياغة الشريحة ("يُقلَّم N منها")، ولهذا يجب أن تذكر الاصطلاح. والخيار B صحيح فقط في 2:4 حيث تتفق القراءتان.
Layer l and layer l+1 are 3x3 convolutions. You prune 50 percent of the output channels of each. What fraction of layer l+1's weights remain?
الطبقتان l وl+1 التفافان 3x3. قلّمت 50% من قنوات الإخراج في كل منهما. ما نسبة أوزان الطبقة l+1 المتبقية؟
Layer l+1 keeps half its output channels and, because layer l lost half its feature maps, half its input channels: 0.5 x 0.5 = 0.25. The kernel size multiplies before and after equally, so it cancels (option D). Option B is the uncompounded mistake.
الطبقة l+1 تحتفظ بنصف قنوات إخراجها، ولأن الطبقة l فقدت نصف feature maps فإنها تحتفظ أيضاً بنصف قنوات إدخالها: 0.5 x 0.5 = 0.25. وحجم النواة يضرب القيمتين قبل وبعد بالتساوي فيُلغى (الخيار D). والخيار B هو خطأ تجاهل التراكم.
You must speed up a CNN on a commodity mobile CPU with a standard dense inference library and no sparse kernels. Which granularity gives a real speedup?
عليك تسريع CNN على معالج جوال عادي بمكتبة استدلال كثيفة قياسية وبدون sparse kernels. أي granularity يعطي تسريعاً حقيقياً؟
Channel pruning produces ordinary dense layers with fewer channels, so any dense library runs fewer MACs directly. Fine-grained zeros are still multiplied by a dense kernel. The 2x of 2:4 needs Sparse Tensor Cores (Ampere GPUs), not a generic CPU.
Channel pruning ينتج طبقات كثيفة عادية بقنوات أقل، فأي مكتبة كثيفة تنفّذ MACs أقل مباشرة. أما أصفار fine-grained فتُضرب على أي حال في dense kernel. وتسريع 2x في 2:4 يحتاج Sparse Tensor Cores (معالجات Ampere)، لا معالجاً عاماً.
On a Pixel 1, uniformly scaled 0.75 MobileNet gets 68.4 percent at 72.3 ms, and AMC channel pruning gets 70.5 percent at 68.3 ms. What does this show?
على Pixel 1، تعطي MobileNet المصغَّرة بشكل موحّد (0.75) دقة 68.4% عند 72.3 ms، ويعطي تقليم القنوات بـ AMC دقة 70.5% عند 68.3 ms. ماذا يُظهر ذلك؟
AMC is both more accurate (+2.1 points) and faster (4 ms less). There is no trade-off, which is option A's error. AMC searches per-layer channel ratios; the result is still a dense, channel-pruned network, so option D is wrong.
AMC أدق (+2.1 نقطة) وأسرع (أقل بـ 4 ms) في آن واحد، فلا توجد مقايضة، وهذا خطأ الخيار A. وAMC يبحث عن نسب قنوات لكل طبقة، والناتج شبكة كثيفة مقلَّمة القنوات، فالخيار D خاطئ.