COE 592Lecture 5.2المحاضرة 5.2Referenceالمرجع
Reference sheetالورقة المرجعية
Quantization (II): Linear Quantization and Integer-Only Inference compressed onto one page: the definitions, formulas and numbers to have in your head before a quiz or exam.محاضرة «التكميم (Quantization) الجزء الثاني: التكميم الخطي والاستدلال بالأعداد الصحيحة فقط» مضغوطة في صفحة واحدة: التعريفات والمعادلات والأرقام التي يجب أن تكون حاضرة في ذهنك قبل الاختبار القصير أو الامتحان.
Storage versus computationالتخزين مقابل الحساب
Linear quantization stores integers and also computes with them; K-means only stores them. Bits for the 16 example weights in the last column. Part 01: Linear quantization, Part 05: Integer-only layers
التكميم الخطي يخزّن أعدادًا صحيحة ويحسب بها أيضًا، أما K-means فيخزّنها فقط. العمود الأخير يعطي عدد البتات لأوزان المثال الـ 16. الجزء 01: التكميم الخطي، الجزء 05: طبقات بالأعداد الصحيحة فقط
| Schemeالمخطط | Storageالتخزين | Computationالحساب | 4×4 exampleمثال 4×4 |
|---|---|---|---|
| FP32FP32 | Floating-point weightsأوزان بالفاصلة العائمة | Floating-point arithmeticحساب بالفاصلة العائمة | 512 bits512 bits |
| K-means, 4 clustersK-means، 4 عناقيد | Integer indices plus a float codebookفهارس صحيحة مع codebook بالفاصلة العائمة | Floating-point arithmeticحساب بالفاصلة العائمة | 32 + 128 = 160 bits32 + 128 = 160 bits |
| Linear, 2 bitsخطي، 2 بت | Integer weights plus one S and one Zأوزان صحيحة مع S واحد وZ واحد | Integer arithmeticحساب بالأعداد الصحيحة | 32 + 32 + 2 = 66 bits32 + 32 + 2 = 66 bits |
| Binary/TernaryBinary/Ternary (ثنائي/ثلاثي) | Later lectureمحاضرة لاحقة | Later lectureمحاضرة لاحقة | Not coveredغير مغطى |
The affine mapالتحويل الأفيني
Quantize forward with round and clamp; reconstruct backward with the affine map. Real 0 is code Z exactly. Part 01: Linear quantization, Part 02: Scale and zero point
التكميم في الاتجاه الأمامي يكون بالتقريب ثم القصّ (round and clamp)، وإعادة البناء في الاتجاه العكسي تكون بالتحويل الأفيني. القيمة الحقيقية 0 تساوي الرمز Z تمامًا. الجزء 01: التكميم الخطي، الجزء 02: المقياس ونقطة الصفر
| Symbolالرمز | Meaningالمعنى | Typeالنوع | How manyالعدد |
|---|---|---|---|
| rr | Real valueقيمة حقيقية | Floatعدد عائم (float) | Per valueلكل قيمة |
| Stored codeالرمز المخزَّن | N-bit signed integerعدد صحيح بإشارة بطول N بت | Per valueلكل قيمة | |
| ZZ | Zero point, the code for real 0نقطة الصفر، أي رمز القيمة الحقيقية 0 | Integer, same type as qعدد صحيح من نوع q نفسه | One per tensorواحد لكل tensor |
| SS | Scale, real step between codesالمقياس، أي الخطوة الحقيقية بين رمزين متتاليين | Floatعدد عائم (float) | One per tensorواحد لكل tensor |
Deriving S and Zاشتقاق S وZ
Pin both range ends, subtract to cancel Z, solve for S, then back-substitute and round. Part 02: Scale and zero point
ثبّت طرفي المدى، واطرح المعادلتين لتلغي Z، وحُلّ لإيجاد S، ثم عوّض عكسيًا وقرّب. الجزء 02: المقياس ونقطة الصفر
| NN | Codesعدد الرموز | q_minq_min | q_maxq_max | Gaps q_max - q_minالفجوات q_max - q_min |
|---|---|---|---|---|
| 22 | 44 | -2-2 | 11 | 33 |
| 33 | 88 | -4-4 | 33 | 77 |
| 44 | 1616 | -8-8 | 77 | 1515 |
| 88 | 256256 | -128-128 | 127127 | 255255 |
Running example, 2 bits (slides 3 to 10)المثال الجاري، 2 بت (الشرائح من 3 إلى 10)
- Rangeالمدى
- r_min = -1.08, r_max = 2.12, width 3.20r_min = -1.08، r_max = 2.12، والعرض 3.20
- Scaleالمقياس
- 3.20 / 3 = 1.0667, written 1.07 on the slides3.20 / 3 = 1.0667، ومكتوب 1.07 في الشرائح
- Zero pointنقطة الصفر
- round(-2 + 1.08 / 1.07) = round(-0.991) = -1, binary 11round(-2 + 1.08 / 1.07) = round(-0.991) = -1، وبالثنائي 11
- Codesالرموز
- [[1, -2, 0, -1], [-1, -1, -2, 1], [-2, 1, -1, -2], [1, -1, 0, 0]][[1, -2, 0, -1], [-1, -1, -2, 1], [-2, 1, -1, -2], [1, -1, 0, 0]]
- Levelsالمستويات
- 01 → 2.14, 00 → 1.07, 11 → 0, 10 → -1.0701 → 2.14, 00 → 1.07, 11 → 0, 10 → -1.07
- Error boundحد الخطأ
- |e| ≤ S/2 = 0.535. Worst cell 1.53 → 1.07, error 0.46|e| ≤ S/2 = 0.535. أسوأ خلية 1.53 → 1.07، وخطؤها 0.46
- Why top level is 2.14لماذا المستوى الأعلى 2.14
- S rounded up to 1.07 and Z rounded to an integer; unrounded S and Z give exactly 2.12قُرّب S إلى الأعلى ليصبح 1.07، وقُرّبت Z إلى عدد صحيح؛ ولو لم نقرّب S وZ لحصلنا على 2.12 تمامًا
Drillsتمارين
- ReLU6 [0, 6], 8-bit
- S = 6 / 255 = 0.02353, Z = -128, real 6 is code 127S = 6 / 255 = 0.02353، Z = -128، والقيمة الحقيقية 6 رمزها 127
- [-1, 3], 4-bit, r = 1.0
- S = 4 / 15 = 0.2667, Z = round(-4.25) = -4, q = 4 - 4 = 0, back to 1.067S = 4 / 15 = 0.2667، Z = round(-4.25) = -4، q = 4 - 4 = 0، وتعود إلى 1.067
- [-0.5, 1.5], 2-bit
- S = 0.6667, Z = round(-1.25) = -1S = 0.6667, Z = round(-1.25) = -1
Quantized matrix multiplicationضرب المصفوفات المكمّمة
Substitute S(q - Z) for Y, W and X, divide by S_Y, add Z_Y. Part 03: Quantized matrix multiplication
عوّض S(q - Z) مكان Y وW وX، ثم اقسم على S_Y، وأضف Z_Y. الجزء 03: ضرب المصفوفات المكمّمة
| Termالحد | Depends onيعتمد على | Whenمتى | Costالتكلفة |
|---|---|---|---|
| q_W q_Xq_W q_X | Weights and inputالأوزان والمدخل | Every inferenceفي كل استدلال | N-bit multiplies, int32 addsضرب بـ N بت، وجمع بـ int32 |
| -Z_W q_X-Z_W q_X | Inputالمدخل | Every inferenceفي كل استدلال | Column sums of q_X, an extra passمجاميع أعمدة q_X، أي مرور إضافي على البيانات |
| -Z_X q_W-Z_X q_W | Weights onlyالأوزان فقط | Offlineمسبقًا (offline) | Row sums of q_Wمجاميع صفوف q_W |
| +Z_W Z_X+Z_W Z_X | Constantsثوابت | Offlineمسبقًا (offline) | Really K Z_W Z_Xوهو في الحقيقة K Z_W Z_X |
| × S_W S_X / S_Y× S_W S_X / S_Y | Constantsثوابت | Every inferenceفي كل استدلال | Fixed-point M0 and a shiftM0 بالفاصلة الثابتة مع إزاحة |
| +Z_Y+Z_Y | Constantثابت | Every inferenceفي كل استدلال | N-bit add, then clampجمع بـ N بت، ثم قصّ (clamp) |
Why the accumulator is int32لماذا يكون المجمّع int32
- int8 × int8 productحاصل ضرب int8 × int8
- at most (-128)(-128) = 2^14, so 16 signed bitsلا يتجاوز (-128)(-128) = 2^14، أي 16 بت بإشارة
- Sum of K productsمجموع K من حواصل الضرب
- about 16 + log2(K) bitsنحو 16 + log2(K) بت
- K = 4,096
- 16 + 12 = 28 bits, fits int3216 + 12 = 28 بت، يتسع في int32
- int32 worst-case limitحد int32 في أسوأ حالة
- about 131,071 termsنحو 131,071 حدًّا
Fixed-point rescaleإعادة التحجيم بالفاصلة الثابتة
Double M until it lands in [0.5, 1); the count is n. Store M0 as the int32 nearest 2^31 M0 (Q31, at least 2^30), multiply, then rounding right shift by n. Part 03: Quantized matrix multiplication
ضاعف M حتى تقع في [0.5, 1)، وعدد المضاعفات هو n. خزّن M0 كأقرب عدد int32 إلى 2^31 M0 (صيغة Q31، ولا يقل عن 2^30)، ثم اضرب، ثم أزِح لليمين مع التقريب بمقدار n. الجزء 03: ضرب المصفوفات المكمّمة
| MM | nn | M0M0 | Q31 int32Q31 int32 |
|---|---|---|---|
| 0.30.3 | 11 | 0.60.6 | 1,288,490,1891,288,490,189 |
| 0.0450.045 | 44 | 0.720.72 | 1,546,188,2271,546,188,227 |
| 0.00720.0072 | 77 | 0.92160.9216 | 1,979,120,9301,979,120,930 |
| 0.0050.005 | 77 | 0.640.64 | Used in part 05مستخدم في الجزء 05 |
- Worked: accumulator 5,000, M = 0.0072: 5,000 × M0_q31 >> 31 = 4,608, then 4,608 >> 7 = 36.مثال محلول: المجمّع 5,000 وM = 0.0072: 5,000 × M0_q31 >> 31 = 4,608، ثم 4,608 >> 7 = 36.
- The shift alone is too coarse: 2^(-7) = 0.0078 versus 0.0072, about 8% off.الإزاحة وحدها خشنة جدًا: 2^(-7) = 0.0078 مقابل 0.0072، أي خطأ بنحو 8%.
Symmetric quantizationالتكميم المتماثل
Fix Z = 0 and quantize [-|r|max, |r|max], so r = Sq. Weights are bell-shaped around 0, so they lose almost nothing; one-sided ReLU outputs would waste half the codes. Part 04: Symmetric quantization and bias
ثبّت Z = 0 وكمّم المدى [-|r|max, |r|max]، فتصبح r = Sq. توزيع الأوزان جرسي حول 0، لذلك لا تخسر شيئًا تقريبًا، أما مخرجات ReLU أحادية الجانب فستُهدر نصف الرموز. الجزء 04: التكميم المتماثل والانحياز
| Asymmetricغير متماثل | Symmetric full rangeمتماثل بالمدى الكامل | Symmetric restrictedمتماثل بالمدى المقيّد | |
|---|---|---|---|
| Zero pointنقطة الصفر | round(q_min - r_min / S)round(q_min - r_min / S) | 00 | 00 |
| Rangeالمدى | [r_min, r_max][r_min, r_max] | [-|r|max, |r|max][-|r|max, |r|max] | [-|r|max, |r|max][-|r|max, |r|max] |
| Scaleالمقياس | (r_max - r_min) / (2^N - 1)(r_max - r_min) / (2^N - 1) | |r|max / 2^(N-1)|r|max / 2^(N-1) | |r|max / (2^(N-1) - 1)|r|max / (2^(N-1) - 1) |
| INT8 codesرموز INT8 | [-128, 127][-128, 127] | [-128, 127][-128, 127] | [-127, 127][-127, 127] |
| +|r|max+|r|max | Exact endطرف دقيق | Maps to 2^(N-1), clippedيقابل 2^(N-1)، فيُقصّ | Exactدقيق |
| Running matrix, INT8المصفوفة الجارية، INT8 | S = 0.01255S = 0.01255 | S = 0.01656, 2.12 → 2.103S = 0.01656، 2.12 → 2.103 | S = 0.01669S = 0.01669 |
| Running matrix, 2-bitالمصفوفة الجارية، 2 بت | S = 1.07, 2.09 → 2.14S = 1.07، 2.09 → 2.14 | S = 1.06, 2.09 → 1.06S = 1.06، 2.09 → 1.06 | Not on slidesغير موجود في الشرائح |
| Typical useالاستخدام المعتاد | ReLU activationsمخرجات ReLU | Weights (slides)الأوزان (في الشرائح) | Weights (LiteRT)الأوزان (LiteRT) |
- ReLU6 [0, 6] as INT8: symmetric S = 6/128 = 0.0469, asymmetric S = 6/255 = 0.0235 with Z = -128, half the step.ReLU6 بالمدى [0, 6] كـ INT8: المتماثل يعطي S = 6/128 = 0.0469، وغير المتماثل يعطي S = 6/255 = 0.0235 مع Z = -128، أي نصف الخطوة.
- Skewed running matrix: symmetric INT8 is about 32% coarser and leaves 63 of 256 codes unused.المصفوفة الجارية المنحرفة: INT8 المتماثل أخشن بنحو 32% ويترك 63 رمزًا من 256 دون استخدام.
Bias quantizationتكميم الانحياز
Give the bias the accumulator's scale and no offset. Part 04: Symmetric quantization and bias
أعطِ الانحياز (bias) مقياس المجمّع نفسه ودون إزاحة (offset). الجزء 04: التكميم المتماثل والانحياز
Bias factsحقائق عن الانحياز
- Choiceالاختيار
- Z_b = 0, S_b = S_W S_XZ_b = 0, S_b = S_W S_X
- Whyالسبب
- q_b is already in accumulator units, so it adds as an integer with no second rescaleq_b مكتوب أصلًا بوحدات المجمّع، فيُجمع كعدد صحيح دون إعادة تحجيم ثانية
- Exampleمثال
- S_W = 0.01, S_X = 0.05, b = 0.37 gives q_b = 740S_W = 0.01، S_X = 0.05، b = 0.37 تعطي q_b = 740
- Widthالعرض بالبتات
- int32, since int8 would cap the bias at 127 × 0.0005 = 0.0635int32، لأن int8 ستحدّ الانحياز عند 127 × 0.0005 = 0.0635
Integer-only FC and convolution layersطبقات FC وconvolution بالأعداد الصحيحة فقط
Fold the two constants into one int32 q_bias, computed offline. Part 05: Integer-only layers
ادمج الثابتين في q_bias واحد من نوع int32 يُحسب مسبقًا (offline). الجزء 05: طبقات بالأعداد الصحيحة فقط
| Operationالعملية | Inputsالمدخلات | Outputالمخرج | Slide labelالتسمية في الشريحة |
|---|---|---|---|
| Conv or q_W q_XConv أو q_W q_X | int8 × int8int8 × int8 | int32int32 | N-bit Int Mult., 32-bit Int Add.N-bit Int Mult., 32-bit Int Add. |
| Add q_biasإضافة q_bias | int32 + int32int32 + int32 | int32int32 | Same boxالصندوق نفسه |
| × S_W S_X / S_Y× S_W S_X / S_Y | int32 × M0, shift nint32 × M0، وإزاحة n | N-bit intعدد صحيح بـ N بت | Rescale to N-bit IntRescale to N-bit Int |
| Add Z_Y, clampإضافة Z_Y ثم القصّ | int + intint + int | int8int8 | N-bit Int AddN-bit Int Add |
One neuron in integers (part 05)عصبون واحد بالأعداد الصحيحة (الجزء 05)
- Parametersالمعاملات
- S_X = 0.05, Z_X = -10; S_W = 0.01, Z_W = 0; S_Y = 0.1, Z_Y = -5S_X = 0.05, Z_X = -10; S_W = 0.01, Z_W = 0; S_Y = 0.1, Z_Y = -5
- Codesالرموز
- q_W = [50, -30, 20], q_X = [30, 10, 70], q_b = 540q_W = [50, -30, 20], q_X = [30, 10, 70], q_b = 540
- Offlineمسبقًا (offline)
- q_bias = 540 - (-10)(40) = 940q_bias = 540 - (-10)(40) = 940
- Accumulateالتجميع
- 2600 + 940 = 3540, check 0.0005 × 3540 = 1.772600 + 940 = 3540، وللتحقق 0.0005 × 3540 = 1.77
- Rescaleإعادة التحجيم
- M = 0.005 = 2^(-7) × 0.64, 3540 × 0.64 / 128 = 17.7 → 18M = 0.005 = 2^(-7) × 0.64, 3540 × 0.64 / 128 = 17.7 → 18
- Outputالمخرج
- q_Y = 18 - 5 = 13, decodes to 1.8 against 1.77q_Y = 18 - 5 = 13، ويُفكّ إلى 1.8 مقابل 1.77
INT8 results (Jacob et al.)نتائج INT8 (Jacob et al.)
Fixed network: INT8 loses a little. Fixed latency: INT8 MobileNets on Snapdragon 835 big cores reach higher top-1, a better frontier, not a more accurate network. Part 05: Integer-only layers
عند تثبيت الشبكة: يخسر INT8 قليلًا. وعند تثبيت زمن الاستجابة: تصل شبكات MobileNet بـ INT8 على الأنوية الكبيرة لـ Snapdragon 835 إلى دقة top-1 أعلى، وهذا يعني منحنى مفاضلة (frontier) أفضل، لا شبكة أدق. الجزء 05: طبقات بالأعداد الصحيحة فقط
| Networkالشبكة | FloatFloat | INT8INT8 | Dropالانخفاض |
|---|---|---|---|
| ResNet-50ResNet-50 | 76.4%76.4% | 74.9%74.9% | -1.5 points-1.5 نقطة |
| Inception-V3 (ReLU6)Inception-V3 (ReLU6) | 78.4%78.4% | 75.4%75.4% | -3.0 points-3.0 نقاط |
- Plain-ReLU Inception-V3 loses more in the paper: 78.3% to 74.2%.Inception-V3 مع ReLU العادية تخسر أكثر في الورقة: من 78.3% إلى 74.2%.
- About 10% top-1 gap at the 33 ms budget on Snapdragon 835 LITTLE cores; smaller on the Snapdragon 821, whose float path is better optimized.فجوة بنحو 10% في top-1 عند ميزانية 33 ms على الأنوية الصغيرة (LITTLE) لـ Snapdragon 835، وأصغر على Snapdragon 821 لأن مسار الفاصلة العائمة فيه أفضل تحسينًا.
Slide errataأخطاء الشرائح
Answer with the corrected fact, and name the slide if a question depends on it. Part 02, Part 03, Part 04, Part 05
أجب بالمعلومة المصححة، واذكر رقم الشريحة إذا كان السؤال يعتمد عليها. الجزء 02، الجزء 03، الجزء 04، الجزء 05
What the slides get wrongما تخطئ فيه الشرائح
- Slides 6, 7, 9الشرائح 6 و7 و9
- Draw an 8-dot (3-bit) grid. The numbers use 2 bits, [-2, 1].ترسم شبكة من 8 نقاط (3 بت)، لكن الأرقام تستخدم 2 بت، أي [-2, 1].
- Slide 8الشريحة 8
- 3.20 / 3 = 1.0667, not exactly 1.07. Keep four figures until the end.3.20 / 3 = 1.0667، وليست 1.07 تمامًا. احتفظ بأربعة أرقام معنوية حتى النهاية.
- Slide 10الشريحة 10
- q_min - r_min / S is -0.991. Only its round() equals -1.قيمة q_min - r_min / S هي -0.991، وround() لها فقط تساوي -1.
- Slide 11الشريحة 11
- Scalar shorthand: read row sums of q_W, column sums of q_X, and K Z_W Z_X.اختصار بصيغة عددية مفردة: اقرأه على أنه مجاميع صفوف q_W، ومجاميع أعمدة q_X، وK Z_W Z_X.
- Slide 13الشريحة 13
- M in (0, 1) is empirical, not guaranteed. M ≥ 1 needs a left shift.وقوع M في (0, 1) ملاحظة تجريبية وليس مضمونًا. وإذا كانت M ≥ 1 فنحتاج إزاحة لليسار.
- Slide 16الشريحة 16
- Full range clips +|r|max by one step. Restricted range is not on the slides.المدى الكامل يقصّ +|r|max بمقدار خطوة واحدة. والمدى المقيّد غير موجود في الشرائح.
- Slides 22, 23الشريحتان 22 و23
- Titled Fully-Connected Layer but show a convolution layer.عنوانهما Fully-Connected Layer لكنهما تعرضان طبقة convolution.
- Slide 23الشريحة 23
- Output is labelled q_W; it is q_Y.المخرج مكتوب q_W، والصحيح q_Y.
- Slide 24الشريحة 24
- "Acurracy" should read "Accuracy".كلمة "Acurracy" صوابها "Accuracy".
- Slide 26الشريحة 26
- References are copied from the pruning lecture and omit Jacob et al. (CVPR 2018).المراجع منسوخة من محاضرة التقليم (pruning) ولا تذكر Jacob et al. (CVPR 2018).