Lecture 03 · NN Efficiency Metrics

المحاضرة 03 · مقاييس كفاءة الشبكات العصبية

Activations, peak memory and SRAM

التنشيطات (activations) وذروة الذاكرة وSRAM

Slides 18–22 الشرائح 18–22 P1 MUST

Weights live in flash and are read. Activations live in SRAM and are written and overwritten. On a microcontroller the SRAM runs out first, so the number that decides "does it run" is the peak activation, not the parameter count.

الأوزان تُخزَّن في flash وتُقرأ فقط. أما activations فتعيش في SRAM وتُكتب ثم يُكتب فوقها. وفي microcontroller تنفد SRAM أولاً، فالرقم الذي يقرر "هل يعمل النموذج" هو peak activation لا عدد الأوزان.

What an activation is

ما هو التنشيط (activation)

An activation is one value of a feature map: an output of some layer that the next layer reads. The number of activations of a layer is just the size of its output tensor, C × H × W. Total #activations sums this over the whole network. Peak #activations is the largest amount that has to be held in memory at the same moment.

activation هي قيمة واحدة من feature map، أي خرج طبقة ما تقرؤه الطبقة التالية. وعدد activations لطبقة هو ببساطة حجم tensor الخرج، أي C × H × W. وtotal #activations هو مجموع ذلك على الشبكة كلها. أما peak #activations فهو أكبر كمية يجب الاحتفاظ بها في الذاكرة في اللحظة نفسها.

Inference: activations, not weights, are the bottleneck

الاستدلال: التنشيطات، لا الأوزان، هي عنق الزجاجة

Slide 18 compares two networks at the same accuracy, about 70% ImageNet top-1, with every parameter and activation stored as an 8-bit integer.

تقارن الشريحة 18 شبكتين بنفس الدقة تقريباً، حوالي 70% في ImageNet top-1، مع تخزين كل وزن وكل activation كعدد صحيح بـ 8 بت.

ResNet-18 → MobileNetV2-0.75ChangeالتغيّرReadingالقراءة
Parameters (MB)المعاملات (MB)4.6× smallerأصغر بـ 4.6×about 11.7 MB down to about 2.5 MBمن نحو 11.7 MB إلى نحو 2.5 MB
Peak activation (MB)ذروة التنشيطات (MB)1.8× largerأكبر بـ 1.8×the efficient model needs more SRAMالنموذج الفعّال يحتاج أكثر من SRAM

الجدول يقول: عند الانتقال من ResNet-18 إلى MobileNetV2-0.75 صغُرت الأوزان 4.6 مرة (من حوالي 11.7 MB إلى حوالي 2.5 MB)، لكن peak activation كبُر 1.8 مرة. أي أن النموذج "الكفء" يحتاج SRAM أكثر لا أقل.

The right half of slide 18 shows why. MobileNetV2's memory per block is wildly imbalanced: block 2 peaks at 1372 kB, and the first five blocks (0 to 4) all sit above the 256 kB SRAM line of a typical MCU, while the later blocks are tiny. The inverted residual block expands channels about 6× with a 1×1 conv while the spatial resolution is still high, so the early expanded tensors are huge even though their weights are cheap.

النصف الأيمن من الشريحة 18 يوضح السبب. توزيع الذاكرة على كتل MobileNetV2 غير متوازن إطلاقاً: الكتلة 2 تصل إلى 1372 kB، والكتل الخمس الأولى (من 0 إلى 4) كلها فوق خط 256 kB الخاص بـ SRAM في MCU نموذجي، بينما الكتل المتأخرة صغيرة جداً. والسبب أن inverted residual block يوسّع القنوات حوالي 6× بطبقة 1×1 conv بينما الدقة المكانية ما زالت عالية، فتكون tensors الموسَّعة في البداية ضخمة رغم أن أوزانها رخيصة.

Training: every activation is kept

التدريب: كل التنشيطات تُحفظ

During training the backward pass needs every forward activation, so they are all stored at once, not just the peak pair. Slide 19, fp32, ResNet-50 at batch 8 (TinyTL):

أثناء التدريب يحتاج backward pass كل activations الناتجة في forward pass، فتُخزَّن كلها معاً، لا أكبر زوج فقط. الشريحة 19 بدقة fp32، وResNet-50 بحجم batch 8 (من ورقة TinyTL):

ResNet-50MobileNetV2-1.4Reductionنسبة التخفيض
Parametersالمعاملات102 MB≈ 24 MB4.3×
Activationsالتنشيطات707 MB≈ 640 MB1.1×

الجدول: في ResNet-50 الأوزان 102 MB وactivations تساوي 707 MB، أي حوالي 7 أضعاف. والانتقال إلى MobileNetV2-1.4 يقلّص الأوزان 4.3× لكنه يقلّص activations بـ 1.1× فقط. أي أن عنق الزجاجة الحقيقي في الذاكرة لا يتحسن تقريباً.

Activations are about 7× the weights, and the "efficient" architecture barely touches them. The slide's sentence: the main memory bottleneck does not improve much.

activations تساوي حوالي 7 أضعاف الأوزان، والمعمارية "الكفؤة" بالكاد تمسّها. وجملة الشريحة: عنق الزجاجة الرئيسي في الذاكرة لا يتحسن كثيراً.

Early layers vs late layers

الطبقات المبكرة مقابل الطبقات المتأخرة

Slide 20 plots MCUNet layer by layer. Early layers have large spatial maps and few channels, so they are activation heavy with tiny weights. Late layers have small maps and many channels, so they are weight heavy with small activations. The middle is cheap in both. The two costs live at opposite ends of the network, which is why a single number like "model size" hides the problem.

ترسم الشريحة 20 شبكة MCUNet طبقة بطبقة. الطبقات الأولى فيها خرائط مكانية كبيرة وقنوات قليلة، فهي ثقيلة في activations وأوزانها صغيرة. والطبقات الأخيرة فيها خرائط صغيرة وقنوات كثيرة، فهي ثقيلة في الأوزان وactivations فيها صغيرة. والوسط رخيص في الاثنين. فالتكلفتان تقعان في طرفين متقابلين من الشبكة، ولهذا يُخفي رقم واحد مثل model size المشكلة.

AlexNet: total and peak by hand

AlexNet: المجموع والذروة باليد

Slides 21 and 22. Count C × H × W for the input and every layer output:

الشريحتان 21 و22. احسب C × H × W للدخل ولخرج كل طبقة:

Tensorالموتر (tensor)C × H × W#Activations#Activations
image (input)الصورة (الدخل)3×224×224150,528
conv196×55×55290,400
maxpool196×27×2769,984
conv2256×27×27186,624
maxpool2256×13×1343,264
conv3384×13×1364,896
conv4384×13×1364,896
conv5256×13×1343,264
maxpool3256×6×69,216
fc640964,096
fc740964,096
fc810001,000
Totalالمجموع932,264

الجدول يسرد عدد activations لكل tensor: الصورة 150,528، وconv1 يساوي 290,400، وmaxpool1 يساوي 69,984، وconv2 يساوي 186,624، ثم تصغر القيم حتى fc8 بـ 1,000. والمجموع 932,264.

img c1 p1 c2 p2 c3 c4 c5 p3 f6 f7 f8 150,528 290,400 186,624 peak window: image + conv1 = 440,928 values total = sum of all bars = 932,264 peak = largest adjacent pair = 440,928
AlexNet's activation skyline. Running layer by layer, only a layer's input and output must coexist, so the peak is the largest adjacent pair of bars: the image plus conv1's output.
أفق التنشيطات في AlexNet. عند التنفيذ طبقة بطبقة لا يلزم أن يتعايش في الذاكرة إلا دخل الطبقة وخرجها، لذا فالذروة هي أكبر زوج متجاور من الأعمدة: الصورة مع خرج conv1.

الرسم يُظهر activations لكل tensor في AlexNet كأعمدة. عند التنفيذ طبقة بطبقة لا يجب أن يتعايش في الذاكرة إلا دخل الطبقة وخرجها، فتكون القمة هي أكبر زوج من الأعمدة المتجاورة: الصورة مع خرج conv1، أي 440,928 قيمة. أما المجموع فهو مجموع كل الأعمدة، 932,264.

Peak #activations ≈ max over layers of (#input activations + #output activations)

تقول المعادلة: peak #activations يساوي تقريباً أكبر قيمة، على كل الطبقات، لمجموع activations الدخل وactivations الخرج للطبقة نفسها.

Workedمثال محلول
AlexNet peak, checking the candidate pairs

  image + conv1   150,528 + 290,400 = 440,928   <- max
  conv1 + pool1   290,400 +  69,984 = 360,384
  pool1 + conv2    69,984 + 186,624 = 256,608
  conv2 + pool2   186,624 +  43,264 = 229,888
  everything later is smaller

bytes for the peak
  int8   440,928 x 1 B = 440,928 B  = 441 kB
  fp32   440,928 x 4 B = 1,763,712 B = 1.76 MB

does it fit an MCU with 320 kB SRAM (STM32F746 class)?
  441 kB > 320 kB  -> no, not even at int8.
  (and far above the 256 kB line drawn on slide 18)

total, for comparison
  int8   932,264 B = 932 kB     fp32   3.73 MB

المثال المحلول: نفحص الأزواج المرشّحة. الصورة مع conv1 تعطي 440,928 وهي الأكبر، ثم conv1 مع pool1 تعطي 360,384، وما بعدها أصغر. بالبايتات: int8 يعطي 441 kB، وfp32 يعطي 1.76 MB. وفي MCU فيه 320 kB من SRAM لا يتسع النموذج حتى بدقة int8، لأن 441 kB > 320 kB.

Trapفخ

Total is not peak. 932,264 is how many activations the network produces overall. 440,928 is how many must sit in SRAM at once during inference. SRAM is sized by the peak. Training is the exception: it keeps everything for backprop, so there the total (times batch size) matters.

المجموع ليس القمة. فـ 932,264 هو عدد activations التي تنتجها الشبكة كلها، و440,928 هو عدد ما يجب أن يبقى في SRAM في لحظة واحدة أثناء inference. وحجم SRAM يُحدَّد بالقمة. والاستثناء هو التدريب: فهو يحتفظ بكل شيء من أجل backprop، فيصبح المجموع (مضروباً في batch size) هو المهم.

The peak formula has assumptions. It assumes layers run one at a time, there are no branches or skip connections that keep an older tensor alive, and no in-place operation lets the output overwrite the input. A residual connection adds the held tensor to the sum. An in-place ReLU needs no extra buffer.

معادلة القمة لها افتراضات. فهي تفترض أن الطبقات تُنفَّذ واحدة واحدة، وأنه لا توجد تفرعات أو skip connections تُبقي tensor قديماً حياً، وأنه لا توجد عملية in-place تسمح للخرج بالكتابة فوق الدخل. فـ residual connection يضيف tensor المحتفَظ به إلى المجموع، وReLU من نوع in-place لا يحتاج ذاكرة إضافية.

Weights go to flash, activations to SRAM. Model size is checked against flash, peak activations against SRAM. Shrinking the weights does nothing for an SRAM overflow.

الأوزان تذهب إلى flash، وactivations إلى SRAM. فحجم النموذج يُقارن بـ flash، وpeak activations تُقارن بـ SRAM. وتصغير الأوزان لا يحل أبداً مشكلة امتلاء SRAM.

Memory hookمفتاح للحفظ

Flash holds what you know, SRAM holds what you are thinking. Weights are knowledge, read only. Activations are the thought in progress, and the busiest moment of thought sets the SRAM bill.

flash تحمل ما تعرفه، وSRAM تحمل ما تفكر فيه الآن. الأوزان معرفة للقراءة فقط، وactivations هي التفكير الجاري، وأكثر لحظة ازدحاماً في التفكير هي التي تحدد فاتورة SRAM.

Check yourself

اختبر نفسك

A small CNN runs layer by layer with no branches: input 1×28×28, conv1 → 8×28×28, pool → 8×14×14, conv2 → 16×14×14, pool → 16×7×7, fc → 10. What is the peak #activations?

شبكة CNN صغيرة تُنفَّذ طبقة بطبقة بلا تفرعات: الدخل 1×28×28، ثم conv1 → 8×28×28، ثم pool → 8×14×14، ثم conv2 → 16×14×14، ثم pool → 16×7×7، ثم fc → 10. ما قيمة peak #activations؟

Pairs: 784 + 6,272 = 7,056; 6,272 + 1,568 = 7,840; 1,568 + 3,136 = 4,704; 3,136 + 784 = 3,920; 784 + 10 = 794. The max is 7,840, at the first pool. The peak is not always at the first layer: here the input is a single channel, so conv1's output plus pool's output beats it. Option B is the total, option D a single tensor.

الأزواج: 784 + 6,272 = 7,056، و6,272 + 1,568 = 7,840، و1,568 + 3,136 = 4,704، و3,136 + 784 = 3,920، و784 + 10 = 794. الأكبر 7,840 عند أول pool. فالقمة ليست دائماً عند الطبقة الأولى: هنا الدخل قناة واحدة فقط، فيتفوّق عليه زوج خرج conv1 وخرج pool. والخيار B هو المجموع، والخيار D هو tensor واحد فقط.

AlexNet's peak is 440,928 activations. At fp32, how much SRAM does that need?

قمة AlexNet هي 440,928 activation. بدقة fp32، كم تحتاج من SRAM؟

440,928 × 4 B = 1,763,712 B ≈ 1.76 MB. Option A is int8. Option C is the total at fp32 (932,264 × 4). Option D multiplies by 32 bits and forgets to divide by 8.

440,928 × 4 B = 1,763,712 B ≈ 1.76 MB. والخيار A هو int8. والخيار C هو المجموع بدقة fp32 (932,264 × 4). والخيار D يضرب في 32 بت وينسى القسمة على 8.

Going from ResNet-18 to MobileNetV2-0.75 at the same accuracy (int8), what happens on slide 18?

عند الانتقال من ResNet-18 إلى MobileNetV2-0.75 بنفس الدقة (int8)، ماذا يحدث في الشريحة 18؟

This is the slide's headline: #activations, not #parameters, is the inference memory bottleneck, and MobileNetV2's expanded early blocks make its peak larger (1372 kB at block 2).

هذه هي الرسالة الرئيسية للشريحة: #activations لا #parameters هو عنق الزجاجة في ذاكرة inference، والكتل الأولى الموسَّعة في MobileNetV2 تجعل قمتها أكبر (1372 kB عند الكتلة 2).

Why does training need far more activation memory than inference?

لماذا يحتاج التدريب ذاكرة activations أكبر بكثير من inference؟

Gradients of the weights depend on the stored layer inputs, so nothing can be freed after the forward step. That is why ResNet-50's activations reach 707 MB versus 102 MB of weights, and why a 4.3× lighter MobileNetV2 only saves 1.1× of activation memory.

مشتقات الأوزان تعتمد على مداخل الطبقات المخزّنة، فلا يمكن تحرير شيء بعد خطوة forward. ولهذا تصل activations في ResNet-50 إلى 707 MB مقابل 102 MB للأوزان، ولهذا لا يوفّر MobileNetV2 الأخف 4.3× إلا 1.1× من ذاكرة activations.

A quantized model has a 600 kB int8 model size and a 380 kB peak activation. The MCU has 1 MB flash and 320 kB SRAM. What fails?

نموذج بعد quantization حجمه 600 kB بدقة int8، وpeak activation فيه 380 kB. والـ MCU فيه 1 MB من flash و320 kB من SRAM. ما الذي يفشل؟

Weights (600 kB) go to flash and fit under 1 MB. Activations go to SRAM and 380 kB > 320 kB. Option A wrongly pools the two memories. The fix is a smaller peak (lower resolution, fewer early channels, patch-based inference), not fewer weights.

الأوزان (600 kB) تذهب إلى flash وتتسع تحت 1 MB. وactivations تذهب إلى SRAM و380 kB > 320 kB. والخيار A يجمع الذاكرتين خطأً. والحل هو تصغير القمة (دقة أقل، أو قنوات أقل في البداية، أو patch-based inference)، لا تقليل الأوزان.