COE 592Lecture 5.3المحاضرة 5.3Part 09الجزء 09
Mixed-precision quantization and HAQالتكميم بدقة مختلطة وHAQ
Different layers tolerate different bit widths, but choosing weight and activation bits per layer gives a design space of 64^n. HAQ automates the choice with an actor-critic reinforcement-learning agent that gets latency and energy feedback directly from the target hardware, beating uniform quantization and producing different policies for edge and cloud.تتحمل الطبقات المختلفة عروض بت مختلفة، لكن اختيار بتات الأوزان والتنشيطات لكل طبقة يعطي فضاء تصميم حجمه 64^n. يؤتمت HAQ هذا الاختيار بوكيل تعلم معزز من نوع actor-critic يتلقى تغذية راجعة عن زمن الاستجابة والطاقة مباشرة من العتاد المستهدف، فيتفوق على التكميم المنتظم وينتج سياسات مختلفة للحافة (edge) والسحابة.
- Conceptsالمفاهيم
- 6
- Slidesالشرائح
- 65-76
- Readingمدة القراءة
- 36 min36 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Every technique so far in this lecture asked how to quantize a layer well. This last part asks a different question: should every layer get the same number of bits at all? The answer is no, and once you allow layers to differ, the number of possible configurations explodes beyond any manual search. HAQ solves this with a reinforcement learning agent that tries bit widths on the real accelerator and learns from what it measures.
For the midterm on Wed 28 Oct 2026, expect four kinds of question: why mixed precision helps, computing the design space (64^n and its power of two), why HAQ uses hardware feedback instead of FLOPs, and explaining the edge versus cloud policy with memory bound versus compute bound layers. For your research and for real embedded deployments, the deeper lesson is that the best compression policy depends on the hardware it will run on.
كل تقنية رأيناها حتى الآن في هذه المحاضرة كانت تسأل كيف نكمّم طبقة واحدة جيدًا. هذا الجزء الأخير يطرح سؤالًا مختلفًا: هل يجب أن تحصل كل الطبقات على عدد البتات نفسه أصلًا؟ الجواب لا، وبمجرد أن نسمح للطبقات بأن تختلف، ينفجر عدد التكوينات الممكنة إلى حد يتجاوز أي بحث يدوي. تحل HAQ هذه المشكلة بوكيل تعلّم معزّز (reinforcement learning agent) يجرّب عروض البتات على المسرّع الحقيقي ويتعلم مما يقيسه.
في اختبار منتصف الفصل يوم Wed 28 Oct 2026، توقّع أربعة أنواع من الأسئلة: لماذا تفيد الدقة المختلطة، وحساب حجم فضاء التصميم (64^n وكتابته كقوة للعدد 2)، ولماذا تستخدم HAQ القياس المباشر من العتاد (hardware feedback) بدلًا من FLOPs، وشرح سياسة الحافة (edge) مقابل السحابة (cloud) باستخدام الطبقات المحدودة بالذاكرة (memory bound) مقابل المحدودة بالحساب (compute bound). أما لبحثك وللتطبيقات المدمجة الحقيقية، فالدرس الأعمق هو أن أفضل سياسة ضغط تعتمد على العتاد الذي ستعمل عليه.
By the end you canفي نهاية هذا الجزء ستستطيع
- Explain why one bit width for every layer wastes bits, and what mixed precision changes.تشرح لماذا يهدر استخدام عرض بتات واحد لكل الطبقات بعض البتات، وما الذي تغيّره الدقة المختلطة (mixed precision).
- Compute the size of the mixed-precision design space for any number of layers and bit choices, in powers of two.تحسب حجم فضاء التصميم للدقة المختلطة لأي عدد من الطبقات وخيارات البتات، مكتوبًا كقوة للعدد 2.
- Describe HAQ as a reinforcement learning loop: state, action, reward, and where the accelerator sits.تصف HAQ كحلقة تعلّم معزّز (reinforcement learning): الحالة (state) والفعل (action) والمكافأة (reward)، وأين يقع المسرّع (accelerator).
- Argue why measured latency and energy beat proxies such as FLOPs or model size.تبرهن لماذا يتفوق زمن الاستجابة (latency) والطاقة المقيسان فعليًا على المؤشرات التقريبية مثل FLOPs أو حجم النموذج.
- Explain the edge and cloud policies for MobileNet using memory bound depthwise and compute bound pointwise layers.تشرح سياستي الحافة (edge) والسحابة (cloud) لشبكة MobileNet باستخدام طبقات depthwise المحدودة بالذاكرة (memory bound) وطبقات pointwise المحدودة بالحساب (compute bound).
- Place every technique in this lecture (PTQ, QAT, binary, ternary, mixed precision) on one map.تضع كل تقنية في هذه المحاضرة (PTQ و QAT والثنائية binary والثلاثية ternary والدقة المختلطة) على خريطة واحدة.
Take a three-layer network quantized the usual way: layer 1 gets 8-bit weights and 8-bit activations, and so do layers 2 and 3. Every value in the model now costs one byte. That is uniform quantization, and it is what you get from a default integer pipeline such as the one by Jacob et al.
خذ شبكة من ثلاث طبقات مكمّمة بالطريقة المعتادة: الطبقة 1 تحصل على أوزان من 8 بت وتنشيطات من 8 بت، وكذلك الطبقتان 2 و3. كل قيمة في النموذج تكلّف الآن بايتًا واحدًا. هذا هو التكميم الموحّد (uniform quantization)، وهو ما تحصل عليه من خط معالجة افتراضي بالأعداد الصحيحة (integer pipeline) مثل الذي قدّمه Jacob et al.
Now suppose you measured each layer and found that layer 1 still works with 4-bit weights and 5-bit activations, layer 2 needs 6 / 7, and layer 3 is happy with 5 / 4. That assignment is Mixed-precision quantization. The weight and activation of the same layer get their own widths, and neighbouring layers can differ freely. Count the bits per weight-activation pair: uniform spends 16 + 16 + 16 = 48, the mixed policy spends 9 + 13 + 9 = 31, about 35% fewer bits for the same three layers.
افترض الآن أنك قست كل طبقة ووجدت أن الطبقة 1 ما زالت تعمل بأوزان من 4 بت وتنشيطات من 5 بت، وأن الطبقة 2 تحتاج 6 / 7، وأن الطبقة 3 تكفيها 5 / 4. هذا التوزيع هو mixed-precision quantization (التكميم بدقة مختلطة). وزن الطبقة الواحدة وتنشيطها يحصل كل منهما على عرضه الخاص، ويمكن للطبقات المتجاورة أن تختلف بحرية. احسب البتات لكل زوج وزن وتنشيط: التكميم الموحّد ينفق 16 + 16 + 16 = 48، والسياسة المختلطة تنفق 9 + 13 + 9 = 31، أي بتات أقل بحوالي 35% للطبقات الثلاث نفسها.
Why layers deserve different widthsلماذا تستحق الطبقات عروضًا مختلفة
Layers are not equal in two separate ways. First, they differ in sensitivity: some layers, often the first and last, lose a lot of accuracy when squeezed, while many middle layers barely notice. Second, they differ in cost: a layer with huge feature maps or huge weight tensors dominates memory traffic or arithmetic, so each bit removed there saves far more time and energy than a bit removed from a tiny layer. Uniform precision ignores both facts. It pays full price in cheap, robust layers and gives no extra protection to fragile ones.
الطبقات ليست متساوية من ناحيتين منفصلتين. أولًا، تختلف في الحساسية (sensitivity): بعض الطبقات، وغالبًا الأولى والأخيرة، تفقد قدرًا كبيرًا من الدقة عند ضغطها، بينما كثير من الطبقات الوسطى بالكاد تتأثر. ثانيًا، تختلف في التكلفة: الطبقة ذات خرائط السمات (feature maps) الضخمة أو موترات الأوزان الضخمة تهيمن على حركة الذاكرة أو على العمليات الحسابية، لذا فكل بت يُزال منها يوفّر وقتًا وطاقة أكثر بكثير من بت يُزال من طبقة صغيرة. الدقة الموحّدة تتجاهل الحقيقتين معًا. فهي تدفع السعر كاملًا في الطبقات الرخيصة المتينة، ولا تعطي أي حماية إضافية للطبقات الهشّة.
| Uniformالموحّدة | Mixed precisionالدقة المختلطة | |
|---|---|---|
| Bit width decisionقرار عرض البتات | One global choice, for example 8 / 8اختيار عام واحد، مثلًا 8 / 8 | One choice per layer, separately for weights and activationsاختيار لكل طبقة، منفصل للأوزان وللتنشيطات |
| Who choosesمن يختار | The engineer, onceالمهندس، مرة واحدة | A search procedure, or an expert with rules of thumbإجراء بحث، أو خبير يتبع قواعد تقريبية |
| Sensitive layersالطبقات الحساسة | Get the same bits as robust layersتحصل على البتات نفسها التي تحصل عليها الطبقات المتينة | Can keep more bitsيمكنها الاحتفاظ ببتات أكثر |
| Robust or expensive layersالطبقات المتينة أو المكلفة | Pay for bits they do not needتدفع ثمن بتات لا تحتاجها | Can drop to 4 bits or fewerيمكن خفضها إلى 4 بت أو أقل |
| Hardware neededالعتاد المطلوب | Any integer engineأي محرّك للأعداد الصحيحة (integer engine) | An engine that runs several bit widths efficiently (BISMO, BitFusion)محرّك يشغّل عدة عروض بتات بكفاءة (BISMO وBitFusion) |
Recall
Give two different reasons why one layer should get more bits than another.اذكر سببين مختلفين يجعلان طبقة ما تستحق بتات أكثر من طبقة أخرى.
Suppose each weight tensor may use any width from 1 to 8 bits, and so may each activation. One layer then has 8 × 8 = 64 possible pairs. Two layers have 64 × 64 = 4,096 combined policies, because every choice in layer 1 can pair with every choice in layer 2. Three layers have 64^3 = 262,144.
افترض أن كل موتر أوزان يمكنه استخدام أي عرض من 1 إلى 8 بت، وكذلك كل تنشيط. عندها يكون للطبقة الواحدة 8 × 8 = 64 زوجًا ممكنًا. وللطبقتين 64 × 64 = 4,096 سياسة مجمّعة، لأن كل خيار في الطبقة 1 يمكن أن يقترن بكل خيار في الطبقة 2. وللطبقات الثلاث 64^3 = 262,144.
The general rule is the multiplication principle. With B_w weight choices, B_a activation choices and n layers, the Mixed-precision design space has
القاعدة العامة هي مبدأ الضرب (multiplication principle). مع B_w خيارًا للأوزان وB_a خيارًا للتنشيطات وn طبقة، يكون حجم design space (فضاء التصميم)
Writing it as a power of two is the exam trick: each layer adds log2(64) = 6 bits of "choice", so the exponent is just 6n. To turn it into a decimal number, multiply the exponent by log10(2) ≈ 0.301.
كتابته كقوة للعدد 2 هي حيلة الامتحان: كل طبقة تضيف log2(64) = 6 بتات من «الاختيار»، فيصبح الأس ببساطة 6n. ولتحويله إلى عدد عشري، اضرب الأس في log10(2) ≈ 0.301.
Worked exampleمثال محلول
How big is the space for MobileNetV1?ما حجم الفضاء لشبكة MobileNetV1؟
Count the layersعُدّ الطبقات
Counting depthwise and pointwise convolutions as separate layers, MobileNetV1 has 28 layers (Howard et al.).إذا عددنا التفافات depthwise وpointwise كطبقات منفصلة، فإن MobileNetV1 فيها 28 طبقة (Howard et al.).Apply the formulaطبّق الصيغة
64^28 = 2^(6 × 28) = 2^168.64^28 = 2^(6 × 28) = 2^168.Convert to decimalحوّل إلى النظام العشري
168 × 0.301 ≈ 50.6, so 2^168 ≈ 10^50.6 ≈ 3.7 × 10^50.168 × 0.301 ≈ 50.6، إذن 2^168 ≈ 10^50.6 ≈ 3.7 × 10^50.Sanity check with a smaller networkتحقّق منطقي بشبكة أصغر
Even 10 layers give 2^60 ≈ 1.15 × 10^18. Evaluating one policy per second would take about 3.7 × 10^10 years, more than twice the age of the universe. And evaluating a policy really means quantizing and fine-tuning, which takes far longer than a second.حتى 10 طبقات تعطي 2^60 ≈ 1.15 × 10^18. لو قيّمنا سياسة واحدة كل ثانية لاستغرق الأمر حوالي 3.7 × 10^10 سنة، أي أكثر من ضعف عمر الكون. وتقييم السياسة فعليًا يعني تكميم النموذج وضبطه الدقيق (fine-tuning)، وهذا يستغرق أطول بكثير من ثانية.Resultالنتيجة
The space grows exponentially in depth. Brute force is impossible for any real network, and hand-written rules ("keep the first and last layers at 8 bits") explore only a sliver of it.الفضاء ينمو أسيًا مع العمق. البحث الشامل (brute force) مستحيل لأي شبكة حقيقية، والقواعد المكتوبة يدويًا («أبقِ الطبقتين الأولى والأخيرة على 8 بت») لا تستكشف إلا شريحة صغيرة منه.
Quick checkاختبار سريع
With 8 weight and 8 activation bit choices per layer, how many mixed-precision policies does a 10-layer network have?مع 8 خيارات لبتات الأوزان و8 خيارات لبتات التنشيطات في كل طبقة، كم سياسة دقة مختلطة توجد لشبكة من 10 طبقات؟
Picture the search as a game played one layer at a time. The player looks at layer k: how many input and output channels it has, its kernel size, stride, feature map size, number of parameters, whether it is depthwise, and what it chose for the previous step. It then announces a bit width. After it has chosen for every layer, the whole quantized network is run on the accelerator and scored. Over many games, the player learns which choices pay off. That is HAQ (Hardware-Aware Automated Quantization).
تخيّل البحث كلعبة تُلعب طبقة بعد طبقة. ينظر اللاعب إلى الطبقة k: كم قناة دخل وقناة خرج فيها، وحجم النواة (kernel)، والخطوة (stride)، وحجم خريطة السمات، وعدد المعاملات، وهل هي depthwise، وماذا اختار في الخطوة السابقة. ثم يعلن عرض بتات. بعد أن يختار لكل الطبقات، تُشغَّل الشبكة المكمّمة كاملة على المسرّع وتُقيَّم. ومع تكرار اللعب مرات كثيرة، يتعلم اللاعب أي الاختيارات تؤتي ثمارها. هذه هي HAQ.
The reinforcement learning formulationصياغة المسألة كتعلّم معزّز (reinforcement learning)
- Agent. DDPG, an off-policy actor-critic algorithm for continuous actions (Lillicrap et al.). The actor proposes the action; the critic estimates how good that action is in that state, and the actor is trained to raise the critic's estimate.الوكيل (Agent). خوارزمية DDPG، وهي خوارزمية actor-critic خارج السياسة (off-policy) للأفعال المتصلة (Lillicrap et al.). الممثّل (actor) يقترح الفعل، والناقد (critic) يقدّر مدى جودة هذا الفعل في هذه الحالة، ويُدرَّب الممثّل على رفع تقدير الناقد.
- State. A ten-feature vector per layer, normalised to [0, 1]: (k, c_in, c_out, s_kernel, s_stride, s_feat, n_params, i_dw, i_w/a, a_(k-1)). Each layer takes two steps, one for weights and one for activations, flagged by i_w/a.الحالة (State). متجه من عشر خصائص لكل طبقة، مُطبَّع إلى [0, 1]: (k, c_in, c_out, s_kernel, s_stride, s_feat, n_params, i_dw, i_w/a, a_(k-1)). كل طبقة تأخذ خطوتين، واحدة للأوزان وواحدة للتنشيطات، ويميّز بينهما i_w/a.
- Action. A continuous value a_k ∈ [0, 1], rounded to a bit width. A continuous action keeps the order of widths (2 bits is more aggressive than 4, which is more aggressive than 8), which a set of unordered discrete labels would lose.الفعل (Action). قيمة متصلة a_k ∈ [0, 1] تُقرَّب إلى عرض بتات. الفعل المتصل يحافظ على ترتيب العروض (2 بت أشد ضغطًا من 4، و4 أشد ضغطًا من 8)، وهو ترتيب تفقده مجموعة من التسميات المنفصلة غير المرتّبة.
- Reward. Accuracy only, after one epoch of fine-tuning: R = 0.1 × (acc_quant − acc_origin). Resource limits are not in the reward; they are enforced separately, as below.المكافأة (Reward). الدقة فقط، بعد حقبة (epoch) واحدة من الضبط الدقيق: R = 0.1 × (acc_quant − acc_origin). حدود الموارد ليست جزءًا من المكافأة، بل تُفرض بشكل منفصل كما يلي.
Each layer is then quantized linearly, with the clipping range chosen to minimise the KL divergence between the original and quantized weight distributions, the same idea you met for activation clipping earlier in this lecture.
بعد ذلك تُكمَّم كل طبقة خطيًا، ويُختار مدى القص (clipping range) بحيث يقلّل KL divergence بين توزيع الأوزان الأصلي وتوزيعها المكمّم، وهي الفكرة نفسها التي رأيتها في قص التنشيطات سابقًا في هذه المحاضرة.
Hardware in the loopالعتاد داخل الحلقة (hardware in the loop)
The decisive step is how the budget is checked. After the agent proposes a full policy, HAQ maps the quantized model onto a real mixed-precision accelerator, or a cycle-accurate simulator of it, and reads back the latency and energy. If the policy exceeds the budget, HAQ lowers the bit widths layer by layer until it fits, and only then fine-tunes and computes the reward. This is Direct hardware feedback. Three targets were used: the BISMO bit-serial FPGA overlay as an edge design (Zynq-7020) and a cloud design (VU9P), and the BitFusion spatial ASIC, whose processing elements fuse to match each layer's width.
الخطوة الحاسمة هي طريقة التحقق من الميزانية. بعد أن يقترح الوكيل سياسة كاملة، تضع HAQ النموذج المكمّم على مسرّع حقيقي يدعم الدقة المختلطة، أو على محاكٍ دقيق على مستوى الدورة (cycle-accurate simulator) له، وتقرأ زمن الاستجابة والطاقة. إذا تجاوزت السياسة الميزانية، تخفّض HAQ عروض البتات طبقة بعد طبقة حتى تتسع لها، وبعدها فقط تجري الضبط الدقيق وتحسب المكافأة. هذا هو hardware feedback (التغذية الراجعة من العتاد). استُخدمت ثلاثة أهداف: طبقة BISMO التسلسلية على مستوى البت فوق FPGA (bit-serial FPGA overlay) بتصميم للحافة (Zynq-7020) وتصميم للسحابة (VU9P)، ومسرّع BitFusion المكاني (spatial ASIC) الذي تندمج عناصر المعالجة فيه لتطابق عرض كل طبقة.
Why not simply count FLOPs or model size? Because they are proxies, and the proxy can be wrong in exactly the places that matter. Two layers with equal FLOPs can have very different latency: an ordinary convolution reuses each loaded value many times, while a depthwise convolution reuses little and spends its time waiting on memory. Cache locality, kernel launch counts and memory bandwidth all shift the real number. The HAQ paper also shows that a policy tuned for one accelerator is not the best on another, so no single proxy can serve every target.
لماذا لا نكتفي بعدّ FLOPs أو حجم النموذج؟ لأنها مؤشرات تقريبية (proxies)، والمؤشر التقريبي قد يخطئ في المواضع المهمة بالضبط. طبقتان لهما FLOPs متساوية قد يختلف زمن استجابتهما كثيرًا: الالتفاف العادي يعيد استخدام كل قيمة محمَّلة مرات كثيرة، بينما التفاف depthwise يعيد الاستخدام قليلًا ويقضي وقته في انتظار الذاكرة. محلية الذاكرة المخبئية (cache locality) وعدد مرات إطلاق النوى (kernel launches) وعرض نطاق الذاكرة (memory bandwidth) كلها تغيّر الرقم الحقيقي. وتُظهر ورقة HAQ أيضًا أن السياسة المضبوطة لمسرّع واحد ليست الأفضل على مسرّع آخر، فلا يمكن لمؤشر تقريبي واحد أن يخدم كل الأهداف.
Recall
What are HAQ's state, action and reward, and where do latency and energy enter?ما الحالة والفعل والمكافأة في HAQ، وأين يدخل زمن الاستجابة والطاقة؟
Quick checkاختبار سريع
Why does HAQ query the accelerator instead of minimising FLOPs?لماذا تستعلم HAQ من المسرّع بدلًا من تقليل عدد FLOPs؟
Fix a latency budget on the BISMO edge accelerator and ask which MobileNetV1 is most accurate within it. At about 45.5 ms, uniform 4-bit quantization (trained with PACT) reaches 62.44% top-1 on ImageNet. HAQ's mixed policy, at the same 45.5 ms, reaches 67.40%. Same speed, almost five points more accuracy.
ثبّت ميزانية زمن استجابة على مسرّع BISMO للحافة، واسأل: أي نسخة من MobileNetV1 هي الأدق ضمنها؟ عند حوالي 45.5 ms، يصل التكميم الموحّد بـ 4 بت (المدرَّب بطريقة PACT) إلى دقة top-1 قدرها 62.44% على ImageNet. أما السياسة المختلطة من HAQ، عند 45.5 ms نفسها، فتصل إلى 67.40%. السرعة نفسها، وحوالي خمس نقاط دقة إضافية.
| Latency budgetميزانية زمن الاستجابة | Uniform (PACT)الموحّد (PACT) | HAQHAQ | Gain (points)المكسب (نقاط) |
|---|---|---|---|
| ≈45.5 ms≈45.5 ms | 62.44%62.44% | 67.40%67.40% | +4.96+4.96 |
| ≈57.7 ms≈57.7 ms | 67.00%67.00% | 70.58%70.58% | +3.58+3.58 |
| ≈70 ms≈70 ms | 70.46%70.46% | 71.20%71.20% | +0.74+0.74 |
| 96.20 ms96.20 ms | 70.82% (8 / 8)70.82% (8 / 8) | referenceالمرجع | dashed lineالخط المتقطع |
Three patterns in slide 71 are worth stating in an exam. First, HAQ is above uniform at every budget. Second, the gap is largest when the budget is tightest, because that is where choosing which layers to squeeze matters most. Third, at about 70 ms HAQ (71.20%) slightly beats even the full 8-bit model (70.82%, the grey dashed line), which needs 96.20 ms. That is roughly a 1.4× speed-up with no accuracy loss. Across settings, the paper reports 1.4× to 1.95× lower latency and 1.9× lower energy than fixed 8-bit quantization with negligible accuracy loss.
ثلاثة أنماط في الشريحة 71 تستحق الذكر في الامتحان. أولًا، HAQ أعلى من التكميم الموحّد عند كل ميزانية. ثانيًا، الفجوة تكون أكبر عندما تكون الميزانية أضيق، لأن هناك بالضبط يصبح اختيار الطبقات التي نضغطها أهم ما يكون. ثالثًا، عند حوالي 70 ms تتفوق HAQ (71.20%) قليلًا حتى على النموذج الكامل بـ 8 بت (70.82%، الخط الرمادي المتقطع)، الذي يحتاج 96.20 ms. أي تسريع بحوالي 1.4× دون خسارة في الدقة. وعبر الإعدادات المختلفة، تذكر الورقة زمن استجابة أقل بمقدار 1.4× إلى 1.95× وطاقة أقل بمقدار 1.9× مقارنة بالتكميم الثابت بـ 8 بت، مع خسارة دقة لا تُذكر.
One method, three budgetsطريقة واحدة، وثلاث ميزانيات
Because the budget is just whatever the hardware or the size counter reports, the same agent can optimise different objectives. Slide 72 shows MobileNetV1 under a model size budget, a latency budget and an energy budget. The shape repeats: HAQ dominates, with the biggest margin at the tightest budget. Under the size budget around 1.1 MB the gap is about 20 points (about 57% versus 37%); under the energy budget around 8 mJ it is about 5 points (about 67.5% versus 62.5%).
لأن الميزانية ببساطة هي أي رقم يعطيه العتاد أو عدّاد الحجم، يستطيع الوكيل نفسه أن يحسّن أهدافًا مختلفة. تعرض الشريحة 72 شبكة MobileNetV1 تحت ميزانية لحجم النموذج، وميزانية لزمن الاستجابة، وميزانية للطاقة. والشكل يتكرر: HAQ تتفوق، وأكبر هامش عند أضيق ميزانية. تحت ميزانية حجم حول 1.1 MB تكون الفجوة حوالي 20 نقطة (حوالي 57% مقابل 37%)، وتحت ميزانية طاقة حول 8 mJ تكون حوالي 5 نقاط (حوالي 67.5% مقابل 62.5%).
Recall
Why is HAQ's advantage over uniform quantization largest at the tightest budget?لماذا تكون أفضلية HAQ على التكميم الموحّد أكبر ما تكون عند أضيق ميزانية؟
Take one block of MobileNetV1 on a 14 × 14 feature map with 512 channels. It has a 3 × 3 depthwise convolution followed by a 1 × 1 pointwise convolution from 512 to 512 channels. Count what each one does and what it moves.
خذ كتلة واحدة من MobileNetV1 على خريطة سمات 14 × 14 بعدد 512 قناة. فيها التفاف depthwise بحجم 3 × 3 يليه التفاف pointwise بحجم 1 × 1 من 512 إلى 512 قناة. احسب ما يفعله كل منهما وما ينقله.
Worked exampleمثال محلول
Operational intensity of a depthwise and a pointwise layerالكثافة التشغيلية (operational intensity) لطبقة depthwise وطبقة pointwise
Arithmeticالعمليات الحسابية
Depthwise: 14 × 14 × 512 × 9 = 903,168 MACs. Pointwise: 14 × 14 × 512 × 512 = 51,380,224 MACs, about 57× more.Depthwise: 14 × 14 × 512 × 9 = 903,168 عملية MAC. Pointwise: 14 × 14 × 512 × 512 = 51,380,224 عملية MAC، أي أكثر بحوالي 57×.Memory at 8 bitsالذاكرة عند 8 بت
Both read a 100,352-value input and write a 100,352-value output. Depthwise has only 512 × 9 = 4,608 weights; pointwise has 512 × 512 = 262,144. Bytes moved: 205,312 versus 462,848.كلاهما يقرأ دخلًا من 100,352 قيمة ويكتب خرجًا من 100,352 قيمة. طبقة depthwise فيها 512 × 9 = 4,608 وزنًا فقط، وطبقة pointwise فيها 512 × 512 = 262,144. البايتات المنقولة: 205,312 مقابل 462,848.Operations per byteالعمليات لكل بايت
Counting a MAC as two operations, depthwise does 1,806,336 / 205,312 ≈ 8.8 ops/byte and pointwise does 102,760,448 / 462,848 ≈ 222 ops/byte, about 25× higher.إذا عددنا كل MAC كعمليتين، فإن depthwise تنفّذ 1,806,336 / 205,312 ≈ 8.8 عملية لكل بايت (ops/byte)، وpointwise تنفّذ 102,760,448 / 462,848 ≈ 222 عملية لكل بايت، أي أعلى بحوالي 25×.Resultالنتيجة
Depthwise is memory bound: it does little work per byte, so it waits on memory, and 97.8% of its bytes are feature maps. Pointwise is compute bound: it holds 98.3% of the block's MACs and almost all of its weights.طبقة depthwise memory bound (محدودة بالذاكرة): تنجز عملًا قليلًا لكل بايت، فتنتظر الذاكرة، و97.8% من بايتاتها خرائط سمات. أما pointwise فهي محدودة بالحساب (compute bound): تحمل 98.3% من عمليات MAC في الكتلة وكل أوزانها تقريبًا.
| Quantityالكمية | Depthwise 3 × 3Depthwise 3 × 3 | Pointwise 1 × 1Pointwise 1 × 1 |
|---|---|---|
| MACsعمليات MAC | 903,168903,168 | 51,380,22451,380,224 |
| Weightsالأوزان | 4,6084,608 | 262,144262,144 |
| Feature maps in + outخرائط السمات دخلًا وخرجًا | 200,704200,704 | 200,704200,704 |
| Bytes moved at 8 bitsالبايتات المنقولة عند 8 بت | 205,312205,312 | 462,848462,848 |
| Operational intensityالكثافة التشغيلية (operational intensity) | ≈8.8 ops/byte≈8.8 ops/byte | ≈222 ops/byte≈222 ops/byte |
| Share of bytes that are feature mapsنسبة البايتات التي هي خرائط سمات | ≈97.8%≈97.8% | ≈43.4%≈43.4% |
This is the roofline view (Williams, Waterman and Patterson): a layer whose operational intensity sits below the machine's ratio of peak compute to memory bandwidth is limited by memory; above it, by arithmetic. The same layer can land on different sides on different machines, which is exactly why HAQ finds different policies for edge and cloud.
هذه هي نظرة نموذج السقف (roofline) من Williams, Waterman and Patterson: الطبقة التي تقع كثافتها التشغيلية تحت نسبة ذروة الحساب إلى عرض نطاق الذاكرة في الجهاز تكون محدودة بالذاكرة، وإذا كانت فوقها فهي محدودة بالحساب. والطبقة نفسها قد تقع على جانبين مختلفين في أجهزة مختلفة، وهذا بالضبط سبب أن HAQ تجد سياسات مختلفة للحافة والسحابة.
Edge: batch 1, small bandwidthالحافة (edge): دفعة بحجم 1، وعرض نطاق صغير
On the edge accelerator with batch size 1, MobileNetV1's latency is dominated by the depthwise layers. Their time is spent moving feature maps, so the fastest way to speed them up is to shrink those activations. Dropping depthwise activations from 8 to 4 bits cuts this layer's traffic from 205,312 to about 104,960 bytes, nearly halving a memory bound layer's time. So HAQ gives depthwise activations fewer bits and spends the saved budget on pointwise layers.
على مسرّع الحافة بحجم دفعة 1، تهيمن طبقات depthwise على زمن استجابة MobileNetV1. وقتها يُصرف في نقل خرائط السمات، لذا فأسرع طريقة لتسريعها هي تقليص تلك التنشيطات. خفض تنشيطات depthwise من 8 إلى 4 بت يقلّل حركة البيانات في هذه الطبقة من 205,312 إلى حوالي 104,960 بايت، فيكاد يخفض زمن طبقة محدودة بالذاكرة إلى النصف. لذلك تعطي HAQ تنشيطات depthwise بتات أقل، وتنفق الميزانية الموفَّرة على طبقات pointwise.
Cloud: batch 16, large bandwidth and parallelismالسحابة (cloud): دفعة بحجم 16، وعرض نطاق كبير، وتوازٍ عالٍ
On the cloud accelerator, a batch of 16 images reuses every loaded weight 16 times, and the device has much more bandwidth and a bigger processing array. The network now behaves as compute bound, and compute lives in the pointwise layers (the MobileNet paper reports 95% of computation time and 75% of parameters in 1 × 1 convolutions). On a bit-serial engine such as BISMO, the work of a multiply grows with the number of weight bits times activation bits, so cutting pointwise widths saves the most time. Meanwhile HAQ raises depthwise widths to protect accuracy, which costs almost nothing because depthwise layers hold a tiny share of the weights (1.7% in the block above).
على مسرّع السحابة، تعيد دفعة من 16 صورة استخدام كل وزن محمَّل 16 مرة، والجهاز لديه عرض نطاق أكبر بكثير ومصفوفة معالجة أكبر. الشبكة الآن تتصرف كمحدودة بالحساب، والحساب يتركز في طبقات pointwise (تذكر ورقة MobileNet أن 95% من زمن الحساب و75% من المعاملات في التفافات 1 × 1). وفي محرّك تسلسلي على مستوى البت (bit-serial) مثل BISMO، يزداد عمل عملية الضرب مع حاصل ضرب عدد بتات الوزن في عدد بتات التنشيط، لذا فخفض عروض pointwise يوفّر أكبر قدر من الوقت. وفي الوقت نفسه ترفع HAQ عروض depthwise لحماية الدقة، وهذا لا يكلّف شيئًا تقريبًا لأن طبقات depthwise تحمل حصة ضئيلة من الأوزان (1.7% في الكتلة أعلاه).
| Edge (BISMO)الحافة (BISMO) | Cloud (BISMO)السحابة (BISMO) | |
|---|---|---|
| Batch sizeحجم الدفعة (batch size) | 11 | 1616 |
| Platform in HAQالمنصة في HAQ | Xilinx Zynq-7020, 8×8 PE arrayXilinx Zynq-7020، مع مصفوفة عناصر معالجة 8×8 | Xilinx VU9P, 16×16 PE arrayXilinx VU9P، مع مصفوفة عناصر معالجة 16×16 |
| What limits latencyما يحدّ زمن الاستجابة | Memory traffic of depthwise layersحركة الذاكرة في طبقات depthwise | Arithmetic of pointwise layersالعمليات الحسابية في طبقات pointwise |
| Depthwise layersطبقات depthwise | Fewer activation bitsبتات تنشيط أقل | More bits (cheap, few weights)بتات أكثر (رخيصة، أوزانها قليلة) |
| Pointwise layersطبقات pointwise | More bitsبتات أكثر | Fewer bitsبتات أقل |
The figure's annotations add a finer pattern on the cloud: depthwise layers get fewer bits in the first few layers and more in the last few. A reasonable reading, though the paper does not spell it out, is that early layers have large feature maps, so their depthwise traffic still costs time, while late layers have small feature maps, so extra precision there is cheap.
تضيف تعليقات الشكل نمطًا أدق على السحابة: طبقات depthwise تحصل على بتات أقل في الطبقات القليلة الأولى وعلى بتات أكثر في الطبقات القليلة الأخيرة. التفسير المعقول، مع أن الورقة لا تصرّح به، هو أن الطبقات المبكرة لها خرائط سمات كبيرة، فتظل حركة بيانات depthwise فيها مكلفة زمنيًا، بينما الطبقات المتأخرة لها خرائط سمات صغيرة، فتكون الدقة الإضافية فيها رخيصة.
Recall
Explain in four sentences why HAQ gives depthwise layers fewer bits on the edge and more on the cloud.اشرح في أربع جمل لماذا تعطي HAQ طبقات depthwise بتات أقل على الحافة وبتات أكثر على السحابة.
Quick checkاختبار سريع
On the cloud accelerator with batch 16, why can HAQ give depthwise layers more bits cheaply?على مسرّع السحابة بدفعة 16، لماذا تستطيع HAQ إعطاء طبقات depthwise بتات أكثر بتكلفة زهيدة؟
Quick checkاختبار سريع
On the edge BISMO accelerator with batch 1, which bits does HAQ cut most for MobileNetV1?على مسرّع BISMO للحافة بدفعة 1، أي البتات تخفضها HAQ أكثر من غيرها في MobileNetV1؟
Picture a trained floating-point MobileNet you must ship to a microcontroller next week. This lecture gave you a sequence of decisions, from the cheapest to the most involved, and slide 75 lists them.
تخيّل شبكة MobileNet مدرَّبة بالفاصلة العائمة (floating point) يجب أن تشحنها إلى متحكم دقيق (microcontroller) الأسبوع القادم. أعطتك هذه المحاضرة سلسلة من القرارات، من الأرخص إلى الأكثر تعقيدًا، والشريحة 75 تسردها.
- Quantize without retraining. Post-training quantization (PTQ) maps each real range onto the integer grid with a scale and zero point. Decide the granularity (per-tensor, per-channel or group) and the range, using min/max, an Exponential moving average (EMA), KL divergence or other clipping.كمّم دون إعادة تدريب. post-training quantization (التكميم بعد التدريب) يربط كل مدى حقيقي بشبكة الأعداد الصحيحة باستخدام scale وzero point. حدّد مستوى الحبيبية (granularity): per-tensor أو per-channel أو group، وحدّد المدى باستخدام min/max أو exponential moving average أو KL divergence أو أنواع أخرى من clipping (القص).
- Retrain if accuracy drops. Quantization-aware training (QAT) inserts Simulated (fake) quantization in the forward pass and uses the Straight-through estimator (STE) so gradients pass through the rounding.أعد التدريب إذا انخفضت الدقة. quantization-aware training (التدريب المراعي للتكميم) يُدخل fake quantization في التمرير الأمامي (forward pass) ويستخدم straight-through estimator حتى تمر التدرجات عبر عملية التقريب.
- Go to the extreme if the hardware wants it. Binarization and ternary weights turn multiplies into sign flips or XNOR and popcount.اذهب إلى الحد الأقصى إذا كان العتاد يريد ذلك. binarization (التحويل الثنائي) وternary weights (الأوزان الثلاثية) تحوّلان عمليات الضرب إلى قلب للإشارة أو إلى XNOR وpopcount.
- Let the bits differ per layer. Mixed precision, searched automatically with the target hardware in the loop, as HAQ does.اجعل البتات تختلف من طبقة لأخرى. الدقة المختلطة (mixed precision)، يُبحث عنها آليًا مع وجود العتاد المستهدف داخل الحلقة، كما تفعل HAQ.
| Stageالمرحلة | Techniquesالتقنيات | Question it answersالسؤال الذي تجيب عنه |
|---|---|---|
| PTQ: granularityPTQ: الحبيبية | Per-tensor, per-channel, group (VS-Quant, MX)per-tensor وper-channel وgroup (VS-Quant وMX) | How many scale factors, and over which valuesكم معامل scale نستخدم، وعلى أي قيم |
| PTQ: rangePTQ: المدى | Min/max, EMA, KL, clippingmin/max وEMA وKL والقص (clipping) | Which real range maps onto the integer gridأي مدى حقيقي يُربط بشبكة الأعداد الصحيحة |
| PTQ: roundingPTQ: التقريب | AdaRoundAdaRound | Round up or down per weight to minimise layer output errorالتقريب للأعلى أو للأسفل لكل وزن لتقليل خطأ خرج الطبقة |
| QATQAT | Fake quantization + STEالتكميم الزائف (fake quantization) مع STE | Train with the rounding in the loop so weights adaptالتدريب مع وجود التقريب داخل الحلقة لكي تتكيف الأوزان |
| Extreme low bitالبتات المنخفضة جدًا | Binary (BNN, XNOR-Net), ternary (TWN, TTQ)الثنائي (BNN وXNOR-Net)، والثلاثي (TWN وTTQ) | 1 or 2 bits, multiplies become sign flips, XNOR and popcount1 أو 2 بت، فتصبح عمليات الضرب قلبًا للإشارة وXNOR وpopcount |
| Mixed precisionالدقة المختلطة (mixed precision) | HAQHAQ | Different bits per layer, chosen with hardware in the loopبتات مختلفة لكل طبقة، تُختار مع وجود العتاد داخل الحلقة |
Slide 76 lists the 16 references the lecture draws on, taken from the MIT EfficientML.ai course. The ones worth opening first for this part are HAQ (Wang et al., CVPR 2019), Jacob et al. (CVPR 2018) for integer-only inference, and Krishnamoorthi's 2018 whitepaper for a practical PTQ and QAT guide. The rest map onto this lecture as follows: Deep Compression (Han et al.) and Intel's Distiller docs for the basics; data-free quantization (Nagel et al., listed as "Markus et al."), Banner et al. and NVIDIA's TensorRT talk for PTQ ranges; Hinton's lecture and Bengio et al. for the straight-through estimator; BNN, DoReFa-Net, PACT, WRPN, ABC-Net and INQ for low-bit training.
تسرد الشريحة 76 المراجع الـ 16 التي تعتمد عليها المحاضرة، وهي مأخوذة من مقرر MIT EfficientML.ai. أولى المراجع بالفتح لهذا الجزء هي HAQ (Wang et al., CVPR 2019)، وJacob et al. (CVPR 2018) للاستدلال بالأعداد الصحيحة فقط (integer-only inference)، والورقة البيضاء (whitepaper) لـ Krishnamoorthi عام 2018 كدليل عملي لـ PTQ وQAT. وبقية المراجع ترتبط بالمحاضرة كما يلي: Deep Compression (Han et al.) ووثائق Distiller من Intel للأساسيات؛ والتكميم دون بيانات (data-free quantization) لـ Nagel et al. (المدرج باسم «Markus et al.»)، وBanner et al.، ومحاضرة TensorRT من NVIDIA لمديات PTQ؛ ومحاضرة Hinton وBengio et al. لمقدّر straight-through؛ وBNN وDoReFa-Net وPACT وWRPN وABC-Net وINQ للتدريب منخفض البتات.
Recall
From memory, list the four families of techniques in this lecture and one key idea of each.من الذاكرة، اذكر العائلات الأربع من التقنيات في هذه المحاضرة وفكرة أساسية واحدة لكل منها.
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Uniform quantization gives every layer the same weight and activation width (for example 8 / 8). Mixed precision lets each layer choose, for example 4 / 5, 6 / 7, 5 / 4.التكميم الموحّد (uniform quantization) يعطي كل الطبقات عرض البتات نفسه للأوزان والتنشيطات (مثلًا 8 / 8). أما الدقة المختلطة (mixed precision) فتسمح لكل طبقة بأن تختار، مثلًا 4 / 5 و 6 / 7 و 5 / 4.
- With B_w weight choices and B_a activation choices, one layer has B_w × B_a options and n layers have (B_w × B_a)^n. With 8 × 8 = 64, that is 64^n = 2^(6n).مع B_w خيارًا للأوزان و B_a خيارًا للتنشيطات، يكون للطبقة الواحدة B_w × B_a خيارًا، ولعدد n من الطبقات (B_w × B_a)^n. ومع 8 × 8 = 64 يصبح العدد 64^n = 2^(6n).
- MobileNetV1 has 28 layers, so 64^28 = 2^168 (about 3.7e50). Even 10 layers give 2^60 (about 1.15e18). Brute force is impossible.شبكة MobileNetV1 فيها 28 طبقة، إذن 64^28 = 2^168 (حوالي 3.7e50). وحتى 10 طبقات تعطي 2^60 (حوالي 1.15e18). البحث الشامل (brute force) مستحيل.
- HAQ (Wang et al., CVPR 2019) uses a DDPG actor-critic agent. State: a 10-feature description of the layer. Action: a continuous value rounded to a bit width in [2, 8]. Reward: 0.1 × (quantized accuracy minus original accuracy).تستخدم HAQ (Wang et al., CVPR 2019) وكيل actor-critic من نوع DDPG. الحالة (state): وصف للطبقة من 10 خصائص. الفعل (action): قيمة متصلة تُقرَّب إلى عرض بتات في [2, 8]. المكافأة (reward): 0.1 × (دقة النموذج المكمّم ناقص الدقة الأصلية).
- Resource limits are enforced by measuring the policy on the accelerator (BISMO edge, BISMO cloud, BitFusion) and lowering bits until the budget is met. That is direct hardware feedback, not FLOPs.تُفرض حدود الموارد بقياس السياسة على المسرّع نفسه (BISMO edge و BISMO cloud و BitFusion)، ثم تخفيض البتات حتى تتحقق الميزانية. هذه تغذية راجعة مباشرة من العتاد (hardware feedback)، وليست FLOPs.
- On MobileNetV1 at about 45 ms on the edge, HAQ reaches 67.40% top-1 versus 62.44% for uniform 4-bit. HAQ wins under latency, energy and model size budgets alike.على MobileNetV1 عند حوالي 45 ms على الحافة (edge)، تصل HAQ إلى دقة top-1 قدرها 67.40% مقابل 62.44% للتكميم الموحّد بـ 4 بت. وتفوز HAQ تحت ميزانيات زمن الاستجابة والطاقة وحجم النموذج على حد سواء.
- Depthwise convolution is memory bound and pointwise is compute bound. Edge, batch 1: depthwise activations get fewer bits. Cloud, batch 16: depthwise gets more bits (cheap, few weights) and pointwise gets fewer.الالتفاف depthwise محدود بالذاكرة (memory bound)، والالتفاف pointwise محدود بالحساب (compute bound). على الحافة بدفعة 1 (batch 1): تحصل تنشيطات depthwise على بتات أقل. على السحابة بدفعة 16: تحصل depthwise على بتات أكثر (رخيصة، أوزانها قليلة) وتحصل pointwise على بتات أقل.
Sourcesالمصادر
- HAQ: Hardware-Aware Automated Quantization with Mixed PrecisionPaperWang, Liu, Lin, Lin, Han. CVPR 2019 (arXiv)State, action, reward, DDPG agent, BISMO and BitFusion setups, Table 3 latency results, Figures 3 and 4 policiesالحالة والفعل والمكافأة، ووكيل DDPG، وإعدادات BISMO و BitFusion، ونتائج زمن الاستجابة في الجدول 3، والسياسات في الشكلين 3 و 4(opens in a new tab)(يفتح في تبويب جديد)
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision ApplicationsPaperHoward et al. (arXiv 2017)28 layers; 95% of computation time and 75% of parameters in 1 × 1 convolutions28 طبقة، و95% من زمن الحساب و75% من المعاملات في التفافات 1 × 1(opens in a new tab)(يفتح في تبويب جديد)
- BISMO: A Scalable Bit-Serial Matrix Multiplication Overlay for Reconfigurable ComputingPaperUmuroglu, Rasnayake, Själander. FPL 2018 (arXiv)The bit-serial accelerator whose performance scales with the required precisionالمسرّع التسلسلي على مستوى البت (bit-serial) الذي يتناسب أداؤه مع الدقة المطلوبة(opens in a new tab)(يفتح في تبويب جديد)
- Bit Fusion: Bit-Level Dynamically Composable Architecture for Accelerating Deep Neural NetworksPaperSharma et al. ISCA 2018 (arXiv)The spatial accelerator that fuses bit-level elements to match each layer's widthالمسرّع المكاني الذي يدمج عناصر على مستوى البت لتطابق عرض كل طبقة(opens in a new tab)(يفتح في تبويب جديد)
- Continuous control with deep reinforcement learningPaperLillicrap et al. ICLR 2016 (arXiv)DDPG, the actor-critic algorithm HAQ uses for continuous actionsDDPG، خوارزمية actor-critic التي تستخدمها HAQ للأفعال المتصلة(opens in a new tab)(يفتح في تبويب جديد)
- Roofline: An Insightful Visual Performance Model for Floating-Point Programs and Multicore ArchitecturesPaperWilliams, Waterman, Patterson. UC Berkeley EECS-2008-134Memory bound versus compute bound by operational intensityالمحدود بالذاكرة (memory bound) مقابل المحدود بالحساب (compute bound) حسب الكثافة التشغيلية(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 6: Quantization (Part II)DocsMIT HAN LabThe EfficientML.ai course the slides and the reference list come fromمقرر EfficientML.ai الذي أُخذت منه الشرائح وقائمة المراجع(opens in a new tab)(يفتح في تبويب جديد)