COE 592Lecture 5.3المحاضرة 5.3Part 08الجزء 08
Accuracy cost of binarization and ternary quantizationكلفة التثنيث على الدقة والتكميم الثلاثي
Binarizing activations as well as weights costs a lot of accuracy, though scale factors help. Adding a zero level gives ternary weights: TWN thresholds at 0.7 times the mean magnitude with one fixed scale, and TTQ learns separate positive and negative scales, closing more of the gap to full precision.تثنيث التنشيطات مع الأوزان يكلّف كثيرًا من الدقة، وإن كانت معاملات القياس تساعد. إضافة مستوى صفري تعطي أوزانًا ثلاثية: يضع TWN العتبة عند 0.7 من متوسط المقدار مع مقياس ثابت واحد، ويتعلم TTQ مقياسين منفصلين للموجب والسالب، فيسد جزءًا أكبر من الفجوة مع الدقة الكاملة.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 61-64
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Going from 8 bits to 1 bit promises a 32× smaller model and multiplications replaced by bit tricks, which is exactly what a microcontroller or a tiny accelerator wants. This part puts a price on that promise. It reads the accuracy table for binary networks, then shows how adding a single zero state, the ternary idea, buys back most of what binarization loses.
The argument runs in four steps. First, the cost of binarization and the two things that drive it. Second, Ternary Weight Networks, with a full calculation on the same 4×4 matrix the lecture has used since K-means quantization. Third, Trained Ternary Quantization, which learns its scales. Fourth, a map of every quantization family by what it stores and what arithmetic it runs. For the midterm, expect to compute Δ, r_t and a ternary matrix by hand, to rank the binary variants, and to state the difference between TWN and TTQ in one sentence.
الانتقال من 8 بت إلى 1 بت يعد بنموذج أصغر بـ 32×، وبعمليات ضرب تُستبدل بحيل على مستوى البت، وهذا بالضبط ما يريده المتحكم الدقيق (microcontroller) أو المسرّع الصغير. هذا الجزء يضع ثمنًا لهذا الوعد. يقرأ جدول الدقة للشبكات الثنائية، ثم يبيّن كيف أن إضافة حالة صفر واحدة، وهي فكرة التكميم الثلاثي (ternary)، تستعيد معظم ما يضيع بسبب التحويل الثنائي.
يسير الشرح في أربع خطوات. أولًا، كلفة التحويل الثنائي والعاملان اللذان يحددانها. ثانيًا، شبكات الأوزان الثلاثية Ternary Weight Networks، مع حساب كامل على مصفوفة 4×4 نفسها التي تستخدمها المحاضرة منذ تكميم K-means. ثالثًا، التكميم الثلاثي المدرَّب Trained Ternary Quantizationالذي يتعلّم مقاييسه. رابعًا، خريطة لكل عائلات التكميم بحسب ما تخزّنه وما تنفّذه من عمليات حسابية. في الاختبار النصفي، توقّع أن تحسب Δ وr_t والمصفوفة الثلاثية يدويًا، وأن ترتّب المتغيرات الثنائية، وأن تذكر الفرق بين TWN وTTQ في جملة واحدة.
By the end you canفي نهاية هذا الجزء ستستطيع
- Rank BWN, BNN and XNOR-Net by AlexNet accuracy and explain the ranking with two causes: scale factors and binarized activations.رتّب BWN وBNN وXNOR-Net حسب دقة AlexNet، وفسّر الترتيب بسببين: معاملات المقياس (scale factors) وتحويل التنشيطات إلى قيم ثنائية.
- Compute the TWN threshold Δ, the shared scale r_t and the ternary matrix for a given weight tensor.احسب عتبة TWN أي Δ، والمقياس المشترك r_t، والمصفوفة الثلاثية لمصفوفة أوزان معطاة.
- Explain why a zero state makes ternary weights far more accurate than binary ones for only one extra bit.اشرح لماذا تجعل حالة الصفر الأوزانَ الثلاثية أدق بكثير من الثنائية مقابل بت إضافي واحد فقط.
- State exactly what TTQ changes relative to TWN, and why learned asymmetric scales help.اذكر بدقة ما الذي يغيّره TTQ مقارنة بـ TWN، ولماذا تفيد المقاييس غير المتماثلة المتعلَّمة.
- Place K-means, linear and binary or ternary quantization on the storage and arithmetic axes.ضع تكميم K-means والتكميم الخطي والتكميم الثنائي أو الثلاثي على محوري التخزين والعمليات الحسابية.
Take AlexNet on ImageNet and binarize it three different ways. Binarize only the weights and keep a scale factor: top-1 accuracy goes up by 0.2 points. Binarize weights and activations with scale factors on both: it drops 12.4 points. Binarize both without any scale factors: it drops 28.7 points. Same network, same bit width for the weights, and a spread of almost 29 points.
خذ AlexNet على ImageNet وحوّلها إلى ثنائية بثلاث طرق مختلفة. حوّل الأوزان فقط مع الإبقاء على معامل مقياس: ترتفع دقة top-1 بمقدار 0.2 نقطة. حوّل الأوزان والتنشيطات مع معاملات مقياس على كليهما: تنخفض 12.4 نقطة. حوّل الاثنين دون أي معاملات مقياس: تنخفض 28.7 نقطة. الشبكة نفسها، وعرض البت نفسه للأوزان، ومع ذلك يبلغ الفارق قرابة 29 نقطة.
| Methodالطريقة | W bitsبتات الأوزان W | A bitsبتات التنشيطات A | Scale factors onمعاملات المقياس على | Top-1 deltaفرق top-1 |
|---|---|---|---|---|
| BWNBWN | 11 | 3232 | Weights onlyالأوزان فقط | +0.2%+0.2% |
| XNOR-NetXNOR-Net | 11 | 11 | Weights and activationsالأوزان والتنشيطات | -12.4%-12.4% |
| BNNBNN | 11 | 11 | Noneلا شيء | -28.7%-28.7% |
Two causes, read off the tableسببان نقرؤهما من الجدول
The three rows differ in exactly two switches, so you can isolate each one. Compare XNOR-Net with Binarized Neural Network (BNN): both have 1 bit weights and 1 bit activations, and the only difference is the scale factors. That switch alone is worth about 16.3 points (-12.4 against -28.7). Now compare the Binary Weight Network (BWN) with XNOR-Net: both are scaled, and the only difference is whether activations are binarized. That switch costs 12.6 points (+0.2 against -12.4).
تختلف الصفوف الثلاثة في مفتاحين فقط، لذا يمكنك عزل أثر كل منهما. قارن XNOR-Net مع BNN: كلاهما بأوزان 1 بت وتنشيطات 1 بت، والفرق الوحيد هو معاملات المقياس. هذا المفتاح وحده يساوي نحو 16.3 نقطة (-12.4 مقابل -28.7). الآن قارن BWN مع XNOR-Net: كلاهما يستخدم المقياس، والفرق الوحيد هو هل التنشيطات ثنائية أم لا. هذا المفتاح يكلّف 12.6 نقطة (+0.2 مقابل -12.4).
- Scale factors matter. A sign function throws away magnitude. The scale puts the average magnitude back, so the binarized layer outputs numbers of the right size. Without it, every layer sees signals of the wrong scale, and the errors compound through depth.معاملات المقياس مهمة. دالة الإشارة (sign) تتخلص من المقدار. والمقياس يعيد متوسط المقدار، فتُخرج الطبقة الثنائية أرقامًا بالحجم الصحيح. ومن دونه ترى كل طبقة إشارات بمقياس خاطئ، وتتراكم الأخطاء مع العمق.
- Binarizing activations is the expensive part. Weights are learned, so training can adapt them to a two-level grid. Activations are fresh data at every input. Forcing each one to ±1 discards most of what the previous layer computed, and no amount of training can recover information that never reaches the next layer.تحويل التنشيطات إلى ثنائية هو الجزء المكلف. الأوزان تُتعلَّم، لذا يستطيع التدريب أن يكيّفها مع شبكة من مستويين. أما التنشيطات فهي بيانات جديدة مع كل مدخل. إجبار كل واحدة منها على ±1 يُهدر معظم ما حسبته الطبقة السابقة، ولا يستطيع أي قدر من التدريب أن يستعيد معلومات لم تصل أصلًا إلى الطبقة التالية.
The reward for paying the activation cost is speed. With both operands in ±1, a dot product becomes XNOR and popcount, which is why XNOR-Net reports 58× faster convolutions and 32× less memory. BWN keeps real-valued activations, so it saves memory but still needs additions for every weight. That is the trade the table prices: accuracy against arithmetic.
المكافأة على دفع كلفة التنشيطات هي السرعة. عندما يكون المعاملان كلاهما في ±1، يتحول الضرب النقطي إلى XNOR وpopcount، ولهذا يذكر XNOR-Net التفافات أسرع بـ 58× وذاكرة أقل بـ 32×. أما BWN فيُبقي التنشيطات بقيم حقيقية، فيوفر الذاكرة لكنه يظل يحتاج عمليات جمع لكل وزن. هذه هي المقايضة التي يسعّرها الجدول: الدقة مقابل كلفة العمليات الحسابية.
Quick checkاختبار سريع
Rank the AlexNet variants by ImageNet top-1 accuracy, best first.رتّب متغيرات AlexNet حسب دقة top-1 على ImageNet، من الأفضل إلى الأسوأ.
Recall
Which single change costs more on AlexNet: removing scale factors, or binarizing activations? Give the numbers.أي تغيير منفرد يكلّف أكثر على AlexNet: إزالة معاملات المقياس، أم تحويل التنشيطات إلى ثنائية؟ اذكر الأرقام.
Removing scale factors costs more: XNOR-Net to BNN is -12.4 to -28.7, about 16.3 points. Binarizing activations (BWN to XNOR-Net) costs 12.6 points. Both are large; neither is free.
إزالة معاملات المقياس تكلّف أكثر: من XNOR-Net إلى BNN ننتقل من -12.4 إلى -28.7، أي نحو 16.3 نقطة. وتحويل التنشيطات إلى ثنائية (من BWN إلى XNOR-Net) يكلّف 12.6 نقطة. كلاهما كبير، ولا أحد منهما مجاني.
Look at the running matrix again. Most weights sit near ±1 to ±2, but 0.09, 0.05, -0.14 and the two zeros are tiny. Binarization has no choice but to push them all the way to ±α, about ±1.05. A weight of 0.05 becomes 1.05, an error twenty times its own size. What these weights need is a third option: zero.
انظر مجددًا إلى المصفوفة المرجعية. معظم الأوزان تقع قرب ±1 إلى ±2، لكن 0.09 و0.05 و-0.14 والصفرين صغيرة جدًا. التحويل الثنائي لا يملك خيارًا سوى دفعها كلها إلى ±α، أي نحو ±1.05. فالوزن 0.05 يصبح 1.05، بخطأ يساوي عشرين ضعف حجمه. ما تحتاجه هذه الأوزان هو خيار ثالث: الصفر.
The ternary ruleالقاعدة الثلاثية
Ternary Weight Networks (TWN) (Li, Zhang and Liu, 2016) give every weight three choices. A dead zone of half-width around zero maps small weights to 0. Everything outside it keeps its sign and takes one shared magnitude .
تعطي Ternary Weight Networks (TWN) (Li, Zhang and Liu, 2016) كل وزن ثلاثة خيارات. منطقة ميتة (dead zone) نصف عرضها حول الصفر تحوّل الأوزان الصغيرة إلى 0. وكل ما يقع خارجها يحتفظ بإشارته ويأخذ مقدارًا واحدًا مشتركًا .
Both quantities come from minimizing the squared distance between the real and the ternary weights. For a fixed , the best scale is simply the mean magnitude of the weights that survive, the same logic as the binary but averaged only over the nonzero positions. The best has no closed form, so the paper assumes a weight distribution. Uniform weights give ; Gaussian weights give . The rule of thumb 0.7 sits between the two.
الكميتان كلتاهما تأتيان من تقليل المسافة التربيعية بين الأوزان الحقيقية والأوزان الثلاثية. عند تثبيت ، يكون أفضل مقياس ببساطة هو متوسط مقدار الأوزان الناجية، وهو المنطق نفسه لـ في الحالة الثنائية لكن المتوسط يؤخذ فقط على المواقع غير الصفرية. أما أفضل فليس له صيغة مغلقة، لذا تفترض الورقة توزيعًا للأوزان. الأوزان المنتظمة (uniform) تعطي ، والأوزان الغاوسية تعطي . والقاعدة التقريبية 0.7 تقع بين الحالتين.
Worked exampleمثال محلول
Ternarize the running 4×4 matrixحوّل المصفوفة المرجعية 4×4 إلى ثلاثية
Sum the magnitudesاجمع المقادير
Row sums of |W|: 4.64, 3.39, 3.86, 4.89. So ‖W‖₁ = 16.78 and E(|W|) = 16.78 / 16 = 1.049.
مجاميع صفوف |W|: 4.64 و3.39 و3.86 و4.89. إذن ‖W‖₁ = 16.78 وE(|W|) = 16.78 / 16 = 1.049.
Thresholdالعتبة
Δ = 0.7 × 1.049 = 0.734 ≈ 0.73.
Δ = 0.7 × 1.049 = 0.734 ≈ 0.73.
Find the survivorsحدّد الأوزان الناجية
Five weights have |r| ≤ 0.73: 0.09, 0.05, -0.14 and the two zeros. The other eleven survive, the smallest being -0.91.
خمسة أوزان تحقق |r| ≤ 0.73: 0.09 و0.05 و-0.14 والصفران. الأحد عشر الباقية تنجو، وأصغرها -0.91.
Shared scaleالمقياس المشترك
Their magnitudes sum to 16.78 - 0.09 - 0.05 - 0.14 = 16.50, so r_t = 16.50 / 11 = 1.5.
مجموع مقاديرها 16.78 - 0.09 - 0.05 - 0.14 = 16.50، إذن r_t = 16.50 / 11 = 1.5.
Write the ternary matrixاكتب المصفوفة الثلاثية
Rows: [1, -1, 1, 0], [0, 0, -1, 1], [-1, 1, 0, -1], [1, 0, 1, 1]. The deployed weight is this matrix times 1.5.
الصفوف: [1, -1, 1, 0] و[0, 0, -1, 1] و[-1, 1, 0, -1] و[1, 0, 1, 1]. والوزن المستخدم فعليًا عند النشر هو هذه المصفوفة مضروبة في 1.5.
Resultالنتيجة
Δ = 0.73, r_t = 1.5, eleven nonzeros, five zeros. Stored in 2 bits per weight plus one FP scale.Δ = 0.73، وr_t = 1.5، وأحد عشر عنصرًا غير صفري، وخمسة أصفار. تُخزَّن بـ 2 بت لكل وزن مع مقياس واحد بالفاصلة العائمة FP.
What the zero buysماذا يشتري الصفر
Measure squared error on the same matrix. Binary with α = 1.049 gives Σ(r - q)² ≈ 9.25. Ternary with Δ = 0.73 and r_t = 1.5 gives about 2.09, more than four times smaller. Two things happen at once. The tiny weights stop paying a 1.05 penalty, and because they no longer drag the average down, the scale moves up to 1.5, much closer to the large weights that actually carry the signal.
قِس الخطأ التربيعي على المصفوفة نفسها. التحويل الثنائي مع α = 1.049 يعطي Σ(r - q)² ≈ 9.25. والتحويل الثلاثي مع Δ = 0.73 وr_t = 1.5 يعطي نحو 2.09، أي أصغر بأكثر من أربع مرات. يحدث أمران في الوقت نفسه. الأوزان الصغيرة جدًا تتوقف عن دفع عقوبة 1.05، ولأنها لم تعد تسحب المتوسط إلى الأسفل، يرتفع المقياس إلى 1.5، وهو أقرب بكثير إلى الأوزان الكبيرة التي تحمل الإشارة فعلًا.
The price is one extra bit: three states need 2 bits, so the model is 16× smaller than FP32 instead of 32×. The arithmetic stays cheap. A zero weight is skipped, and the others are adds or subtracts followed by one multiply by per output. On the slide’s ResNet-18 numbers the trade is clearly worth it: 65.3 top-1 for TWN against 60.8 for BWN, with full precision at 69.6 (see the erratum below for which backbone the 65.3 really comes from).
الثمن بت إضافي واحد: ثلاث حالات تحتاج 2 بت، فيكون النموذج أصغر بـ 16× من FP32 بدلًا من 32×. وتبقى العمليات الحسابية رخيصة. الوزن الصفري يُتخطّى، والبقية عمليات جمع أو طرح يليها ضرب واحد في لكل مخرج. وبحسب أرقام ResNet-18 في الشريحة، فالمقايضة تستحق بوضوح: 65.3 في top-1 لـ TWN مقابل 60.8 لـ BWN، والدقة الكاملة عند 69.6 (انظر التصحيح أدناه لمعرفة الشبكة الأساسية التي يأتي منها الرقم 65.3 فعلًا).
Quick checkاختبار سريع
A layer has mean absolute weight 0.40. What threshold does the TWN rule of thumb give?طبقة متوسط القيمة المطلقة لأوزانها 0.40. ما العتبة التي تعطيها القاعدة التقريبية في TWN؟
Recall
Weights are [1.2, -0.1, 0.4, -0.9]. Compute Δ, r_t and the ternary vector.الأوزان هي [1.2, -0.1, 0.4, -0.9]. احسب Δ و r_t والمتجه الثلاثي.
E(|r|) = 2.6 / 4 = 0.65, so Δ = 0.455. Survivors are 1.2 and -0.9, so r_t = 2.1 / 2 = 1.05. Ternary vector [1, 0, 0, -1], deployed as [1.05, 0, 0, -1.05].
E(|r|) = 2.6 / 4 = 0.65، إذن Δ = 0.455. الناجيان هما 1.2 و-0.9، إذن r_t = 2.1 / 2 = 1.05. المتجه الثلاثي [1, 0, 0, -1]، ويُستخدم عند النشر بالشكل [1.05, 0, 0, -1.05].
Split the eleven TWN survivors by sign. The seven positives have mean magnitude 12.50 / 7 ≈ 1.79. The four negatives have mean magnitude 4.00 / 4 = 1.00. TWN forces both sides to share r_t = 1.5, which is too small for the positives and too large for the negatives. If each side had its own scale, the squared error on this matrix would fall from about 2.09 to about 0.52.
قسّم الأوزان الأحد عشر الناجية في TWN حسب الإشارة. الأوزان الموجبة السبعة متوسط مقدارها 12.50 / 7 ≈ 1.79. والسالبة الأربعة متوسط مقدارها 4.00 / 4 = 1.00. يجبر TWN الجانبين على مشاركة r_t = 1.5، وهو صغير جدًا على الموجبة وكبير جدًا على السالبة. لو كان لكل جانب مقياسه الخاص، لانخفض الخطأ التربيعي على هذه المصفوفة من نحو 2.09 إلى نحو 0.52.
Trained Ternary Quantization (TTQ) (Zhu, Han, Mao and Dally, ICLR 2017) takes that idea further: it gives each layer two scales, for positive weights and for negative ones, and it does not compute them from the weights at all. They are parameters, trained by gradient descent along with everything else.
يأخذ Trained Ternary Quantization (TTQ) (Zhu, Han, Mao and Dally, ICLR 2017) هذه الفكرة أبعد: يعطي كل طبقة مقياسين، للأوزان الموجبة و للسالبة، ولا يحسبهما من الأوزان إطلاقًا. إنهما معاملات تُدرَّب بالانحدار التدريجي (gradient descent) مع كل شيء آخر.
The pipelineخط المعالجة
- Normalize. Divide each layer’s full-precision weights by their largest magnitude so they lie in [-1, 1].التطبيع. اقسم الأوزان كاملة الدقة في كل طبقة على أكبر مقدار فيها حتى تقع في المجال [-1, 1].
- Quantize. Threshold at ±t to get intermediate weights in {-1, 0, +1}. The paper uses Δ = t × max(|w|) with t = 0.05 for every layer.التكميم. طبّق العتبة عند ±t للحصول على أوزان وسيطة في {-1, 0, +1}. تستخدم الورقة Δ = t × max(|w|) مع t = 0.05 لكل الطبقات.
- Scale. Multiply the +1 entries by and the -1 entries by .التدريج بالمقياس. اضرب العناصر +1 في والعناصر -1 في .
- Train. Backpropagate two gradients: one into and (which learns the ternary values), and one into the Full-precision weight copy of the weights (which learns which weights land in which state).التدريب. مرّر تدرّجين عكسيًا: أحدهما إلى و (وهو الذي يتعلّم القيم الثلاثية)، والآخر إلى النسخة كاملة الدقة full-precision copy من الأوزان (وهو الذي يتعلّم أي وزن يقع في أي حالة).
The latent weights get a Straight-through estimator (STE) style gradient, scaled by or outside the dead zone and passed through unchanged inside it. At inference the full-precision copy is thrown away. Only the 2-bit codes and two FP numbers per layer remain, so storage is still about 16× below FP32.
تحصل الأوزان الكامنة (latent) على تدرّج من نمط straight-through estimator (STE)، مضروب في أو خارج المنطقة الميتة، ويمر كما هو دون تغيير داخلها. عند الاستدلال (inference) تُرمى النسخة كاملة الدقة. ولا يبقى إلا رموز 2 بت ورقمان بالفاصلة العائمة لكل طبقة، فيظل التخزين أقل بنحو 16× من FP32.
Learning the scales from the loss, rather than fitting them to the weights, is the important part. Minimizing weight error is only a proxy; what matters is the network’s output. On ResNet-18 ImageNet, TTQ reaches 66.6 top-1 against 65.3 for TWN, closing the gap to full precision to three points with the same 2 bits per weight. On AlexNet the paper reports TTQ slightly above full precision.
تعلّم المقاييس من دالة الخسارة، بدلًا من ملاءمتها للأوزان، هو الجزء المهم. تقليل خطأ الأوزان ليس إلا بديلًا تقريبيًا؛ ما يهم فعلًا هو مخرج الشبكة. على ResNet-18 مع ImageNet يصل TTQ إلى 66.6 في top-1 مقابل 65.3 لـ TWN، فيقلّص الفجوة مع الدقة الكاملة إلى ثلاث نقاط بالعدد نفسه من البتات، 2 بت لكل وزن. وعلى AlexNet تذكر الورقة أن TTQ أعلى قليلًا من الدقة الكاملة.
| TWNTWN | TTQTTQ | |
|---|---|---|
| Valuesالقيم | {-r_t, 0, +r_t}{-r_t, 0, +r_t} | {-w_n, 0, +w_p}{-w_n, 0, +w_p} |
| Scaleالمقياس | One, computed from the weightsمقياس واحد، يُحسب من الأوزان | Two, trained by gradient descentمقياسان، يُدرَّبان بالانحدار التدريجي (gradient descent) |
| Symmetric?متماثل؟ | Yes, same magnitude both sidesنعم، المقدار نفسه على الجانبين | No, sides can differلا، يمكن أن يختلف الجانبان |
| Thresholdالعتبة | Δ = 0.7 · E(|r|)Δ = 0.7 · E(|r|) | Δ = t · max(|r|), t = 0.05Δ = t · max(|r|), t = 0.05 |
| Storageالتخزين | 2 bits per weight + 1 FP scale2 بت لكل وزن + مقياس واحد بالفاصلة العائمة FP | 2 bits per weight + 2 FP scales2 بت لكل وزن + مقياسان بالفاصلة العائمة FP |
| ResNet-18 top-1دقة top-1 لـ ResNet-18 | 65.365.3 | 66.666.6 |
Quick checkاختبار سريع
What is the essential change TTQ makes compared with TWN?ما التغيير الجوهري الذي يجريه TTQ مقارنة بـ TWN؟
Recall
Why can asymmetric scales help, and how are they found in TTQ?لماذا قد تفيد المقاييس غير المتماثلة، وكيف تُحدَّد في TTQ؟
Positive and negative weights in a layer need not have the same typical magnitude, so one shared scale misfits both sides. TTQ treats w_p and w_n as parameters and updates them by gradient descent on the training loss, summing the gradients of the weights assigned to each side.
لا يلزم أن يكون للأوزان الموجبة والسالبة في الطبقة المقدار المعتاد نفسه، لذا فإن مقياسًا واحدًا مشتركًا لا يناسب أيًّا من الجانبين. يعامل TTQ كلًّا من w_p وw_n كمعاملات ويحدّثهما بالانحدار التدريجي على خسارة التدريب، بجمع تدرّجات الأوزان المسندة إلى كل جانب.
The same 4×4 matrix has now been quantized three ways in this course. K-means turned it into 2-bit indices into a codebook of four FP centroids (2.00, 1.50, 0.00, -1.00). Linear quantization turned it into 2-bit signed integers with zero point -1 and scale 1.07, so . Binary or ternary quantization turned it into signs, optionally with zeros. What differs is not only the bit width but what the hardware has to do with the result.
المصفوفة 4×4 نفسها كُمّمت الآن بثلاث طرق في هذا المقرر. حوّلها K-means إلى فهارس من 2 بت تشير إلى دفتر رموز (codebook) من أربعة مراكز بالفاصلة العائمة (2.00، 1.50، 0.00، -1.00). وحوّلها التكميم الخطي إلى أعداد صحيحة بإشارة من 2 بت مع نقطة صفر -1 ومقياس 1.07، أي . وحوّلها التكميم الثنائي أو الثلاثي إلى إشارات، مع أصفار اختياريًا. والاختلاف ليس في عرض البت فقط، بل في ما يجب على العتاد أن يفعله بالنتيجة.
| Familyالعائلة | Storedما يُخزَّن | Arithmetic at inferenceالعمليات الحسابية عند الاستدلال | Bits per weightعدد البتات لكل وزن |
|---|---|---|---|
| Full precisionالدقة الكاملة | FP32 weightsأوزان FP32 | Floating-point arithmeticعمليات بالفاصلة العائمة | 3232 |
| K-meansK-means | Integer indices plus an FP codebookفهارس صحيحة مع دفتر رموز (codebook) بالفاصلة العائمة | Floating-point arithmeticعمليات بالفاصلة العائمة | 2 (plus codebook)2 (إضافة إلى دفتر الرموز) |
| Linearالخطي | Integer weights plus scale and zero pointأوزان صحيحة مع مقياس (scale) ونقطة صفر (zero point) | Integer arithmeticعمليات بالأعداد الصحيحة | 2 to 8من 2 إلى 8 |
| Binary or ternaryالثنائي أو الثلاثي | 1 or 2 bit weights plus a scaleأوزان 1 أو 2 بت مع مقياس | Bit operations (XNOR, popcount, add and subtract)عمليات على مستوى البت (XNOR وpopcount والجمع والطرح) | 1 or 21 أو 2 |
K-means saves memory only. Before every multiply the index is looked up in the codebook and the arithmetic runs in floating point, so it helps a memory-bound model but not a compute-bound one. Linear quantization saves memory and also lets the multiply-accumulate run in integers, which is why it dominates deployed edge inference. Binary and ternary quantization go furthest: multiplications collapse into sign flips, additions, skips for zeros, or Popcount when activations are binary too. The accuracy table from the first concept is the price of that last step.
K-means يوفر الذاكرة فقط. قبل كل عملية ضرب يُبحث عن الفهرس في دفتر الرموز وتُنفَّذ العمليات بالفاصلة العائمة، لذا يفيد النموذج المقيَّد بالذاكرة (memory-bound) لا المقيَّد بالحساب (compute-bound). والتكميم الخطي يوفر الذاكرة ويسمح أيضًا بتنفيذ الضرب والتجميع (multiply-accumulate) بالأعداد الصحيحة، ولهذا يهيمن على الاستدلال المنشور على أجهزة الحافة (edge). أما التكميم الثنائي والثلاثي فيذهبان أبعد: تنهار عمليات الضرب إلى قلب إشارات، وعمليات جمع، وتخطٍّ للأصفار، أو popcount عندما تكون التنشيطات ثنائية أيضًا. وجدول الدقة في المفهوم الأول هو ثمن هذه الخطوة الأخيرة.
Recall
For K-means, linear and ternary quantization, what is stored and what arithmetic runs?في تكميم K-means والخطي والثلاثي، ما الذي يُخزَّن وما العمليات الحسابية التي تُنفَّذ؟
K-means: integer indices plus an FP codebook, FP arithmetic. Linear: integer weights (with scale and zero point), integer arithmetic. Ternary: 2-bit weights plus a scale, bit operations (add, subtract, skip) with one scale multiply per output.
K-means: فهارس صحيحة مع دفتر رموز بالفاصلة العائمة، وعمليات بالفاصلة العائمة. الخطي: أوزان صحيحة (مع مقياس ونقطة صفر)، وعمليات بالأعداد الصحيحة. الثلاثي: أوزان من 2 بت مع مقياس، وعمليات على مستوى البت (جمع، طرح، تخطٍّ) مع ضرب واحد في المقياس لكل مخرج.
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- AlexNet top-1 change: BWN (W 1 bit, A 32 bit, scaled) +0.2%, XNOR-Net (W and A 1 bit, scaled) -12.4%, BNN (W and A 1 bit, no scale) -28.7%.التغير في دقة top-1 لـ AlexNet: BWN (أوزان 1 بت، تنشيطات 32 بت، مع مقياس) +0.2%، وXNOR-Net (أوزان وتنشيطات 1 بت، مع مقياس) -12.4%، وBNN (أوزان وتنشيطات 1 بت، بلا مقياس) -28.7%.
- Scale factors recover about 16 points between BNN and XNOR-Net. Binarizing activations is the expensive step: BWN to XNOR-Net loses 12.6 points.معاملات المقياس تستعيد نحو 16 نقطة بين BNN وXNOR-Net. وتحويل التنشيطات إلى ثنائية هو الخطوة المكلفة: الانتقال من BWN إلى XNOR-Net يخسر 12.6 نقطة.
- TWN: q = r_t if r > Δ, 0 if |r| ≤ Δ, -r_t if r < -Δ, with Δ = 0.7 · E(|r|) and r_t = mean of |r| over the weights above Δ.قاعدة TWN: q = r_t إذا كان r > Δ، و0 إذا كان |r| ≤ Δ، و-r_t إذا كان r < -Δ، حيث Δ = 0.7 · E(|r|) و r_t هو متوسط |r| على الأوزان التي تتجاوز Δ.
- Running matrix: Δ = 0.7 × 16.78 / 16 = 0.73, eleven weights survive, r_t = 16.50 / 11 = 1.5, and five small weights become 0.المصفوفة المرجعية: Δ = 0.7 × 16.78 / 16 = 0.73، ينجو أحد عشر وزنًا، و r_t = 16.50 / 11 = 1.5، وتصبح خمسة أوزان صغيرة 0.
- Ternary needs 2 bits per weight (16× smaller than FP32). On the running matrix its squared error is 2.09 against 9.25 for binary.التكميم الثلاثي يحتاج 2 بت لكل وزن (أصغر بـ 16× من FP32). وعلى المصفوفة المرجعية يبلغ خطؤه التربيعي 2.09 مقابل 9.25 للثنائي.
- TTQ normalizes weights to [-1, 1], thresholds at t, then scales by two trained parameters: w_p for positives and w_n for negatives.يطبّع TTQ الأوزان إلى المجال [-1, 1]، ثم يطبّق العتبة عند t، ثم يضرب في معاملين مدرَّبين: w_p للموجبة و w_n للسالبة.
- ResNet-18 ImageNet top-1: FP 69.6, BWN 60.8, TWN 65.3, TTQ 66.6.دقة top-1 لـ ResNet-18 على ImageNet: الدقة الكاملة FP 69.6، وBWN 60.8، وTWN 65.3، وTTQ 66.6.
- K-means: integer indices plus FP codebook, FP arithmetic. Linear: integer weights, integer arithmetic. Binary or ternary: 1 or 2 bit weights, bit operations.K-means: فهارس صحيحة مع دفتر رموز (codebook) بالفاصلة العائمة، وعمليات بالفاصلة العائمة. الخطي: أوزان صحيحة وعمليات صحيحة. الثنائي أو الثلاثي: أوزان 1 أو 2 بت، وعمليات على مستوى البت.
Sourcesالمصادر
- XNOR-Net: ImageNet Classification Using Binary Convolutional Neural Networks (Rastegari, Ordonez, Redmon, Farhadi)PaperECCV 2016Table 1: AlexNet top-1 full precision 56.6, BWN 56.8, XNOR-Net 44.2, BNN 27.9. 32× memory saving, 58× faster convolutions.الجدول 1: دقة top-1 لـ AlexNet بالدقة الكاملة 56.6، وBWN 56.8، وXNOR-Net 44.2، وBNN 27.9. توفير في الذاكرة بمقدار 32×، والتفافات أسرع بـ 58×.(opens in a new tab)(يفتح في تبويب جديد)
- Binarized Neural Networks: Training Deep Neural Networks with Weights and Activations Constrained to +1 or -1 (Courbariaux et al.)PaperarXiv 2016BNN: binary weights and activations without scale factors.BNN: أوزان وتنشيطات ثنائية دون معاملات مقياس.(opens in a new tab)(يفتح في تبويب جديد)
- Ternary Weight Networks (Li, Zhang, Liu), original 2016 versionPaperarXiv 2016Section 3.2.3: Δ* = 0.7 · E(|W|) as a rule of thumb between the uniform (2/3) and Gaussian (0.75) cases; scale = mean of survivors.القسم 3.2.3: Δ* = 0.7 · E(|W|) كقاعدة تقريبية بين الحالة المنتظمة (2/3) والحالة الغاوسية (0.75)، والمقياس = متوسط الأوزان الناجية.(opens in a new tab)(يفتح في تبويب جديد)
- Ternary Weight Networks, revised versionPaperarXivTable 2: ResNet-18 top-1 61.8 (ResNet-18B 65.3); revised rule of thumb 0.75 · E(|W|).الجدول 2: دقة top-1 لـ ResNet-18 هي 61.8 (و ResNet-18B هي 65.3)، والقاعدة التقريبية المعدّلة 0.75 · E(|W|).(opens in a new tab)(يفتح في تبويب جديد)
- Trained Ternary Quantization (Zhu, Han, Mao, Dally)PaperICLR 2017Normalize to [-1, 1], threshold Δ = t × max(|w|) with t = 0.05, trained w_p and w_n. Table 3: ResNet-18 error 30.4 / 39.2 / 34.7 / 33.4 for FP, BWN, TWN, TTQ.التطبيع إلى [-1, 1]، والعتبة Δ = t × max(|w|) مع t = 0.05، ومعاملان مدرَّبان w_p و w_n. الجدول 3: خطأ ResNet-18 هو 30.4 / 39.2 / 34.7 / 33.4 لكل من FP وBWN وTWN وTTQ.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 6: Quantization (Part II)DocsMIT HAN LabThe course these slides are adapted from.المقرر الذي اقتُبست منه هذه الشرائح.(opens in a new tab)(يفتح في تبويب جديد)