Lecture 05-2 · Quantization II

المحاضرة 05-2 · التكميم (Quantization II)

Quantized FC and conv layers with bias

طبقات FC وconv المكمّمة مع الـ bias

Slides 18–25الشرائح 18–25 P2 SHOULD

Add a bias to the integer matmul and choose its quantization so it drops straight into the int32 accumulator. Then swap the matmul for a convolution, and see what INT8 costs in accuracy.

نضيف bias إلى ضرب المصفوفات الصحيح ونختار تكميمه بحيث يُجمع مباشرة في المجمّع int32. ثم نستبدل الضرب بـ convolution، ونرى كم تكلّف INT8 من الدقة.

The FC derivation, one step at a time

اشتقاق طبقة FC خطوة بخطوة

Start from Y = WX + b and replace every real tensor by its affine form r = S(q − Z) (slide 18):

نبدأ من Y = WX + b ونستبدل كل tensor حقيقي بصيغته r = S(q − Z) (الشريحة 18):

SY(qY − ZY) = SW(qW − ZW) · SX(qX − ZX) + Sb(qb − Zb)

  1. ZW = 0 (symmetric weights): SY(qY − ZY) = SWSX(qWqX − ZXqW) + Sb(qb − Zb)
  2. ZW = 0 (أوزان متماثلة): SY(qY − ZY) = SWSX(qWqX − ZXqW) + Sb(qb − Zb)
  3. Zb = 0, Sb = SWSX (slide 19): the bias now shares the matmul's scale, so it factors out: SY(qY − ZY) = SWSX(qWqX − ZXqW + qb)
  4. Zb = 0 وSb = SWSX (الشريحة 19): صار للـ bias نفس scale ضرب المصفوفات، فيخرج كعامل مشترك: SY(qY − ZY) = SWSX(qWqX − ZXqW + qb)
  5. Solve for qY (slide 20): qY = (SWSX/SY)(qWqX + qb − ZXqW) + ZY
  6. حُلّ لإيجاد qY (الشريحة 20): qY = (SWSX/SY)(qWqX + qb − ZXqW) + ZY
  7. Fold the constants: qbias = qb − ZXqW, computed once offline.
  8. ادمج الثوابت: qbias = qb − ZXqW، ويُحسب مرة واحدة مسبقاً (offline).

الخطوات: (1) ZW = 0 لأن الأوزان متماثلة. (2) Zb = 0 وSb = SWSX، فيصبح للـ bias نفس scale الضرب ويخرج كعامل مشترك. (3) نحلّ لـ qY. (4) ندمج الثوابت: qbias = qb − ZXqW، ويُحسب مرة واحدة مسبقاً.

qY = SWSXSY ( qWqX + qbias ) + ZY

Slide 21 labels the three boxes: SWSX/SY rescales to N-bit int; qWqX + qbias is N-bit int multiply with 32-bit int add; + ZY is an N-bit int add. Note on the slide: both qb and qbias are 32 bits.

تسمّي الشريحة 21 الصناديق الثلاثة: SWSX/SY يعيد التحجيم إلى عدد صحيح من N بت؛ qWqX + qbias ضرب صحيح N-bit مع جمع 32-bit؛ + ZY جمع صحيح N-bit. وملاحظة الشريحة: qb وqbias كلاهما 32 بت.

Why Sb = SWSX

لماذا Sb = SWSX

The accumulator holds qWqX sums whose real value is SWSX per unit. Giving the bias the same unit lets you add qb directly as an integer, with no extra rescale.

المجمّع يحمل مجاميع qWqX وقيمتها الحقيقية SWSX لكل وحدة. إعطاء الـ bias نفس الوحدة يسمح بجمع qb مباشرة كعدد صحيح، بلا إعادة تحجيم إضافية.

Why 32 bits for the bias

لماذا 32 بت للـ bias

SWSX is tiny (product of two small scales), so b / (SWSX) is a large integer. It would overflow 8 bits; it lives in the int32 accumulator anyway.

SWSX صغير جداً (حاصل ضرب scale صغيرين)، فيكون b / (SWSX) عدداً صحيحاً كبيراً لا يتسع في 8 بت؛ وهو يعيش في المجمّع int32 على أي حال.

Worked exampleمثال محلول

One output neuron, two inputs. SW = 0.01, ZW = 0, qW = [20, −10]. SX = 0.05, ZX = 10, qX = [30, 14]. Real bias b = 0.3. Output SY = 0.004, ZY = −128.

خلية خرج واحدة ومدخلان. SW = 0.01، ZW = 0، qW = [20, −10]. SX = 0.05، ZX = 10، qX = [30, 14]. الـ bias الحقيقي b = 0.3. الخرج SY = 0.004، ZY = −128.

Offline
  S_b    = S_W S_X = 0.01 x 0.05 = 0.0005
  q_b    = round(0.3 / 0.0005)   = 600          (int32)
  Z_X q_W  (row sum)  = 10 x (20 + (-10)) = 100
  q_bias = 600 - 100             = 500          (int32)
  M      = S_W S_X / S_Y = 0.0005 / 0.004 = 0.125

Runtime
  q_W . q_X = 20x30 + (-10)x14 = 600 - 140 = 460   (8-bit mults, 32-bit add)
  acc       = 460 + 500        = 960
  rescale   = 0.125 x 960      = 120
  q_Y       = 120 + (-128)     = -8

Check in floats
  W = [0.2, -0.1], X = 0.05 x ([30,14] - 10) = [1.0, 0.2]
  Y = 0.2 - 0.02 + 0.3 = 0.48
  S_Y (q_Y - Z_Y) = 0.004 x (-8 + 128) = 0.48   matches

For a fully connected layer, "ZXqW" means ZX times the sum of that output's row of qW, because every input xj carried the same offset ZX.

في الطبقة fully connected يعني "ZXqW" حاصل ضرب ZX في مجموع صف ذلك الخرج من qW، لأن كل مدخل xj يحمل نفس الإزاحة ZX.

Convolution: same recipe (slides 22–23)

الـ Convolution: الوصفة نفسها (الشريحتان 22–23)

For Y = Conv(W, X) + b the algebra is identical because convolution is linear. Only the precomputed offset changes:

لـ Y = Conv(W, X) + b الجبر مطابق لأن الـ convolution خطّي. يتغيّر فقط الحد المحسوب مسبقاً:

qbias = qb − Conv(qW, ZX)     qY = SWSXSY ( Conv(qW, qX) + qbias ) + ZY

Conv(qW, ZX) is the kernel convolved with a constant input equal to ZX, which for each output channel is ZX times the sum of that kernel's weights (ignoring padding effects at borders). It depends only on weights and ZX, so it is folded into the bias offline.

Conv(qW, ZX) هو الـ kernel مطبّقاً على مدخل ثابت قيمته ZX، أي لكل قناة خرج: ZX مضروباً في مجموع أوزان ذلك الـ kernel (مع تجاهل أثر الـ padding عند الحواف). يعتمد فقط على الأوزان وZX، لذا يُدمج في الـ bias مسبقاً.

SLIDE 23 DATAFLOW · BOTTOM TO TOP quantized inputs q_X (int8) quantized weights q_W (int8) Conv int int int32 + quantized bias q_bias int32 int32 × scale S_W S_X / S_Y int + zero point Z_Y quantized outputs q_Y (int) int32 between Conv and the rescale, N-bit int after it
Every edge carries an integer. The only non-integer constant is the rescale M = SWSX/SY, which hardware applies as a fixed-point multiply and shift. (The slide's output label reads qW; it is the output qY.)
كل حافة تحمل عدداً صحيحاً. الثابت الوحيد غير الصحيح هو معامل إعادة التحجيم M = SWSX/SY، ويطبّقه العتاد كضرب fixed-point وإزاحة. (تسمية المخرج في الشريحة مكتوبة qW، والمقصود هو المخرج qY.)

INT8 results and the storage/compute table

نتائج INT8 وجدول التخزين والحساب

NetworkالشبكةFloating-pointعشري (Floating-point)8-bit integerعدد صحيح 8-bitDropالهبوط
ResNet-5076.4%74.9%1.5
Inception-V378.4%75.4%3.0

نتائج الشريحة 24 (Jacob et al., CVPR 2018): ResNet-50 من 76.4% إلى 74.9% (هبوط 1.5 نقطة)، وInception-V3 من 78.4% إلى 75.4% (هبوط 3.0 نقاط).

The right half of slide 24 plots top-1 accuracy against latency for float and integer-only MobileNets on ImageNet, on Snapdragon 835 big cores. The 8-bit points sit up and to the left of the float points: at the same latency int8 is more accurate, and at the same accuracy it is faster. A small accuracy loss per model buys a better latency-accuracy trade-off overall.

النصف الأيمن من الشريحة 24 يرسم دقة top-1 مقابل الـ latency لنماذج MobileNet العشرية والصحيحة بالكامل على ImageNet، على أنوية Snapdragon 835 الكبيرة. نقاط 8-bit تقع أعلى ويسار نقاط float: عند نفس الـ latency تكون int8 أدق، وعند نفس الدقة تكون أسرع. خسارة دقة صغيرة لكل نموذج تشتري مقايضة أفضل إجمالاً.

Float baselineالخط الأساسي العشريK-means quantizationتكميم K-meansLinear quantizationالتكميم الخطي (Linear quantization)
StorageالتخزينFloating-point weightsأوزان عشريةInteger weights; floating-point codebookأوزان صحيحة، وcodebook عشريInteger weightsأوزان صحيحة
ComputationالحسابFloating-point arithmeticحساب عشريFloating-point arithmeticحساب عشريInteger arithmeticحساب بالأعداد الصحيحة

جدول الشريحة 25: الـ K-means يوفّر في التخزين فقط (فهارس صحيحة مع codebook عشري) ويبقى الحساب عشرياً، بينما الـ linear quantization يجعل التخزين والحساب كليهما صحيحين. علامة الاستفهام في الشريحة تمهّد للسؤال التالي: كيف نختار S وZ على أفضل وجه؟

Exam trapفخ في الامتحان

Sb is not chosen from the bias's own range. It is forced to SWSX, and Zb = 0. If you compute Sb = |b|max/127 you can no longer add qb into the accumulator.

Sb لا يُختار من مدى الـ bias نفسه. بل يُفرض Sb = SWSX وZb = 0. لو حسبت Sb = |b|max/127 فلن تستطيع جمع qb في المجمّع مباشرة.

Sign of the offset. qbias = qb minus ZXqW (FC) or minus Conv(qW, ZX) (conv). It comes from expanding qW(qX − ZX).

إشارة الإزاحة. qbias = qb − ZXqW (في FC) أو qb − Conv(qW, ZX) (في conv). تأتي من فكّ qW(qX − ZX).

Bit widths. Weights and activations: N-bit (8). Products accumulate in 32 bits. qb and qbias: 32 bits. Output after rescale and + ZY: N bits.

عرض البتات. الأوزان والـ activations: N بت (8). النواتج تُجمع في 32 بت. qb وqbias: 32 بت. الخرج بعد إعادة التحجيم وإضافة ZY: N بت.

Memory trickحيلة للحفظ

"Conv, add bias, scale, shift." The dataflow from bottom to top: int conv (int32 out), + qbias (int32), × M, + ZY.

"Conv، أضف bias، حجّم، أزِح." مسار البيانات من الأسفل للأعلى: conv صحيح (خرج int32)، + qbias (int32)، × M، + ZY.

Check yourself

اختبر نفسك

In the quantized FC layer, which choice of bias quantization parameters does the lecture make?

في طبقة FC المكمّمة، ما اختيار معاملات تكميم الـ bias في المحاضرة؟

C. Matching the accumulator's unit SWSX with no zero point lets qb be added directly to qWqX. B is a reasonable-looking range-based scale that breaks the direct addition. A and D mix in the output parameters.

C. مطابقة وحدة المُراكِم SWSX بلا zero point تسمح بجمع qb مباشرة مع qWqX. الخيار B scale يبدو معقولاً لأنه مبني على المدى، لكنه يكسر الجمع المباشر. والخياران A وD يخلطان معاملات المخرج.

ZX = 4 and one output row of qW is [3, −1, 2]. With qb = 50, what is qbias for that output?

ZX = 4 وصف خرج واحد من qW هو [3, −1, 2]. مع qb = 50، ما qbias لهذا الخرج؟

A. ZX × row sum = 4 × (3 − 1 + 2) = 16; qbias = 50 − 16 = 34. B adds instead of subtracting. C subtracts only ZX. D uses the sum of absolute values (4 × 6 = 24).

A. ZX × مجموع الصف = 4 × (3 − 1 + 2) = 16؛ وqbias = 50 − 16 = 34. الخيار B يجمع بدلاً من أن يطرح. والخيار C يطرح ZX فقط. والخيار D يستخدم مجموع القيم المطلقة (4 × 6 = 24).

How many bits does the lecture assign to qb and qbias?

كم بتاً تخصّص المحاضرة لـ qb وqbias؟

B. Slide 21 states it explicitly: both qb and qbias are 32 bits. Their scale SWSX is very small, so the integers are large, and they are added in the int32 accumulator.

B. الشريحة 21 تنص على ذلك صراحة: qb وqbias كلاهما 32 بت. الـ scale الخاص بهما SWSX صغير جداً، فتكون الأعداد الصحيحة كبيرة، وتُجمع في المُراكِم int32.

Slide 24 reports INT8 accuracy for Inception-V3. What is the top-1 drop from floating point?

تذكر الشريحة 24 دقة INT8 لـ Inception-V3. كم الهبوط في دقة top-1 مقارنة بالعشري؟

D. Inception-V3 goes 78.4% to 75.4%. A is ResNet-50's drop. Knowing both pairs is the safe move.

D. Inception-V3 ينتقل من 78.4% إلى 75.4%. الخيار A هو هبوط ResNet-50. معرفة الزوجين كليهما هي الخطوة الآمنة.

For Y = Conv(W, X) + b, what is precomputed into qbias?

في Y = Conv(W, X) + b، ما الذي يُحسب مسبقاً داخل qbias؟

C. The conv analogue of ZXqW is the kernel applied to a constant input ZX. B contains the runtime input and cannot be precomputed. D is a term that is 0 once ZW = 0.

C. المقابل في الـ conv للحد ZXqW هو الـ kernel مطبَّقاً على مدخل ثابت ZX. الخيار B يحتوي على المدخل وقت التشغيل ولا يمكن حسابه مسبقاً. والخيار D حد يصبح 0 بمجرد أن يكون ZW = 0.