Lecture 03 · NN Efficiency Metrics
المحاضرة 03 · مقاييس كفاءة الشبكات العصبية
MACs, FLOPs, OPs and rates
MACs وFLOPs وOPs والمعدلات
P1 MUSTA MAC is one multiply plus one add. Count them per layer, double for FLOPs, divide by the chip's rate for a time. The only hard part is remembering that a conv layer's kernel is reused at every output pixel.
MAC هو عملية ضرب واحدة مع عملية جمع واحدة. عُدّها لكل طبقة، وضاعفها لتحصل على FLOPs، واقسم على معدل الشريحة لتحصل على الزمن. والجزء الصعب الوحيد هو تذكّر أن kernel في طبقة conv يُعاد استخدامه عند كل pixel في الخرج.
One MAC
عملية MAC واحدة
المعادلة تعني: خذ القيمة الحالية لـ a، وأضف إليها حاصل ضرب b في c، ثم خزّن الناتج في a. هذه عملية multiply-accumulate واحدة.
Every dot product is a chain of MACs into one accumulator. So matrix products count directly (slide 23):
كل dot product هو سلسلة من MACs تُجمع في accumulator واحد. لذلك يُحسب ضرب المصفوفات مباشرة (الشريحة 23):
Matrix × vector (MV)
مصفوفة × متجه (MV)
MACs = m · n
An m × n matrix times an n-vector: m outputs, each a dot product of length n.
مصفوفة m × n في متجه طوله n: عدد المخارج m، وكل منها dot product بطول n.
Matrix × matrix (GEMM)
مصفوفة × مصفوفة (GEMM)
MACs = m · n · k
(m × k) times (k × n): m · n outputs, each a dot product of length k.
(m × k) في (k × n): عدد المخارج m · n، وكل منها dot product بطول k.
MACs per layer (batch 1, bias ignored)
عدد MACs لكل طبقة (batch 1، مع تجاهل bias)
| Layerالطبقة | #Parameters#Parameters | MACs |
|---|---|---|
| Linearخطية (linear) | co · ci | co · ci |
| Convolutionالتفاف (convolution) | co · ci · kh · kw | co · ci · kh · kw · ho · wo |
| Grouped convالتفاف مجمّع (grouped conv) | co · ci · kh · kw / g | co · ci · kh · kw · ho · wo / g |
| Depthwise convالتفاف عمقي (depthwise conv) | co · kh · kw | co · kh · kw · ho · wo |
الجدول يضع عدد الأوزان بجانب عدد MACs لكل نوع طبقة. في linear الاثنان متساويان: co · ci. وفي كل أنواع conv يكون عدد MACs هو عدد الأوزان مضروباً في ho · wo، أي حجم خرج الطبقة المكاني.
Read the table as one rule: conv MACs = conv parameters × ho × wo, because each weight is used once at every output position. A linear layer uses each weight exactly once, so linear MACs = linear parameters.
اقرأ الجدول كقاعدة واحدة: MACs في conv = أوزان conv × ho × wo، لأن كل وزن يُستخدم مرة واحدة عند كل موضع في الخرج. والطبقة الخطية تستخدم كل وزن مرة واحدة بالضبط، إذن MACs في linear = عدد أوزانها.
AlexNet: 724M MACs
AlexNet: 724M MACs
| Layerالطبقة | Paramsالمعاملات | × howo | MACs |
|---|---|---|---|
| conv1 (96×55×55) | 34,848 | 3,025 | 105,415,200 |
| conv2 (256×27×27, g=2) | 307,200 | 729 | 223,948,800 |
| conv3 (384×13×13) | 884,736 | 169 | 149,520,384 |
| conv4 (384×13×13, g=2) | 663,552 | 169 | 112,140,288 |
| conv5 (256×13×13, g=2) | 442,368 | 169 | 74,760,192 |
| fc6 | 37,748,736 | 1 | 37,748,736 |
| fc7 | 16,777,216 | 1 | 16,777,216 |
| fc8 | 4,096,000 | 1 | 4,096,000 |
| Totalالمجموع | 60,954,656 | 724,406,816 |
الجدول يحسب MACs في AlexNet بضرب أوزان كل طبقة conv في howo: conv1 يعطي 105,415,200، وconv2 هو الأكبر بـ 223,948,800، وconv5 يعطي 74,760,192 (وهو الرقم المخفي خلف علامة الاستفهام في الشريحة 26). وطبقات FC تساوي أوزانها. والمجموع 724,406,816، أي حوالي 724M.
في الرسم شريطان كاملا العرض. في شريط الأوزان تأخذ conv شريحة رفيعة (3.8%) وتملأ FC الباقي (96.2%). وفي شريط MACs ينعكس الوضع: conv تملأ 91.9% وFC شريحة رفيعة بـ 8.1%. فطبقات FC تملك التخزين، وطبقات conv تملك الحساب.
FLOPs, OPs, and the per-second versions
FLOPs وOPs ونسخها في الثانية
A multiply is one floating point operation and an add is one, so 1 MAC = 2 FLOPs (slide 27). AlexNet: 724,406,816 × 2 = 1,448,813,632 ≈ 1.45 GFLOPs, which the slide rounds to 1.4G. Because weights and activations are not always floating point (int8 after quantization), slide 28 generalizes to OPs, counted the same way: 1 MAC = 2 OPs.
الضرب عملية floating point واحدة، والجمع عملية واحدة، إذن 1 MAC = 2 FLOPs (الشريحة 27). في AlexNet: 724,406,816 × 2 = 1,448,813,632 ≈ 1.45 GFLOPs، والشريحة تقرّبها إلى 1.4G. ولأن الأوزان وactivations ليست دائماً floating point (مثل int8 بعد quantization)، تعمّم الشريحة 28 المفهوم إلى OPs، وتُحسب بالطريقة نفسها: 1 MAC = 2 OPs.
Counts: a property of the model
أعداد: خاصية للنموذج
FLOPs, OPs, MACs
Lowercase s means plural. How much work one inference takes.
حرف s الصغير يعني الجمع. أي كمية العمل التي يحتاجها inference واحد.
Rates: a property of the processor
معدلات: خاصية للمعالج
FLOPS = FLOPs / second, OPS = OPs / second
Capital S means per second. How fast a chip can do work.
حرف S الكبير يعني "في الثانية". أي سرعة الشريحة في إنجاز العمل.
تقول المعادلة: زمن الحساب Tcomputation يساوي عدد العمليات في النموذج مقسوماً على عدد العمليات التي ينفذها المعالج في الثانية.
one conv layer input 16 x 32 x 32, conv 3x3, 32 filters, stride 1, pad 1 output 32 x 32 x 32 -> h_o = w_o = 32 params = c_o c_i k_h k_w = 32 x 16 x 3 x 3 = 4,608 MACs = params x h_o w_o = 4,608 x 1,024 = 4,718,592 FLOPs = 2 x MACs = 9,437,184 same layer as depthwise 3x3 on 16 channels: MACs = c_o k_h k_w h_o w_o = 16 x 9 x 1,024 = 147,456 T_computation for AlexNet OPs = 2 x 724,406,816 = 1.449 x 10^9 chip A: 100 GOPS T = 1.449e9 / 1e11 = 14.5 ms chip B: 1 TOPS T = 1.449e9 / 1e12 = 1.45 ms this is the ideal compute time at full utilization. real latency is about max(T_computation, T_memory).
المثال المحلول: طبقة conv دخلها 16 × 32 × 32 وkernel بحجم 3x3 و32 filter مع pad 1، فيكون الخرج 32 × 32 × 32. الأوزان 4,608، وMACs تساوي 4,608 × 1,024 = 4,718,592، وFLOPs ضعفها. وفي AlexNet عدد العمليات 1.449 × 109، فعلى شريحة بـ 100 GOPS يكون الزمن 14.5 ms، وعلى 1 TOPS يكون 1.45 ms. هذا زمن الحساب المثالي فقط، والزمن الحقيقي تقريباً max(Tcomputation, Tmemory).
Use the output size, not the input size. conv1 MACs use howo = 55 × 55, not 224 × 224. With the input size you get 1.75 billion MACs for conv1 alone, which is wrong by 16.6×.
استخدم حجم الخرج لا حجم الدخل. فـ MACs في conv1 تستخدم howo = 55 × 55 لا 224 × 224. وباستخدام حجم الدخل تحصل على 1.75 مليار MAC لـ conv1 وحدها، وهذا خطأ بمعامل 16.6×.
FLOPs is not FLOPS. FLOPs (lowercase s) counts work in the model. FLOPS (capital S) is a processor rate. "AlexNet needs 1.4 GFLOPS" is a category error.
FLOPs ليست FLOPS. فـ FLOPs (بحرف s صغير) تعدّ العمل في النموذج، وFLOPS (بحرف S كبير) معدل للمعالج. وعبارة "AlexNet needs 1.4 GFLOPS" خطأ في التصنيف.
Two operations per MAC. 724M MACs is 1.45G FLOPs, not 724M FLOPs. Forgetting the ×2 halves your Tcomputation.
عمليتان لكل MAC. فـ 724M MACs تساوي 1.45G FLOPs لا 724M FLOPs. ونسيان الضرب في 2 يجعل Tcomputation نصف قيمته الصحيحة.
Weights are counted once, MACs are counted everywhere they are used. A conv kernel is used at every output pixel, so multiply by howo. A linear weight is used once, so MACs equal parameters.
الأوزان تُعدّ مرة واحدة، وMACs تُعدّ في كل مكان يُستخدم فيه الوزن. فـ kernel في conv يُستخدم عند كل pixel في الخرج، فاضرب في howo. والوزن الخطي يُستخدم مرة واحدة، فتكون MACs مساوية للأوزان.
Check yourself
اختبر نفسك
A 3×3 conv maps 64 channels to 128 channels and produces a 56×56 output. How many MACs (batch 1, no bias)?
طبقة conv بحجم 3×3 تحوّل 64 قناة إلى 128 قناة وتنتج خرجاً بحجم 56×56. كم عدد MACs (batch 1، بلا bias)؟
Params = 128 × 64 × 9 = 73,728; MACs = 73,728 × 56 × 56 = 73,728 × 3,136 = 231,211,008. Option A is the parameter count, option C is the FLOPs (×2), and option D is a depthwise-style count that drops ci.
الأوزان 128 × 64 × 9 = 73,728، وMACs تساوي 73,728 × 56 × 56 = 73,728 × 3,136 = 231,211,008. والخيار A هو عدد الأوزان، والخيار C هو FLOPs (×2)، والخيار D حساب بأسلوب depthwise يحذف ci.
AlexNet has about 724M MACs. How many floating point operations is one inference?
في AlexNet حوالي 724M MACs. كم عملية floating point يحتاجها inference واحد؟
1 MAC = 1 multiply + 1 add = 2 FLOPs, so 724,406,816 × 2 ≈ 1.45G (the slide rounds to 1.4G). Option D has the right number with the wrong unit: FLOPS is a rate of a processor, not a count of work.
1 MAC = ضرب واحد + جمع واحد = 2 FLOPs، إذن 724,406,816 × 2 ≈ 1.45G (والشريحة تقرّبها إلى 1.4G). والخيار D فيه الرقم الصحيح بوحدة خاطئة: FLOPS معدل للمعالج، لا عدد للعمل.
A model needs 2 GOPs per inference and runs on an accelerator rated at 500 GOPS. What is the ideal Tcomputation?
نموذج يحتاج 2 GOPs لكل inference ويعمل على مسرّع معدله 500 GOPS. ما قيمة Tcomputation المثالية؟
T = OPs / OPS = 2×109 / 5×1011 = 0.004 s = 4 ms. Option B inverts the ratio (500/2). Option C multiplies. Option D doubles a count that is already in OPs; only MACs need the ×2.
T = OPs / OPS = 2×109 / 5×1011 = 0.004 s = 4 ms. والخيار B يعكس النسبة (500/2). والخيار C يضرب بدل أن يقسم. والخيار D يضاعف رقماً هو أصلاً بوحدة OPs، والضرب في 2 خاص بـ MACs فقط.
In AlexNet, conv layers hold 3.8% of the parameters. What share of the MACs do they perform?
في AlexNet تحمل طبقات conv نسبة 3.8% من الأوزان. فما نسبتها من MACs؟
665,784,864 / 724,406,816 = 91.9%. Conv weights are reused at every output position (×3,025 for conv1, ×729 for conv2, ×169 for conv3 to conv5), while FC weights are used once. Option C is FC's share of MACs.
665,784,864 / 724,406,816 = 91.9%. فأوزان conv يُعاد استخدامها عند كل موضع في الخرج (×3,025 في conv1، و×729 في conv2، و×169 من conv3 إلى conv5)، بينما أوزان FC تُستخدم مرة واحدة. والخيار C هو نسبة FC من MACs.
A depthwise 3×3 conv on 32 channels produces a 112×112 output. How many MACs?
طبقة depthwise conv بحجم 3×3 على 32 قناة تنتج خرجاً بحجم 112×112. كم عدد MACs؟
Depthwise MACs = co · kh · kw · ho · wo = 32 × 9 × 12,544 = 3,612,672. Option A is the parameter count. Option C is a full conv with ci = 32 (32× more). Option D is the FLOPs.
MACs في depthwise تساوي co · kh · kw · ho · wo = 32 × 9 × 12,544 = 3,612,672. والخيار A هو عدد الأوزان. والخيار C هو conv كامل مع ci = 32 (أكبر 32 مرة). والخيار D هو FLOPs.