COE 592Lecture 5.2المحاضرة 5.2Part 05الجزء 05
Integer-Only FC and Conv Layers, and INT8 Resultsطبقات FC وConv بالأعداد الصحيحة فقط، ونتائج INT8
Folds 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.، وتُختتم المحاضرة بالعودة إلى جدول التخزين مقابل الحساب.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 20-26
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Everything so far in this lecture was preparation. You have the affine map, the scale and zero point, the quantized matrix product and the decision to quantize weights symmetrically and bias at the accumulator’s scale. This part puts those pieces into one real layer, shows that the layer runs on integer hardware from input to output, and then asks the question that matters on an embedded board: what does it cost in accuracy, and what does it buy in speed?
For the COE 592 midterm this is the part most likely to become a long question. Expect to write the integer-only expression, label the bit width of each operation, draw the dataflow, explain q_bias and interpret the results slide. For your own embedded work, this is the exact recipe that TensorFlow Lite, now LiteRT, uses for its INT8 kernels.
كل ما سبق في هذه المحاضرة كان تمهيدًا. لديك الآن التحويل الأفيني (affine map)، وscale وzero point، وضرب المصفوفات المكمَّم، وقرار تكميم الأوزان تكميمًا متماثلًا وتكميم الانحياز (bias) على مقياس المُراكِم (accumulator). هذا الجزء يجمع هذه القطع في طبقة حقيقية واحدة، ويبيّن أن الطبقة تعمل على عتاد الأعداد الصحيحة من المدخل حتى المخرج، ثم يطرح السؤال الذي يهمّ فعلًا على لوحة مدمجة: كم تكلّف من الدقة، وكم تمنح من السرعة؟
في اختبار منتصف الفصل لمقرر COE 592 هذا هو الجزء الأرجح أن يأتي منه سؤال طويل. توقّع أن تكتب التعبير الصحيح بالأعداد الصحيحة فقط، وتكتب عرض البتات لكل عملية، وترسم مسار البيانات، وتشرح q_bias، وتفسّر شريحة النتائج. وفي عملك الخاص على الأنظمة المدمجة، هذه هي بالضبط الوصفة التي يستخدمها TensorFlow Lite (واسمه الآن LiteRT) في نوى INT8 الخاصة به.
By the end you canفي نهاية هذا الجزء ستستطيع
- Derive the integer-only fully-connected layer q_Y = (S_W S_X / S_Y)(q_W q_X + q_bias) + Z_Y from Y = WX + b.تشتق الطبقة المتصلة كليًا بالأعداد الصحيحة فقط q_Y = (S_W S_X / S_Y)(q_W q_X + q_bias) + Z_Y انطلاقًا من Y = WX + b.
- Explain what q_bias is, why it is 32 bits wide, and why it can be computed before deployment.تشرح ما هو q_bias، ولماذا عرضه 32 بت، ولماذا يمكن حسابه قبل النشر على الجهاز.
- Extend the same expression to a convolution layer and explain what Conv(q_W, Z_X) reduces to.توسّع التعبير نفسه ليشمل طبقة الالتفاف (convolution) وتشرح إلامَ يختزل Conv(q_W, Z_X).
- Draw the integer dataflow and label the bit width of every edge and the cost of every operation.ترسم مسار البيانات (dataflow) بالأعداد الصحيحة وتكتب عرض البتات على كل حافة وتكلفة كل عملية.
- Read the INT8 accuracy table and the MobileNet latency plot, and state what each one does and does not show.تقرأ جدول دقة INT8 ورسم زمن استجابة MobileNet، وتذكر ما يُظهره كل منهما وما لا يُظهره.
Take one output neuron with three inputs. Its float weights are 0.5, -0.3 and 0.2, its inputs are 2.0, 1.0 and 4.0, and its bias is 0.27. In float it computes 1.0 - 0.3 + 0.8 + 0.27 = 1.77. The goal is to get the same answer, to within one output step, without a single floating-point operation at run time.
خذ عصبونًا واحدًا في المخرج له ثلاثة مدخلات. أوزانه العائمة هي 0.5 و-0.3 و0.2، ومدخلاته 2.0 و1.0 و4.0، وانحيازه 0.27. بالفاصلة العائمة يحسب 1.0 - 0.3 + 0.8 + 0.27 = 1.77. الهدف أن نحصل على الجواب نفسه، بفارق لا يتجاوز خطوة مخرج واحدة، دون عملية فاصلة عائمة واحدة وقت التشغيل.
From Y = WX + b to integersمن Y = WX + b إلى الأعداد الصحيحة
Every tensor in the layer is stored with Linear quantization, so each one is replaced by its Affine mapping r = S(q - Z). Three choices from the previous parts make the algebra small. Weights use Symmetric quantization, so Z_W = 0 holds. The bias follows Bias quantization: Z_b = 0 and S_b = S_W S_X, which puts it on exactly the same scale as the products q_W q_X. Activations keep a general Zero point (Z) Z_X, because ReLU outputs are one-sided and suit Asymmetric quantization.
كل موتّر (tensor) في الطبقة مخزَّن بـLinear quantization، لذلك يُستبدل كل واحد منها بـAffine mapping r = S(q - Z). ثلاثة اختيارات من الأجزاء السابقة تجعل الجبر قصيرًا. الأوزان تستخدم Symmetric quantization، فيتحقق Z_W = 0. والانحياز يتبع Bias quantization: Z_b = 0 وS_b = S_W S_X، وهذا يضعه على المقياس نفسه تمامًا الذي عليه حواصل الضرب q_W q_X. أما التنشيطات (activations) فتحتفظ بـZero point (Z) عام Z_X، لأن مخرجات ReLU أحادية الجانب وتناسبها Asymmetric quantization.
This is the Linear quantized fully-connected layer. Read the notation Z_X q_W carefully: it is the weight matrix times a vector whose every entry is Z_X. For output row i it equals , the zero point times that row’s weight sum. Nothing in it depends on the input image.
هذه هي Linear quantized fully-connected layer. اقرأ الترميز Z_X q_W بعناية: إنه مصفوفة الأوزان مضروبة في متجه كل عناصره تساوي Z_X. في صف المخرج i يساوي ، أي نقطة الصفر مضروبة في مجموع أوزان ذلك الصف. لا شيء فيه يعتمد على صورة المدخل.
Why q_bias can be precomputed, and why it is 32 bitsلماذا يمكن حساب q_bias مسبقًا، ولماذا عرضه 32 بت
q_bias contains three things: q_b, the weights and Z_X. All three are fixed once the model is quantized and calibrated, so the subtraction is done once on the laptop, not once per inference on the device. That is precomputation in the strongest sense: the zero-point correction costs zero run-time operations, because it has been folded into a number the layer has to add anyway.
يحتوي q_bias على ثلاثة أشياء: q_b والأوزان وZ_X. الثلاثة كلها ثابتة بمجرد تكميم النموذج ومعايرته، لذلك يُجرى الطرح مرة واحدة على الحاسوب المحمول، لا مرة مع كل استدلال على الجهاز. هذا هو الحساب المسبق بأقوى معانيه: تصحيح نقطة الصفر لا يكلّف أي عملية وقت التشغيل، لأنه دُمج في عدد على الطبقة أن تجمعه في كل الأحوال.
It must be stored in 32 bits for two reasons. First, its scale S_W S_X is tiny, so its integer values are large: in the example below q_b = 540, already far outside [-128, 127]. Second, it is added straight into the 32-bit accumulator, so it must live in the same format. Jacob et al. add a reason that is easy to miss: each bias entry is added to many output activations, so any rounding error in it acts as a systematic offset rather than noise that averages out. Biases are a tiny fraction of the parameters, so the extra width is nearly free.
يجب أن يُخزَّن بـ32 بت لسببين. أولًا، مقياسه S_W S_X صغير جدًا، فتكون قيمه الصحيحة كبيرة: في المثال أدناه q_b = 540، وهذا بعيد جدًا خارج [-128, 127]. ثانيًا، يُجمع مباشرة داخل 32-bit accumulator، فلا بد أن يكون بالصيغة نفسها. ويضيف Jacob et al. سببًا يسهل أن يفوتك: كل عنصر من الانحياز يُضاف إلى عدد كبير من تنشيطات المخرج، فأي خطأ تقريب فيه يعمل كإزاحة منهجية ثابتة، لا كضجيج يتلاشى بالمتوسط. والانحيازات نسبة ضئيلة من المعاملات، فالعرض الإضافي يكاد يكون مجانيًا.
What each operation costsكم تكلّف كل عملية
Slide 21 boxes the three pieces of the expression and labels their cost.
تحيط الشريحة 21 القطع الثلاث من التعبير بمربعات وتكتب تكلفة كل منها.
- Inside the bracket, q_W q_X is a dot product: N-bit integer multiplies whose products are summed in 32 bits, followed by one 32-bit add of q_bias. This is almost all of the work. The label is “N-bit Int Mult., 32-bit Int Add.”داخل القوس، q_W q_X هو ضرب نقطي (dot product): ضربات أعداد صحيحة بعرض N بت تُجمع حواصلها بعرض 32 بت، يليها جمع واحد بعرض 32 بت لـq_bias. هذا هو تقريبًا كل العمل. التسمية هي «N-bit Int Mult., 32-bit Int Add.».
- The factor S_W S_X / S_Y is the only real number left. It is applied once per output as a Requantization (rescale), implemented by Fixed-point multiplication and a shift (M0 and bit shift). The label is “Rescale to N-bit Int.”المعامل S_W S_X / S_Y هو العدد الحقيقي الوحيد المتبقي. يُطبَّق مرة واحدة لكل مخرج على هيئة Requantization (rescale)، ويُنفَّذ بـFixed-point multiplication وإزاحة (M0 and bit shift). التسمية هي «Rescale to N-bit Int.».
- Finally Z_Y is added. The label is “N-bit Int Add.” In practice the result is then clamped to the output range, which is also where a fused ReLU or ReLU6 is applied.أخيرًا يُضاف Z_Y. التسمية هي «N-bit Int Add.». عمليًا تُقصّ (clamp) النتيجة بعد ذلك إلى مدى المخرج، وهذا أيضًا هو الموضع الذي تُطبَّق فيه ReLU أو ReLU6 المدمجة.
Why 32 bits for the sum? An int8 product is at most 128 × 128 = 16,384 = 2^14 in magnitude, so it needs 16 bits with sign. Summing K of them adds about log2 K bits. A signed 32-bit register holds up to about 2^31, so even a worst-case dot product of about 2^17 = 131,072 terms cannot overflow.
لماذا 32 بت للمجموع؟ حاصل ضرب قيمتين int8 لا تتجاوز قيمته المطلقة 128 × 128 = 16,384 = 2^14، فيحتاج 16 بت مع الإشارة. جمع K من هذه الحواصل يضيف نحو log2 K بت. والسجل الموقَّع بعرض 32 بت يتسع لقيم حتى نحو 2^31، لذلك حتى ضرب نقطي في أسوأ الحالات بنحو 2^17 = 131,072 حدًا لا يمكن أن يفيض.
Worked exampleمثال محلول
One neuron, start to finish, in integersعصبون واحد من البداية إلى النهاية بالأعداد الصحيحة
Quantization parametersمعاملات التكميم
Activations: S_X = 0.05, Z_X = -10. Weights (symmetric): S_W = 0.01, Z_W = 0. Output: S_Y = 0.1, Z_Y = -5. Bias: S_b = S_W S_X = 0.0005, Z_b = 0.التنشيطات: S_X = 0.05، Z_X = -10. الأوزان (متماثلة): S_W = 0.01، Z_W = 0. المخرج: S_Y = 0.1، Z_Y = -5. الانحياز: S_b = S_W S_X = 0.0005، Z_b = 0.Codesالرموز الصحيحة
q_W = [50, -30, 20] (so W = [0.5, -0.3, 0.2]). q_X = [30, 10, 70] (so X = 0.05 × [40, 20, 80] = [2.0, 1.0, 4.0]). q_b = 0.27 / 0.0005 = 540.q_W = [50, -30, 20] (إذن W = [0.5, -0.3, 0.2]). q_X = [30, 10, 70] (إذن X = 0.05 × [40, 20, 80] = [2.0, 1.0, 4.0]). q_b = 0.27 / 0.0005 = 540.Offline: q_biasمسبقًا (offline): حساب q_bias
Σ q_W = 50 - 30 + 20 = 40, so Z_X Σ q_W = -10 × 40 = -400 and q_bias = 540 - (-400) = 940. Stored as int32.Σ q_W = 50 - 30 + 20 = 40، إذن Z_X Σ q_W = -10 × 40 = -400 وq_bias = 540 - (-400) = 940. يُخزَّن بصيغة int32.Run time: int8 multiplies, int32 accumulateوقت التشغيل: ضرب int8 ومراكمة int32
q_W · q_X = 1500 - 300 + 1400 = 2600, then 2600 + 940 = 3540. Check: 0.0005 × 3540 = 1.77, exactly the float result.q_W · q_X = 1500 - 300 + 1400 = 2600، ثم 2600 + 940 = 3540. للتحقق: 0.0005 × 3540 = 1.77، وهي نتيجة الفاصلة العائمة بالضبط.Rescaleإعادة التحجيم
M = S_W S_X / S_Y = 0.005 = 2^(-7) × 0.64, so M0 = 0.64 and n = 7. 3540 × 0.64 = 2265.6, and shifting right by 7 bits gives 17.7, which rounds to 18.M = S_W S_X / S_Y = 0.005 = 2^(-7) × 0.64، إذن M0 = 0.64 وn = 7. 3540 × 0.64 = 2265.6، والإزاحة لليمين بـ7 بتات تعطي 17.7، التي تُقرَّب إلى 18.Add the output zero pointجمع نقطة الصفر للمخرج
18 + (-5) = 13, inside [-128, 127], so no clamping.18 + (-5) = 13، وهي داخل [-128, 127]، فلا حاجة إلى القصّ.Resultالنتيجة
q_Y = 13, which decodes to 0.1 × (13 + 5) = 1.8. The float answer was 1.77; the error 0.03 is below half an output step (0.05). Every run-time operation was an integer multiply, add or shift.q_Y = 13، وفكّ ترميزها يعطي 0.1 × (13 + 5) = 1.8. كان جواب الفاصلة العائمة 1.77؛ والخطأ 0.03 أقل من نصف خطوة مخرج (0.05). كل عملية وقت التشغيل كانت ضربًا أو جمعًا أو إزاحة لأعداد صحيحة.
Recall
Starting from Y = WX + b with Z_W = 0, Z_b = 0 and S_b = S_W S_X, write the integer-only expression for q_Y and define q_bias.انطلاقًا من Y = WX + b مع Z_W = 0 و Z_b = 0 و S_b = S_W S_X، اكتب تعبير q_Y بالأعداد الصحيحة فقط وعرّف q_bias.
Quick checkاختبار سريع
Why can q_bias = q_b - Z_X q_W be computed before deployment?لماذا يمكن حساب q_bias = q_b - Z_X q_W قبل النشر؟
Picture one 3 × 3 filter sliding over an image whose activations have zero point Z_X = -10. At every position the integer convolution computes . The -Z_X part of that sum is the same at every position, because the filter is the same and Z_X is the same. That repetition is the whole trick.
تخيّل مرشّحًا واحدًا بحجم 3 × 3 ينزلق فوق صورة تنشيطاتها لها نقطة صفر Z_X = -10. في كل موضع يحسب الالتفاف الصحيح . الجزء -Z_X من هذا المجموع هو نفسه في كل موضع، لأن المرشّح نفسه وZ_X نفسه. هذا التكرار هو الحيلة كلها.
The same expression with Conv in place of a productالتعبير نفسه مع Conv بدل حاصل الضرب
A convolution is linear in its input, exactly like a matrix product, so the fully-connected derivation goes through unchanged. Write Y = Conv(W, X) + b, substitute the affine maps with the same three choices, and use .
الالتفاف خطي في مدخله، تمامًا مثل ضرب المصفوفات، لذلك يصلح اشتقاق الطبقة المتصلة كليًا دون تغيير. اكتب Y = Conv(W, X) + b، وعوّض بالتحويلات الأفينية مع الاختيارات الثلاثة نفسها، واستخدم .
This is the Linear quantized convolution layer. The cost labels are identical to the fully-connected case: the convolution is N-bit multiplies with 32-bit adds, the rescale produces N-bit integers, and Z_Y is an N-bit add. Again both q_b and q_bias are32 bits.
هذه هي Linear quantized convolution layer. تسميات التكلفة مطابقة لحالة الطبقة المتصلة كليًا: الالتفاف ضربات بعرض N بت مع جمع بعرض 32 بت، وإعادة التحجيم تنتج أعدادًا صحيحة بعرض N بت، وZ_Y جمع بعرض N بت. ومرة أخرى كلٌّ من q_b وq_bias بعرض 32 بت.
What Conv(q_W, Z_X) actually isما هو Conv(q_W, Z_X) فعلًا
Conv(q_W, Z_X) means convolving the weights with an input tensor in which every entry equals Z_X. Every window of that tensor looks the same, so for output channel k the result is one number repeated at every position:
Conv(q_W, Z_X) يعني التفاف الأوزان مع موتّر مدخل كل عناصره تساوي Z_X. كل نافذة في هذا الموتّر متطابقة، لذلك تكون النتيجة لقناة المخرج k عددًا واحدًا يتكرر في كل موضع:
With the numbers in the figure, the filter’s weights sum to 5, so the correction is -10 × 5 = -50 and q_bias = q_b + 50 for that channel. One subtraction per output channel, done once, replaces a correction that would otherwise be paid at every pixel of every image.
بأرقام الشكل، مجموع أوزان المرشّح 5، فيكون التصحيح -10 × 5 = -50 وq_bias = q_b + 50 لتلك القناة. طرح واحد لكل قناة مخرج، يُجرى مرة واحدة، يحلّ محل تصحيح كان سيُدفع ثمنه عند كل بكسل من كل صورة.
The dataflow, edge by edgeمسار البيانات حافةً بحافة
Slide 23 draws the layer as a small circuit. Follow one output value from the bottom up: integer inputs and integer weights enter the convolution, which emits an int32 sum; q_bias (int32) is added; the result (int32) is multiplied by the scale factor S_W S_X / S_Y; Z_Y (int) is added; out come the quantized outputs.
ترسم الشريحة 23 الطبقة على هيئة دائرة صغيرة. تتبّع قيمة مخرج واحدة من الأسفل إلى الأعلى: المدخلات الصحيحة والأوزان الصحيحة تدخل الالتفاف، الذي يُخرج مجموعًا بصيغة int32؛ ثم يُضاف q_bias (int32)؛ ثم تُضرب النتيجة (int32) في معامل المقياس S_W S_X / S_Y؛ ثم يُضاف Z_Y (int)؛ فتخرج المخرجات المكمَّمة.
| Operationالعملية | Inputsالمدخلات | Outputالمخرج | Slide labelتسمية الشريحة |
|---|---|---|---|
| Multiply-accumulate (Conv or q_W q_X)ضرب ومراكمة (Conv أو q_W q_X) | int8 × int8int8 × int8 | int32int32 | N-bit Int Mult., 32-bit Int Add.ضرب أعداد صحيحة بعرض N بت، وجمع أعداد صحيحة بعرض 32 بت (N-bit Int Mult., 32-bit Int Add.) |
| Add q_biasجمع q_bias | int32 + int32int32 + int32 | int32int32 | Same box (32-bit Int Add.)المربع نفسه (جمع أعداد صحيحة بعرض 32 بت، 32-bit Int Add.) |
| Multiply by S_W S_X / S_Yالضرب في S_W S_X / S_Y | int32 × fixed-point M0, then shift by nint32 × M0 بالفاصلة الثابتة، ثم إزاحة بمقدار n | N-bit intعدد صحيح بعرض N بت | Rescale to N-bit Intإعادة تحجيم إلى أعداد صحيحة بعرض N بت (Rescale to N-bit Int) |
| Add Z_Y, then clampجمع Z_Y ثم القصّ (clamp) | int + intint + int | int8int8 | N-bit Int Addجمع أعداد صحيحة بعرض N بت (N-bit Int Add) |
The shape to remember is narrow, wide, narrow. Values are stored and moved in 8 bits, which is where the memory and bandwidth savings come from. They are summed in 32 bits so nothing overflows. One rescale per output brings them back to 8 bits for the next layer, which is exactly the input format that layer expects. That is why a whole network can be chained with no float in between: this is Integer-only inference.
الشكل الذي يجب تذكّره هو: ضيّق، عريض، ضيّق. القيم تُخزَّن وتُنقل بعرض 8 بت، ومن هنا يأتي توفير الذاكرة وعرض النطاق. وتُجمع بعرض 32 بت حتى لا يفيض شيء. وإعادة تحجيم واحدة لكل مخرج تعيدها إلى 8 بت للطبقة التالية، وهي بالضبط صيغة المدخل التي تتوقعها تلك الطبقة. لهذا يمكن ربط شبكة كاملة دون أي فاصلة عائمة بين الطبقات: هذا هو Integer-only inference.
Recall
What is Conv(q_W, Z_X), and why can it be folded into q_bias?ما هو Conv(q_W, Z_X)، ولماذا يمكن دمجه في q_bias؟
Recall
Draw the slide 23 dataflow from memory and write the bit width on each edge.ارسم مسار البيانات في الشريحة 23 من الذاكرة واكتب عرض البتات على كل حافة.
Quick checkاختبار سريع
For one output channel of a convolution, what does Conv(q_W, Z_X) reduce to?لقناة مخرج واحدة في الالتفاف، إلامَ يختزل Conv(q_W, Z_X)؟
Quick checkاختبار سريع
Which part of the integer-only layer runs at 32-bit width?أي جزء من الطبقة ذات الأعداد الصحيحة فقط يعمل بعرض 32 بت؟
Suppose a phone app must classify a camera frame in 33 ms to keep up with 30 frames per second. You can pick any MobileNet size you like. The question is not “which format is more accurate?” but “which format gives the best accuracy inside 33 ms?” Slide 24 answers both questions, and they have different answers.
افترض أن تطبيقًا على الهاتف يجب أن يصنّف إطار الكاميرا خلال 33 ms ليواكب 30 إطارًا في الثانية. يمكنك اختيار أي حجم تريده من MobileNet. السؤال ليس «أي صيغة أدق؟» بل «أي صيغة تعطي أفضل دقة ضمن 33 ms؟». الشريحة 24 تجيب عن السؤالين، وجواباهما مختلفان.
The accuracy table: the price on a fixed networkجدول الدقة: الثمن على شبكة ثابتة
The left half of slide 24 takes two large networks, keeps the architecture fixed and compares float with INT8 top-1 accuracy on ImageNet, from Jacob et al. (CVPR 2018).
يأخذ النصف الأيسر من الشريحة 24 شبكتين كبيرتين، ويثبّت البنية، ويقارن دقة top-1 على ImageNet بين float وINT8، نقلًا عن Jacob et al. (CVPR 2018).
- ResNet-50 drops from 76.4% to 74.9%: 1.5 percentage points, about 2% of its accuracy in relative terms.تنخفض ResNet-50 من 76.4% إلى 74.9%: أي 1.5 نقطة مئوية، وهذا نحو 2% من دقتها بالقيمة النسبية.
- Inception-V3 drops from 78.4% to 75.4%: 3.0 points, about 3.8% relative.تنخفض Inception-V3 من 78.4% إلى 75.4%: أي 3.0 نقاط، ونحو 3.8% بالقيمة النسبية.
So on a fixed network, INT8 is slightly less accurate. That is expected: every tensor now lives on a grid of step S, and the rounding errors add up through dozens of layers. Two details make these numbers better than naive rounding would give. The models were trained with simulated quantization, so the weights adapted to the grid. And the Inception-V3 row is the ReLU6 version; in the paper, plain ReLU loses more (78.3% to 74.2%), because a bounded activation range is easier to quantize finely.
إذن على شبكة ثابتة، INT8 أقل دقة بقليل. وهذا متوقع: كل موتّر يعيش الآن على شبكة خطوتها S، وأخطاء التقريب تتراكم عبر عشرات الطبقات. وتفصيلان يجعلان هذه الأرقام أفضل مما يعطيه التقريب الساذج. النماذج دُرّبت مع تكميم محاكى (simulated quantization)، فتكيّفت الأوزان مع الشبكة. وصف Inception-V3 هو نسخة ReLU6؛ ففي الورقة تخسر ReLU العادية أكثر (من 78.3% إلى 74.2%)، لأن مدى التنشيط المحدود أسهل في التكميم الدقيق.
The latency plot: what the price buysرسم زمن الاستجابة: ماذا يشتري هذا الثمن
The right half plots top-1 accuracy against latency for many MobileNets (different widths and input resolutions) on the big cores of a Snapdragon 835, the chip in the Google Pixel 2. Circles are float models; squares are 8-bit integer-only models. Latency is on a log axis with ticks from 5 ms to 120 ms; the slowest float models lie past the last tick.
يرسم النصف الأيمن دقة top-1 مقابل زمن الاستجابة لعدد كبير من شبكات MobileNet (بعروض ودقّات مدخل مختلفة) على الأنوية الكبيرة لمعالج Snapdragon 835، وهو الشريحة الموجودة في Google Pixel 2. الدوائر نماذج float؛ والمربعات نماذج أعداد صحيحة فقط بعرض 8 بت. زمن الاستجابة على محور لوغاريتمي بعلامات من 5 ms إلى 120 ms؛ وأبطأ نماذج float تقع بعد آخر علامة.
The squares sit up and to the left of the circles. Read it in either direction. Draw a vertical line at any latency budget: the best 8-bit model under it is more accurate than the best float model under it, because the integer version of a larger MobileNet fits where only a smaller float one would. Draw a horizontal line at any accuracy target: the 8-bit model reaches it sooner. This is the Latency-accuracy trade-off, and INT8 moves the whole frontier.
المربعات تقع أعلى ويسار الدوائر. اقرأ ذلك في أي من الاتجاهين. ارسم خطًا عموديًا عند أي ميزانية لزمن الاستجابة: أفضل نموذج 8 بت تحته أدق من أفضل نموذج float تحته، لأن النسخة الصحيحة من MobileNet أكبر تتسع حيث لا يتسع إلا نموذج float أصغر. وارسم خطًا أفقيًا عند أي هدف للدقة: نموذج 8 بت يبلغه أسرع. هذه هي Latency-accuracy trade-off، وINT8 تُزيح الحدّ كله.
The paper also stresses that the gain depends on the hardware. It reports a gap of about 10% top-1 at the 33 ms real-time budget on Snapdragon 835 LITTLE cores, and a smaller effect on the Snapdragon 821, whose floating-point path is better optimized. INT8 wins where integer arithmetic is much cheaper than float, which is exactly the situation on microcontrollers and mobile DSPs.
وتؤكد الورقة أيضًا أن المكسب يعتمد على العتاد. فهي تذكر فجوة نحو 10% في دقة top-1 عند ميزانية الزمن الحقيقي 33 ms على الأنوية الصغيرة (LITTLE) في Snapdragon 835، وأثرًا أصغر على Snapdragon 821 الذي مسار الفاصلة العائمة فيه أفضل تحسينًا. تفوز INT8 حيث تكون حسابات الأعداد الصحيحة أرخص بكثير من float، وهذا بالضبط الوضع في المتحكمات الدقيقة ومعالجات DSP في الهواتف.
Recall
Give the float and INT8 accuracies on slide 24 and the drop for each network. Then state in one sentence what the latency plot shows.اذكر دقة float ودقة INT8 في الشريحة 24 ومقدار الانخفاض لكل شبكة. ثم اذكر في جملة واحدة ما يُظهره رسم زمن الاستجابة.
Quick checkاختبار سريع
What does the MobileNet latency-versus-accuracy plot on slide 24 show?ماذا يُظهر رسم زمن الاستجابة مقابل الدقة لشبكات MobileNet في الشريحة 24؟
Go back to the 4 × 4 weight matrix used earlier in this lecture, with values such as 2.09 and -0.98. You have now seen it stored three ways, and slide 25 sets them side by side with one question for each: what is stored, and what is computed?
عُد إلى مصفوفة الأوزان 4 × 4 التي استُخدمت سابقًا في هذه المحاضرة، بقيم مثل 2.09 و-0.98. رأيتها الآن مخزَّنة بثلاث طرق، والشريحة 25 تضعها جنبًا إلى جنب مع سؤال واحد لكل منها: ما الذي يُخزَّن، وما الذي يُحسب؟
| Schemeالمخطط | Storageالتخزين | Computationالحساب |
|---|---|---|
| Original networkالشبكة الأصلية | Floating-point weightsأوزان بالفاصلة العائمة (float) | Floating-point arithmeticحساب بالفاصلة العائمة |
| K-means-based quantizationالتكميم المبني على K-means | Integer indices plus a floating-point codebookفهارس صحيحة مع دفتر رموز (codebook) بالفاصلة العائمة | Floating-point arithmeticحساب بالفاصلة العائمة |
| Linear quantizationالتكميم الخطي | Integer weights (plus a few scales and zero points)أوزان صحيحة (مع عدد قليل من قيم scale و zero point) | Integer arithmeticحساب بالأعداد الصحيحة |
K-means-based quantization stores small integer indices, but each index points into a float Codebook, and the arithmetic is still done in float after looking the value up. It saves memory but not compute. Linear quantization stores integers and, as this part has shown, also computes with integers: the only real-valued constant, S_W S_X / S_Y, is turned into a fixed-point multiply and a shift. That second property is what lets a model run on hardware with no floating-point unit at all.
K-means-based quantization يخزّن فهارس صحيحة صغيرة، لكن كل فهرس يشير إلى Codebook بالفاصلة العائمة، والحساب ما زال يُجرى بالفاصلة العائمة بعد البحث عن القيمة. إنه يوفّر الذاكرة لا الحساب. أما التكميم الخطي فيخزّن أعدادًا صحيحة، وكما بيّن هذا الجزء، يحسب أيضًا بأعداد صحيحة: الثابت الحقيقي الوحيد S_W S_X / S_Y يتحوّل إلى ضرب بالفاصلة الثابتة وإزاحة. هذه الخاصية الثانية هي ما يسمح للنموذج بالعمل على عتاد لا يحتوي وحدة فاصلة عائمة إطلاقًا.
The red arrow and question mark on slide 25 point at the next step: Binary/Ternary quantization, which pushes the Bit width (N) down to one or two bits so that multiplies become sign flips or skips. The same storage-versus-computation question will be the right lens for judging it.
السهم الأحمر وعلامة الاستفهام في الشريحة 25 يشيران إلى الخطوة التالية: Binary/Ternary quantization، الذي يخفض Bit width (N) إلى بت واحد أو بتين بحيث تصبح عمليات الضرب مجرد قلب إشارة أو تخطٍّ. وسؤال التخزين مقابل الحساب نفسه سيكون العدسة الصحيحة للحكم عليه.
Recall
Fill in slide 25: for the original network, K-means quantization and linear quantization, what is stored and what is computed?أكمل الشريحة 25: للشبكة الأصلية، ولتكميم K-means، وللتكميم الخطي، ما الذي يُخزَّن وما الذي يُحسب؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- With Z_W = 0, Z_b = 0 and S_b = S_W S_X, the FC layer becomes q_Y = (S_W S_X / S_Y)(q_W q_X + q_bias) + Z_Y.مع Z_W = 0 و Z_b = 0 و S_b = S_W S_X تصبح طبقة FC هي q_Y = (S_W S_X / S_Y)(q_W q_X + q_bias) + Z_Y.
- q_bias = q_b - Z_X q_W depends only on weights, the bias and Z_X, so it is computed once, offline, and stored as int32.q_bias = q_b - Z_X q_W يعتمد فقط على الأوزان والانحياز (bias) و Z_X، لذلك يُحسب مرة واحدة مسبقًا (offline) ويُخزَّن بصيغة int32.
- Cost labels: N-bit integer multiplies with 32-bit adds inside the bracket, one rescale to N-bit integers, one N-bit add of Z_Y.تسميات التكلفة: داخل القوس ضربات أعداد صحيحة بعرض N بت مع جمع بعرض 32 بت، ثم إعادة تحجيم (rescale) واحدة إلى أعداد صحيحة بعرض N بت، ثم جمع واحد بعرض N بت لـ Z_Y.
- Convolution: q_Y = (S_W S_X / S_Y)(Conv(q_W, q_X) + q_bias) + Z_Y with q_bias = q_b - Conv(q_W, Z_X), which is Z_X times each filter's weight sum.الالتفاف: q_Y = (S_W S_X / S_Y)(Conv(q_W, q_X) + q_bias) + Z_Y حيث q_bias = q_b - Conv(q_W, Z_X)، وهذا يساوي Z_X مضروبًا في مجموع أوزان كل مرشّح (filter).
- That per-channel constant is exact at the borders only if padding uses the code Z_X, which is real 0.هذا الثابت الخاص بكل قناة يبقى دقيقًا عند الحواف فقط إذا كانت الحشوة (padding) بالرمز Z_X، الذي يمثّل الصفر الحقيقي.
- Dataflow: int8 inputs and weights, int32 accumulator, int32 bias add, rescale by M = 2^(-n) M0, add Z_Y, saturate to int8.مسار البيانات: مدخلات وأوزان int8، ومُراكِم int32، وجمع الانحياز بصيغة int32، ثم إعادة تحجيم بـ M = 2^(-n) M0، ثم جمع Z_Y، ثم تشبّع (saturate) إلى int8.
- INT8 costs 1.5 points on ResNet-50 (76.4% to 74.9%) and 3.0 points on Inception-V3 (78.4% to 75.4%).تكلّف INT8 نقطة ونصفًا (1.5) على ResNet-50 (من 76.4% إلى 74.9%) وثلاث نقاط (3.0) على Inception-V3 (من 78.4% إلى 75.4%).
- On Snapdragon 835 big cores, INT8 MobileNets sit up and to the left of float ones: a better latency-accuracy frontier, not a more accurate network.على الأنوية الكبيرة في Snapdragon 835 تقع شبكات MobileNet بصيغة INT8 أعلى ويسار شبكات float: أي حدّ أفضل للمقايضة بين زمن الاستجابة والدقة، لا شبكة أدق.
- Linear quantization stores integers and computes with integers; K-means stores integers but computes in float.التكميم الخطي يخزّن أعدادًا صحيحة ويحسب بأعداد صحيحة؛ أما K-means فيخزّن أعدادًا صحيحة لكنه يحسب بالفاصلة العائمة (float).
Sourcesالمصادر
- Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only InferencePaperJacob et al., CVPR 2018 (arXiv)The source of this lecture: int32 bias with S_bias = S1 S2, the M = 2^(-n) M0 rescale, zero padding as the reason for an exact zero, Table 4.1 (ResNet), Table 4.3 (Inception v3) and the Snapdragon 835 MobileNet plots.مصدر هذه المحاضرة: انحياز int32 مع S_bias = S1 S2، وإعادة التحجيم M = 2^(-n) M0، والحشو بالأصفار بوصفه سبب اشتراط صفر دقيق، والجدول 4.1 (ResNet)، والجدول 4.3 (Inception v3)، ورسوم MobileNet على Snapdragon 835.(opens in a new tab)(يفتح في تبويب جديد)
- LiteRT 8-bit quantization specificationDocsGoogle AI EdgeSymmetric int8 weights with zero point 0, int32 bias with scale input_scale × weight_scale, and precomputable zero-point terms.أوزان int8 متماثلة بنقطة صفر 0، وانحياز int32 بمقياس input_scale × weight_scale، وحدود نقطة الصفر القابلة للحساب المسبق.(opens in a new tab)(يفتح في تبويب جديد)
- A White Paper on Neural Network QuantizationPaperNagel et al., Qualcomm AI Research (arXiv 2021)Section 2: 32-bit accumulators and bias, and how the weight-only zero-point terms are precomputed and added to the bias.القسم 2: المُراكِمات والانحياز بعرض 32 بت، وكيف تُحسب مسبقًا حدود نقطة الصفر التي تعتمد على الأوزان فقط وتُضاف إلى الانحياز.(opens in a new tab)(يفتح في تبويب جديد)