Lecture 05-3 · Quantization III
المحاضرة 05-3 · التكميم الثالث
Per-tensor vs per-channel quantization
التكميم per-tensor مقابل per-channel
P1 MUSTP1 ضروريOne scale for a whole weight tensor is simple but lets one outlier channel set the step size for everyone. Give each output channel its own scale and the reconstruction error drops, as the 2-bit worked example shows.
scale واحد لكامل tensor الأوزان بسيط، لكنه يسمح لقناة واحدة شاذة بتحديد حجم الخطوة للجميع. أعطِ كل قناة خرج scale خاصاً بها فينخفض خطأ الاسترجاع، كما يبيّن المثال المحلول بـ 2 بت.
Post-training quantization: three questions
التكميم بعد التدريب: ثلاثة أسئلة
Post-Training Quantization (PTQ) takes an already-trained floating-point model and quantizes it without retraining. The whole question is how to get good linear parameters (S, Z). Slide 3 splits it into three topics: I. quantization granularity (how many scales, over which slices), II. dynamic range clipping (which rmin, rmax), and III. rounding. This page is Topic I.
الـ Post-Training Quantization (PTQ) يأخذ نموذجاً عشرياً مدرّباً ويكمّمه دون إعادة تدريب. السؤال كله: كيف نحصل على معاملات (S, Z) جيدة؟ تقسّمه الشريحة 3 إلى ثلاثة مواضيع: I. دقة التقسيم (granularity) (كم scale وعلى أي شرائح)، II. قص المدى الديناميكي (clipping) (أي rmin وrmax)، وIII. التقريب (rounding). هذه الصفحة عن الموضوع الأول.
Why per-tensor fails on small models (slide 7)
لماذا يفشل per-tensor مع النماذج الصغيرة (الشريحة 7)
Symmetric weight quantization uses |r|max = |W|max. Per-tensor means a single S for the whole weight tensor. It works well for large models but accuracy drops for small models. The box plot (first depthwise-separable layer of MobileNetV2, 32 output channels) shows why: most channels have ranges of a few units, while channels such as 5, 15, 16, 28, 29, 30 stretch to ±50 to ±100. Ranges differ by more than 100× across output channels, driven by outlier weights.
التكميم المتماثل للأوزان يستخدم |r|max = |W|max. والـ per-tensor يعني S واحداً لكامل tensor الأوزان. يعمل جيداً مع النماذج الكبيرة لكن الدقة تهبط مع النماذج الصغيرة. الرسم الصندوقي (أول طبقة depthwise-separable في MobileNetV2، 32 قناة خرج) يبيّن السبب: معظم القنوات مداها بضع وحدات، بينما قنوات مثل 5, 15, 16, 28, 29, 30 تمتد إلى ±50 حتى ±100. المدى يختلف بأكثر من 100× بين قنوات الخرج، بسبب أوزان شاذة (outliers).
With one S set by the widest channel, a channel whose weights all lie in ±1 sees a step size of roughly 100/127 ≈ 0.8 at INT8: nearly all its weights round to 0, −1 or +1. The solution named on the slide: per-channel quantization.
مع S واحد تحدده أوسع قناة، فإن قناة أوزانها كلها ضمن ±1 ترى خطوة قدرها تقريباً 100/127 ≈ 0.8 في INT8: فتُقرَّب معظم أوزانها إلى 0 أو −1 أو +1. والحل المذكور في الشريحة: per-channel quantization.
Worked example: 2-bit, per-tensor vs per-channel (slides 9–13)
مثال محلول: 2 بت، per-tensor مقابل per-channel (الشرائح 9–13)
The running 4×4 weight matrix. Rows are output channels (oc), columns are input channels (ic). Symmetric, 2 bits, scale S = |r|max/qmax with qmax = 22−1 − 1 = 1. So every weight quantizes to q = round(w/S) ∈ {−1, 0, 1}.
المصفوفة 4×4 المتكررة. الصفوف هي قنوات الخرج (oc) والأعمدة قنوات الدخل (ic). تكميم متماثل بـ 2 بت، وS = |r|max/qmax حيث qmax = 22−1 − 1 = 1. إذن كل وزن يُكمَّم إلى q = round(w/S) ∈ {−1, 0, 1}.
| W (oc \ ic) | ic 0 | ic 1 | ic 2 | ic 3 | |r|max of row|r|max للصف |
|---|---|---|---|---|---|
| oc 0 | 2.09 | −0.98 | 1.48 | 0.09 | 2.09 |
| oc 1 | 0.05 | −0.14 | −1.08 | 2.12 | 2.12 |
| oc 2 | −0.91 | 1.92 | 0 | −1.03 | 1.92 |
| oc 3 | 1.87 | 0 | 1.53 | 1.49 | 1.87 |
أكبر قيمة مطلقة في كل صف ملوّنة: 2.09, 2.12, 1.92, 1.87. وأكبرها في المصفوفة كلها 2.12 (الصف 1، العمود 3).
Per-tensor. |r|max = 2.12 over the whole matrix, S = 2.12/(22−1 − 1) = 2.12/1 = 2.12. Divide every entry by 2.12 and round.
Per-tensor. |r|max = 2.12 على المصفوفة كلها، S = 2.12/(22−1 − 1) = 2.12/1 = 2.12. اقسم كل عنصر على 2.12 وقرّب.
w / 2.12 q_W S q_W 0.99 -0.46 0.70 0.04 1 0 1 0 2.12 0 2.12 0 0.02 -0.07 -0.51 1.00 0 0 -1 1 0 0 -2.12 2.12 -0.43 0.91 0 -0.49 0 1 0 0 0 2.12 0 0 0.88 0 0.72 0.70 1 0 1 1 2.12 0 2.12 2.12 ||W - S q_W||_F = sqrt(5.219) = 2.28
Per-channel. One scale per row: S0 = 2.09, S1 = 2.12, S2 = 1.92, S3 = 1.87 (qmax = 1, so S equals the row's |r|max).
Per-channel. scale لكل صف: S0 = 2.09، S1 = 2.12، S2 = 1.92، S3 = 1.87 (لأن qmax = 1 فإن S يساوي |r|max للصف).
w / S_row q_W S ⊙ q_W 1.00 -0.47 0.71 0.04 1 0 1 0 2.09 0 2.09 0 0.02 -0.07 -0.51 1.00 0 0 -1 1 0 0 -2.12 2.12 -0.47 1.00 0 -0.54 0 1 0 -1 0 1.92 0 -1.92 1.00 0 0.82 0.80 1 0 1 1 1.87 0 1.87 1.87 ||W - S ⊙ q_W||_F = sqrt(4.325) = 2.08 < 2.28
Row 1 is identical in both (its own max is the global max). Rows 0 and 3 reconstruct their large entries exactly (2.09, 1.87 instead of 2.12). The headline cell is row 2, column 3: −1.03/2.12 = −0.49 rounds to 0, but −1.03/1.92 = −0.54 rounds to −1, reconstructing −1.92 instead of 0. Squared error per row: per-tensor 1.379, 1.104, 1.929, 0.808; per-channel 1.341, 1.104, 1.620, 0.260.
الصف 1 متطابق في الحالتين (أكبر قيمة فيه هي الأكبر عموماً). الصفان 0 و3 يستعيدان قيمهما الكبيرة بدقة (2.09 و1.87 بدل 2.12). والخلية الأهم هي الصف 2 العمود 3: −1.03/2.12 = −0.49 تُقرَّب إلى 0، لكن −1.03/1.92 = −0.54 تُقرَّب إلى −1، فتُستعاد −1.92 بدل 0. مربع الخطأ لكل صف: per-tensor 1.379, 1.104, 1.929, 0.808؛ per-channel 1.341, 1.104, 1.620, 0.260.
Per-tensor
التكميم per-tensor
One scalar S. Reconstruct with S·qW. Cheapest metadata, but the largest-range channel sets the step for every channel.
S عددي واحد. الاسترجاع بـ S·qW. أقل بيانات وصفية، لكن القناة ذات المدى الأكبر تحدد الخطوة لكل القنوات.
Per-channel
التكميم per-channel
A scale vector S of length Cout. Reconstruct with S ⊙ qW, broadcasting Si along row i. Since each output channel has its own SW, the rescale SWSX/SY simply becomes per output channel.
متجه scale بطول Cout. الاسترجاع بـ S ⊙ qW، حيث يُطبَّق Si على الصف i. وبما أن لكل قناة خرج SW خاصاً، يصبح معامل إعادة التحجيم SWSX/SY خاصاً بكل قناة خرج.
Channel means output channel. Scales go along rows (oc), not columns (ic). Taking column maxima (2.09, 1.92, 1.53, 2.12) gives a different and wrong answer.
القناة تعني قناة الخرج. الـ scales على الصفوف (oc) وليس الأعمدة (ic). أخذ أكبر قيم الأعمدة (2.09, 1.92, 1.53, 2.12) يعطي جواباً مختلفاً وخاطئاً.
The denominator is qmax = 1, not 2. This example uses S = |r|max/(2N−1 − 1). Using the full-range |r|max/2N−1 would give S = 1.06 and different matrices.
المقام هو qmax = 1 وليس 2. هذا المثال يستخدم S = |r|max/(2N−1 − 1). استخدام صيغة المدى الكامل |r|max/2N−1 يعطي S = 1.06 ومصفوفات مختلفة.
Rounding at 0.5. −0.49 rounds to 0 and −0.54 to −1. One flipped cell accounts for most of the gain in row 2.
التقريب حول 0.5. −0.49 تُقرَّب إلى 0 و−0.54 إلى −1. خلية واحدة انقلبت هي سبب معظم التحسّن في الصف 2.
Frobenius norm. ||A||F = √(sum of squared entries). Report 2.28 and 2.08, not the squared sums 5.22 and 4.32.
معيار Frobenius. ||A||F هو الجذر التربيعي لمجموع مربعات العناصر. الجواب 2.28 و2.08، وليس المجموعين 5.22 و4.32.
"2.28 tensor, 2.08 channel: channel is lower." The 2 in both, the 8 in both; per-tensor carries the extra 0.2 of error.
"2.28 للـ tensor، 2.08 للـ channel: القناة أقل." الـ per-tensor يحمل 0.2 إضافية من الخطأ.
Check yourself
اختبر نفسك
Slide 3 splits the PTQ question "how to get optimal (S, Z)?" into three topics. Which list is correct?
تقسّم الشريحة 3 سؤال PTQ "كيف نحصل على (S, Z) الأمثل؟" إلى ثلاثة مواضيع. أي قائمة صحيحة؟
B. Topics I to III of PTQ. A is Deep Compression. C belongs to quantization-aware training. D are the later parts of the lecture outline, not the PTQ topics.
B. المواضيع من I إلى III في PTQ. الخيار A هو Deep Compression. والخيار C يخص التدريب الواعي بالتكميم (quantization-aware training). والخيار D هو الأجزاء اللاحقة من مخطط المحاضرة، وليست مواضيع PTQ.
Per-output-channel weight ranges in MobileNetV2's first depthwise-separable layer differ by more than 100×. What does a single per-tensor scale do to the narrow channels?
يختلف مدى الأوزان لكل قناة خرج في أول طبقة depthwise-separable من MobileNetV2 بأكثر من 100×. ماذا يفعل scale واحد للـ tensor بالقنوات الضيقة؟
C. The widest channel sets S, so narrow channels use only a handful of levels and lose precision. Clipping (A) hits the wide channels only if the range is cut. D does not apply in symmetric quantization.
C. أوسع قناة تحدد S، فتستخدم القنوات الضيقة عدداً قليلاً من المستويات وتفقد الدقة. القص (A) يصيب القنوات الواسعة فقط إذا قُطع المدى. والخيار D لا ينطبق على التكميم المتماثل.
In the 2-bit example, what are the per-channel scales S0, S1, S2, S3?
في مثال 2 بت، ما قيم الـ scales لكل قناة S0, S1, S2, S3؟
A. Row-wise |r|max divided by qmax = 1. B takes column maxima (input channels). C divides by 2 (full-range convention). D is per-tensor.
A. |r|max لكل صف مقسوماً على qmax = 1. الخيار B يأخذ أكبر قيم الأعمدة (قنوات الدخل). والخيار C يقسم على 2 (اصطلاح المدى الكامل). والخيار D هو per-tensor.
Weight −1.03 sits in row 2, whose per-channel scale is 1.92 (2-bit, q ∈ {−1, 0, 1}). Per-tensor S = 2.12. What does it reconstruct to in each scheme?
الوزن −1.03 في الصف 2، وscale القناة 1.92 (2 بت). وS للـ tensor هو 2.12. إلى ماذا يُستعاد في كل طريقة؟
D. −1.03/2.12 = −0.486 rounds to 0; −1.03/1.92 = −0.536 rounds to −1, giving −1.92. This is the cell slide 13 highlights. A rounds −0.486 the wrong way. B misses the per-channel flip. C forgets that 2-bit codes can only be −1, 0 or 1 times S.
D. −1.03/2.12 = −0.486 تُقرَّب إلى 0؛ و−1.03/1.92 = −0.536 تُقرَّب إلى −1، فتعطي −1.92. هذه هي الخلية التي تبرزها الشريحة 13. الخيار A يقرّب −0.486 في الاتجاه الخاطئ. والخيار B يغفل انقلاب القيمة في per-channel. والخيار C ينسى أن رموز 2 بت لا تكون إلا −1 أو 0 أو 1 مضروبة في S.
Which reconstruction errors does the lecture report for this example?
ما أخطاء الاسترجاع التي تذكرها المحاضرة لهذا المثال؟
B. Per-channel wins, 2.08 < 2.28. A swaps them. C gives the squared sums before the square root. D ignores the improvement in rows 0, 2 and 3.
B. يفوز per-channel، 2.08 < 2.28. الخيار A يعكسهما. والخيار C يعطي مجموع المربعات قبل الجذر التربيعي. والخيار D يتجاهل التحسن في الصفوف 0 و2 و3.