COE 592Lecture 5.2المحاضرة 5.2
Quantization (II): Linear Quantization and Integer-Only Inferenceالتكميم (Quantization) الجزء الثاني: التكميم الخطي والاستدلال بالأعداد الصحيحة فقط
Linear (affine) quantization r = S(q - Z): how to compute the scale and zero point from a tensor's range, why symmetric quantization sets Z = 0 for weights, and how a fully-connected or convolution layer runs entirely in integer arithmetic. It closes with INT8 accuracy and latency results from Jacob et al. (CVPR 2018).التكميم الخطي (affine) r = S(q - Z): كيف نحسب المقياس (scale) ونقطة الصفر (zero point) من مدى الموتر، ولماذا يضع التكميم المتماثل Z = 0 للأوزان، وكيف تعمل طبقة متصلة كليًا أو التفافية بالكامل بحساب الأعداد الصحيحة. وتُختتم بنتائج الدقة وزمن الاستجابة لـINT8 من Jacob et al. (CVPR 2018).
- Partsالأجزاء
- 5
- Conceptsالمفاهيم
- 19
- Slidesالشرائح
- 26
- Readingمدة القراءة
- 114 min114 دقيقة
AOverviewنظرة عامة
Lecture 05-1 shrank a model with K-means quantization, but every multiply still ran in floating point. This lecture finishes the job. It takes one 4×4 weight matrix, puts it on an evenly spaced integer grid with r = S(q - Z), and follows that single idea all the way to a convolution layer that runs from int8 input to int8 output without one floating-point instruction.
في المحاضرة 05-1 صغّرنا النموذج باستخدام K-means quantization، لكن كل عملية ضرب بقيت تُنفَّذ بالفاصلة العائمة (floating point). هذه المحاضرة تُكمل المهمة. تأخذ مصفوفة أوزان واحدة بحجم 4×4، وتضعها على شبكة أعداد صحيحة متساوية التباعد باستخدام r = S(q - Z)، ثم تتتبع هذه الفكرة الواحدة حتى نصل إلى طبقة convolution تعمل من مدخل int8 إلى مخرج int8 دون أي تعليمة فاصلة عائمة واحدة.
As a PhD student you need it for three things. The COE 592 midterm on Wed 28 Oct 2026 will ask you to compute S and Z by hand, derive the quantized matrix product, say which terms are precomputed, and draw the integer dataflow with its bit widths. Your research on embedded ML depends on it, because this is the exact INT8 scheme that LiteRT and most microcontroller and NPU toolchains deploy. And when you read a quantization paper, every later method (per-channel scales, calibration, QAT) is a way of choosing the S and Z defined here.
كطالب دكتوراه تحتاج هذا لثلاثة أمور. اختبار منتصف الفصل لمقرر COE 592 يوم Wed 28 Oct 2026 سيطلب منك حساب S وZ يدويًا، واشتقاق ضرب المصفوفات بعد التكميم، وتحديد أي الحدود تُحسب مسبقًا، ورسم مسار البيانات الصحيح (integer dataflow) مع عرض كل إشارة بالبتات. وبحثك في تعلم الآلة على الأنظمة المدمجة يعتمد عليه، لأن هذا هو بالضبط مخطط INT8 الذي تنشره LiteRT ومعظم أدوات المتحكمات الدقيقة وNPU. وعندما تقرأ ورقة بحثية في التكميم، فكل طريقة لاحقة (per-channel scales، calibration، QAT) هي في الحقيقة طريقة لاختيار S وZ المعرّفين هنا.
The path has five stops. You compare what K-means and linear quantization store and compute, and quantize the running matrix to 2-bit codes. You derive the scale and zero point from the two ends of the range. You substitute the affine map into Y = WX and sort the four resulting terms into runtime work and offline constants, with one fixed-point rescale at the end. You set Z_W = 0 for weights and give the bias the accumulator's scale. Finally you assemble the integer-only fully-connected and convolution layers and read the INT8 accuracy and latency results of Jacob et al.
للمسار خمس محطات. تقارن أولًا ما يخزنه ويحسبه كل من K-means والتكميم الخطي (linear quantization)، وتكمّم المصفوفة الجارية إلى رموز بطول 2 بت. ثم تشتق المقياس (scale) ونقطة الصفر (zero point) من طرفي المدى. بعدها تعوّض التحويل الأفيني في Y = WX وتصنّف الحدود الأربعة الناتجة إلى عمل وقت التشغيل (runtime) وثوابت تُحسب مسبقًا (offline)، مع إعادة تحجيم واحدة بالفاصلة الثابتة (fixed-point rescale) في النهاية. ثم تجعل Z_W = 0 للأوزان وتعطي الانحياز (bias) نفس مقياس المجمّع (accumulator). وأخيرًا تبني طبقتي fully-connected وconvolution بالأعداد الصحيحة فقط، وتقرأ نتائج الدقة وزمن الاستجابة لـ INT8 في ورقة Jacob et al.
Success looks likeعلامات النجاح
- Fill in the storage and computation table for FP32, K-means and linear quantization, and explain why only linear quantization computes with integers.املأ جدول التخزين والحساب لكل من FP32 وK-means والتكميم الخطي، واشرح لماذا يحسب التكميم الخطي وحده بالأعداد الصحيحة.
- Derive S = (r_max - r_min) / (q_max - q_min) and Z = round(q_min - r_min / S), then quantize, reconstruct and bound the error of any value by S/2.اشتق S = (r_max - r_min) / (q_max - q_min) وZ = round(q_min - r_min / S)، ثم كمّم أي قيمة وأعد بناءها، وحدّ خطأها بـ S/2.
- Derive q_Y = (S_W S_X / S_Y)(q_W - Z_W)(q_X - Z_X) + Z_Y, expand it, and label each term as runtime or precomputed, including the row sums, column sums and the factor K.اشتق q_Y = (S_W S_X / S_Y)(q_W - Z_W)(q_X - Z_X) + Z_Y، ثم افكّ الأقواس، وصنّف كل حد على أنه وقت تشغيل أو محسوب مسبقًا، بما في ذلك مجاميع الصفوف ومجاميع الأعمدة والعامل K.
- Split any rescale factor into M = 2^(-n) M0 with M0 in [0.5, 1) and justify the 32-bit accumulator with a bit count.جزّئ أي معامل إعادة تحجيم إلى M = 2^(-n) M0 بحيث تقع M0 في [0.5, 1)، وبرّر استخدام مجمّع بطول 32 بت بعدّ البتات.
- Compute a full range and a restricted range symmetric scale, and defend Z_W = 0 for weights but a free Z_X for ReLU activations.احسب مقياسًا متماثلًا بالمدى الكامل (full range) وآخر بالمدى المقيّد (restricted range)، ودافع عن Z_W = 0 للأوزان مع ترك Z_X حرة لمخرجات ReLU.
- Write the integer-only FC and convolution layers with q_bias, draw the dataflow with every bit width, and interpret the INT8 results on slide 24 correctly.اكتب طبقتي FC وconvolution بالأعداد الصحيحة فقط مع q_bias، وارسم مسار البيانات مع عرض كل إشارة بالبتات، وفسّر نتائج INT8 في الشريحة 24 تفسيرًا صحيحًا.
How to study this lectureكيف تذاكر هذه المحاضرة
- Choose your route. Read the full guide in one sitting for the whole derivation chain, or take one part at a time when you want depth. Parts build on each other, so keep the order.اختر طريقتك. اقرأ الدليل الكامل في جلسة واحدة لترى سلسلة الاشتقاق كاملة، أو خذ جزءًا واحدًا في كل مرة عندما تريد التعمق. الأجزاء يبني بعضها على بعض، فحافظ على ترتيبها.
- Work every worked example with a pencil before reading its result. The midterm rewards fluent arithmetic on S, Z, q and M0, and that only comes from doing it.حلّ كل مثال محلول بالقلم قبل قراءة نتيجته. اختبار منتصف الفصل يكافئ الطلاقة في حساب S وZ وq وM0، وهذه الطلاقة لا تأتي إلا بالتطبيق.
- Answer every recall prompt in your head or on paper before you reveal it, and take each quiz even when you feel sure. Read the explanation either way.أجب عن كل سؤال استرجاع في ذهنك أو على الورق قبل كشف الإجابة، وحلّ كل اختبار قصير حتى لو كنت واثقًا. واقرأ الشرح في الحالتين.
- Play with the simulators: change the bit width in part 1, drag the range in part 2, try your own M in part 3, and skew the data in part 4.جرّب المحاكيات: غيّر عدد البتات في الجزء 1، واسحب المدى في الجزء 2، وجرّب قيمة M خاصة بك في الجزء 3، واجعل البيانات منحرفة في الجزء 4.
- Mark a concept as understood only when you could derive it on a blank page. Unmarked concepts show you where to return.لا تضع على أي مفهوم علامة «فهمته» إلا عندما تستطيع اشتقاقه على صفحة فارغة. المفاهيم غير المعلَّمة تدلك على المواضع التي يجب أن تعود إليها.
- Use the reference sheet for formulas, tables and slide errata in the last days before the exam, then retry the recall prompts cold a few days later.استخدم صفحة المرجع للصيغ والجداول وأخطاء الشرائح في الأيام الأخيرة قبل الاختبار، ثم أعد أسئلة الاسترجاع من الذاكرة بعد بضعة أيام.
Sourcesالمصادر
- Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only InferencePaperJacob et al., CVPR 2018 (arXiv:1712.05877)The source of the lecture: r = S(q - Z) with an integer zero point, the quantized matrix product, M = 2^(-n) M0, the int32 bias and the INT8 results on slide 24مصدر المحاضرة: r = S(q - Z) مع نقطة صفر صحيحة، وضرب المصفوفات المكمّم، وM = 2^(-n) M0، والانحياز بطول int32، ونتائج INT8 في الشريحة 24(opens in a new tab)(يفتح في تبويب جديد)
- A White Paper on Neural Network QuantizationPaperNagel et al., Qualcomm AI Research (arXiv:2106.08295)Clear treatment of the forward quantize formula, rounding versus clipping error, symmetric versus asymmetric grids and why weights go symmetricشرح واضح لصيغة التكميم الأمامية، والفرق بين خطأ التقريب وخطأ القصّ، والشبكات المتماثلة مقابل غير المتماثلة، ولماذا تُكمَّم الأوزان تكميمًا متماثلًا(opens in a new tab)(يفتح في تبويب جديد)
BThe 5 partsالأجزاء الـ5
- 01Linear Quantization: From Codebooks to an Affine Gridالتكميم الخطي: من دفاتر الترميز إلى شبكة أفينية (affine)Places linear quantization next to K-means quantization by asking what is stored and what is computed in each. It then works the lecture's 4x4 FP32 weight matrix through 2-bit linear quantization from start to finish: integer codes, zero point -1, scale 1.07, reconstruction and quantization error.يضع التكميم الخطي بجانب تكميم K-means بالسؤال عما يُخزَّن وما يُحسَب في كل منهما. ثم يحل مصفوفة أوزان FP32 بحجم 4x4 من المحاضرة عبر تكميم خطي بـ2 بت من البداية إلى النهاية: الرموز الصحيحة، ونقطة الصفر -1، والمقياس 1.07، وإعادة البناء، وخطأ التكميم.3 concepts3 مفاهيمSlides 1-4الشرائح 1-418 min18 دقيقة
- 02Scale and Zero Point: Deriving S and Zالمقياس (Scale) ونقطة الصفر (Zero Point): اشتقاق S وZDefines the affine map r = S(q - Z), where the scale S is a float and the zero point Z is an integer, along with the signed N-bit integer range. Both parameters are then derived by matching the endpoints of the float range to the endpoints of the integer range, and computed for the running example.يعرّف الربط الأفيني r = S(q - Z)، حيث المقياس S عدد عائم ونقطة الصفر Z عدد صحيح، مع مدى العدد الصحيح ذي الإشارة من N بت. ثم يُشتق المعاملان بمطابقة طرفي مدى الأعداد العائمة مع طرفي مدى الأعداد الصحيحة، ويُحسبان للمثال الجاري.4 concepts4 مفاهيمSlides 5-10الشرائح 5-1024 min24 دقيقة
- 2.1r = S(q - Z): two integers, two floatsr = S(q - Z): عددان صحيحان وعددان عشريان
- 2.2Two number lines and the signed N-bit rangeخطّا أعداد ومدى الأعداد ذات الإشارة بعدد N من البتات
- 2.3Deriving the scale S from the range endpointsاشتقاق المقياس S من طرفي المدى
- 2.4Deriving the zero point Z, and why it must be an integerاشتقاق نقطة الصفر Z، ولماذا يجب أن تكون عددًا صحيحًا
- 03Linear Quantized Matrix Multiplicationضرب المصفوفات المكمّم خطيًاSubstitutes r = S(q - Z) into Y = WX and expands the result. The terms then sort into runtime N-bit integer MACs, offline precomputation, a single rescale by S_W S_X / S_Y (done as a fixed-point multiply plus a bit shift) and an N-bit addition of Z_Y. The section ends by asking whether Z_W can be 0.يعوّض r = S(q - Z) في Y = WX ويفك الناتج. ثم تنقسم الحدود إلى عمليات MAC بأعداد صحيحة من N بت وقت التشغيل، وحسابات مسبقة خارج التشغيل (offline)، وإعادة قياس واحدة بـS_W S_X / S_Y (تُنفَّذ بضرب بالفاصلة الثابتة مع إزاحة بت)، وجمع Z_Y بـN بت. ويختتم القسم بالسؤال عما إذا كان Z_W يمكن أن يساوي 0.4 concepts4 مفاهيمSlides 11-14الشرائح 11-1424 min24 دقيقة
- 3.1Deriving q_Y from Y = WXاشتقاق q_Y من Y = WX
- 3.2Which terms cost what: runtime, offline and the int32 accumulatorتكلفة كل حد: وقت التشغيل، وما يُحسب مسبقاً، ومراكم int32
- 3.3The float rescale without a float unit: M = 2^(-n) M0إعادة القياس العشرية دون وحدة فاصلة عائمة: M = 2^(-n) M0
- 3.4Why weights want Z_W = 0لماذا نريد Z_W = 0 للأوزان
- 04Symmetric Quantization and Bias Handlingالتكميم المتماثل ومعالجة الانحياز (Bias)Symmetric quantization fixes Z = 0 and uses the range [-|r|max, |r|max], which makes the S formula simpler and removes the runtime Z_W q_X term. Bias then enters the fully-connected layer and is quantized with Z_b = 0 and S_b = S_W S_X, so that it shares the accumulator's scale.التكميم المتماثل يثبّت Z = 0 ويستخدم المدى [-|r|max, |r|max]، مما يبسّط صيغة S ويزيل الحد Z_W q_X وقت التشغيل. ثم يدخل الانحياز (bias) في الطبقة المتصلة كليًا ويُكمَّم بـZ_b = 0 وS_b = S_W S_X، ليشارك المُجمِّع (accumulator) مقياسه نفسه.4 concepts4 مفاهيمSlides 15-19الشرائح 15-1924 min24 دقيقة
- 4.1Pinning the zero point at 0: symmetric quantizationتثبيت نقطة الصفر عند 0: التكميم المتماثل
- 4.2The full range scale and the value it clipsمقياس المدى الكامل والقيمة التي يقصّها
- 4.3What Z_W = 0 removes from the matrix multiplyما الذي يحذفه Z_W = 0 من ضرب المصفوفات
- 4.4Quantizing the bias into the int32 accumulatorتكميم الانحياز داخل المجمّع int32
- 05Integer-Only FC and Conv Layers, and INT8 Resultsطبقات FC وConv بالأعداد الصحيحة فقط، ونتائج INT8Folds the precomputed -Z_X q_W into a single 32-bit q_bias to give the final integer-only FC formula. The same pattern is then applied to convolution and drawn as an int8 to int32 to int8 dataflow. INT8 accuracy and latency evidence from Jacob et al. follows, and the lecture closes by returning to the storage/computation table.يدمج الحد المحسوب مسبقًا -Z_X q_W في q_bias واحد من 32 بت ليعطي صيغة FC النهائية بالأعداد الصحيحة فقط. ثم يُطبَّق النمط نفسه على الالتفاف ويُرسم كتدفق بيانات int8 إلى int32 إلى int8. تليه أدلة الدقة وزمن الاستجابة لـINT8 من Jacob et al.، وتُختتم المحاضرة بالعودة إلى جدول التخزين مقابل الحساب.4 concepts4 مفاهيمSlides 20-26الشرائح 20-2624 min24 دقيقة
- 5.1The integer-only fully-connected layer and q_biasالطبقة المتصلة كليًا بالأعداد الصحيحة فقط و q_bias
- 5.2Convolution in integers, and the dataflow with bit widthsالالتفاف بالأعداد الصحيحة، ومسار البيانات مع عرض البتات
- 5.3What INT8 costs and buys: accuracy table and latency frontierما تكلّفه INT8 وما تمنحه: جدول الدقة وحدّ زمن الاستجابة
- 5.4Storage versus computation across the schemes, and what comes nextالتخزين مقابل الحساب في المخططات المختلفة، وما الخطوة التالية
CGlossary and referenceالمصطلحات والمرجع
- GlossaryالمصطلحاتEvery term from the lecture, defined once, with links back to the slides that use it.كل مصطلح في المحاضرة معرّف مرة واحدة، مع روابط تعيدك إلى الشرائح التي تستخدمه.
- Reference sheetالورقة المرجعيةThe lecture on one printable page, for revision before a quiz or exam.المحاضرة كاملة في صفحة واحدة قابلة للطباعة، للمراجعة قبل الاختبار القصير أو الامتحان.