Lecture 05-3 · Quantization III

المحاضرة 05-3 · التكميم الثالث

Ternary weights: TWN and TTQ

الأوزان الثلاثية: TWN وTTQ

Slides 62–64الشرائح 62–64 P1 MUSTP1 ضروري

Add a third level, 0, so small weights stop being forced to ±1. Ternary Weight Networks pick a threshold Δ and a scale rt from the weights; Trained Ternary Quantization learns separate positive and negative scales. Two bits of storage close much of the gap to full precision.

نضيف مستوى ثالثاً هو 0 حتى لا تُجبَر الأوزان الصغيرة على ±1. شبكات Ternary Weight Networks تختار عتبة Δ ومقياساً r_t من الأوزان نفسها، وTrained Ternary Quantization تتعلم مقياسين منفصلين للموجب والسالب. بتّان من التخزين يغلقان جزءاً كبيراً من الفجوة مع الدقة الكاملة.

TWN: threshold, mask, scale

TWN: العتبة والقناع والمقياس

Ternary Weight Networks (TWN, slide 62) quantize every weight to +rt, 0 or −rt. A weight is zeroed if its magnitude is at most Δ; otherwise it keeps only its sign and is scaled by rt.

شبكات Ternary Weight Networks (TWN) في الشريحة 62 تكمّم كل وزن إلى +r_t أو 0 أو −r_t. يُصفَّر الوزن إذا كان مقداره لا يتجاوز Δ، وإلا يحتفظ بإشارته فقط ويُضرب في r_t.

q = rt if r > Δ,   0 if |r| ≤ Δ,   −rt if r < −Δ

Δ = 0.7 × E(|r|),    rt = E|r| > Δ(|r|)

العتبة Δ = 0.7 × E(|r|) أي 0.7 مضروبة في متوسط القيمة المطلقة لكل الأوزان. والمقياس r_t = E_{|r| > Δ}(|r|) أي متوسط القيمة المطلقة للأوزان الناجية فقط (التي تجاوزت العتبة).

Two different averages are involved. Δ uses the mean |r| over all n weights. rt uses the mean |r| over only the surviving (nonzero) weights, so rt is always larger than E(|r|).

هناك متوسطان مختلفان. Δ يستخدم متوسط |r| على كل الأوزان n. وr_t يستخدم متوسط |r| على الأوزان الناجية (غير الصفرية) فقط، لذلك يكون r_t دائماً أكبر من E(|r|).

W (32-bit float) WT ternary (2-bit) 2.09−0.981.480.09 0.05−0.14−1.082.12 −0.911.920−1.03 1.8701.531.49 |r| ≤ 0.73 → 0 1−110 00−11 −110−1 1011 Δ = 0.7 × 16.78/16 = 0.73 × rt = 1.5 = 16.50 / 11 5 zeros, 11 nonzero Orange cells: |r| ≤ Δ, quantized to 0
Slide 62. The five orange weights (0.09, 0.05, −0.14 and the two zeros) fall inside ±Δ and become 0. The other eleven keep their sign and share rt = 1.5.الشريحة 62. الأوزان البرتقالية الخمسة (0.09 و0.05 و−0.14 والصفران) تقع داخل ±Δ فتصبح 0. أما الأحد عشر الباقية فتحتفظ بإشاراتها وتشترك في r_t = 1.5.
Worked exampleمثال محلول

Run TWN on the slide 62 matrix: Δ, the mask, rt and the error.

طبّق TWN على مصفوفة الشريحة 62: Δ والقناع وr_t والخطأ.

Step 1: mean |r| over all 16 weights
  ‖W‖₁ = 4.64 + 3.39 + 3.86 + 4.89 = 16.78
  E(|r|) = 16.78 / 16 = 1.04875 ≈ 1.05

Step 2: threshold
  Δ = 0.7 × 1.04875 = 0.734 ≈ 0.73

Step 3: mask (|r| ≤ 0.73 → 0)
  zeroed: 0.09, 0.05, −0.14, 0, 0          (5 entries)
  kept:   2.09, −0.98, 1.48, −1.08, 2.12,
          −0.91, 1.92, −1.03, 1.87, 1.53, 1.49   (11 entries)

  W^T =  1  −1   1   0
         0   0  −1   1
        −1   1   0  −1
         1   0   1   1

Step 4: scale over survivors only
  sum of kept |r| = 16.78 − (0.09 + 0.05 + 0.14) = 16.50
  r_t = 16.50 / 11 = 1.5

Step 5: error
  ‖W − 1.5 W^T‖²_F = 2.09
  compare BWN: ‖W − 1.05 W^B‖²_F = 9.24

Allowing a zero cuts the squared error on this matrix from 9.24 to 2.09, because the five tiny weights are no longer pushed to ±1.05 and the larger weights get a scale (1.5) closer to their true size.

السماح بالصفر يخفض الخطأ التربيعي لهذه المصفوفة من 9.24 إلى 2.09، لأن الأوزان الخمسة الصغيرة لم تعد تُدفع إلى ±1.05، والأوزان الأكبر تحصل على مقياس (1.5) أقرب إلى حجمها الحقيقي.

TTQ: learn the two scales

TTQ: تعلّم المقياسين

Trained Ternary Quantization (TTQ, slide 63) replaces the fixed rt with two trainable parameters: wp for the positive level and wn for the negative level. The levels become asymmetric: {−wn, 0, +wp}.

Trained Ternary Quantization (TTQ) في الشريحة 63 تستبدل r_t الثابت بمعاملين قابلين للتدريب: w_p للمستوى الموجب وw_n للمستوى السالب. فتصبح المستويات غير متماثلة: {−w_n, 0, +w_p}.

q = wp if r > Δ,   0 if |r| ≤ Δ,   −wn if r < −Δ

The pipeline on the slide: take the full-precision weights, normalize them to [−1, 1], quantize with threshold t to an intermediate ternary weight in {−1, 0, 1}, then multiply the positive entries by wp and the negative entries by wn. Both scales are updated by gradient descent along with the network.

خط المعالجة في الشريحة: خذ الأوزان كاملة الدقة، وطبّعها (normalize) إلى [−1, 1]، ثم كمّمها بالعتبة t إلى وزن ثلاثي وسيط في {−1, 0, 1}، ثم اضرب العناصر الموجبة في w_p والسالبة في w_n. ويُحدَّث المقياسان بالانحدار التدريجي (gradient descent) مع الشبكة.

Full-precisionweight Normalizeto [−1, 1] Quantizethreshold ±t Scaletrained wn, wp Final ternary{−wn, 0, wp} {−1, 0, +1} learned by backprop TWN uses one fixed rt
Slide 63. TTQ's only change from TWN is the scaling step: two learned, possibly unequal scales instead of one statistic.الشريحة 63. التغيير الوحيد في TTQ عن TWN هو خطوة التحجيم: مقياسان متعلَّمان قد يكونان غير متساويين، بدلاً من إحصائية واحدة.

Results on ResNet-18

النتائج على ResNet-18

ImageNet top-1دقة top-1 على ImageNetFull precisionالدقة الكاملة1 bit (BWN)1 بت (BWN)2 bit (TWN)2 بت (TWN)TTQ
ResNet-1869.660.865.366.6

نتائج الشريحتين 62 و63 على ResNet-18: الدقة الكاملة 69.6، وBWN بـ 1 بت 60.8 (خسارة 8.8)، وTWN بـ 2 بت 65.3 (خسارة 4.3)، وTTQ 66.6 (خسارة 3.0).

Each step up the ladder buys accuracy: adding the zero level (BWN to TWN) gains 4.5 points, and learning the scales (TWN to TTQ) gains another 1.3. TTQ is still 3.0 below full precision.

كل درجة في السلم تشتري دقة: إضافة مستوى الصفر (من BWN إلى TWN) تكسب 4.5 نقطة، وتعلّم المقاييس (من TWN إلى TTQ) تكسب 1.3 أخرى. ويبقى TTQ أقل من الدقة الكاملة بـ 3.0.

Memory trickحيلة للتذكر

"0.7 of the mean sets the gate; the survivors set the scale". Δ comes from all weights, rt from only the ones that pass Δ.

"0.7 من المتوسط تحدد البوابة، والناجون يحددون المقياس". Δ يأتي من كل الأوزان، وr_t فقط من الأوزان التي تتجاوز Δ.

Exam trapفخ في الاختبار

rt divides by the survivors, not by n. 16.50 / 11 = 1.5. Dividing by 16 gives 1.03, and using all 16 values (16.78 / 16) gives 1.05, which is the BWN α, not rt.

r_t يقسم على الناجين وليس على n. 16.50 / 11 = 1.5. القسمة على 16 تعطي 1.03، واستخدام كل القيم الـ 16 (16.78 / 16) يعطي 1.05 وهو α الخاص بـ BWN وليس r_t.

The boundary is |r| ≤ Δ → 0. A weight exactly equal to Δ is zeroed, and the two true zeros in W stay 0 (unlike binary, where sign(0) = +1).

الحد هو |r| ≤ Δ → 0. الوزن المساوي لـ Δ تماماً يُصفَّر، والصفران الحقيقيان في W يبقيان 0 (بخلاف الثنائي حيث sign(0) = +1).

3 levels need 2 bits. log2(3) ≈ 1.58, rounded up to 2 bits of storage per weight, so ternary is 16x smaller than fp32, not 32x.

ثلاثة مستويات تحتاج 2 بت. log2(3) ≈ 1.58 ويُقرَّب إلى 2 بت تخزين لكل وزن، لذلك يكون ternary أصغر من fp32 بـ 16x وليس 32x.

Check yourself

اختبر نفسك

W = [1.2, −0.1, 0.6, −2.0, 0.1, 0.4, −0.8, 0]. Using TWN, what is Δ?

W = [1.2, −0.1, 0.6, −2.0, 0.1, 0.4, −0.8, 0]. باستخدام TWN، ما قيمة Δ؟

C. Σ|r| = 1.2 + 0.1 + 0.6 + 2.0 + 0.1 + 0.4 + 0.8 + 0 = 5.2, E(|r|) = 5.2 / 8 = 0.65, Δ = 0.7 × 0.65 = 0.455. A forgets the 0.7. B uses the signed mean (0.7 × −0.6 / 8 ≈ −0.05). D is 0.7 × max|r|.

C. Σ|r| = 1.2 + 0.1 + 0.6 + 2.0 + 0.1 + 0.4 + 0.8 + 0 = 5.2، وE(|r|) = 5.2 / 8 = 0.65، وΔ = 0.7 × 0.65 = 0.455. الخيار A ينسى 0.7. والخيار B يستخدم المتوسط مع الإشارة (0.7 × −0.6 / 8 ≈ −0.05). والخيار D هو 0.7 × max|r|.

Continuing with Δ = 0.455, what is rt?

بمتابعة Δ = 0.455، ما قيمة r_t؟

A. Survivors are 1.2, 0.6, −2.0, −0.8 (|r| > 0.455), sum 4.6, rt = 4.6 / 4 = 1.15. B averages all eight. C divides 4.6 by 8. D is the max.

A. الناجون هم 1.2 و0.6 و−2.0 و−0.8 (|r| > 0.455)، والمجموع 4.6، وr_t = 4.6 / 4 = 1.15. الخيار B يأخذ متوسط الثمانية كلها. والخيار C يقسم 4.6 على 8. والخيار D هو القيمة العظمى.

What does TTQ change compared with TWN?

ما الذي يغيّره TTQ مقارنة بـ TWN؟

D. TTQ learns separate positive and negative scales, so the levels are {−wn, 0, wp}. A would make it binary. B is wrong, ternary is 2 bits. C is not on the slide.

D. يتعلم TTQ مقياسين منفصلين للموجب والسالب، فتكون المستويات {−w_n, 0, w_p}. الخيار A يجعله ثنائياً. والخيار B خاطئ، فالثلاثي يحتاج 2 بت. والخيار C غير موجود في الشريحة.

ResNet-18 ImageNet top-1: which ordering matches the slides?

دقة top-1 لـ ResNet-18 على ImageNet: أي ترتيب يطابق الشرائح؟

B. TTQ (66.6) beats TWN (65.3), which beats BWN (60.8). A swaps TWN and TTQ. C swaps BWN and TWN. D shifts the numbers.

B. TTQ (66.6) يتفوق على TWN (65.3)، الذي يتفوق على BWN (60.8). الخيار A يبدّل TWN وTTQ. والخيار C يبدّل BWN وTWN. والخيار D يزيح الأرقام.

In the slide 62 matrix, how many weights are zeroed by TWN, and why does that make rt = 1.5?

في مصفوفة الشريحة 62، كم وزناً يُصفّره TWN، ولماذا يجعل ذلك r_t = 1.5؟

C. 0.09, 0.05, −0.14 and the two zeros are within Δ = 0.73, leaving 11 survivors whose |r| sum is 16.78 − 0.28 = 16.50. A counts only the true zeros. B forgets the two zeros in the count. D keeps the zeroed weights in the numerator.

C. القيم 0.09 و0.05 و−0.14 والصفران تقع ضمن Δ = 0.73، فيبقى 11 وزناً ناجياً مجموع |r| لها 16.78 − 0.28 = 16.50. الخيار A يعدّ الصفرين الحقيقيين فقط. والخيار B ينسى الصفرين في العدّ. والخيار D يُبقي الأوزان المصفّرة في البسط.