Lecture 04-2 · Pruning and Sparsity IIالمحاضرة 04-2 · التقليم والتناثر II

2:4 sparsity on sparse tensor cores

تناثر 2:4 على sparse tensor cores

Slides 78–83الشرائح 78–83 P1 MUST

EIE needed a custom chip. 2:4 sparsity runs on every Ampere GPU because it fixes the count: exactly two of every four weights survive, so two bits per survivor say where it came from and a multiplexer skips half the multiplies.

احتاج EIE إلى شريحة مخصّصة. أما 2:4 sparsity فيعمل على كل GPU من جيل Ampere لأنه يثبّت العدد: يبقى وزنان بالضبط من كل أربعة، فيكفي بتّان لكل وزن باقٍ لتحديد موضعه، ويتخطّى multiplexer نصف عمليات الضرب.

High exam riskاحتمال عالٍ في الاختبار

Expect a storage calculation (values plus 2-bit metadata, FP16 or INT8), a question on which operand the indices act on, and "why is the speedup below 2x?". All three are pure arithmetic or one-line reasoning once you know the format.

توقّع سؤال حساب تخزين (القيم مع metadata بطول 2 بت، بصيغة FP16 أو INT8)، وسؤالاً عن أي operand تعمل عليه الفهارس، وسؤال "لماذا التسريع أقل من 2x؟". الثلاثة حساب مباشر أو تعليل في سطر واحد بمجرد أن تفهم الصيغة.

The definition

التعريف

2:4 sparsity: in every group of four consecutive weights along a row of W, at most two are nonzero. That is exactly 50 percent sparsity. The slides title it "M:N sparsity"; NVIDIA writes 2:4; papers often write N:M. Read any such label as survivors first, block size second.

2:4 sparsity: في كل مجموعة من أربعة أوزان متتالية على طول صف من W، يكون اثنان على الأكثر غير صفريين. هذه بالضبط 50% sparsity. الشرائح تسمّيها "M:N sparsity"، وNVIDIA تكتب 2:4، والأوراق البحثية تكتب غالباً N:M. اقرأ أي تسمية من هذا النوع هكذا: عدد الباقين أولاً، ثم حجم الكتلة.

It is fine-grained in what it removes (single weights, chosen by magnitude inside each block, so accuracy survives) and structured in how many it removes (the count per block is constant, so the hardware knows in advance that every four weights hide exactly two multiplies).

هو fine-grained في ما يحذفه (أوزان مفردة تُختار حسب القيمة المطلقة داخل كل كتلة، فتنجو الدقة)، وstructured في عدد ما يحذفه (العدد ثابت في كل كتلة، فيعرف العتاد مسبقاً أن كل أربعة أوزان تخفي عمليتي ضرب بالضبط).

The compressed format

الصيغة المضغوطة

A 2:4 matrix W of size R x C is stored as an R x C/2 block of nonzero values plus an R x C/2 block of 2-bit indices. Survivors are pushed to the left so every memory line carries useful values; each 2-bit tag records which of the four slots (00, 01, 10, 11) its value came from.

مصفوفة 2:4 بحجم R x C تُخزَّن على شكل كتلة قيم غير صفرية بحجم R x C/2 مع كتلة فهارس بحجم R x C/2 كل فهرس فيها 2 بت. تُدفع القيم الباقية إلى اليسار حتى يحمل كل سطر ذاكرة قيماً مفيدة، وكل وسم من بتّين يسجّل أي خانة من الأربع (00 أو 01 أو 10 أو 11) جاءت منها القيمة.

1. DENSE ROW OF W (K = 8) 0.9 0.1 -0.4 0.2 -0.05 0.7 0.3 -1.2 group 0: keep slots 0, 2 group 1: keep slots 1, 3 grey = two smallest |w| in each group, pruned to zero 2. COMPRESSED: C/2 VALUES + C/2 TWO-BIT INDICES 0.9 -0.4 0.7 -1.2 00 10 01 11 values, b bits each indices, 2 bits each FP16 group: 64 to 36 bits INT8 group: 32 to 20 bits 3. INDICES SELECT K/2 ELEMENTS OF THE DENSE B COLUMN b0 b1 b2 b3 b4 b5 b6 b7 y = 0.9·b0 + (-0.4)·b2 + 0.7·b5 + (-1.2)·b7 4 multiplies instead of 8. Greyed activations are never fetched. multiplexer + MAC
The 2-bit tags belong to the weights but act on the activations: they open K/2 of the K gates on the dense B column.وسوم 2-bit تخص الأوزان لكنها تعمل على activations: فهي تفتح K/2 من البوابات الـ K على عمود B الكثيف.

How the tensor core uses it

كيف تستخدمها tensor core

The sparse tensor core computes sparse A x dense B = dense C. Only the weight operand A (M x K) is compressed, to M x K/2 values plus indices. B (K x N) and C (M x N) stay dense. For each output element the indices pick the K/2 matching elements of the B column, so only half the multiplies run. That is why the pattern must lie along K, the reduction dimension: only there does skipping a weight also skip a multiply.

ينفّذ sparse tensor core العملية sparse A x dense B = dense C. الـ operand الوحيد المضغوط هو الأوزان A بحجم M x K، فيصبح M x K/2 قيمة مع الفهارس. أما B بحجم K x N وC بحجم M x N فيبقيان كثيفين. لكل عنصر خرج تختار الفهارس K/2 عنصراً مطابقاً من عمود B، فتُنفَّذ نصف عمليات الضرب فقط. ولهذا يجب أن يكون النمط على طول K، بُعد الجمع: فهناك فقط يعني تخطّي وزن تخطّي عملية ضرب.

Peak A100 throughput doubles on the sparse path: FP16 312 to 624 TOPS, INT8 624 to 1248 TOPS (TF32 uses a 1:2 pattern, 156 to 312).

تتضاعف الإنتاجية القصوى لـ A100 على المسار المتناثر: FP16 من 312 إلى 624 TOPS، وINT8 من 624 إلى 1248 TOPS (أما TF32 فيستخدم نمط 1:2، من 156 إلى 312).

Worked: compress a 2 x 8 matrix and count bits

مثال محلول: اضغط مصفوفة 2 x 8 واحسب البتات

Prune two smallest |w| in each group of four, then write values and indices.

احذف أصغر وزنين بالقيمة المطلقة في كل مجموعة من أربعة، ثم اكتب القيم والفهارس.

Workedمثال محلول
dense W (2 x 8), groups of four along each row
  row 0:  0.9  0.1 -0.4  0.2 | -0.05 0.7  0.3 -1.2
  row 1:  0.2  0.3  0.5 -0.1 | -0.8  0.4  0.1  0.6

keep the 2 largest |w| per group
  row 0:  g0 keep slots 0,2   g1 keep slots 1,3
  row 1:  g0 keep slots 1,2   g1 keep slots 0,3

values  (R x C/2 = 2 x 4)      indices (2 x 4, 2 bits)
  [ 0.9 -0.4  0.7 -1.2 ]         [ 00 10 01 11 ]
  [ 0.3  0.5 -0.8  0.6 ]         [ 01 10 00 11 ]

FP16  dense      = 16 x 16          = 256 bits
      values     =  8 x 16          = 128 bits
      indices    =  8 x 2           =  16 bits
      total      = 144 bits   ratio 144/256 = 0.5625   saving 43.75%

INT8  dense      = 16 x 8           = 128 bits
      total      =  8 x 8 + 8 x 2   =  80 bits   ratio 0.625   saving 37.5%

row 0 times B column:  0.9·b0 - 0.4·b2 + 0.7·b5 - 1.2·b7   (4 MACs, not 8)

Group 1 indices are local: slot 1 of the second group is column 4 + 1 = 5. The saving is below 50 percent because every stored value carries a 2-bit tag: 2/16 = 12.5 percent overhead for FP16, 2/8 = 25 percent for INT8.

فهارس المجموعة الثانية محلية: الخانة 1 في المجموعة الثانية هي العمود 4 + 1 = 5. والتوفير أقل من 50% لأن كل قيمة مخزّنة تحمل وسماً من بتّين: 2/16 = 12.5% حمل إضافي في FP16، و2/8 = 25% في INT8.

Group of fourمجموعة من أربعةDense bitsبتات الصيغة الكثيفة2:4 bitsبتات 2:4Savingالتوفير
FP164 x 16 = 642 x 16 + 2 x 2 = 3643.75%
INT84 x 8 = 322 x 8 + 2 x 2 = 2037.5%

الجدول يلخّص كلفة مجموعة واحدة من أربعة أوزان: 64 بت تصبح 36 في FP16 (توفير نحو 44%)، و32 تصبح 20 في INT8 (توفير نحو 38%).

Recipe and accuracy

الوصفة والدقة

  1. Train dense, exactly as usual.

    درّب النموذج كثيفاً كالمعتاد تماماً.

  2. Prune the two smallest-magnitude weights in every group of four.

    احذف الوزنين الأصغر قيمة مطلقة في كل مجموعة من أربعة.

  3. Retrain with the same optimizer, schedule and epochs, zeros held fixed. No hyper-parameter search.

    أعد التدريب بنفس optimizer والجدول وعدد epochs، مع تثبيت الأصفار. لا حاجة لأي بحث عن hyper-parameters.

Result: sparse FP16 and INT8 match dense within run-to-run noise on large ImageNet CNNs, for example ResNet-50 76.1 dense vs 76.2 sparse. Small, parameter-efficient nets (MobileNet v2, EfficientNet B0) lose accuracy under the plain recipe and need channel permutation before pruning to recover.

النتيجة: FP16 وINT8 المتناثران يطابقان الكثيف ضمن ضجيج التشغيل على شبكات ImageNet CNN الكبيرة، مثلاً ResNet-50: 76.1 كثيف مقابل 76.2 متناثر. أما الشبكات الصغيرة الموفّرة للمعاملات (MobileNet v2 وEfficientNet B0) فتفقد دقة مع الوصفة البسيطة وتحتاج channel permutation قبل التقليم كي تستعيدها.

Speedup: a staircase toward 2x

التسريع: درج يصعد نحو 2x

INT8 GEMM on A100 (M = N = 10240) speeds up only about 1.2x at K = 1280 and climbs to about 1.95x at K = 20480. The sparse core halves only the arithmetic; small GEMMs have low arithmetic intensity and are bound by memory traffic and fixed costs. End to end, NVIDIA reports about 1.2x (ResNeXt-101 in TensorRT), because non-GEMM layers and small GEMMs gain little.

ضرب المصفوفات INT8 GEMM على A100 (M = N = 10240) يتسارع بنحو 1.2x فقط عند K = 1280، ويصعد إلى نحو 1.95x عند K = 20480. فالنواة المتناثرة تنصّف الحساب فقط، وGEMM الصغير ذو arithmetic intensity منخفضة ومحكوم بحركة الذاكرة والتكاليف الثابتة. وعلى مستوى النموذج كاملاً تذكر NVIDIA نحو 1.2x (ResNeXt-101 في TensorRT)، لأن الطبقات غير GEMM وGEMM الصغيرة لا تكسب إلا قليلاً.

Trapفخ

2x is the math ceiling only. It is a peak on the matrix arithmetic of one large GEMM, not a promise for latency or for the whole model.

2x سقف للحساب فقط. إنه قمة على عمليات المصفوفات في GEMM كبير واحد، وليس وعداً بالـ latency ولا بالنموذج كله.

Storage is not halved, and the ratio depends on the width. FP16 total is 56.25 percent of dense (43.75 saved), INT8 is 62.5 percent (37.5 saved). Answering "50 percent" loses the metadata marks.

التخزين لا ينخفض إلى النصف، والنسبة تعتمد على عرض القيمة. في FP16 يصبح المجموع 56.25% من الكثيف (توفير 43.75%)، وفي INT8 يصبح 62.5% (توفير 37.5%). الإجابة بـ "50%" تُضيّع درجات metadata.

The indices select activations, not weights. The weights are already compacted; the tags drive the multiplexer on dense B.

الفهارس تختار التنشيطات، لا الأوزان. الأوزان مضغوطة أصلاً، والوسوم تقود multiplexer على B الكثيف.

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

Two of four, two bits each, half the multiplies. Fixed count is what turns EIE's search into a 2-bit lookup inside an ordinary tensor core.

اثنان من أربعة، بتّان لكل منهما، نصف عمليات الضرب. العدد الثابت هو ما يحوّل بحث EIE إلى قراءة وسم من 2 بت داخل tensor core عادي.

Check yourself

اختبر نفسك

A 2:4 sparse FP16 weight matrix has R = 512 and C = 1024. How many metadata bits does the compressed format hold?

مصفوفة أوزان 2:4 بصيغة FP16 فيها R = 512 وC = 1024. كم بت metadata تحمل الصيغة المضغوطة؟

R x C/2 = 512 x 512 = 262,144 stored values, each with a 2-bit index: 524,288 bits (64 KiB). Option A counts indices, not bits. Option D is the value array, 262,144 x 16.

R x C/2 = 512 x 512 = 262,144 قيمة مخزّنة، لكل منها فهرس من بتّين: 524,288 بت (64 KiB). الخيار A يعدّ الفهارس لا البتات. والخيار D هو مصفوفة القيم 262,144 x 16.

For INT8 weights, what is the storage saving of 2:4 compression for one group of four?

لأوزان INT8، ما نسبة توفير التخزين في ضغط 2:4 لمجموعة واحدة من أربعة؟

Dense 4 x 8 = 32. Compressed 2 x 8 + 2 x 2 = 20. Saving 12/32 = 37.5 percent. Option C is the FP16 number. Option A ignores the metadata.

الكثيف 4 x 8 = 32. المضغوط 2 x 8 + 2 x 2 = 20. التوفير 12/32 = 37.5%. الخيار C هو رقم FP16. والخيار A يتجاهل metadata.

In a sparse tensor core GEMM, what do the 2-bit indices do?

في GEMM على sparse tensor core، ما وظيفة الفهارس ذات البتّين؟

The weights are already packed left; the tags tell the multiplexer which of each four B elements to fetch. B and C stay dense, and nothing is skipped along M or N.

الأوزان مضغوطة إلى اليسار أصلاً، والوسوم تخبر multiplexer أي عنصر من كل أربعة عناصر في B يجلب. يبقى B وC كثيفين، ولا شيء يُتخطّى على M أو N.

On the A100 chart, INT8 2:4 GEMM speedup is about 1.2x at K = 1280 but about 1.95x at K = 20480. Why?

في رسم A100، تسريع INT8 2:4 GEMM نحو 1.2x عند K = 1280 لكنه نحو 1.95x عند K = 20480. لماذا؟

Sparsity is fixed at 50 percent and metadata is always 2 bits per value. Only the math halves; small GEMMs are bound by memory and fixed overheads, large ones are math bound and approach 2x.

الـ sparsity ثابتة عند 50% وmetadata دائماً بتّان لكل قيمة. الذي ينخفض للنصف هو الحساب فقط، وGEMM الصغير محكوم بالذاكرة والتكاليف الثابتة، أما الكبير فمحكوم بالحساب فيقترب من 2x.

Which group of four consecutive weights violates 2:4?

أي مجموعة من أربعة أوزان متتالية تخالف 2:4؟

The rule is at most two nonzeros per four. C has three. B and D are legal; the format still stores two values for them (one or two of them zero), so the matrix never shrinks below half.

القاعدة: اثنان غير صفريين على الأكثر في كل أربعة. الخيار C فيه ثلاثة. B وD مسموحان، والصيغة تخزّن لهما قيمتين رغم ذلك (إحداهما أو كلتاهما صفر)، فلا تصغر المصفوفة أبداً عن النصف.