Lecture 01 · Introduction

المحاضرة 01 · المقدمة

Accuracy at a cost: why efficient ML

الدقة لها ثمن: لماذا نحتاج تعلّم آلة فعّالاً

Slides 2–8 الشرائح 2–8 P3 SKIM

The one trade-off the whole course answers: accuracy is bought with computation and memory, and the devices you want to deploy on cannot pay the bill.

المقايضة الوحيدة التي يجيب عنها المقرر كله: الدقة تُشترى بالحساب والذاكرة، والأجهزة التي تريد التشغيل عليها لا تستطيع دفع الفاتورة.

What gets askedما الذي يُسأل عنه

This part is motivation, so expect short questions rather than long derivations: name the three pillars, define top-5 error versus top-1 accuracy, read a bubble chart (x, y, bubble area), compute one reduction ratio (8400 / 595 = 14x), and list the benefits and costs of on-device training.

هذا الجزء تحفيزي، فتوقّع أسئلة قصيرة لا اشتقاقات طويلة: اذكر الركائز الثلاث، وعرّف top-5 error مقابل top-1 accuracy، واقرأ bubble chart (المحور الأفقي والرأسي ومساحة الفقاعة)، واحسب نسبة تخفيض واحدة (8400 / 595 = 14x)، واسرد فوائد on-device training وتكاليفه.

The challenge and the three pillars, slides 2 and 3

التحدي والركائز الثلاث، الشريحتان 2 و3

Slide 2 states the course in one line: machine learning and deep learning are applied everywhere, they are computationally expensive, and the challenge is to make them lightweight and fast without losing accuracy. Written as an optimization problem, you maximize accuracy subject to budgets on MACs, memory, latency and energy. The budgets change from device to device; the question does not.

تلخّص الشريحة 2 المقرر في سطر واحد: machine learning وdeep learning مستخدمان في كل مكان، وهما مكلفان حسابياً، والتحدي هو جعلهما خفيفين وسريعين دون خسارة الدقة. وإذا كتبتها كمسألة تحسين: تعظّم الدقة بشرط قيود على MACs والذاكرة وlatency والطاقة. القيود تتغير من جهاز لآخر، أما السؤال فثابت.

max Acc(θ, A)   s.t.   MACs ≤ C, Memory ≤ M, Latency ≤ T, Energy ≤ E

الصيغة أعلاه تعني: اختر الأوزان θ والمعمارية A لتعظيم الدقة، بشرط ألا تتجاوز الحساب C والذاكرة M وزمن الاستجابة T والطاقة E.

Slide 3 gives the lens for everything after it: progress needs three pillars improving together. Know one efficiency lever per pillar.

تعطي الشريحة 3 العدسة التي يُقرأ بها كل ما بعدها: التقدم يحتاج إلى ثلاث ركائز تتحسن معاً. احفظ رافعة كفاءة واحدة لكل ركيزة.

Algorithm

الخوارزمية

The model and how it is trained. Levers: pruning, quantization, distillation, neural architecture search. Most of this course lives here.

النموذج وطريقة تدريبه. الروافع: pruning وquantization وdistillation وneural architecture search. معظم المقرر يقع هنا.

Hardware

العتاد

The chip it runs on. Levers: accelerators from cloud GPUs to phone NPUs, Jetson modules and microcontrollers. Slide 17 reopens this pillar.

الشريحة التي يعمل عليها النموذج. الروافع: المسرّعات من GPU السحابية إلى NPU في الجوال ووحدات Jetson وmicrocontrollers. الشريحة 17 تعيد فتح هذه الركيزة.

Data

البيانات

What it learns from. AlexNet needed about 1.2 million labeled ImageNet images; on slide 8 data that cannot leave the device forces learning onto the device.

ما يتعلم منه النموذج. احتاج AlexNet إلى نحو 1.2 مليون صورة مُعلَّمة من ImageNet، وفي الشريحة 8 البيانات التي لا يمكنها مغادرة الجهاز تفرض أن يحدث التعلم على الجهاز نفسه.

Top-5 error, top-1 accuracy, slide 4

Top-5 error وtop-1 accuracy، الشريحة 4

Top-5 error is the fraction of test images whose true label is not among the model's five highest-scoring classes. Top-1 accuracy is the fraction of images whose single highest-scoring class is the true label. Top-1 is stricter, so for the same model top-1 error is always at least as large as top-5 error.

Top-5 error هو نسبة صور الاختبار التي لا يظهر صنفها الحقيقي ضمن أعلى خمسة أصناف في تقدير النموذج. وTop-1 accuracy هو نسبة الصور التي يكون فيها الصنف الأعلى وحده هو الصنف الحقيقي. معيار top-1 أشد صرامة، لذلك يكون top-1 error للنموذج نفسه دائماً أكبر من أو يساوي top-5 error.

Slide 4's left panel shows why the rule matters. For the mushroom photo AlexNet's top guess is "agaric" and "mushroom" is second: a top-5 hit, a top-1 miss. For the cherry photo the top guess is "dalmatian" and cherry is absent from the five: a miss under both rules.

اللوحة اليسرى في الشريحة 4 توضح أهمية القاعدة. في صورة الفطر كان تخمين AlexNet الأول "agaric" وجاء "mushroom" ثانياً: إصابة في top-5 وخطأ في top-1. وفي صورة الكرز كان التخمين الأول "dalmatian" ولم يظهر الكرز ضمن الخمسة إطلاقاً: خطأ في المعيارين.

ILSVRC winning top-5 error (%) 28.2 2010 25.8 2011 16.4 2012 AlexNet 11.7 2013 6.7 2014 3.57 2015 ResNet 2.99 2016 2.25 2017 human 5.1 2011 to 2012: 25.8 to 16.4, a 9.4 point drop 2015: first winner below the human line
Slide 4 as numbers. Violet bars sit below the dashed 5.1% human estimate. Every bar is top-5 error of a contest winner, often an ensemble.
الشريحة 4 بالأرقام. الأعمدة البنفسجية تقع تحت الخط المتقطع لتقدير الأداء البشري 5.1%. كل عمود هو top-5 error للفائز في المسابقة، وغالباً ما يكون مجموعة نماذج (ensemble).

الشكل يعرض الشريحة 4 بالأرقام: top-5 error للفائز سنوياً من 28.2% في 2010 إلى 2.3% في 2017. عام 2012 هو قفزة AlexNet (انخفاض 9.4 نقطة)، وResNet عام 2015 بنسبة 3.57% هو أول فائز تحت تقدير الإنسان 5.1%.

Worked exampleمثال محلول

The slide 4 arithmetic you might be asked for:

الحسابات المحتملة على الشريحة 4:

2011 to 2012:  25.8 - 16.4 = 9.4 points   (9.4 / 25.8 = 36% relative)
2012 to 2014:  16.4 / 6.7  = 2.45x lower error
2010 to 2017:  28.2 / 2.3  ≈ 12x lower error
2017 margin:   5.1 - 2.25  = 2.85 points better than the human estimate

The "human" is one trained annotator who labeled 1500 test images at 5.1% top-5 error. "Super-human" here means beating that person on the top-5 rule over 1000 classes, nothing more general.

الـ "إنسان" هنا هو مُعلِّم واحد مُدرَّب صنّف 1500 صورة اختبار بنسبة top-5 error تساوي 5.1%. وعبارة "أفضل من الإنسان" تعني التفوق على هذا الشخص بمعيار top-5 على 1000 صنف، لا أكثر من ذلك.

Accuracy is bought with MACs, slide 5

الدقة تُشترى بـ MACs، الشريحة 5

A MAC (multiply-accumulate) is one multiplication of an input by a weight added into a running sum. Counting MACs per input gives a hardware-neutral cost: it depends on the model and the input size, not on the chip. Parameters count the weights, so they measure storage, not work. The two are related but move independently.

MAC (multiply-accumulate) هو عملية ضرب واحدة لمدخل في وزن ثم إضافتها إلى مجموع جارٍ. وعدّ MACs لكل مدخل يعطي تكلفة مستقلة عن العتاد: تعتمد على النموذج وحجم المدخل، لا على الشريحة. أما عدد المعاملات (parameters) فيعدّ الأوزان، فهو يقيس التخزين لا العمل. الاثنان مرتبطان لكنهما يتحركان باستقلال.

The bubble chart encodes three quantities: the x axis is MACs in billions (left is cheaper), the y axis is ImageNet top-1 accuracy (up is better), and bubble area is the parameter count (2M to 64M in the legend). The goal corner is the upper left.

الـ bubble chart يرمّز ثلاث كميات: المحور الأفقي هو MACs بالمليارات (اليسار أرخص)، والمحور الرأسي هو top-1 accuracy على ImageNet (الأعلى أفضل)، ومساحة الفقاعة هي عدد المعاملات (من 2M إلى 64M في المفتاح). الزاوية المطلوبة هي أعلى اليسار.

0 2 4 6 8 70 72 74 76 78 80 MACs (billion), cost Top-1 (%) MobileNetV1 0.57B, 70.6 ResNet-50 3.9B, 75.3 ResNet-101 7.6B, 76.4 InceptionV3 DPN-92 Xception 8.4B, 79.0 2x MACs, +1.1 point Once-for-All 0.595B, 80.0 14x fewer MACs
Slides 5 and 6 redrawn. Circle area is parameters (storage), position is cost and accuracy. Points marked by the slide are approximate chart readings except the paper values for MobileNetV1, ResNet-50, ResNet-101 and Once-for-All.
الشريحتان 5 و6 مُعاد رسمهما. مساحة الدائرة هي عدد المعاملات (التخزين)، والموقع هو التكلفة والدقة. النقاط المأخوذة من الشريحة قراءات تقريبية من الرسم، باستثناء قيم الأوراق البحثية لـ MobileNetV1 وResNet-50 وResNet-101 وOnce-for-All.

الشكل يعيد رسم الشريحتين 5 و6: مساحة الدائرة هي عدد المعاملات (التخزين)، والموقع هو التكلفة والدقة. الخط المتقطع بين ResNet-50 وResNet-101 يُظهر مضاعفة MACs مقابل نحو نقطة واحدة فقط، والنجمة Once-for-All في أعلى اليسار بـ 14x حساب أقل من Xception.

Diminishing returns

تناقص العائد

Divide the accuracy gained by the extra MACs spent. The exchange rate collapses as you climb the chart.

اقسم الدقة المكتسبة على MACs الإضافية المصروفة. سعر الصرف ينهار كلما صعدت في الرسم.

UpgradeالترقيةExtra MACs (B)MACs الإضافية (B)Extra top-1 (points)زيادة top-1 (نقاط)Points per B MACsنقاط لكل مليار MACs
MobileNetV1 to ResNet-50من MobileNetV1 إلى ResNet-503.334.71.41
ResNet-50 to Xceptionمن ResNet-50 إلى Xception4.53.70.82
ResNet-50 to ResNet-101من ResNet-50 إلى ResNet-1013.71.10.30

الجدول يُظهر أن قيمة كل مليار MACs إضافي تهبط من 1.41 نقطة إلى 0.30 نقطة، أي نحو خمسة أضعاف. وأسوأ صفقة داخل العائلة نفسها: ResNet-101 يضاعف حساب ResNet-50 (7.6 مقابل 3.9 مليار) مقابل نحو نقطة واحدة (76.4 مقابل 75.3).

Search shifts the frontier, slide 6

البحث يزيح الحدّ الأمثل، الشريحة 6

Once-for-All reaches 80.0% top-1 at 595M MACs. Xception needs about 8.4B MACs for 79.0%. The stars on slide 6 are AutoML (neural architecture search) results; the circles are handcrafted. The Pareto frontier is the set of models nobody beats on both accuracy and cost, and search moved it to the upper left.

يصل Once-for-All إلى 80.0% top-1 عند 595M MACs، بينما يحتاج Xception إلى نحو 8.4B MACs للوصول إلى 79.0%. النجوم في الشريحة 6 هي نتائج AutoML (neural architecture search)، والدوائر مصممة يدوياً. وPareto frontier هي مجموعة النماذج التي لا يتفوق عليها أي نموذج آخر في الدقة والتكلفة معاً، والبحث الآلي دفعها نحو أعلى اليسار.

Worked exampleمثال محلول

Reduction ratio = reference MACs divided by new MACs. Put both in the same unit first.

نسبة التخفيض = MACs المرجعية مقسومة على MACs الجديدة. حوّل الاثنين إلى الوحدة نفسها أولاً.

Xception   8.4 B = 8400 M MACs     8400 / 595 = 14.1x
ResNet-50  3.9 B = 3900 M MACs     3900 / 595 = 6.6x
MobileNetV1     569 M MACs         same budget, but 80.0 - 70.6 = 9.4 points more accurate

So the 14x arrow means the same accuracy, actually one point more, for one fourteenth of the computation.

إذن سهم 14x يعني الدقة نفسها، بل أعلى بنقطة، بجزء من أربعة عشر من الحساب.

TinyML on a microcontroller, slide 7

TinyML على متحكم دقيق (microcontroller)، الشريحة 7

Slide 7 shows MCUNet doing mask detection and person detection on a small camera board with an ARM Cortex-M7. MCUNet's reference microcontroller, the STM32F746, has 320 kB of SRAM and 1 MB of flash, no DRAM and no operating system. SRAM holds activations (read and write), flash holds weights (read only).

تعرض الشريحة 7 نظام MCUNet وهو يكشف الكمامات والأشخاص على لوحة كاميرا صغيرة بمعالج ARM Cortex-M7. المتحكم المرجعي في MCUNet هو STM32F746، وفيه 320 kB من SRAM و1 MB من flash، بلا DRAM وبلا نظام تشغيل. SRAM تحمل activations (قراءة وكتابة)، وflash تحمل الأوزان (قراءة فقط).

Why nothing off the shelf fits: ResNet-50 has 25.5M parameters, so 25.5M × 4 bytes = 102 MB of FP32 weights, about 100x the flash. Even INT8 (25.5 MB) is 25x too big. MobileNetV2's int8 peak activation is 1.7 MB, still 5.3x over 320 kB. Quantization alone cannot close the gap; MCUNet co-designs the network (TinyNAS) and the engine (TinyEngine).

لماذا لا يناسب أي نموذج جاهز: ResNet-50 فيه 25.5M معامل، أي 25.5M × 4 bytes = 102 MB من أوزان FP32، وهذا نحو 100x حجم flash. وحتى بصيغة INT8 (25.5 MB) يبقى أكبر بـ 25x. وذروة activations لنموذج MobileNetV2 بصيغة int8 هي 1.7 MB، أي أكبر من 320 kB بـ 5.3x. لذلك quantization وحدها لا تكفي، وMCUNet يصمم الشبكة (TinyNAS) والمحرك (TinyEngine) معاً.

On-device training, slide 8

التدريب على الجهاز (on-device training)، الشريحة 8

Slide 8's figure: new and sensitive data (a face, a voice) flows from the user to an edge device, and the arrow to the cloud is crossed out because the data cannot be sent for privacy reasons. So the model must adapt where the data is. The slide lists four benefits and three costs.

شكل الشريحة 8: بيانات جديدة وحساسة (وجه، صوت) تنتقل من المستخدم إلى الجهاز الطرفي، والسهم نحو السحابة مشطوب لأن البيانات لا يمكن إرسالها لأسباب الخصوصية. لذلك يجب أن يتكيف النموذج حيث توجد البيانات. تسرد الشريحة أربع فوائد وثلاث تكاليف.

Benefits

الفوائد

  • Better privacy: nothing leaves the device.

    خصوصية أفضل: لا شيء يغادر الجهاز.

  • Lower cost: no upload, no cloud training bill.

    تكلفة أقل: لا رفع للبيانات ولا فاتورة تدريب سحابي.

  • Customization: adapts to this user and this environment.

    التخصيص: يتكيف مع هذا المستخدم وهذه البيئة.

  • Life-long learning: adaptation never stops.

    التعلم المستمر (life-long learning): التكيف لا يتوقف.

Costs

التكاليف

  • Compute: the backward pass is about 2x the forward, so one training step is about 3x an inference pass.

    الحساب: backward pass يكلف نحو ضعف forward pass، فخطوة التدريب الواحدة تكلف نحو 3x تمرير الاستدلال.

  • Memory: every layer's activations must be stored until the backward pass returns, plus gradients and optimizer state.

    الذاكرة: يجب تخزين activations كل طبقة حتى يعود backward pass، إضافة إلى gradients وحالة optimizer.

  • Power: a battery device cannot run that continuously.

    الطاقة: جهاز يعمل بالبطارية لا يستطيع تشغيل ذلك باستمرار.

Worked exampleمثال محلول

Training MobileNetV1 (4.2M parameters) with SGD plus momentum, state only:

تدريب MobileNetV1 (4.2M معامل) باستخدام SGD مع momentum، الحالة فقط:

weights   4.2 M × 4 B = 16.8 MB
gradients               16.8 MB
momentum                16.8 MB
total                   50.4 MB     vs 320 kB SRAM: about 157x over, before any activation
Exam trapفخ امتحاني

Two traps live on these slides. First, slide 4 is top-5 error; slides 5 and 6 are top-1 accuracy. Never subtract one from the other: a model with about 5% top-5 error typically has 20 to 25% top-1 error, which is the 75 to 80% band on slide 5. Second, MACs are a hardware-neutral cost, parameters are storage. Bubble area on slide 5 is parameters, not MACs. Xception and InceptionV3 have about the same parameters (about 23M) but 8.4B versus 5.7B MACs.

في هذه الشرائح فخّان. الأول: الشريحة 4 تستخدم top-5 error، والشريحتان 5 و6 تستخدمان top-1 accuracy. لا تطرح أحدهما من الآخر أبداً: نموذج بـ top-5 error نحو 5% يكون عادة top-1 error عنده بين 20% و25%، وهذا هو نطاق 75% إلى 80% في الشريحة 5. الثاني: MACs تكلفة مستقلة عن العتاد، وparameters تخزين. مساحة الفقاعة في الشريحة 5 هي المعاملات لا MACs. فـ Xception وInceptionV3 لهما عدد معاملات متقارب (نحو 23M) لكن 8.4B مقابل 5.7B MACs.

Memory trickحيلة للحفظ

"Five chances, one chance." Top-5 lets you circle five answers; top-1 allows one. And "MACs are work, params are weight": work happens per input, weight sits in storage.

"خمس فرص، فرصة واحدة." معيار top-5 يسمح لك بتحديد خمس إجابات، وtop-1 يسمح بواحدة فقط. وتذكّر: MACs هي العمل، وparams هي الوزن: العمل يحدث مع كل مدخل، والوزن يجلس في التخزين.

Check yourself

اختبر نفسك

In ILSVRC top-5 evaluation, when does one test image count as correctly classified?

في تقييم top-5 في ILSVRC، متى تُحسب صورة اختبار واحدة مصنفة بشكل صحيح؟

B. The image is wrong only if none of the five returned labels equals the ground truth. A is the top-1 rule, the tempting answer because it is the stricter metric used on slides 5 and 6. The mushroom photo is a top-5 hit and a top-1 miss for exactly this reason.

الجواب B. تُحسب الصورة خاطئة فقط إذا لم يطابق أي من الأصناف الخمسة الصنف الحقيقي. والخيار A هو قاعدة top-1، وهو مُغرٍ لأنه المعيار الأشد المستخدم في الشريحتين 5 و6. ولهذا السبب بالضبط كانت صورة الفطر إصابة في top-5 وخطأ في top-1.

On slide 5, what does the area of each bubble encode?

في الشريحة 5، ماذا تمثل مساحة كل فقاعة؟

A. The legend runs 2M to 64M under "Model Size", which is parameters. MACs are the x axis and accuracy the y axis; the chart has no latency. Confusing area with MACs is the classic slip because both grow with bigger models.

الجواب A. المفتاح يمتد من 2M إلى 64M تحت عنوان "Model Size"، أي عدد المعاملات. MACs هي المحور الأفقي والدقة هي المحور الرأسي، ولا يوجد latency في الرسم. الخلط بين المساحة وMACs هو الخطأ الكلاسيكي لأن الاثنين يكبران مع النماذج الأكبر.

Xception needs about 8.4B MACs for 79.0% top-1; Once-for-All needs 595M MACs for 80.0%. By what factor is computation reduced?

يحتاج Xception إلى نحو 8.4B MACs للوصول إلى 79.0% top-1، ويحتاج Once-for-All إلى 595M MACs للوصول إلى 80.0%. بأي معامل انخفض الحساب؟

D. 8400 / 595 = 14.1. A is the ratio to ResNet-50 (3900 / 595). C is the decimal slip of writing 595M as 0.0595B instead of 0.595B: always convert units first.

الجواب D. 8400 / 595 = 14.1. والخيار A هو النسبة إلى ResNet-50 (3900 / 595). والخيار C ينتج عن خطأ الفاصلة العشرية بكتابة 595M على أنها 0.0595B بدلاً من 0.595B: حوّل الوحدات دائماً أولاً.

ResNet-101 roughly doubles the MACs of ResNet-50. What does slide 5 show it buys?

يضاعف ResNet-101 تقريباً MACs الخاصة بـ ResNet-50. ماذا يُظهر الرسم في الشريحة 5 أنه يشتري بذلك؟

C. About 75.3% to 76.4%, roughly 1.1 points for 3.7B extra MACs, 0.30 points per billion. That is the diminishing-returns message of the slide. B mixes in the top-5 metric from slide 4.

الجواب C. من نحو 75.3% إلى 76.4%، أي نحو 1.1 نقطة مقابل 3.7B من MACs الإضافية، بمعدل 0.30 نقطة لكل مليار. هذه هي رسالة تناقص العوائد في الشريحة. والخيار B يخلط مقياس top-5 من الشريحة 4.

Why is on-device training much harder to fit than on-device inference?

لماذا يصعب تشغيل on-device training أكثر بكثير من on-device inference؟

B. Forward plus a backward pass of about twice the forward gives about 3x the arithmetic, and the backward pass needs the stored activations, while the optimizer keeps extra copies of the weights' size. D is backwards: inference can free each layer's activations once consumed. A contradicts the slide's point that data stays local.

الجواب B. forward pass زائد backward pass يكلف نحو ضعفه يعطي نحو 3x من الحساب، وbackward pass يحتاج إلى activations المخزنة، بينما يحتفظ optimizer بنسخ إضافية بحجم الأوزان. والخيار D معكوس: الاستدلال يحرر activations كل طبقة بعد استهلاكها. والخيار A يناقض فكرة الشريحة بأن البيانات تبقى محلياً.