Majid Al-RaimiActivations and peak memoryالتنشيطات وذروة الذاكرة

COE 592Lecture 03المحاضرة 03Part 03الجزء 03

Activations and peak memoryالتنشيطات وذروة الذاكرة

Why activations rather than parameters are the memory bottleneck in inference and training, how memory is spread across layers of MobileNetV2 and MCUNet, and how to compute total and peak activations for AlexNet.لماذا تكون التنشيطات لا المعاملات هي عنق الزجاجة للذاكرة في الاستدلال والتدريب، وكيف تتوزع الذاكرة على طبقات MobileNetV2 وMCUNet، وكيف نحسب مجموع التنشيطات وذروتها في AlexNet.

Conceptsالمفاهيم
4
Slidesالشرائح
18-22
Readingمدة القراءة
24 min24 دقيقة
Understoodما فهمته
0/4 conceptsمفاهيم

Why this part mattersلماذا يهمّ هذا الجزء

When a vision model is pushed onto a UAV, a robot controller or an IoT board, the first question a compiler or a reviewer asks is not "how many parameters" but "what is the peak activation". Those boards carry 256 kB to 512 kB of SRAM and no DRAM, and it is the activations, not the weights, that have to live there.

Part 02 counted weights and turned them into a model size. This part counts the other tensor family, the activations, and shows why they decide fit. You get the two ratios to quote in an exam (parameters fell 4.6x while peak activation rose 1.8x; in training, 4.3x against 1.1x), the reason MobileNet-style models can still fail on a microcontroller, a rule for where in a network the memory sits, and a formula for peak memory that you can apply to any layer list.

عندما يُنقل نموذج رؤية إلى UAV أو متحكم روبوت أو لوحة IoT، فإن أول سؤال يطرحه الـ compiler أو المراجع ليس "كم عدد الـ parameters" بل "ما هي ذروة الـ activation". هذه اللوحات تحمل من 256 kB إلى 512 kB من الـ SRAM ولا يوجد فيها DRAM، والـ activations، لا الأوزان، هي التي يجب أن تعيش هناك.

الجزء 02 عدّ الأوزان وحوّلها إلى حجم نموذج. هذا الجزء يعدّ العائلة الأخرى من الـ tensors، أي الـ activations، ويبيّن لماذا هي التي تقرر هل يتّسع النموذج. ستحصل على النسبتين اللتين تستشهد بهما في الامتحان (الـ parameters انخفضت 4.6x بينما ارتفعت ذروة الـ activation بـ 1.8x؛ وفي التدريب 4.3x مقابل 1.1x)، والسبب في أن نماذج على طراز MobileNet قد تفشل مع ذلك على microcontroller، وقاعدة تحدد أين تقع الذاكرة داخل الشبكة، ومعادلة لذروة الذاكرة يمكنك تطبيقها على أي قائمة طبقات.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Explain why activations, not parameters, bound memory in both CNN inference and training, quoting 4.6x against 1.8x and 4.3x against 1.1x.تشرح لماذا تحدّ الـ activations، لا الـ parameters، الذاكرة في كلٍّ من inference وtraining للـ CNN، مستشهدًا بـ 4.6x مقابل 1.8x و4.3x مقابل 1.1x.
  2. Read a per-block or per-layer memory profile and say which layers set the SRAM peak, which set the flash budget, and why resolution and channel growth cause it.تقرأ مخطط ذاكرة لكل block أو لكل طبقة وتحدد أي الطبقات تضع ذروة الـ SRAM وأيها تحدد ميزانية الـ flash، ولماذا يسبب ذلك كلٌّ من الـ resolution ونمو عدد الـ channels.
  3. Compute total and peak #activations for a given layer list using peak ≈ max over layers of input + output, and convert to bytes for a chosen bit width.تحسب العدد الكلي وعدد الذروة لـ #activations لقائمة طبقات معطاة باستخدام peak ≈ أكبر قيمة عبر الطبقات لـ input + output، وتحوّله إلى bytes حسب الـ bit width المختار.
  4. State the assumptions behind the peak approximation: layer-by-layer execution, no branches, no in-place operations, no scratch buffers.تذكر الافتراضات وراء تقريب الذروة: التنفيذ طبقة بطبقة، لا توجد branches، لا توجد عمليات in-place، ولا scratch buffers.
  5. Place weights in flash and activations in SRAM, and connect activation size to the memory term of the latency model from part 01.تضع الأوزان في الـ flash والـ activations في الـ SRAM، وتربط حجم الـ activations بحد الذاكرة في نموذج الـ latency من الجزء 01.

Take a real target: an STM32F746 microcontroller has a Cortex-M7 core, 1 MB of flash and 320 kB of SRAM. It has no DRAM and no operating system; MCUNet describes such boards as bare-metal devices (Lin et al., 2020). Now take two ImageNet classifiers that both reach about 70 percent top-1 accuracy with every value stored as an 8-bit integer. ResNet-18 needs 11.2 MB for its weights. MobileNetV2 at width 0.75 needs about 2.5 MB as MCUNet's Figure 8 labels the bar, a 4.6x reduction on the slide (strictly 11.2 / 2.5 ≈ 4.5; the paper rounds to 4.6x). If parameters were the memory story, the second model would be the obvious choice.

خذ هدفًا حقيقيًا: الـ microcontroller من نوع STM32F746 فيه نواة Cortex-M7 و1 MB من الـ flash و320 kB من الـ SRAM. لا يوجد فيه DRAM ولا نظام تشغيل؛ وورقة MCUNet تصف هذه اللوحات بأنها أجهزة bare-metal (Lin et al., 2020). الآن خذ مصنّفين على ImageNet يصل كلاهما إلى حوالي 70 percent دقة top-1 مع تخزين كل قيمة كعدد صحيح بـ 8-bit. الـ ResNet-18 يحتاج 11.2 MB لأوزانه. والـ MobileNetV2 بعرض 0.75 يحتاج حوالي 2.5 MB كما يكتبها Figure 8 في MCUNet على العمود، أي تخفيض بـ 4.6x على الشريحة (بدقة 11.2 / 2.5 ≈ 4.5؛ والورقة تقرّبها إلى 4.6x). لو كانت الـ parameters هي كل قصة الذاكرة، لكان النموذج الثاني هو الاختيار البديهي.

Then look at the other pair of bars. The largest amount of activation memory that ResNet-18 needs at any moment is 0.9 MB. For MobileNetV2-0.75 it is 1.7 MB. The efficient model is 1.8x worse on peak activations, and the MCUNet paper says so plainly: MobileNetV2 "reduces the model size by 4.6x compared to ResNet-18, but the peak activation size increases by 1.8x, making it even more difficult to fit the SRAM on microcontrollers" (Lin et al., 2020). Neither model fits 320 kB. The memory bottleneck moved without anyone shrinking it.

ثم انظر إلى الزوج الآخر من الأعمدة. أكبر كمية من ذاكرة الـ activation يحتاجها ResNet-18 في أي لحظة هي 0.9 MB. أما MobileNetV2-0.75 فيحتاج 1.7 MB. النموذج "الكفء" أسوأ بـ 1.8x في peak activations، وورقة MCUNet تقول ذلك صراحة: الـ MobileNetV2 "يقلّل حجم النموذج بـ 4.6x مقارنة بـ ResNet-18، لكن حجم ذروة الـ activation يزيد بـ 1.8x، مما يجعل الاتساع في الـ SRAM على الـ microcontrollers أصعب حتى" (Lin et al., 2020). لا يتّسع أيٌّ من النموذجين في 320 kB. الـ memory bottleneck انتقل من مكانه دون أن يقلّصه أحد.

Parameter memory drops 4.6x from ResNet-18 to MobileNetV2-0.75, but peak activation rises 1.8x. Both peak bars stand far above a 320 kB SRAM line.ذاكرة الـ parameters تنخفض 4.6x من ResNet-18 إلى MobileNetV2-0.75، لكن ذروة الـ activation ترتفع 1.8x. وكلا عمودي الذروة يقفان أعلى بكثير من خط SRAM عند 320 kB.

Two budgets, and the smaller one belongs to activationsميزانيتان، والأصغر منهما تخص الـ activations

The reason the two bars behave differently is that they are paid from different memories. Weights are read-only: they are written once when the firmware is flashed and only read during inference, so they live in flash, the larger and cheaper memory. Activations are produced by one layer and consumed by the next on every single inference, so they must sit in memory that can be written and read at speed, which on a microcontroller is the on-chip SRAM. MCUNet puts it in one sentence: "SRAM constrains the activation size (read&write); Flash constrains the model size (read-only)" (Lin et al., 2020). The model size from part 02 answers the flash question. The peak activation answers the SRAM question, and SRAM is the scarcer of the two by a factor of two to four on every board in MCUNet's list (the STM32F746 has about 3x more flash than SRAM).

السبب في أن العمودين يتصرفان بشكل مختلف هو أنهما يُدفعان من ذاكرتين مختلفتين. الأوزان للقراءة فقط: تُكتب مرة واحدة عند تحميل الـ firmware ولا تُقرأ إلا أثناء الـ inference، لذلك تعيش في الـ flash، وهي الذاكرة الأكبر والأرخص. أما الـ activations فتنتجها طبقة وتستهلكها الطبقة التالية في كل inference، لذلك يجب أن تكون في ذاكرة يمكن الكتابة فيها والقراءة منها بسرعة، وهي على الـ microcontroller الـ SRAM الموجودة على الشريحة. ورقة MCUNet تلخص ذلك في جملة واحدة: "الـ SRAM يقيّد حجم الـ activation (قراءة وكتابة)؛ والـ Flash يقيّد حجم النموذج (قراءة فقط)" (Lin et al., 2020). الـ model size من الجزء 02 يجيب عن سؤال الـ flash. وذروة الـ activation تجيب عن سؤال الـ SRAM، والـ SRAM هي الأندر بين الاثنتين بعامل يتراوح بين اثنين وأربعة في كل لوحة في قائمة MCUNet (الـ STM32F746 فيه flash أكثر من الـ SRAM بحوالي 3x).

This also explains why bit width alone does not rescue MobileNetV2. The slide already assumes 8-bit integers for both weights and activations. Even after that 4x shrink from 32-bit floats, the peak sits at 1.7 MB. MCUNet's Table 1 reports 1.7 MB for full-width int8 MobileNetV2 (6.8 MB in fp32), 5.3x over the 320 kB of an STM32F746, not counting im2col or other runtime buffers. Quantization scales both bars by the same factor; it does not change which bar is the problem.

وهذا يفسر أيضًا لماذا لا يُنقذ الـ bit width وحده الـ MobileNetV2. الشريحة تفترض أصلًا أعدادًا صحيحة بـ 8-bit للأوزان والـ activations معًا. وحتى بعد هذا التقليص بـ 4x من الـ 32-bit floats، تبقى الذروة عند 1.7 MB. الـ Table 1 في MCUNet يذكر 1.7 MB لـ MobileNetV2 بالعرض الكامل وبـ int8 (و6.8 MB بـ fp32)، أي 5.3x فوق الـ 320 kB في STM32F746، دون احتساب im2col أو أي buffers أخرى للـ runtime. الـ quantization يقلّص العمودين بالعامل نفسه؛ ولا يغيّر أي العمودين هو المشكلة.

ModelالنموذجParam memory (flash)ذاكرة الـ parameters (flash)Peak activation (SRAM)ذروة الـ activation (SRAM)Fits STM32F746 (1 MB flash, 320 kB SRAM)?هل يتّسع في STM32F746 (1 MB flash، 320 kB SRAM)؟Fits STM32H743 (2 MB flash, 512 kB SRAM)?هل يتّسع في STM32H743 (2 MB flash، 512 kB SRAM)؟
ResNet-18ResNet-1811.2 MB11.2 MB0.9 MB0.9 MBNo (flash and SRAM)لا (flash وSRAM)No (flash and SRAM)لا (flash وSRAM)
MobileNetV2-0.75MobileNetV2-0.752.5 MB2.5 MB1.7 MB1.7 MBNo (flash and SRAM)لا (flash وSRAM)No (flash and SRAM)لا (flash وSRAM)
MCUNet (paper, Figure 8)MCUNet (الورقة، Figure 8)1.9 MB1.9 MB0.49 MB0.49 MBNo (flash and SRAM)لا (flash وSRAM)Yesنعم
Two models at about 70 percent ImageNet top-1, int8, against two MCUNet target boardsنموذجان عند حوالي 70 بالمئة top-1 على ImageNet وبـ int8، مقابل لوحتين مستهدفتين من MCUNet

The MCUNet row comes from Figure 8 of the same paper and is there to show what solving the right problem looks like: its designers shrank the peak activation below 0.5 MB rather than chasing a smaller parameter count, and only then did an ImageNet model fit a 512 kB board. The 4.6x model is not an efficient model for this class of device; it is an efficient model for a phone, where DRAM is plentiful and flash is not the constraint.

صف MCUNet مأخوذ من Figure 8 في الورقة نفسها، وهو موجود ليُظهر شكل حل المشكلة الصحيحة: مصمموه قلّصوا ذروة الـ activation إلى أقل من 0.5 MB بدل مطاردة عدد أصغر من الـ parameters، وعندها فقط اتّسع نموذج ImageNet في لوحة بـ 512 kB. النموذج صاحب الـ 4.6x ليس نموذجًا كفؤًا لهذه الفئة من الأجهزة؛ بل هو نموذج كفء للهاتف، حيث الـ DRAM وفيرة والـ flash ليس هو القيد.

Why MobileNetV2 in particular: an imbalanced profileلماذا MobileNetV2 تحديدًا: مخطط ذاكرة غير متوازن

The right-hand chart explains where MobileNetV2's peak comes from. Plot the memory that each of its eighteen blocks needs and the profile is wildly uneven: block 2 needs 1372 kB, blocks 0, 1, 3 and 4 sit between roughly 500 kB and 600 kB, and the remaining thirteen blocks mostly need 40 kB to 170 kB. Draw the 256 kB constraint of a typical microcontroller and exactly five blocks cross it. MCUNetV2, the follow-up paper that this chart comes from, states it directly: "The first 5 blocks have large peak memory, exceeding the memory constraints of MCUs, while the remaining 13 blocks easily fit 256kB memory constraints. The third block has 8x larger memory usage than the rest of the network" (Lin et al., 2021).

المخطط على اليمين يشرح من أين تأتي ذروة MobileNetV2. ارسم الذاكرة التي يحتاجها كل block من الـ 18 block وستجد المخطط متفاوتًا بشدة: block 2 يحتاج 1372 kB، والـ blocks 0, 1, 3 و4 تقع بين حوالي 500 kB و600 kB، والـ blocks الثلاثة عشر الباقية تحتاج في الغالب من 40 kB إلى 170 kB. ارسم 256 kB constraint الخاص بـ microcontroller نموذجي وستجد أن خمسة blocks بالضبط تتجاوزه. ورقة MCUNetV2، وهي الورقة اللاحقة التي جاء منها هذا المخطط، تقولها مباشرة: "أول 5 blocks لها ذروة ذاكرة كبيرة تتجاوز قيود الذاكرة في الـ MCUs، بينما الـ 13 block الباقية تتسع بسهولة في قيد ذاكرة 256kB. الـ block الثالث يستخدم ذاكرة أكبر بـ 8x من بقية الشبكة" (Lin et al., 2021).

Eighteen MobileNetV2 blocks against a 256 kB line: the first five stand above it, with block 2 at 1372 kB, while thirteen stay below.ثمانية عشر block من MobileNetV2 مقابل خط عند 256 kB: أول خمسة تقف فوقه، وblock 2 عند 1372 kB، بينما تبقى ثلاثة عشر تحته.

The two charts on the slide come from different papers with different memory accounting: the left bars are MCUNet's (2020) figures for MobileNetV2-0.75, while the right chart is MCUNetV2's analytic input-plus-output count for full-width MobileNetV2 in int8, so the numbers are not directly comparable. Its block 2 peak is 16×112×112 + 96×112×112 = 1,404,928 B, which is 1372 kB in 1024-byte kilobytes.

المخططان على الشريحة يأتيان من ورقتين مختلفتين بطريقتين مختلفتين في حساب الذاكرة: الأعمدة على اليسار هي أرقام MCUNet (2020) لـ MobileNetV2-0.75، بينما المخطط على اليمين هو عدّ تحليلي لـ input زائد output من MCUNetV2 لـ MobileNetV2 بالعرض الكامل وبـ int8، لذلك الأرقام غير قابلة للمقارنة المباشرة. ذروة block 2 فيه هي 16×112×112 + 96×112×112 = 1,404,928 B، وهذا يساوي 1372 kB إذا كان الكيلوبايت 1024 byte.

The cause is the shape of MobileNetV2's inverted residual block. Each block first expands the channel count about six times with a 1×1 convolution, applies a depthwise 3×3, and projects back down. The depthwise step is what makes the block cheap in weights and MACs, exactly as part 02 computed. But the expanded tensor exists at full spatial resolution, and in the early blocks that resolution is 112×112 or 56×56. Six times the channels at a large H×W gives a huge activation in a block that owns almost no parameters. MCUNet's per-block peak memory analysis (Figure 11) measures the same imbalance on a MobileNetV2 scaled to 0.3x width to fit 320 kB: even then one block has 2.2x the peak activation of the average block (Lin et al., 2020). A model can be light in weights and heavy in activations at the same time, and this architecture is the canonical example.

السبب هو شكل الـ inverted residual block في MobileNetV2. كل block يوسّع أولًا عدد الـ channels حوالي ست مرات بـ convolution من نوع 1×1، ثم يطبق depthwise 3×3، ثم يُسقط العدد مرة أخرى إلى الأسفل. خطوة الـ depthwise هي ما يجعل الـ block رخيصًا في الأوزان والـ MACs، تمامًا كما حسب الجزء 02. لكن الـ tensor الموسَّع يوجد بالـ resolution المكانية الكاملة، وفي الـ blocks الأولى تكون هذه الـ resolution 112×112 أو 56×56. ست مرات عدد الـ channels عند H×W كبير تعطي activation ضخمًا في block لا يملك تقريبًا أي parameters. تحليل ذروة الذاكرة لكل block في MCUNet (Figure 11) يقيس عدم التوازن نفسه على MobileNetV2 مصغَّر إلى عرض 0.3x ليتّسع في 320 kB: حتى في هذه الحالة يملك block واحد ذروة activation تساوي 2.2x من ذروة الـ block المتوسط (Lin et al., 2020). يمكن لنموذج أن يكون خفيفًا في الأوزان وثقيلًا في الـ activations في الوقت نفسه، وهذه البنية هي المثال المعياري على ذلك.

Quick checkاختبار سريع

At about 70 percent ImageNet top-1 with 8-bit values, what happens to peak activation memory going from ResNet-18 to MobileNetV2-0.75?عند حوالي 70 بالمئة top-1 على ImageNet وبقيم 8-bit، ماذا يحدث لذاكرة ذروة الـ activation عند الانتقال من ResNet-18 إلى MobileNetV2-0.75؟

Recall

Why is MobileNetV2 not microcontroller friendly even though it has few parameters?لماذا لا يناسب MobileNetV2 الـ microcontrollers رغم أن عدد الـ parameters فيه قليل؟

Its early inverted residual blocks expand channels about six times while the feature map is still at high resolution, so block 2 needs 1372 kB and the first five blocks all exceed 256 kB. The binding budget on a microcontroller is SRAM, which holds activations, not flash, which holds the weights.
الـ inverted residual blocks الأولى فيه توسّع الـ channels حوالي ست مرات بينما الـ feature map لا تزال بـ resolution عالية، لذلك يحتاج block 2 إلى 1372 kB وتتجاوز أول خمسة blocks كلها 256 kB. الميزانية الحاكمة على الـ microcontroller هي الـ SRAM التي تحمل الـ activations، لا الـ flash الذي يحمل الأوزان.

Recall

Where do weights and activations live on a microcontroller at run time, and which one sets the peak?أين تعيش الأوزان والـ activations على الـ microcontroller وقت التشغيل، وأيهما يحدد الذروة؟

Weights live in flash (read only, written once). Activations live in SRAM (written and read every inference). The largest input plus output pair sets the SRAM peak.
الأوزان تعيش في الـ flash (قراءة فقط، تُكتب مرة واحدة). والـ activations تعيش في الـ SRAM (تُكتب وتُقرأ في كل inference). وأكبر زوج input زائد output هو الذي يحدد ذروة الـ SRAM.

Inference keeps a few activations alive; training keeps all of them. Switch to 32-bit floats and a training step of ResNet-50. Its weights take 102 MB. Its activations, for one mini-batch, take 707 MB, about 7x more. Now swap in MobileNetV2 at width 1.4: the weights fall to about 24 MB, a 4.3x reduction, but the activations fall only to about 626 MB, a 1.1x reduction. The TinyTL paper that produced these numbers concludes: "It is the activation that bottlenecks the training memory, not the parameters" (Cai et al., 2020).

الـ inference يُبقي عددًا قليلًا من الـ activations حيًّا؛ أما التدريب فيُبقيها كلها. انتقل إلى 32-bit floats وإلى خطوة تدريب لـ ResNet-50. أوزانه تأخذ 102 MB. والـ activations الخاصة به، لـ mini-batch واحد، تأخذ 707 MB، أي حوالي 7x أكثر. الآن ضع مكانه MobileNetV2 بعرض 1.4: الأوزان تنخفض إلى حوالي 24 MB، أي تخفيض بـ 4.3x، لكن الـ activations تنخفض فقط إلى حوالي 626 MB، أي تخفيض بـ 1.1x. ورقة TinyTL التي أنتجت هذه الأرقام تستنتج: "الـ activation هو ما يخنق ذاكرة التدريب، لا الـ parameters" (Cai et al., 2020).

ModelالنموذجParameters (MB)الـ parameters (MB)Activations (MB)الـ activations (MB)Activations ÷ parametersالـ activations ÷ الـ parameters
ResNet-50ResNet-50102102707707about 7xحوالي 7x
MobileNetV2-1.4MobileNetV2-1.4about 24حوالي 24about 626حوالي 626about 26xحوالي 26x
Reductionالتخفيض4.3x4.3x1.1x1.1x
Training memory per mini-batch, fp32 (slide values, batch 8; TinyTL Figure 1 shows exactly double at batch 16)ذاكرة التدريب لكل mini-batch بـ fp32 (قيم الشريحة عند batch 8؛ و Figure 1 في TinyTL يُظهر الضعف بالضبط عند batch 16)

The mechanism is backpropagation itself. Write one linear layer as a_(i+1) = a_i W + b, where a_i is the layer's input activation. To update the weight you need the gradient of the loss with respect to W, and that gradient is the outer product of the input activation with the gradient flowing back from the next layer.

الآلية هي الـ backpropagation نفسها. اكتب طبقة خطية واحدة على الشكل a_(i+1) = a_i W + b، حيث a_i هو الـ input activation للطبقة. لتحديث الوزن تحتاج إلى gradient الـ loss بالنسبة لـ W، وهذا الـ gradient هو الـ outer product للـ input activation مع الـ gradient العائد من الطبقة التالية.

∂L∂Wi=aiT ∂L∂ai+1,∂L∂bi=∂L∂ai+1\frac{\partial L}{\partial W_i} = a_i^{\mathsf{T}}\,\frac{\partial L}{\partial a_{i+1}}, \qquad \frac{\partial L}{\partial b_i} = \frac{\partial L}{\partial a_{i+1}}
Backpropagation through one linear layer (TinyTL, equation 2). The weight gradient needs the stored input a_i; the bias gradient does not.الانتشار العكسي عبر طبقة خطية واحدة (TinyTL، المعادلة 2). تدرّج الوزن يحتاج المدخل المخزن a_i؛ أما تدرّج الانحياز فلا.

The forward pass computes a_1, then a_2, and so on up to the loss. The backward pass returns in reverse order, and when it reaches layer i it must still have a_i. So every layer's input has to be held from the moment it is produced until the backward pass comes back to it. Goodfellow, Bengio and Courville make the same point for a multilayer perceptron: "we need to store the input to the nonlinearity of the hidden layer. This value is stored from the time it is computed until the backward pass has returned to the same point," and the cost per layer is O(m·n_h) for a mini-batch of m examples and n_h hidden units (Goodfellow et al., 2016, section 6.5.7). Depth multiplies that cost by the number of layers, and the batch size multiplies it again.

الـ forward pass يحسب a_1، ثم a_2، وهكذا حتى الـ loss. والـ backward pass يعود بالترتيب العكسي، وعندما يصل إلى الطبقة i يجب أن يكون a_i لا يزال موجودًا. إذن يجب الاحتفاظ بـ input كل طبقة من لحظة إنتاجه حتى يعود إليه الـ backward pass. يذكر Goodfellow وBengio وCourville النقطة نفسها عن الـ multilayer perceptron: "نحتاج إلى تخزين الـ input الداخل إلى الـ nonlinearity في الطبقة المخفية. هذه القيمة تُخزَّن من وقت حسابها حتى يعود الـ backward pass إلى النقطة نفسها"، والتكلفة لكل طبقة هي O(m·n_h) لـ mini-batch من m أمثلة وn_h وحدة مخفية (Goodfellow et al., 2016, section 6.5.7). العمق يضرب هذه التكلفة في عدد الطبقات، والـ batch size يضربها مرة أخرى.

Inference slides a two-buffer window along the network. Training fills every buffer on the forward pass and only frees each one when the backward pass reads it.الـ inference يحرّك نافذة من buffer اثنين على طول الشبكة. أما التدريب فيملأ كل buffer أثناء الـ forward pass ولا يحرر أيًّا منها إلا عندما يقرؤه الـ backward pass.
ModeالوضعWhich activations are heldأي الـ activations تُحفَظHow manyكم عددهاMemory ruleقاعدة الذاكرة
Inferenceالـ inferenceInput and output of the layer running nowالـ input والـ output للطبقة التي تعمل الآنTwo tensorsاثنان من الـ tensorsLargest input + output pairأكبر زوج input + output
TrainingالتدريبEvery layer input a_i until its dL/dW_i is computedكل input لطبقة a_i حتى يُحسب dL/dW_i الخاص بهاAll tensors, times the batchكل الـ tensors، مضروبة في الـ batchSum of all activations × batch sizeمجموع كل الـ activations × الـ batch size
What has to be alive at onceما الذي يجب أن يكون حيًّا في وقت واحد
Mtrain≈m∑i∣ai∣⋅bytes  +  Mweights  +  Mgradients  +  MoptimizerM_{\text{train}} \approx m \sum_i |a_i| \cdot \text{bytes} \;+\; M_{\text{weights}} \;+\; M_{\text{gradients}} \;+\; M_{\text{optimizer}}
Training memory. The slide counts only the first two terms, so the true gap to inference is even larger.ذاكرة التدريب. الشريحة تحسب الحدين الأولين فقط، لذلك فالفجوة الحقيقية مقارنةً بالاستدلال أكبر من ذلك.

Worked exampleمثال محلول

AlexNet in fp32, inference against trainingAlexNet بـ fp32: الـ inference مقابل التدريب

  1. Inference peakذروة الـ inference

    The last concept derives AlexNet's peak (440,928 values) and total (932,264 values); take them as given here. At 4 bytes each, the peak is 1.76 MB of activations alive at once.
    المفهوم الأخير يشتق ذروة AlexNet (440,928 قيمة) والعدد الكلي (932,264 قيمة)؛ خذهما هنا كمعطيات. بـ 4 bytes لكل قيمة، تكون الذروة 1.76 MB من الـ activations الحية في وقت واحد.
  2. Training, batch of oneالتدريب بـ batch من مثال واحد

    All 932,264 activations are held for the backward pass: 932,264 × 4 B = 3.73 MB, more than double the inference peak before counting gradients.
    كل الـ 932,264 activations تُحفظ من أجل الـ backward pass: 932,264 × 4 B = 3.73 MB، أي أكثر من ضعف ذروة الـ inference قبل احتساب الـ gradients.
  3. Training, batch of sixteenالتدريب بـ batch من ستة عشر مثالًا

    Activations scale with the batch: 16 × 3.73 MB ≈ 60 MB. The weights do not scale; AlexNet's 61M parameters stay at 244 MB whatever the batch, and their gradients add another 244 MB.
    الـ activations تتضاعف مع الـ batch: 16 × 3.73 MB ≈ 60 MB. أما الأوزان فلا تتضاعف؛ الـ 61M parameter في AlexNet تبقى عند 244 MB مهما كان الـ batch، والـ gradients الخاصة بها تضيف 244 MB أخرى.
  4. Why the slide's bars look the way they doلماذا تبدو أعمدة الشريحة بهذا الشكل

    AlexNet is unusual: its huge fully connected layers make weights dominate even in training. ResNet-50 and MobileNetV2 are mostly convolutions with large feature maps, so at the slide's batch of 8 their activations (707 MB and about 626 MB) dwarf their weights (102 MB and 24 MB).
    AlexNet حالة غير معتادة: طبقاته الـ fully connected الضخمة تجعل الأوزان هي المسيطرة حتى في التدريب. أما ResNet-50 وMobileNetV2 فمعظمهما convolutions بـ feature maps كبيرة، لذلك عند الـ batch المستخدم في الشريحة وهو 8 تطغى الـ activations (707 MB وحوالي 626 MB) على الأوزان (102 MB و24 MB).

The formula also points to the cure that TinyTL proposes and that this course returns to when it reaches on-device learning. The bias gradient in equation 2 needs no stored activation at all. If you freeze the weights and update only biases (plus a small added module), the a_i tensors never have to be kept, and the 707 MB term collapses. That is a memory argument, not an accuracy argument, and it is only visible once you have separated activations from parameters as this slide does. It matters doubly on a microcontroller, because TinyTL notes that DRAM access consumes two orders of magnitude more energy than on-chip SRAM access (Cai et al., 2020), echoing the Horowitz numbers from part 01.

المعادلة تشير أيضًا إلى العلاج الذي تقترحه TinyTL والذي يعود إليه هذا المقرر عندما يصل إلى التعلم على الجهاز (on-device learning). gradient الـ bias في equation 2 لا يحتاج إلى أي activation مخزّن إطلاقًا. إذا جمّدت الأوزان وحدّثت الـ biases فقط (مع وحدة صغيرة مضافة)، فلن تحتاج أبدًا إلى الاحتفاظ بالـ tensors a_i، وينهار حد الـ 707 MB. هذه حجة ذاكرة، لا حجة دقة، ولا تظهر إلا بعد أن تفصل الـ activations عن الـ parameters كما تفعل هذه الشريحة. وتزداد أهميتها على الـ microcontroller، لأن TinyTL تذكر أن الوصول إلى الـ DRAM يستهلك طاقة أكثر برتبتين عشريتين من الوصول إلى الـ SRAM الموجودة على الشريحة (Cai et al., 2020)، وهذا صدى لـ Horowitz numbers من الجزء 01.

Quick checkاختبار سريع

Why does training need far more activation memory than inference for the same network?لماذا يحتاج التدريب إلى ذاكرة activations أكبر بكثير من الـ inference للشبكة نفسها؟

Recall

Quote the four ratios from slides 18 and 19 and say what each one compares.اذكر النسب الأربع من الشريحتين 18 و19 وقل ماذا تقارن كل واحدة منها.

4.6x less parameter memory and 1.8x more peak activation: ResNet-18 to MobileNetV2-0.75, int8, inference. 4.3x less parameter memory and 1.1x less activation memory: ResNet-50 to MobileNetV2-1.4, fp32, training at batch 8.
4.6x ذاكرة parameters أقل و1.8x ذروة activation أكبر: من ResNet-18 إلى MobileNetV2-0.75، بـ int8، في الـ inference. 4.3x ذاكرة parameters أقل و1.1x ذاكرة activation أقل: من ResNet-50 إلى MobileNetV2-1.4، بـ fp32، في التدريب عند batch 8.

Recall

Why does training need every activation while inference needs only two?لماذا يحتاج التدريب إلى كل الـ activations بينما يحتاج الـ inference إلى اثنين فقط؟

The weight gradient dL/dW_i is the product of the stored input a_i and the gradient from the next layer, so every a_i is held until the backward pass reaches it. Inference has no backward pass and frees an input as soon as the layer's output exists.
gradient الوزن dL/dW_i هو حاصل ضرب الـ input المخزَّن a_i في الـ gradient القادم من الطبقة التالية، لذلك يُحتفظ بكل a_i حتى يصل إليه الـ backward pass. أما الـ inference فليس فيه backward pass، ويحرر الـ input بمجرد أن يوجد output الطبقة.

Plot, layer by layer, how much memory the activations and the weights of an efficient network take, and a clear shape appears. For MCUNet, a network designed for microcontrollers, the first six layers need 50 kB to 75 kB of activation memory and almost no weight memory, and activation stays the dominant term, mostly 20 kB to 55 kB, out to about layer 17. Around layers 18 to 30 both are small, under about 15 kB. From layer 32 onward the weight memory climbs to 35 kB to 75 kB while activations stay near 5 kB to 10 kB. The total traces a U: expensive at both ends, cheap in the middle, for two different reasons. The chart comes from MCUNet's on-device training work and shows the memory needed to update each layer, which is the stored input activation plus the weight and its gradient. The same geometry governs inference, as the AlexNet arithmetic below shows.

ارسم، طبقة بطبقة، مقدار الذاكرة الذي تأخذه الـ activations والأوزان في شبكة كفؤة، وسيظهر شكل واضح. في MCUNet، وهي شبكة مصممة للـ microcontrollers، تحتاج أول ست طبقات من 50 kB إلى 75 kB من ذاكرة الـ activation وتقريبًا لا شيء من ذاكرة الأوزان، ويبقى الـ activation هو الحد المسيطر، غالبًا بين 20 kB و55 kB، حتى حوالي الطبقة 17. وحول الطبقات 18 إلى 30 يكون الاثنان صغيرين، تحت حوالي 15 kB. ومن الطبقة 32 فصاعدًا ترتفع ذاكرة الأوزان إلى 35 kB حتى 75 kB بينما تبقى الـ activations قرب 5 kB إلى 10 kB. المجموع يرسم حرف U: مكلف في الطرفين ورخيص في المنتصف، لسببين مختلفين. المخطط مأخوذ من عمل MCUNet على التدريب على الجهاز ويعرض الذاكرة اللازمة لتحديث كل طبقة، وهي الـ input activation المخزّن زائد الوزن والـ gradient الخاص به. والهندسة نفسها تحكم الـ inference، كما تبيّن حسابات AlexNet أدناه.

LayersالطبقاتActivation memoryذاكرة الـ activationWeight memoryذاكرة الأوزانDominantالمهيمن
0 to 50 إلى 550 to 75 kB50 إلى 75 kBabout 0حوالي 0Activationالـ activation
6 to 176 إلى 1720 to 55 kB20 إلى 55 kBabout 0حوالي 0Activationالـ activation
18 to 3018 إلى 30under 15 kBأقل من 15 kBunder 15 kBأقل من 15 kBNeitherلا أحد منهما
32 to 4232 إلى 425 to 10 kB5 إلى 10 kB35 to 75 kB35 إلى 75 kBWeightالأوزان
The U in one table: which tensor dominates each band of the MCUNet profileحرف U في جدول واحد: أي tensor يهيمن على كل نطاق في مخطط MCUNet
Activation memory (amber) decays across the early layers while weight memory (teal) climbs across the late ones; the dashed U is their sum.ذاكرة الـ activation (بالكهرماني) تتناقص عبر الطبقات الأولى بينما ترتفع ذاكرة الأوزان (بالأزرق المخضر) عبر الطبقات الأخيرة؛ وحرف U المتقطع هو مجموعهما.

The two counting rules from part 02 predict this. An activation tensor holds C × H × W values. A convolution weight tensor holds C_o × C_i × k_h × k_w values. Now follow a CNN from input to output. At every downsampling stage the spatial size halves in each direction, so H × W drops by 4x, while the channel count typically only doubles. The activation count C × H × W therefore falls by about 2x per stage. The weight count contains C_o × C_i, which grows by about 4x when channels double and does not care about H and W at all. MCUNetV2 states the consequence: "the memory bottleneck tends to appear at the early stage of the network" because resolution shrinks faster than channels grow (Lin et al., 2021).

قاعدتا العدّ من الجزء 02 تتنبآن بهذا. الـ activation tensor يحمل C × H × W قيمة. والـ weight tensor في الـ convolution يحمل C_o × C_i × k_h × k_w قيمة. الآن تتبّع CNN من الـ input إلى الـ output. في كل مرحلة downsampling يتنصّف الـ spatial size في كل اتجاه، فينخفض H × W بـ 4x، بينما يتضاعف عدد الـ channels عادةً مرة واحدة فقط. لذلك ينخفض عدد الـ activations C × H × W بحوالي 2x في كل مرحلة. أما عدد الأوزان فيحتوي على C_o × C_i، الذي ينمو بحوالي 4x عندما تتضاعف الـ channels ولا يهتم بـ H وW إطلاقًا. ورقة MCUNetV2 تذكر النتيجة: "الـ memory bottleneck يميل إلى الظهور في المرحلة المبكرة من الشبكة" لأن الـ resolution تتقلص أسرع مما تنمو الـ channels (Lin et al., 2021).

The same U in numbers you already computedحرف U نفسه بأرقام حسبتها من قبل

You do not have to trust the chart. Take the AlexNet layers from part 02, put each layer's output activation count next to its weight count, and the profile reproduces itself from arithmetic.

لست مضطرًا للثقة في المخطط. خذ طبقات AlexNet من الجزء 02، وضع عدد الـ output activations لكل طبقة بجانب عدد أوزانها، وسيعيد المخطط إنتاج نفسه من الحساب.

LayerالطبقةOutput C×H×Wالـ output بصيغة C×H×WActivationsالـ activationsWeightsالأوزانWhich dominatesأيهما يهيمن
conv1conv196×55×5596×55×55290,400290,40034,84834,848Activations, 8xالـ activations، 8x
conv2conv2256×27×27256×27×27186,624186,624307,200307,200Weights, 1.6xالأوزان، 1.6x
conv3conv3384×13×13384×13×1364,89664,896884,736884,736Weights, 14xالأوزان، 14x
conv4conv4384×13×13384×13×1364,89664,896663,552663,552Weights, 10xالأوزان، 10x
conv5conv5256×13×13256×13×1343,26443,264442,368442,368Weights, 10xالأوزان، 10x
fc6fc6409640964,0964,09637,748,73637,748,736Weights, 9,216xالأوزان، 9,216x
fc7fc7409640964,0964,09616,777,21616,777,216Weights, 4,096xالأوزان، 4,096x
fc8fc8100010001,0001,0004,096,0004,096,000Weights, 4,096xالأوزان، 4,096x
AlexNet per layer: output activations against weights (biases ignored, as in this lecture)AlexNet لكل طبقة: الـ output activations مقابل الأوزان (مع تجاهل الـ biases كما في هذه المحاضرة)

The first convolution has 8x more activations than weights: a 55×55 map with 96 channels is large, and an 11×11 filter over only 3 input channels is small. By conv3 the map is 13×13 and the weights are already 14x the activations. The fully connected layers are the extreme: fc6 emits 4,096 values and owns 37.7M weights. Nothing about AlexNet was designed to show this; it is the geometry of every CNN.

الـ convolution الأولى فيها activations أكثر من الأوزان بـ 8x: خريطة 55×55 بـ 96 channel كبيرة، وفلتر 11×11 على 3 input channels فقط صغير. وعند conv3 تكون الخريطة 13×13 والأوزان أصبحت بالفعل 14x من الـ activations. والطبقات الـ fully connected هي الحالة القصوى: fc6 تُخرج 4,096 قيمة وتملك 37.7M وزنًا. لم يُصمَّم شيء في AlexNet ليُظهر هذا؛ إنها هندسة كل CNN.

What each end of the U decidesما الذي يقرره كل طرف من طرفي الـ U

  • The early, activation-heavy stage sets the SRAM peak. That is why MobileNetV2's problem in the previous concept was in blocks 0 to 4, and why MCUNetV2 runs only that stage in patches.المرحلة المبكرة، الثقيلة بالـ activations، هي التي تحدد ذروة الـ SRAM. ولهذا كانت مشكلة MobileNetV2 في المفهوم السابق في الـ blocks من 0 إلى 4، ولهذا يشغّل MCUNetV2 تلك المرحلة وحدها على شكل patches.
  • The late, weight-heavy stage sets the flash budget and most of the parameter count. Pruning and quantization pay off most there.المرحلة المتأخرة، الثقيلة بالأوزان، هي التي تحدد flash budget ومعظم parameter count. والـ pruning والـ quantization يؤتيان أكبر ثمارهما هناك.
  • The cheap middle is where on-device training is affordable: updating a middle layer needs neither a large stored activation nor a large weight gradient. This band is exactly what the training chart above measures, and the paper it comes from exploits exactly that band (Lin et al., 2022).المنتصف الرخيص هو حيث يصبح التدريب على الجهاز ممكنًا من حيث التكلفة: تحديث طبقة في المنتصف لا يحتاج إلى activation مخزّن كبير ولا إلى gradient وزن كبير. وهذا النطاق هو بالضبط ما يقيسه مخطط التدريب أعلاه، والورقة التي جاء منها تستغل هذا النطاق تحديدًا (Lin et al., 2022).

Quick checkاختبار سريع

In the MCUNet per-layer profile, which memory dominates the last layers, and why?في مخطط MCUNet لكل طبقة، أي ذاكرة تهيمن على الطبقات الأخيرة، ولماذا؟

Recall

In one sentence each: why is activation memory high in early layers, and why is weight memory high in late layers?بجملة واحدة لكلٍّ منهما: لماذا تكون ذاكرة الـ activation عالية في الطبقات الأولى، ولماذا تكون ذاكرة الأوزان عالية في الطبقات الأخيرة؟

Early layers keep a large H × W with few channels, so C × H × W is large. Late layers have many input and output channels, so C_o × C_i × k_h × k_w is large while the map has shrunk to a few pixels.
الطبقات الأولى تحتفظ بـ H × W كبير مع عدد قليل من الـ channels، فيكون C × H × W كبيرًا. والطبقات الأخيرة فيها عدد كبير من الـ input والـ output channels، فيكون C_o × C_i × k_h × k_w كبيرًا بينما تقلّصت الخريطة إلى بضع pixels.

Now make the peak a number you can compute. AlexNet takes a 3×224×224 image: 150,528 values. Its first convolution emits 96×55×55 = 290,400 values. While that convolution runs, both tensors must exist, because the layer is still reading the image as it writes the output: 150,528 + 290,400 = 440,928. When the first pooling layer runs, the image is no longer needed and can be freed, but the convolution output (290,400) and the pooling output (69,984) are both alive: 360,384. Memory rises and falls one layer at a time.

الآن اجعل الذروة رقمًا يمكنك حسابه. AlexNet يأخذ صورة 3×224×224: أي 150,528 قيمة. والـ convolution الأولى فيه تُخرج 96×55×55 = 290,400 قيمة. أثناء تشغيل هذه الـ convolution يجب أن يوجد الـ tensor الاثنان، لأن الطبقة لا تزال تقرأ الصورة وهي تكتب الـ output: 150,528 + 290,400 = 440,928. وعندما تعمل طبقة الـ pooling الأولى، لم تعد الصورة مطلوبة ويمكن تحريرها، لكن output الـ convolution (290,400) وoutput الـ pooling (69,984) كلاهما حي: 360,384. الذاكرة ترتفع وتنخفض طبقة واحدة في كل مرة.

Two counts fall out of this picture. The total number of activations is the sum over the input and every layer output, which for AlexNet is 932,264. The peak number of activations is the largest number alive at any one moment. For a plain chain of layers executed one at a time, that is the largest input plus output pair. MCUNetV2 gives the rule and the reason: "the memory required for a layer is the sum of input and output activation (since weights can be partially fetched from Flash)", and the runtime "allocates the input and output activation buffer in SRAM, and releases the input buffer after the whole layer computation is finished" (Lin et al., 2021).

من هذه الصورة يخرج عدّان. الـ total number of activations هو المجموع على الـ input وكل output لكل طبقة، وهو في AlexNet 932,264. والـ peak number of activations هو أكبر عدد حي في أي لحظة واحدة. في سلسلة بسيطة من الطبقات تُنفَّذ واحدة تلو الأخرى، هذا هو أكبر زوج input زائد output. ورقة MCUNetV2 تعطي القاعدة والسبب: "الذاكرة المطلوبة لطبقة هي مجموع الـ input activation والـ output activation (لأن الأوزان يمكن جلبها جزئيًا من الـ Flash)"، والـ runtime "يحجز buffer الـ input activation والـ output activation في الـ SRAM، ويحرر buffer الـ input بعد انتهاء حساب الطبقة كاملة" (Lin et al., 2021).

#acttotal=∣x0∣+∑i=1L∣ai∣\#\text{act}_{\text{total}} = |x_0| + \sum_{i=1}^{L} |a_i|
Total activations: the input plus every layer outputإجمالي التنشيطات: المدخل مضافًا إليه مخرج كل طبقة
#actpeak≈max⁡i(∣ai−1∣+∣ai∣)\#\text{act}_{\text{peak}} \approx \max_{i}\big(|a_{i-1}| + |a_i|\big)
Peak activations: the largest input plus output pair over all layersذروة التنشيطات: أكبر زوج من المدخل والمخرج عبر كل الطبقات

The approximation sign carries four assumptions, and an exam answer should name at least one of them. The network runs layer by layer, with no two layers in flight together. Weights are streamed from flash and are not counted in SRAM. There are no residual branches; if there are, MCUNetV2 says to add the memory of all branches alive at the same time, counting a shared input once. And there are no in-place tricks: MCUNet shows that a depthwise layer can overwrite its input channel by channel, needing N + 1 instead of 2N values (Lin et al., 2020). Real runtimes also need scratch buffers for im2col or accumulation, which push the true peak slightly above the formula.

علامة التقريب تحمل أربعة افتراضات، وإجابة الامتحان يجب أن تذكر واحدًا منها على الأقل. الشبكة تعمل طبقة بطبقة، دون أن تعمل طبقتان في الوقت نفسه. الأوزان تُجلب تدريجيًا من الـ flash ولا تُحسب في الـ SRAM. لا توجد residual branches؛ وإن وُجدت، يقول MCUNetV2 أن تجمع ذاكرة كل الـ branches الحية في الوقت نفسه، مع احتساب الـ input المشترك مرة واحدة. ولا توجد حيل in-place: يبيّن MCUNet أن طبقة depthwise يمكنها الكتابة فوق الـ input الخاص بها channel بعد channel، فتحتاج N + 1 بدل 2N قيمة (Lin et al., 2020). والـ runtimes الحقيقية تحتاج أيضًا إلى scratch buffers للـ im2col أو للتجميع (accumulation)، مما يدفع الذروة الحقيقية قليلًا فوق المعادلة.

Sweep through AlexNet yourself before reading the worked example. Watch which two bars are highlighted, how the running peak stops moving after the first layer, and how the bit width and the SRAM preset change whether the same 440,928 values fit.

مُرّ على AlexNet بنفسك قبل قراءة المثال المحلول. لاحظ أي عمودين يُبرزان، وكيف تتوقف الذروة الجارية عن التحرك بعد الطبقة الأولى، وكيف يغيّر الـ bit width والإعداد المسبق للـ SRAM ما إذا كانت الـ 440,928 قيمة نفسها تتّسع أم لا.

SimulatorمحاكيPeak memory sweeper: AlexNet, one layer at a timeماسح ذروة الذاكرة: AlexNet، طبقة في كل مرة
image150,528conv1290,400pool1conv2pool2conv3conv4conv5pool3fc6fc7fc8256 kB SRAM290.4 kB0
Live now (input + output)في الذاكرة الآن (الدخل + الخرج)440,928elements150,528 + 290,400 = 440.9 kB, over the 256 kB limitيتجاوز حد 256 kB
Running peakالذروة حتى الآن440,928elements440.9 kB at conv1; whole network peaks at 440.9 kB440.9 kB عند conv1؛ وتبلغ ذروة الشبكة كلها 440.9 kB
Total so farالمجموع حتى الآن440,928elementsevery tensor produced so far; all 12 sum to 932,264 = 932.3 kBكل موتر أُنتج حتى الآن؛ ومجموع الموترات الـ12 كلها 932,264 = 932.3 kB
Layer executingالطبقة المنفَّذةInputالمُدخلOutputالمُخرجLiveالحيةAt 8-bitعند 8-bit
150,528290,400440,928440.9 kB
290,40069,984360,384360.4 kB
69,984186,624256,608256.6 kB
186,62443,264229,888229.9 kB
43,26464,896108,160108.2 kB
64,89664,896129,792129.8 kB
64,89643,264108,160108.2 kB
43,2649,21652,48052.5 kB
9,2164,09613,31213.3 kB
4,0964,0968,1928.2 kB
4,0961,0005,0965.1 kB

Each bar is one tensor at 8-bit, linear scale; the dashed window holds the executing layer's input and output. Bytes are elements × bits ÷ 8 and kB is decimal (÷ 1000), as on slide 17. Selected preset: STM32F412, Cortex-M4. Weights are not counted here because on a microcontroller they stay in flash and are streamed in, so SRAM only has to hold the two live activation buffers.كل شريط هو موتر واحد بدقة 8-bit على مقياس خطي؛ والنافذة المتقطعة تحيط بمُدخل الطبقة المنفَّذة ومُخرجها. البايتات تساوي elements × bits ÷ 8، والكيلوبايت kB عشري (÷ 1000) كما في الشريحة 17. الإعداد المختار: STM32F412, Cortex-M4. لا تُحسب الأوزان هنا لأنها في المتحكم الدقيق تبقى في ذاكرة flash وتُبث منها عند الحاجة، فلا يلزم SRAM إلا أن تحمل مخزنَي التنشيطات الحيين.

Worked exampleمثال محلول

Total and peak activations of AlexNetالعدد الكلي وذروة الـ activations في AlexNet

  1. Write the C × H × W columnاكتب عمود C × H × W

    150,528; 290,400; 69,984; 186,624; 43,264; 64,896; 64,896; 43,264; 9,216; 4,096; 4,096; 1,000, from the image through fc8. Each entry is channels × height × width, or just the channel count for a linear layer.
    150,528; 290,400; 69,984; 186,624; 43,264; 64,896; 64,896; 43,264; 9,216; 4,096; 4,096; 1,000، من الصورة حتى fc8. كل قيمة هي channels × height × width، أو عدد الـ channels فقط في حالة linear layer.
  2. Sum for the totalاجمع لتحصل على العدد الكلي

    932,264 values, matching the slide. This is the number that training would have to hold.
    932,264 قيمة، مطابقة للشريحة. هذا هو العدد الذي سيتعين على التدريب الاحتفاظ به.
  3. Form the eleven adjacent sumsكوّن المجاميع الأحد عشر للأزواج المتجاورة

    Input + output while each layer executesالـ input + الـ output أثناء تنفيذ كل طبقة

    conv1 runs (image + conv1 out)تشغيل conv1 (الصورة + output الـ conv1)
    150,528 + 290,400 = 440,928150,528 + 290,400 = 440,928
    pool1 runs (conv1 out + pool1 out)تشغيل pool1 (output الـ conv1 + output الـ pool1)
    290,400 + 69,984 = 360,384290,400 + 69,984 = 360,384
    conv2 runs (pool1 out + conv2 out)تشغيل conv2 (output الـ pool1 + output الـ conv2)
    69,984 + 186,624 = 256,60869,984 + 186,624 = 256,608
    pool2 runs (conv2 out + pool2 out)تشغيل pool2 (output الـ conv2 + output الـ pool2)
    186,624 + 43,264 = 229,888186,624 + 43,264 = 229,888
    conv3 runs (pool2 out + conv3 out)تشغيل conv3 (output الـ pool2 + output الـ conv3)
    43,264 + 64,896 = 108,16043,264 + 64,896 = 108,160
    conv4 runs (conv3 out + conv4 out)تشغيل conv4 (output الـ conv3 + output الـ conv4)
    64,896 + 64,896 = 129,79264,896 + 64,896 = 129,792
    conv5 runs (conv4 out + conv5 out)تشغيل conv5 (output الـ conv4 + output الـ conv5)
    64,896 + 43,264 = 108,16064,896 + 43,264 = 108,160
    pool3 runs (conv5 out + pool3 out)تشغيل pool3 (output الـ conv5 + output الـ pool3)
    43,264 + 9,216 = 52,48043,264 + 9,216 = 52,480
    fc6 runs (pool3 out + fc6 out)تشغيل fc6 (output الـ pool3 + output الـ fc6)
    9,216 + 4,096 = 13,3129,216 + 4,096 = 13,312
    fc7 runs (fc6 out + fc7 out)تشغيل fc7 (output الـ fc6 + output الـ fc7)
    4,096 + 4,096 = 8,1924,096 + 4,096 = 8,192
    fc8 runs (fc7 out + fc8 out)تشغيل fc8 (output الـ fc7 + output الـ fc8)
    4,096 + 1,000 = 5,0964,096 + 1,000 = 5,096
  4. Pick the maximumاختر القيمة العظمى

    The first row wins: 440,928 while conv1 runs. The runner-up, conv1 output plus pool1 output, is 360,384, and by conv3 the pairs are a quarter of the peak.
    الصف الأول هو الفائز: 440,928 أثناء تشغيل conv1. والوصيف، output الـ conv1 زائد output الـ pool1، يساوي 360,384، وعند conv3 تصبح الأزواج ربع الذروة.
  5. Convert to bytes for a bit widthحوّل إلى bytes حسب الـ bit width

    AlexNet activation memory by bit width (kB decimal, KiB binary)ذاكرة الـ activations في AlexNet حسب الـ bit width (الـ kB عشري، والـ KiB ثنائي)

    Peak, 8-bit integersالذروة، أعداد صحيحة بـ 8-bit
    440,928 B ≈ 441 kB (431 KiB)440,928 B ≈ 441 kB (431 KiB)
    Peak, 32-bit floatsالذروة، floats بـ 32-bit
    1,763,712 B ≈ 1.76 MB (1.68 MiB)1,763,712 B ≈ 1.76 MB (1.68 MiB)
    Total, 8-bit integersالعدد الكلي، أعداد صحيحة بـ 8-bit
    932,264 B ≈ 932 kB (910 KiB)932,264 B ≈ 932 kB (910 KiB)
    Total, 32-bit floatsالعدد الكلي، floats بـ 32-bit
    3,729,056 B ≈ 3.73 MB (3.56 MiB)3,729,056 B ≈ 3.73 MB (3.56 MiB)
  6. Peak = 440,928 valuesالذروة = 440,928 قيمة

    441 kB at 8-bit and 1.76 MB at 32-bit. Even fully quantized, AlexNet's first layer alone exceeds a 256 kB or 320 kB microcontroller, and the peak is 47 percent of the total.
    441 kB بـ 8-bit و1.76 MB بـ 32-bit. حتى مع الـ quantization الكامل، تتجاوز الطبقة الأولى وحدها في AlexNet قدرة microcontroller بـ 256 kB أو 320 kB، والذروة تساوي 47 percent من العدد الكلي.

The same arithmetic explains why MobileNetV2 failed in the first concept. Its first convolution maps a 3×224×224 image to 32×112×112: 150,528 + 401,408 = 551,936 values, which MCUNetV2 quotes as "539kB even when quantized in int8" (using 1024-byte kilobytes). A 224 input through a standard stem can never run layer by layer on a 256 kB board, whatever happens to the weights afterwards.

الحساب نفسه يفسر لماذا فشل MobileNetV2 في المفهوم الأول. الـ convolution الأولى فيه تحوّل صورة 3×224×224 إلى 32×112×112: 150,528 + 401,408 = 551,936 قيمة، وهو ما يذكره MCUNetV2 بأنه "539kB حتى بعد الـ quantization إلى int8" (باستخدام كيلوبايت من 1024 byte). الـ input بحجم 224 عبر stem قياسي لا يمكن أبدًا أن يعمل طبقة بطبقة على لوحة بـ 256 kB، مهما حدث للأوزان بعد ذلك.

Finally, connect the counts back to latency. Part 01 approximated memory time as the bytes of activations and weights moved divided by the memory bandwidth. The per-layer input plus output is the activation traffic of that layer, so the same column that gives the peak also feeds the latency model. Peak tells you whether the model fits; total tells you how many activation bytes cross the memory interface per inference.

أخيرًا، اربط الأعداد بالـ latency. الجزء 01 قرّب memory time بأنه bytes الـ activations والأوزان المنقولة مقسومة على الـ memory bandwidth. الـ input زائد الـ output لكل طبقة هو حركة الـ activations لتلك الطبقة، لذلك العمود نفسه الذي يعطي الذروة يغذّي أيضًا نموذج الـ latency. الذروة تخبرك هل يتّسع النموذج؛ والعدد الكلي يخبرك كم byte من الـ activations يعبر واجهة الذاكرة في كل inference.

Quick checkاختبار سريع

During layer-by-layer inference, which quantity approximates the peak activation memory of a CNN?أثناء الـ inference طبقة بطبقة، أي كمية تقرّب ذروة ذاكرة الـ activation في الـ CNN؟

Recall

A toy CNN: input 3×32×32, a convolution to 16×32×32, a pool to 16×16×16, then a linear layer to 10 outputs. Total and peak #activations?CNN تجريبية: input بحجم 3×32×32، ثم convolution إلى 16×32×32، ثم pool إلى 16×16×16، ثم linear layer إلى 10 outputs. ما العدد الكلي وما ذروة #activations؟

Counts: 3,072; 16,384; 4,096; 10. Total 23,562. Adjacent sums 19,456, 20,480, 4,106. Peak 20,480, while the pool runs (convolution output plus pool output), not the input pair. About 20.5 kB at 8-bit.
الأعداد: 3,072; 16,384; 4,096; 10. العدد الكلي 23,562. مجاميع الأزواج المتجاورة 19,456 و20,480 و4,106. الذروة 20,480، أثناء تشغيل الـ pool (output الـ convolution زائد output الـ pool)، وليست زوج الـ input. أي حوالي 20.5 kB بـ 8-bit.

Recall

AlexNet's peak is 440,928 values. How many kB is that at 8-bit and at 32-bit, and does either fit a 320 kB STM32F746?ذروة AlexNet هي 440,928 قيمة. كم kB تساوي بـ 8-bit وبـ 32-bit، وهل تتّسع أيٌّ منهما في STM32F746 بـ 320 kB؟

441 kB at 8-bit and 1.76 MB at 32-bit (decimal kilobytes). Neither fits 320 kB; the first layer alone is over budget.
441 kB بـ 8-bit و1.76 MB بـ 32-bit (كيلوبايت عشري). لا تتّسع أيٌّ منهما في 320 kB؛ فالطبقة الأولى وحدها تتجاوز الميزانية.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • SRAM holds activations (read and write); flash holds weights (read only). SRAM is the smaller budget, so activations decide whether a model fits.الـ SRAM يحمل الـ activations (قراءة وكتابة)، والـ flash يحمل الأوزان (قراءة فقط). الـ SRAM هو الميزانية الأصغر، لذلك الـ activations هي التي تقرر هل يتّسع النموذج أم لا.
  • ResNet-18 to MobileNetV2-0.75 at about 70 percent top-1, int8: parameter memory 4.6x smaller, peak activation 1.8x larger.من ResNet-18 إلى MobileNetV2-0.75 عند حوالي 70 بالمئة top-1 وبـ int8: ذاكرة الـ parameters أصغر بـ 4.6x، وذروة الـ activation أكبر بـ 1.8x.
  • MobileNetV2's memory is imbalanced: 1372 kB at block 2, the first five blocks above 256 kB, the remaining thirteen below it.ذاكرة MobileNetV2 غير متوازنة: 1372 kB عند block 2، وأول خمسة blocks فوق 256 kB، والثلاثة عشر الباقية تحته.
  • Training keeps every layer input for dL/dW. ResNet-50 in fp32 at batch 8: 102 MB of weights against 707 MB of activations; MobileNetV2-1.4 cuts weights 4.3x but activations only 1.1x.التدريب يُبقي كل input لكل طبقة من أجل dL/dW. في ResNet-50 بـ fp32 وbatch 8: الأوزان 102 MB مقابل 707 MB للـ activations؛ وMobileNetV2-1.4 يقلّص الأوزان 4.3x لكن الـ activations بـ 1.1x فقط.
  • Activation memory is high in early layers (large H×W) and weight memory is high in late layers (large C_o·C_i); the middle is cheap.ذاكرة الـ activations عالية في الطبقات الأولى (H×W كبير) وذاكرة الأوزان عالية في الطبقات الأخيرة (C_o·C_i كبير)؛ والمنتصف رخيص.
  • Total #activations sums every tensor (AlexNet: 932,264). Peak ≈ the largest input + output pair (AlexNet: 150,528 + 290,400 = 440,928 at conv1).العدد الكلي لـ #activations يجمع كل tensor (في AlexNet: 932,264). والذروة ≈ أكبر زوج input + output (في AlexNet: 150,528 + 290,400 = 440,928 عند conv1).
  • 440,928 values are 441 kB at 8-bit and 1.76 MB at 32-bit, above a 256 kB or 320 kB microcontroller either way.440,928 قيمة تساوي 441 kB بـ 8-bit و1.76 MB بـ 32-bit، وهي فوق microcontroller بـ 256 kB أو 320 kB في الحالتين.
  • The peak formula assumes layer-by-layer execution and ignores residual branches, in-place tricks and runtime scratch buffers.معادلة الذروة تفترض التنفيذ طبقة بطبقة، وتتجاهل الـ residual branches وحيل الـ in-place والـ scratch buffers الخاصة بالـ runtime.

Sourcesالمصادر