Lecture 05-3 · Quantization III
المحاضرة 05-3 · التكميم الثالث
Mixed precision and HAQ
الدقة المختلطة وHAQ
P2 SHOULDP2 مهمUniform quantization gives every layer the same bit width. Mixed precision lets each layer pick its own weight and activation bits. The catch is a design space of 64n configurations, so HAQ hands the choice to a reinforcement learning agent that is graded by the real hardware.
الـ uniform quantization يعطي كل طبقة نفس عرض البتات. أما الـ mixed precision فيسمح لكل طبقة أن تختار عدد بتات الأوزان والـ activations الخاص بها. المشكلة أن فضاء التصميم فيه 64n توزيعاً، لذلك يسلّم HAQ هذا الاختيار إلى وكيل reinforcement learning يُقيَّم بواسطة العتاد الحقيقي.
Contrast uniform and mixed-precision quantization. Compute the size of the design space (64 per layer, 64n in total). Draw the HAQ loop: actor-critic agent, action = bit widths, hardware accelerator in the loop with direct latency and energy feedback. Explain why the learned policy gives depthwise layers fewer bits on the edge and more bits on the cloud.
قارن بين الـ uniform quantization والـ mixed-precision quantization. احسب حجم فضاء التصميم (64 لكل طبقة، و64n إجمالاً). ارسم حلقة HAQ: وكيل actor-critic، والـ action هو عرض البتات، والمسرّع العتادي داخل الحلقة يعطي feedback مباشراً للزمن والطاقة. واشرح لماذا تعطي السياسة المتعلَّمة طبقات depthwise بتات أقل على الـ edge وبتات أكثر على الـ cloud.
Uniform versus mixed precision, slides 66 and 67
التكميم الموحّد مقابل الدقة المختلطة، الشريحتان 66 و67
Slide 66 shows uniform quantization: layer 1, layer 2 and layer 3 all store 8-bit weights and 8-bit activations (written "8 bits / 8 bits", weight first, activation second). Every layer pays the same cost whether it needs the precision or not.
تعرض الشريحة 66 الـ uniform quantization: الطبقات 1 و2 و3 كلها تخزّن أوزاناً بعرض 8-bit وactivations بعرض 8-bit (تُكتب "8 bits / 8 bits"، الوزن أولاً ثم الـ activation). كل طبقة تدفع نفس الكلفة سواء احتاجت هذه الدقة أم لا.
Slide 67 shows mixed-precision quantization: layer 1 uses 4-bit weights and 5-bit activations, layer 2 uses 6 / 7, layer 3 uses 5 / 4. Layers that tolerate coarse grids get fewer bits, sensitive layers keep more. The bit strings in the figure literally get shorter or longer per layer: 9 bits for layer 1, 13 for layer 2, 9 for layer 3, against 16 per layer in the uniform model.
تعرض الشريحة 67 الـ mixed-precision quantization: الطبقة 1 تستخدم أوزاناً بعرض 4-bit وactivations بعرض 5-bit، والطبقة 2 تستخدم 6 / 7، والطبقة 3 تستخدم 5 / 4. الطبقات التي تتحمل شبكة خشنة تأخذ بتات أقل، والطبقات الحساسة تحتفظ ببتات أكثر. سلاسل البتات في الشكل تقصر وتطول فعلاً من طبقة لأخرى: 9 بتات للطبقة 1، و13 للطبقة 2، و9 للطبقة 3، مقابل 16 لكل طبقة في النموذج المنتظم.
The challenge: a huge design space, slide 68
التحدي: فضاء تصميم ضخم، الشريحة 68
Each layer chooses a weight width and an activation width independently. With 8 candidate widths for each, one layer has 8 × 8 = 64 choices. Choices in different layers multiply, so a network with n layers has 64n configurations. Exhaustive search is impossible, and hand-tuning by experts does not scale either.
كل طبقة تختار عرض الأوزان وعرض الـ activations بشكل مستقل. مع 8 عروض مرشحة لكل منهما، يكون للطبقة الواحدة 8 × 8 = 64 خياراً. والخيارات في الطبقات المختلفة تُضرب في بعضها، لذلك الشبكة ذات n طبقة لها 64n توزيعاً. البحث الشامل مستحيل، والضبط اليدوي من الخبراء لا يتوسع أيضاً.
choices per layer = 8 × 8 = 64 design space = 64n = 26n
How many mixed-precision configurations exist for the 3-layer network on slide 68? And for 10 layers?
كم توزيعاً من الـ mixed precision يوجد للشبكة ذات الطبقات الثلاث في الشريحة 68؟ وكم لعشر طبقات؟
per layer: 8 weight widths x 8 activation widths = 64
n = 3: 64^3 = 64 x 64 x 64
= 4,096 x 64
= 262,144 (= 2^18)
n = 10: 64^10 = 2^60
= 1,152,921,504,606,846,976
~ 1.15 x 10^18
Multiply across layers, never add: 3 layers is 643, not 3 × 64 = 192.
اضرب عبر الطبقات ولا تجمع: 3 طبقات تعطي 643 وليس 3 × 64 = 192.
Weight storage under the slide 67 policy. Suppose layer 1 has 100k weights, layer 2 has 200k and layer 3 has 300k. Compare uniform 8-bit weights with the mixed 4 / 6 / 5 weight bits.
مساحة تخزين الأوزان تحت سياسة الشريحة 67. افترض أن الطبقة 1 فيها 100k وزن، والطبقة 2 فيها 200k، والطبقة 3 فيها 300k. قارن بين أوزان منتظمة بعرض 8-bit وأوزان مختلطة 4 / 6 / 5.
uniform: 600k weights x 8 bits = 4,800k bits = 600 KB
mixed: 100k x 4 = 400k bits
200k x 6 = 1,200k bits
300k x 5 = 1,500k bits
----------
3,100k bits / 8 = 387.5 KB
saving: 1 - 387.5 / 600 = 35.4 %
Model size is the easy part to compute. Latency and energy are not a simple function of bits, which is why HAQ measures them on hardware.
حجم النموذج هو الجزء السهل في الحساب. أما الزمن والطاقة فليسا دالة بسيطة في عدد البتات، ولهذا يقيسهما HAQ على العتاد.
HAQ: design automation with the hardware in the loop, slides 69 and 70
HAQ: أتمتة التصميم مع العتاد داخل الحلقة، الشريحتان 69 و70
HAQ (Hardware-Aware Automated Quantization, Wang et al., CVPR 2019) treats bit allocation as reinforcement learning, just as AMC did for pruning ratios. An actor-critic agent walks the network layer by layer. For each layer it receives a state describing that layer and emits an action: the weight and activation bit widths. The quantized model is then mapped onto a hardware accelerator, and the accelerator returns direct feedback, measured latency and energy, which shapes the reward that trains the agent.
HAQ (Hardware-Aware Automated Quantization, Wang et al., CVPR 2019) يعامل توزيع البتات كمسألة reinforcement learning، تماماً كما فعل AMC مع نسب الـ pruning. وكيل actor-critic يمر على الشبكة طبقة طبقة. لكل طبقة يستقبل state يصف تلك الطبقة ويُخرج action: عرض بتات الأوزان والـ activations. بعدها يُرسم النموذج المكمَّم على مسرّع عتادي (hardware mapping)، ويعيد المسرّع direct feedback أي الزمن والطاقة المقاسين فعلاً، وهذا يشكّل الـ reward الذي يدرّب الوكيل.
Agent · actor-critic
الوكيل · actor-critic
The actor proposes bit widths; the critic estimates how good that action is so the actor can be updated. Same family as AMC (DDPG in the paper, with a continuous output rounded to an integer bit width).
الـ actor يقترح عرض البتات، والـ critic يقدّر جودة هذا الـ action حتى يُحدَّث الـ actor. نفس عائلة AMC (DDPG في الورقة، بمخرج مستمر يُقرَّب إلى عدد صحيح من البتات).
Action · bit widths
الفعل · عرض البتات
For each layer: how many bits for its weights and how many for its activations (the "4 bits / 5 bits" pairs).
لكل طبقة: كم بتاً لأوزانها وكم بتاً لـ activations الخاصة بها (أزواج "4 bits / 5 bits").
State · layer description
الحالة · وصف الطبقة
Features of the current layer and the feedback so far. In the paper it includes the layer index, channel counts, kernel size, stride, feature map size, parameter count, whether it is depthwise, and the previous action.
خصائص الطبقة الحالية والـ feedback حتى الآن. في الورقة يتضمن رقم الطبقة، وعدد القنوات، وحجم الـ kernel، والـ stride، وحجم الـ feature map، وعدد المعاملات، وهل هي depthwise، والـ action السابق.
Reward · accuracy under a hardware budget
المكافأة · الدقة ضمن ميزانية العتاد
The agent is rewarded for keeping accuracy high while the measured latency, energy or model size stays within the budget. The budget is checked with hardware numbers, not estimates.
يُكافأ الوكيل على إبقاء الدقة عالية بينما يبقى الزمن أو الطاقة أو حجم النموذج المقاس ضمن الميزانية. والميزانية تُفحص بأرقام العتاد، لا بتقديرات.
Why measure instead of counting FLOPs or bits? Because the same bit reduction buys very different speedups depending on the hardware and the layer. Slide 70 shows BISMO, a bit-serial accelerator that processes one bit plane per cycle (cycle 0 is the MSB, cycle T the LSB), so its time grows with the bit widths. BitFusion fuses small bit-level units to match the operand widths. A layer that is memory bound gains from fewer activation bits; a layer that is compute bound gains from fewer bits in the multiply. A FLOP count sees none of this.
لماذا نقيس بدل أن نعدّ الـ FLOPs أو البتات؟ لأن نفس التخفيض في البتات يعطي تسريعاً مختلفاً جداً حسب العتاد والطبقة. تعرض الشريحة 70 مسرّع BISMO، وهو bit-serial يعالج مستوى بت واحداً في كل دورة (الدورة 0 هي الـ MSB والدورة T هي الـ LSB)، فيزداد زمنه مع عرض البتات. أما BitFusion فيدمج وحدات صغيرة على مستوى البت لتناسب عرض المعاملات. الطبقة المحدودة بالذاكرة (memory bound) تستفيد من تقليل بتات الـ activations، والطبقة المحدودة بالحساب (compute bound) تستفيد من تقليل بتات عملية الضرب. وعدّ الـ FLOPs لا يرى أياً من هذا.
Results on MobileNetV1, slides 71 and 72
النتائج على MobileNetV1، الشريحتان 71 و72
Slide 71 plots top-1 accuracy against latency for mixed-precision quantized MobileNetV1. HAQ's curve sits above uniform quantization at every latency, and the gap is largest at the tightest budget. The dashed horizontal line is the full-precision accuracy (about 71%).
ترسم الشريحة 71 دقة top-1 مقابل الزمن لنموذج MobileNetV1 المكمَّم بـ mixed precision. منحنى HAQ أعلى من الـ uniform quantization عند كل زمن، والفجوة أكبر ما تكون عند أضيق ميزانية. والخط الأفقي المتقطع هو دقة النموذج بالدقة الكاملة (حوالي 71%).
| Constraint (slide)القيد (الشريحة) | Tight budgetالميزانية الضيقة | HAQ top-1top-1 لـ HAQ | Uniform top-1top-1 للتكميم الموحّد |
|---|---|---|---|
| Latency (71)زمن الاستجابة (71) | ~45 ms | ~67.4% | ~62.4% |
| Latency (71)زمن الاستجابة (71) | ~58 ms | ~70.6% | ~67.0% |
| Model size (72)حجم النموذج (72) | ~1.1 MB | ~57% | ~37.5% |
| Latency (72)زمن الاستجابة (72) | ~13.5 ms | ~64.8% | ~62.4% |
| Energy (72)الطاقة (72) | ~8 mJ | ~67.4% | ~62.5% |
الأرقام مقروءة تقريبياً من الرسوم. عند ~45 ms يصل HAQ إلى ~67.4% مقابل ~62.4% للمنتظم. وعند قيد حجم النموذج ~1.1 MB تكون الفجوة حوالي 20 نقطة (~57% مقابل ~37.5%). وعند قيد الطاقة ~8 mJ يصل HAQ إلى ~67.4% مقابل ~62.5%.
Values are read off the plots, so treat them as approximate. The pattern is what matters: slide 72 says HAQ supports multiple objectives. The same agent can be constrained by model size, by latency, or by energy, and in all three it beats uniform quantization, most of all when the budget is tight. As the budget loosens, both curves approach full precision and the gap closes.
القيم مقروءة من الرسوم فاعتبرها تقريبية. المهم هو النمط: تقول الشريحة 72 إن HAQ يدعم أهدافاً متعددة. نفس الوكيل يمكن تقييده بحجم النموذج أو بالزمن أو بالطاقة، وفي الثلاثة يتفوق على الـ uniform quantization، وأكثر ما يكون عندما تكون الميزانية ضيقة. ومع اتساع الميزانية يقترب المنحنيان من الدقة الكاملة وتنغلق الفجوة.
The learned policy: edge versus cloud, slides 73 and 74
السياسة المتعلَّمة: edge مقابل cloud، الشريحتان 73 و74
Slide 73 plots the bits chosen per layer for MobileNet-V2 on BISMO under a latency constraint (slide 74's text discusses MobileNet-V1; the conclusion is the same). Bars above the axis are activation bits, bars below are weight bits; dark colours are pointwise (1×1) layers, light colours are depthwise (3×3) layers. The two hardware targets produce opposite policies for depthwise layers.
ترسم الشريحة 73 البتات المختارة لكل طبقة في MobileNet-V2 على BISMO تحت قيد زمني (ونص الشريحة 74 يتحدث عن MobileNet-V1؛ والاستنتاج واحد). الأعمدة فوق المحور هي بتات الـ activations، والأعمدة تحته هي بتات الأوزان؛ الألوان الداكنة طبقات pointwise (1×1) والفاتحة طبقات depthwise (3×3). والهدفان العتاديان ينتجان سياستين متعاكستين لطبقات depthwise.
| Edge acceleratorمسرّع edge | Cloud acceleratorمسرّع cloud | |
|---|---|---|
| Batch sizeحجم الدفعة (batch) | Smallصغير | Largeكبير |
| What dominates latencyما يهيمن على زمن الاستجابة | Depthwise layers (memory bound)طبقات depthwise (محدودة بالذاكرة) | Pointwise layers (compute bound)طبقات pointwise (محدودة بالحساب) |
| Depthwise bitsبتات depthwise | Fewer (especially activations)أقل (خصوصاً التفعيلات) | More (fewer only at the first few layers, more at the last few)أكثر (أقل فقط في الطبقات الأولى القليلة، وأكثر في الطبقات الأخيرة القليلة) |
| Pointwise bitsبتات pointwise | Moreأكثر | Fewerأقل |
يلخّص الجدول الفرق: على الـ edge الـ batch صغير، والزمن تهيمن عليه طبقات depthwise المحدودة بالذاكرة، فتأخذ بتات أقل (خصوصاً الـ activations) وتأخذ طبقات pointwise بتات أكثر. وعلى الـ cloud الـ batch كبير، والزمن تهيمن عليه طبقات pointwise المحدودة بالحساب، فتأخذ بتات أقل، وتأخذ طبقات depthwise بتات أكثر (أقل فقط في الطبقات الأولى، وأكثر في الطبقات الأخيرة).
The reasoning on slide 74: depthwise convolution is memory bound, pointwise convolution is compute bound. On edge devices with a small batch, MobileNet's latency is dominated by the depthwise layers. Feature maps take most of the memory traffic in a depthwise layer (its weights are tiny, only k × k per channel), so the agent cuts their activation bits. On the cloud with a large batch, the pointwise matrix multiplies dominate, so pointwise layers lose bits; depthwise layers get more bits to preserve accuracy at low memory overhead, since they hold only a small share of the total weights.
التبرير في الشريحة 74: الـ depthwise convolution محدود بالذاكرة، والـ pointwise convolution محدود بالحساب. على أجهزة الـ edge مع batch صغير، يهيمن على زمن MobileNet طبقات depthwise. والـ feature maps تأخذ معظم حركة الذاكرة في طبقة depthwise (أوزانها صغيرة جداً، k × k فقط لكل قناة)، لذلك يقلّل الوكيل بتات الـ activations. وعلى الـ cloud مع batch كبير، تهيمن عمليات ضرب المصفوفات في pointwise، فتخسر طبقات pointwise بتات؛ بينما تأخذ طبقات depthwise بتات أكثر للحفاظ على الدقة بكلفة ذاكرة منخفضة، لأنها تحمل حصة صغيرة فقط من مجموع الأوزان.
This is the strongest argument for hardware in the loop. No rule based on FLOPs or parameter counts would flip depthwise from "fewer bits" to "more bits" between two devices. Only measuring on each device reveals where the bottleneck sits, which links back to the roofline idea from lecture 03: latency = max(compute time, memory time).
هذه أقوى حجة لوضع العتاد داخل الحلقة. لا توجد قاعدة مبنية على الـ FLOPs أو عدد المعاملات تقلب طبقات depthwise من "بتات أقل" إلى "بتات أكثر" بين جهازين. القياس على كل جهاز فقط يكشف مكان عنق الزجاجة، وهذا يرتبط بفكرة المحاضرة 03: latency = max(compute time, memory time).
Lecture summary, slide 75
ملخص المحاضرة، الشريحة 75
Slide 75 closes the quantization block with four ideas: post-training quantization (PTQ) of an already-trained floating-point model, including per-tensor vs per-channel vs group quantization and how to set the dynamic range; quantization-aware training (QAT), which emulates inference-time quantization during training or fine-tuning using the straight-through estimator; binary and ternary quantization; and automatic mixed-precision quantization (this page). Its diagram recalls the affine map r = S(q − Z) and the QAT graph with weight and activation quantization nodes around Conv, BatchNorm and ReLU.
تختم الشريحة 75 كتلة الـ quantization بأربع أفكار: الـ post-training quantization (PTQ) لنموذج floating-point مدرَّب مسبقاً، ويشمل per-tensor مقابل per-channel مقابل group quantization وكيفية تحديد الـ dynamic range؛ والـ quantization-aware training (QAT) الذي يحاكي quantization وقت الاستدلال أثناء التدريب أو الـ fine-tuning باستخدام الـ straight-through estimator؛ والـ binary وternary quantization؛ والـ mixed-precision quantization الآلي (هذه الصفحة). ويستعيد رسمها التحويل r = S(q − Z) ورسم QAT بعُقد تكميم الأوزان والـ activations حول Conv وBatchNorm وReLU.
Edge and cloud give depthwise layers opposite treatment. Edge (small batch, memory bound): depthwise gets fewer bits, pointwise more. Cloud (large batch, compute bound): depthwise gets more bits, pointwise fewer. Swapping these is the most likely wrong answer.
الـ edge والـ cloud يعاملان طبقات depthwise بشكل متعاكس. على الـ edge (batch صغير، محدود بالذاكرة): depthwise تأخذ بتات أقل وpointwise أكثر. وعلى الـ cloud (batch كبير، محدود بالحساب): depthwise تأخذ بتات أكثر وpointwise أقل. تبديلهما هو الإجابة الخاطئة الأكثر احتمالاً.
Depthwise is memory bound, pointwise is compute bound. Not the other way round. Depthwise has few weights and little arithmetic per byte moved; pointwise is a dense matrix multiply.
depthwise محدودة بالذاكرة، وpointwise محدودة بالحساب. وليس العكس. depthwise فيها أوزان قليلة وحساب قليل لكل بايت يُنقل؛ أما pointwise فهي ضرب مصفوفات كثيف.
HAQ's feedback is measured on hardware, not FLOPs. The accelerator (BISMO, BitFusion) reports real latency and energy. Answering "the reward uses FLOPs or model size as a proxy for speed" misses the point of the method.
الـ feedback في HAQ يُقاس على العتاد، لا بالـ FLOPs. المسرّع (BISMO، BitFusion) يعطي الزمن والطاقة الحقيقيين. الإجابة بأن "الـ reward يستخدم الـ FLOPs أو حجم النموذج كبديل للسرعة" تفوّت جوهر الطريقة.
The design space multiplies. 64 choices per layer gives 64n, not 64n. For 3 layers that is 262,144, not 192.
فضاء التصميم يُضرب. 64 خياراً لكل طبقة يعطي 64n وليس 64n. لثلاث طبقات يكون 262,144 وليس 192.
"Starve the bottleneck." The agent cuts bits wherever the hardware is stuck. On the edge, memory is stuck, and depthwise lives on memory, so depthwise starves. On the cloud, compute is stuck, and pointwise lives on compute, so pointwise starves.
"جوّع عنق الزجاجة." الوكيل يقلّل البتات حيث يتعطل العتاد. على الـ edge الذاكرة هي العائق، وdepthwise تعيش على الذاكرة، فتجوع depthwise. وعلى الـ cloud الحساب هو العائق، وpointwise تعيش على الحساب، فتجوع pointwise.
Check yourself
اختبر نفسك
Each layer chooses one of 8 weight bit widths and one of 8 activation bit widths. How many mixed-precision configurations does a 3-layer network have?
كل طبقة تختار واحداً من 8 عروض لبتات الأوزان وواحداً من 8 عروض لبتات الـ activations. كم توزيعاً من الـ mixed precision لشبكة من 3 طبقات؟
B. 8 × 8 = 64 per layer, and choices multiply across layers: 643 = 262,144. A adds (3 × 64). C is 642, two layers. D is 83, which forgets the activation choice.
B. 8 × 8 = 64 لكل طبقة، والخيارات تتضاعف عبر الطبقات: 643 = 262,144. الخيار A يجمع (3 × 64). والخيار C هو 642، أي طبقتان. والخيار D هو 83، وينسى خيار التفعيلات.
What does the HAQ agent use as its signal for latency and energy?
ما الإشارة التي يستخدمها وكيل HAQ للزمن والطاقة؟
C. Slide 70: the quantized model is mapped onto the accelerator (BISMO edge or cloud, BitFusion) and direct latency and energy feedback returns as state and reward. FLOPs and bit counts are proxies that miss memory-bound versus compute-bound behaviour.
C. الشريحة 70: يُنقل النموذج المكمَّم إلى المسرّع (BISMO على edge أو cloud، وBitFusion)، وتعود تغذية راجعة مباشرة عن زمن الاستجابة والطاقة على شكل حالة ومكافأة. أما FLOPs وعدد البتات فهي مؤشرات بديلة لا ترى الفرق بين السلوك المحدود بالذاكرة والسلوك المحدود بالحساب.
On an edge accelerator with a small batch, how does HAQ's learned policy treat MobileNet's layers?
على مسرّع edge مع batch صغير، كيف تعامل سياسة HAQ المتعلَّمة طبقات MobileNet؟
A. Slides 73 and 74: on the edge, latency is dominated by memory-bound depthwise layers, so their (activation) bits are cut, while pointwise layers keep more bits. B is the cloud policy.
A. الشريحتان 73 و74: على edge تهيمن طبقات depthwise المحدودة بالذاكرة على زمن الاستجابة، فتُقلَّص بتاتها (بتات التفعيلات)، بينما تحتفظ طبقات pointwise ببتات أكثر. والخيار B هو سياسة cloud.
Why does the cloud policy give depthwise layers more bits?
لماذا تعطي سياسة الـ cloud طبقات depthwise بتات أكثر؟
D. Slide 74: depthwise convolution takes only a small proportion of the total weights, so raising its bit width costs little memory and preserves accuracy, while the compute-bound pointwise layers are where cutting bits buys speed. A reverses the bound; C is false, depthwise has only k × k weights per channel.
D. الشريحة 74: الالتفاف depthwise لا يأخذ إلا نسبة صغيرة من مجموع الأوزان، فرفع عرض بتاته يكلّف ذاكرة قليلة ويحافظ على الدقة، بينما طبقات pointwise المحدودة بالحساب هي المكان الذي يشتري فيه تقليص البتات السرعة. الخيار A يعكس نوع التقييد، والخيار C خاطئ، فطبقة depthwise لها k × k وزناً فقط لكل قناة.
On slides 71 and 72, where is HAQ's advantage over uniform quantization largest?
في الشريحتين 71 و72، أين تكون أفضلية HAQ على الـ uniform quantization أكبر ما يمكن؟
B. The gap is widest at the left of every plot, for example about 67.4% versus 62.4% at about 45 ms and about 57% versus 37.5% at about 1.1 MB. As the budget loosens both curves approach the full-precision line. HAQ wins under all three constraints, so C and D are wrong.
B. الفجوة أوسع ما تكون في يسار كل رسم، مثلاً نحو 67.4% مقابل 62.4% عند نحو 45 ms، ونحو 57% مقابل 37.5% عند نحو 1.1 MB. ومع اتساع الميزانية يقترب المنحنيان من خط الدقة الكاملة. HAQ يتفوق تحت القيود الثلاثة كلها، لذلك الخياران C وD خاطئان.