Majid Al-RaimiAccuracy cost of binarization and ternary quantizationكلفة التثنيث على الدقة والتكميم الثلاثي

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 دقيقة
Understoodما فهمته
0/4 conceptsمفاهيم

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في نهاية هذا الجزء ستستطيع

  1. 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) وتحويل التنشيطات إلى قيم ثنائية.
  2. Compute the TWN threshold Δ, the shared scale r_t and the ternary matrix for a given weight tensor.احسب عتبة TWN أي Δ، والمقياس المشترك r_t، والمصفوفة الثلاثية لمصفوفة أوزان معطاة.
  3. Explain why a zero state makes ternary weights far more accurate than binary ones for only one extra bit.اشرح لماذا تجعل حالة الصفر الأوزانَ الثلاثية أدق بكثير من الثنائية مقابل بت إضافي واحد فقط.
  4. State exactly what TTQ changes relative to TWN, and why learned asymmetric scales help.اذكر بدقة ما الذي يغيّره TTQ مقارنة بـ TWN، ولماذا تفيد المقاييس غير المتماثلة المتعلَّمة.
  5. 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بتات الأوزان WA bitsبتات التنشيطات AScale factors onمعاملات المقياس علىTop-1 deltaفرق top-1
BWNBWN113232Weights onlyالأوزان فقط+0.2%+0.2%
XNOR-NetXNOR-Net1111Weights and activationsالأوزان والتنشيطات-12.4%-12.4%
BNNBNN1111Noneلا شيء-28.7%-28.7%
AlexNet on ImageNet, top-1 change relative to full precision (Rastegari et al., Table 1)AlexNet على ImageNet، التغير في دقة top-1 مقارنة بالدقة الكاملة (Rastegari وآخرون، الجدول 1)
AlexNet top-1 change for the three binarized variants: weights-only BWN breaks even, scaled XNOR-Net loses 12.4 points, unscaled BNN loses 28.7التغير في دقة top-1 لـ AlexNet في المتغيرات الثنائية الثلاثة: BWN (الأوزان فقط) يتعادل تقريبًا، وXNOR-Net مع المقياس يخسر 12.4 نقطة، وBNN بلا مقياس يخسر 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 α=1n∥W∥1\alpha = \tfrac{1}{n}\lVert W \rVert_1 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) تتخلص من المقدار. والمقياس α=1n∥W∥1\alpha = \tfrac{1}{n}\lVert W \rVert_1 يعيد متوسط المقدار، فتُخرج الطبقة الثنائية أرقامًا بالحجم الصحيح. ومن دونه ترى كل طبقة إشارات بمقياس خاطئ، وتتراكم الأخطاء مع العمق.
  • 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 Δ\Delta around zero maps small weights to 0. Everything outside it keeps its sign and takes one shared magnitude rtr_t.

تعطي Ternary Weight Networks (TWN) (Li, Zhang and Liu, 2016) كل وزن ثلاثة خيارات. منطقة ميتة (dead zone) نصف عرضها Δ\Delta حول الصفر تحوّل الأوزان الصغيرة إلى 0. وكل ما يقع خارجها يحتفظ بإشارته ويأخذ مقدارًا واحدًا مشتركًا rtr_t.

q={rt,r>Δ0,∣r∣≤Δ−rt,r<−ΔΔ=0.7×E(∣r∣),rt=E∣r∣>Δ(∣r∣)q = \begin{cases} r_t, & r > \Delta \\ 0, & |r| \le \Delta \\ -r_t, & r < -\Delta \end{cases} \qquad \Delta = 0.7 \times \mathbb{E}(|r|), \quad r_t = \mathbb{E}_{|r| > \Delta}(|r|)
TWN quantizer: a dead zone, then one scale for the survivorsمكمِّم TWN: منطقة ميتة، ثم مقياس واحد للأوزان الباقية

Both quantities come from minimizing the squared distance between the real and the ternary weights. For a fixed Δ\Delta, the best scale is simply the mean magnitude of the weights that survive, the same logic as the binary α\alpha but averaged only over the nonzero positions. The best Δ\Delta has no closed form, so the paper assumes a weight distribution. Uniform weights give Δ∗≈23 E(∣W∣)\Delta^\ast \approx \tfrac{2}{3}\,\mathbb{E}(|W|); Gaussian weights give 0.6σ≈0.75 E(∣W∣)0.6\sigma \approx 0.75\,\mathbb{E}(|W|). The rule of thumb 0.7 sits between the two.

الكميتان كلتاهما تأتيان من تقليل المسافة التربيعية بين الأوزان الحقيقية والأوزان الثلاثية. عند تثبيت Δ\Delta، يكون أفضل مقياس ببساطة هو متوسط مقدار الأوزان الناجية، وهو المنطق نفسه لـ α\alpha في الحالة الثنائية لكن المتوسط يؤخذ فقط على المواقع غير الصفرية. أما أفضل Δ\Delta فليس له صيغة مغلقة، لذا تفترض الورقة توزيعًا للأوزان. الأوزان المنتظمة (uniform) تعطي Δ∗≈23 E(∣W∣)\Delta^\ast \approx \tfrac{2}{3}\,\mathbb{E}(|W|)، والأوزان الغاوسية تعطي 0.6σ≈0.75 E(∣W∣)0.6\sigma \approx 0.75\,\mathbb{E}(|W|). والقاعدة التقريبية 0.7 تقع بين الحالتين.

Worked exampleمثال محلول

Ternarize the running 4×4 matrixحوّل المصفوفة المرجعية 4×4 إلى ثلاثية

  1. 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.

  2. Thresholdالعتبة

    Δ = 0.7 × 1.049 = 0.734 ≈ 0.73.

    Δ = 0.7 × 1.049 = 0.734 ≈ 0.73.

  3. 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.

  4. 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.

  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.

  6. 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.
The 4×4 matrix and its magnitudes: the five weights inside Δ = 0.73 fall to 0, the eleven outside share r_t = 1.5المصفوفة 4×4 ومقاديرها: الأوزان الخمسة داخل Δ = 0.73 تسقط إلى 0، والأحد عشر الواقعة خارجها تشترك في r_t = 1.5

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 rtr_t 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×. وتبقى العمليات الحسابية رخيصة. الوزن الصفري يُتخطّى، والبقية عمليات جمع أو طرح يليها ضرب واحد في rtr_t لكل مخرج. وبحسب أرقام 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, wpw_p for positive weights and wnw_n 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) هذه الفكرة أبعد: يعطي كل طبقة مقياسين، wpw_p للأوزان الموجبة وwnw_n للسالبة، ولا يحسبهما من الأوزان إطلاقًا. إنهما معاملات تُدرَّب بالانحدار التدريجي (gradient descent) مع كل شيء آخر.

q={wp,r>Δ0,∣r∣≤Δ−wn,r<−Δq = \begin{cases} w_p, & r > \Delta \\ 0, & |r| \le \Delta \\ -w_n, & r < -\Delta \end{cases}
TTQ quantizer: same dead zone, two independent trained magnitudesمكمِّم TTQ: المنطقة الميتة نفسها، ومقداران مستقلان متعلَّمان

The pipelineخط المعالجة

  1. Normalize. Divide each layer’s full-precision weights by their largest magnitude so they lie in [-1, 1].التطبيع. اقسم الأوزان كاملة الدقة في كل طبقة على أكبر مقدار فيها حتى تقع في المجال [-1, 1].
  2. 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 لكل الطبقات.
  3. Scale. Multiply the +1 entries by wpw_p and the -1 entries by wnw_n.التدريج بالمقياس. اضرب العناصر +1 في wpw_p والعناصر -1 في wnw_n.
  4. Train. Backpropagate two gradients: one into wpw_p and wnw_n (which learns the ternary values), and one into the Full-precision weight copy of the weights (which learns which weights land in which state).التدريب. مرّر تدرّجين عكسيًا: أحدهما إلى wpw_p وwnw_n (وهو الذي يتعلّم القيم الثلاثية)، والآخر إلى النسخة كاملة الدقة full-precision copy من الأوزان (وهو الذي يتعلّم أي وزن يقع في أي حالة).
∂L∂wp=∑i∈Ip∂L∂qi,∂L∂wn=∑i∈In∂L∂qi\frac{\partial L}{\partial w_p} = \sum_{i \in I_p} \frac{\partial L}{\partial q_i}, \qquad \frac{\partial L}{\partial w_n} = \sum_{i \in I_n} \frac{\partial L}{\partial q_i}
Each scale collects the gradients of the weights assigned to its side (sign conventions as in the paper)كل مقياس يجمع تدرجات الأوزان المسندة إلى جهته (اصطلاحات الإشارة كما في الورقة)

The latent weights get a Straight-through estimator (STE) style gradient, scaled by wpw_p or wnw_n 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)، مضروب في wpw_p أو wnw_n خارج المنطقة الميتة، ويمر كما هو دون تغيير داخلها. عند الاستدلال (inference) تُرمى النسخة كاملة الدقة. ولا يبقى إلا رموز 2 بت ورقمان بالفاصلة العائمة لكل طبقة، فيظل التخزين أقل بنحو 16× من FP32.

TTQ starts from three symmetric levels {-1, 0, +1}; training moves the outer levels to -w_n and w_p, which need not mirror each otherيبدأ TTQ من ثلاثة مستويات متماثلة {-1, 0, +1}، ثم ينقل التدريب المستويين الخارجيين إلى -w_n و w_p، ولا يلزم أن يكونا متناظرين

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 أعلى قليلًا من الدقة الكاملة.

ResNet-18 on ImageNet, top-1: full precision 69.6, BWN 60.8, TWN 65.3, TTQ 66.6 (axis starts at 55)دقة top-1 لـ ResNet-18 على ImageNet: الدقة الكاملة 69.6، وBWN 60.8، وTWN 65.3، وTTQ 66.6 (يبدأ المحور من 55)
TWNTWNTTQTTQ
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 بت لكل وزن + مقياس واحد بالفاصلة العائمة FP2 bits per weight + 2 FP scales2 بت لكل وزن + مقياسان بالفاصلة العائمة FP
ResNet-18 top-1دقة top-1 لـ ResNet-1865.365.366.666.6
TWN against TTQTWN مقابل TTQ

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 r=(q−(−1))×1.07r = (q - (-1)) \times 1.07. 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، أي r=(q−(−1))×1.07r = (q - (-1)) \times 1.07. وحوّلها التكميم الثنائي أو الثلاثي إلى إشارات، مع أصفار اختياريًا. والاختلاف ليس في عرض البت فقط، بل في ما يجب على العتاد أن يفعله بالنتيجة.

FamilyالعائلةStoredما يُخزَّنArithmetic at inferenceالعمليات الحسابية عند الاستدلالBits per weightعدد البتات لكل وزن
Full precisionالدقة الكاملةFP32 weightsأوزان FP32Floating-point arithmeticعمليات بالفاصلة العائمة3232
K-meansK-meansInteger 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
Quantization families by storage and arithmeticعائلات التكميم بحسب التخزين والعمليات الحسابية

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المصادر