Majid Al-RaimiFull guideالشرح الكامل

COE 592Lecture 03المحاضرة 03Full guideالشرح الكامل

Neural network efficiency metricsمقاييس كفاءة الشبكات العصبية

The whole lecture on one page, taught concept by concept. Work through the parts in order, mark each concept once you understand it, and open the slide chips when you want the original slides.المحاضرة كاملة في صفحة واحدة، مشروحة مفهومًا مفهومًا. امشِ على الأجزاء بالترتيب، وعلّم كل مفهوم بعد أن تفهمه، وافتح أزرار الشرائح عندما تريد الرجوع إلى الشرائح الأصلية.

Partsالأجزاء
4
Conceptsالمفاهيم
18
Slidesالشرائح
29
Readingمدة القراءة
108 min108 دقائق
Understoodما فهمته
0/18 conceptsمفاهيم

Part 01: Latency, throughput and energyزمن الاستجابة والإنتاجية والطاقة

The efficiency triangle of storage, latency and energy, what latency and throughput each measure, why one does not imply the other, the analytical latency model, and why data movement dominates energy.مثلث الكفاءة (التخزين وزمن الاستجابة والطاقة)، وما يقيسه كل من زمن الاستجابة (latency) والإنتاجية (throughput)، ولماذا لا يستلزم أحدهما الآخر، ونموذج زمن الاستجابة التحليلي، ولماذا يهيمن نقل البيانات على استهلاك الطاقة.

5 concepts5 مفاهيم, ، slides 1-9الشرائح 1-9

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

Your research project will put a detector on an embedded board, and the board will judge it by three costs that have nothing to do with accuracy: does the model fit, does it answer before the next frame arrives, and does it drain the battery. Every metric in this lecture exists to predict one of those three costs before you ever flash the board.

This part gives you the map and the first three costs. It fixes the vocabulary (latency, throughput, energy), shows why a latency number is meaningless without the conditions it was measured under, works the batching example that exams love, derives a one-line latency model that separates what the network controls from what the hardware controls, and ends with the energy table that explains why every efficient design of the last decade is really a design for moving fewer bytes. Parts 02 to 04 then fill in the metrics that feed these costs.

سيضع مشروعك البحثي كاشفًا (detector) على لوحة مدمجة (embedded board)، وستحكم اللوحة عليه بثلاث تكاليف لا علاقة لها بالدقة: هل يتسع النموذج فيها، وهل يُخرج إجابته قبل وصول الإطار التالي، وهل يستنزف البطارية. كل مقياس في هذه المحاضرة موجود ليتنبأ بإحدى هذه التكاليف الثلاث قبل أن تحمّل البرنامج على اللوحة أصلًا.

يعطيك هذا الجزء الخريطة والتكاليف الثلاث الأولى. فهو يثبّت المصطلحات (latency وthroughput والطاقة)، ويبيّن لماذا لا معنى لرقم latency من دون الظروف التي قيس فيها، ويحل مثال الـ batching الذي تحبه الاختبارات، ويشتق نموذجًا للـ latency في سطر واحد يفصل ما تتحكم فيه الشبكة عما يتحكم فيه العتاد، ثم ينتهي بجدول الطاقة الذي يفسر لماذا كان كل تصميم كفء في العقد الأخير في حقيقته تصميمًا لنقل بايتات أقل. ثم تملأ الأجزاء من 02 إلى 04 المقاييس التي تغذي هذه التكاليف.

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

  1. Place any efficiency metric in the memory or computation group and say which of storage, latency and energy it predicts.تضع أي مقياس كفاءة في مجموعة الذاكرة أو مجموعة الحساب، وتحدد أيّ التكاليف الثلاث يتنبأ بها: التخزين أو الـ latency أو الطاقة.
  2. Define latency and throughput, and compute both for a batched design.تعرّف الـ latency والـ throughput، وتحسب كليهما لتصميم يستخدم الـ batching.
  3. Explain with numbers why higher throughput does not imply lower latency, and lower latency does not imply higher throughput.تشرح بالأرقام لماذا لا يعني الـ throughput الأعلى latency أقل، ولا يعني الـ latency الأقل throughput أعلى.
  4. Write Latency ≈ max(T_computation, T_memory) with its sub-formulas, label each term NN or hardware specification, and decide compute-bound versus memory-bound.تكتب Latency ≈ max(T_computation, T_memory) مع صيغها الفرعية، وتصنّف كل حد على أنه مواصفة الشبكة (NN) أو مواصفة العتاد، وتقرر هل النموذج compute-bound أم memory-bound.
  5. Quote the 45 nm energy table and argue why data movement, not arithmetic, sets the energy bill of an inference.تستشهد بجدول الطاقة عند 45 nm وتبرهن لماذا يحدد نقلُ البيانات، لا الحسابُ، فاتورةَ الطاقة لعملية الاستدلال (inference).

Suppose the detector you trained in the cloud is to run on a Jetson-class board mounted on a vehicle. Before the board can use it, three separate questions must be answered. Does the model fit in the flash and RAM you have? Does one inference finish before the next camera frame lands, roughly every 33 ms at 30 frames per second? And how much charge does each inference pull from the battery? Slide 4 draws these as the three outer goals of a triangle, Smaller, Faster and Greener, and the three costs that quantify them: Storage, Latency and Energy.

افترض أن الكاشف الذي دربته في السحابة سيعمل على لوحة من فئة Jetson مثبتة على مركبة. قبل أن تستطيع اللوحة استخدامه يجب الإجابة عن ثلاثة أسئلة منفصلة. هل يتسع النموذج في ذاكرة الـ flashوالـ RAM المتاحة لديك؟ هل ينتهي استدلال واحد قبل وصول إطار الكاميرا التالي، أي تقريبًا كل 33 ms عند 30 إطارًا في الثانية؟ وكم من شحن البطارية يسحب كل استدلال؟ ترسم الشريحة 4 هذه الأسئلة على أنها الأهداف الخارجية الثلاثة لمثلث: أصغر (Smaller) وأسرع (Faster) وأكثر خضرة (Greener)، ومعها التكاليف الثلاث التي تقيسها: التخزين (Storage) والـ Latency والطاقة (Energy).

The part of the triangle that matters is the middle. All three costs are driven by only two resources, Computation and Memory. Storage is purely a memory question: how many bytes the weights occupy. Latency and energy depend on both, because an inference both performs arithmetic and moves data, and each of those takes time and burns charge. That is why the metric list on the side of the slide has exactly two groups. The efficiency metrics that are memory-related are the number of parameters, the model size and the total and peak number of activations. The computation-related ones are MACs, FLOPs with their rate FLOPS, and OPs with their rate OPS.

الجزء المهم من المثلث هو الوسط. فالتكاليف الثلاث كلها يحركها مَوردان فقط: الحساب (Computation) والذاكرة (Memory). التخزين مسألة ذاكرة بحتة: كم بايتًا تشغل الأوزان. أما الـ latency والطاقة فيعتمدان على الاثنين، لأن الاستدلال يجري عمليات حسابية وينقل بيانات معًا، وكل منهما يستهلك وقتًا ويحرق شحنًا. لهذا تحتوي قائمة المقاييس على جانب الشريحة على مجموعتين بالضبط. efficiency metrics المتعلقة بالذاكرة هي number of parameters (عدد المعاملات)، وmodel size (حجم النموذج)، والعدد الكلي للـ activations وpeak number of activations. أما المتعلقة بالحساب فهي MACs، وFLOPs مع معدلها FLOPS، وOPs مع معدلها OPS.

Resources feed costs, costs feed goals. Memory wires into all three costs, computation into latency and energy. Hover Storage to see that only the memory metrics predict it.الموارد تغذي التكاليف، والتكاليف تغذي الأهداف. الذاكرة متصلة بالتكاليف الثلاث كلها، والحساب متصل بالـ latency والطاقة. مرّر المؤشر على Storage لترى أن مقاييس الذاكرة وحدها هي التي تتنبأ به.
GoalالهدفCostالتكلفةResourceالموردMetrics that predict itالمقاييس التي تتنبأ به
SmallerأصغرStorageالتخزينMemoryالذاكرة#parameters, model sizeعدد المعاملات (#parameters)، وحجم النموذج
FasterأسرعLatencyالـ latencyComputation and memoryالحساب والذاكرةMACs, OPs, model size, peak #activationsMACs، وOPs، وحجم النموذج، والعدد الأقصى للـ activations
Greenerأكثر خضرةEnergyالطاقةComputation and memoryالحساب والذاكرةMACs, OPs, and above all the bytes movedMACs وOPs، وقبل كل شيء البايتات المنقولة
Each goal, the cost that quantifies it, the resource it draws on, and the metrics that predict itكل هدف، والتكلفة التي تقيسه، والمورد الذي يعتمد عليه، والمقاييس التي تتنبأ به

This taxonomy is the spine of the whole lecture. Parts 02 and 03 fill the memory group (parameters, model size, activations), part 04 fills the computation group (MACs, FLOPs, OPs), and this part explains the three costs those metrics are meant to predict. Whenever a later metric appears, place it here first: which group is it in, and which cost does it feed?

هذا التصنيف هو العمود الفقري للمحاضرة كلها. يملأ الجزءان 02 و03 مجموعة الذاكرة (المعاملات، حجم النموذج، الـ activations)، ويملأ الجزء 04 مجموعة الحساب (MACs، FLOPs، OPs)، ويشرح هذا الجزء التكاليف الثلاث التي يُفترض أن تتنبأ بها تلك المقاييس. كلما ظهر مقياس لاحقًا، ضعه هنا أولًا: في أي مجموعة هو، وأي تكلفة يغذي؟

Metrics you count versus costs you measureمقاييس تَعُدّها مقابل تكاليف تقيسها

One distinction keeps the rest of the part honest. Parameters and MACs are properties of the network alone. You can count them on paper from the layer shapes, and they do not change when you move the model to another chip. Latency, throughput and energy are properties of the network running on a specific processor with a specific runtime. They must be measured, or at least estimated from hardware numbers. The same split reappears in the latency model of concept 4 as NN specification versus hardware specification.

تمييز واحد يحفظ بقية الجزء دقيقًا. المعاملات والـ MACs خصائص للشبكة وحدها. يمكنك عدّها على الورق من أشكال الطبقات، ولا تتغير عندما تنقل النموذج إلى شريحة أخرى. أما Latency وthroughput والطاقة فهي خصائص للشبكة وهي تعمل على معالج محدد مع runtime محدد. يجب قياسها، أو على الأقل تقديرها من أرقام العتاد. ويعود هذا الانقسام نفسه في نموذج الـ latency في المفهوم 4 بوصفه مواصفة الشبكة (NN specification) مقابل مواصفة العتاد (hardware specification).

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

In the efficiency triangle, which pair of resources drives all three of storage, latency and energy?في مثلث الكفاءة، أي زوج من الموارد يحرّك التكاليف الثلاث كلها: التخزين والـ latency والطاقة؟

Recall

Name the two groups of efficiency metrics, list the members of each, and say which cost each group predicts.سمِّ مجموعتَي مقاييس الكفاءة، واذكر أعضاء كل منهما، وقل أي تكلفة تتنبأ بها كل مجموعة.

Memory-related: #parameters, model size, total and peak #activations; they predict storage and the memory part of latency and energy. Computation-related: MAC, FLOP and FLOPS, OP and OPS; they predict the computation part of latency and energy.
المتعلقة بالذاكرة: #parameters، وحجم النموذج، والعدد الكلي والأقصى للـ activations؛ وهي تتنبأ بالتخزين وبجزء الذاكرة من الـ latency والطاقة. المتعلقة بالحساب: MAC، وFLOP وFLOPS، وOP وOPS؛ وهي تتنبأ بجزء الحساب من الـ latency والطاقة.

Slide 5 shows the same Cityscapes street segmented twice. The two masks look almost identical, and by the usual accuracy metric they are: SegFormer-B5 reaches 82.4 mIoU and EfficientViT-L1 reaches 82.7 mIoU. The difference is time. SegFormer needs 638 ms per frame and EfficientViT needs 45.9 ms (the slide rounds to 46 ms). At 30 frames per second a new frame arrives every 33 ms, so one model falls nineteen frames behind for every frame it finishes, and the other almost keeps up.

تعرض الشريحة 5 شارع Cityscapes نفسه مقسّمًا (segmented) مرتين. يبدو القناعان متطابقين تقريبًا، وهما كذلك وفق مقياس الدقة المعتاد: يصل SegFormer-B5 إلى 82.4 mIoU ويصل EfficientViT-L1 إلى 82.7 mIoU. الفرق هو الزمن. يحتاج SegFormer إلى 638 ms لكل إطار ويحتاج EfficientViT إلى 45.9 ms (تقرّبها الشريحة إلى 46 ms). عند 30 إطارًا في الثانية يصل إطار جديد كل 33 ms، لذا يتأخر أحد النموذجين تسعة عشر إطارًا عن كل إطار ينهيه، بينما يكاد الآخر يواكب.

That per-frame time is latency: the delay between starting one specific task and finishing it. Patterson and Hennessy call the same quantity response time or execution time, the time between the start and completion of a task. It is a property of one task, not of a stream of tasks, and the next concept shows why that restriction matters. The 13.9x gap (638 / 45.9) is exactly the headline of the EfficientViT paper, which reports up to 13.9x GPU latency reduction over SegFormer at the same or better accuracy (Cai et al., ICCV 2023).

هذا الزمن لكل إطار هو latency: التأخير بين بدء مهمة واحدة محددة وإنهائها. يسمي Patterson وHennessy الكمية نفسها زمن الاستجابة (response time) أو زمن التنفيذ (execution time)، أي الزمن بين بدء مهمة واكتمالها. إنه خاصية لمهمة واحدة، لا لسيل من المهام، والمفهوم التالي يبيّن لماذا يهم هذا القيد. الفارق 13.9x (638 / 45.9) هو بالضبط العنوان الرئيسي لورقة EfficientViT، التي تذكر خفضًا في الـ latency على الـ GPU يصل إلى 13.9x مقارنة بـ SegFormer مع دقة مساوية أو أفضل (Cai et al., ICCV 2023).

ModelالنموذجmIoUmIoUParametersالمعاملاتMACsMACsOrin latencyالـ latency على OrinA100 throughputالـ throughput على A100
SegFormer-B5SegFormer-B582.482.485M85M1460G1460G638 ms638 ms12 image/s12 image/s
EfficientViT-L1EfficientViT-L182.782.740M40M282G282G45.9 ms45.9 ms122 image/s122 image/s
Ratioالنسبة+0.3+0.32.1x fewerأقل بمقدار 2.1x5.2x fewerأقل بمقدار 5.2x13.9x fasterأسرع بمقدار 13.9x10x moreأكثر بمقدار 10x
Both models on Cityscapes at 1024 x 2048, from Table 3 of the EfficientViT paper. Latency on Jetson AGX Orin at batch size 1, throughput on an A100.النموذجان على Cityscapes بدقة 1024 x 2048، من الجدول 3 في ورقة EfficientViT. الـ latency على Jetson AGX Orin بـ batch size يساوي 1، والـ throughput على A100.

Read the ratio row carefully, because it plants the seed for concept 4. The MAC count falls 5.2x, but latency falls 13.9x. If latency were simply proportional to arithmetic, the two ratios would match. They do not, because SegFormer's softmax attention moves large intermediate tensors through memory, and that data movement, not its MACs, is what makes it slow on an edge GPU. MACs alone do not predict latency; they are one input to it.

اقرأ صف النسب بعناية، لأنه يزرع بذرة المفهوم 4. ينخفض عدد MAC بمقدار 5.2x، لكن الـ latency ينخفض بمقدار 13.9x. لو كان الـ latency متناسبًا ببساطة مع الحساب لتطابقت النسبتان. لكنهما لا تتطابقان، لأن الـ softmax attention في SegFormer ينقل tensors وسيطة كبيرة عبر الذاكرة، وهذا النقل للبيانات، لا عدد MACs، هو ما يجعله بطيئًا على GPU طرفي (edge). الـ MACs وحدها لا تتنبأ بالـ latency؛ إنها مُدخل واحد من مُدخلاته.

Four conditions every latency must stateأربعة شروط يجب أن يذكرها كل رقم latency

The slide's footnote is not fine print. Latency is measured, so it depends on everything in the measurement. The paper's setup sentence names four things: the hardware, the runtime, the numeric precision and the batch size. Change any one of them and the number changes, sometimes by an order of magnitude. The same paper measures EfficientViT-B1 at 24.3 ms on Orin and 0.82 s on a Jetson Nano, and SegFormer-B1 at 146 ms on Orin and 5.6 s on Nano. Same model, same precision, same batch size, different board: 34x to 38x apart.

الحاشية في أسفل الشريحة ليست تفاصيل هامشية. الـ latency يُقاس، لذا يعتمد على كل شيء في عملية القياس. جملة الإعداد في الورقة تسمي أربعة أشياء: العتاد، والـ runtime، والدقة العددية (precision)، وbatch size. غيّر أيًا منها فيتغير الرقم، أحيانًا بمرتبة عشرية كاملة. تقيس الورقة نفسها EfficientViT-B1 عند 24.3 ms على Orin و0.82 s على Jetson Nano، وSegFormer-B1 عند 146 ms على Orin و5.6 s على Nano. النموذج نفسه، والدقة العددية نفسها، والـ batch size نفسه، ولوحة مختلفة: الفرق من 34x إلى 38x.

The measurement conditions behind slide 5شروط القياس وراء الشريحة 5

Hardwareالعتاد
NVIDIA Jetson AGX Orin: 2048-core Ampere GPU, 64 Tensor Cores at 43 dense FP16 TFLOPS (the CUDA cores alone give 10.6), 204.8 GB/s LPDDR5, 15 W to 60 WNVIDIA Jetson AGX Orin: معالج رسومي GPU بمعمارية Ampere فيه 2048 نواة، و64 من الـ Tensor Cores بأداء 43 dense FP16 TFLOPS (أنوية CUDA وحدها تعطي 10.6)، وذاكرة LPDDR5 بعرض نطاق 204.8 GB/s، واستهلاك قدرة من 15 W إلى 60 W
Runtimeبيئة التشغيل (runtime)
TensorRT, which NVIDIA describes as turning a trained model into a fast, GPU-specific program for inference; the speedup depends on the model, precision, batch size and GPUTensorRT، الذي تصفه NVIDIA بأنه يحوّل النموذج المدرَّب إلى برنامج استدلال سريع ومخصص للـ GPU؛ ويعتمد مقدار التسريع على النموذج والدقة العددية والـ batch size والـ GPU
Precisionالدقة العددية
fp16, half the bytes per weight and activation of fp32fp16، أي نصف عدد البايتات لكل وزن ولكل activation مقارنة بـ fp32
Batch sizeحجم الدفعة (batch size)
1, one image per inference, which is what an on-device detector sees1، أي صورة واحدة لكل استدلال، وهذا ما يراه كاشف يعمل على الجهاز نفسه

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

Which set of conditions must accompany a reported latency for it to be comparable?أي مجموعة من الشروط يجب أن ترافق رقم latency منشورًا حتى يمكن مقارنته؟

Recall

Define latency in one sentence and name the four conditions that slide 5 states for its numbers.عرّف الـ latency في جملة واحدة وسمِّ الشروط الأربعة التي تذكرها الشريحة 5 لأرقامها.

Latency is the delay to complete one specific task, here one segmentation of one frame. Conditions: hardware (Jetson AGX Orin), runtime (TensorRT), precision (fp16) and batch size (1).
الـ latency هو التأخير لإكمال مهمة واحدة محددة، وهي هنا تقسيم (segmentation) واحد لإطار واحد. الشروط: العتاد (Jetson AGX Orin)، والـ runtime (TensorRT)، والدقة العددية (fp16)، والـ batch size (1).

Slide 6 replaces one street with two mosaics of video clips and asks a different question: not how long one clip takes, but how many clips get done per second. One system manages 6.1 videos per second, the other 77.4. Those two numbers come from Table 5 of the TSM paper (Lin, Gan and Han, ICCV 2019): I3D at 306 GFLOPs versus TSM8F at 33 GFLOPs, both on a single Tesla P100.

تستبدل الشريحة 6 الشارعَ الواحد بفسيفساءين من مقاطع الفيديو وتطرح سؤالًا مختلفًا: ليس كم يستغرق مقطع واحد، بل كم مقطعًا يُنجَز في الثانية. أحد النظامين يعالج 6.1 فيديو في الثانية، والآخر 77.4. يأتي هذان الرقمان من الجدول 5 في ورقة TSM (Lin, Gan and Han, ICCV 2019): I3D عند 306 GFLOPs مقابل TSM8F عند 33 GFLOPs، وكلاهما على Tesla P100 واحدة.

That rate is throughput: the amount of work completed per unit time, what Patterson and Hennessy also call bandwidth. The TSM paper is unusually helpful here because it states its protocol: latency was measured with batch size 1 and throughput with batch size 16. TSM8F's latency is 17.4 ms. If throughput were the reciprocal of latency you would expect 1000 / 17.4 = 57.5 videos per second, yet the paper reports 77.4. The extra comes from batching, and it is the whole point of slide 7.

هذا المعدل هو throughput: مقدار العمل المنجز في وحدة الزمن، وهو ما يسميه Patterson وHennessy أيضًا عرض النطاق (bandwidth). ورقة TSM مفيدة على نحو غير معتاد هنا لأنها تذكر بروتوكولها: قيس الـ latency بـ batch size يساوي 1 والـ throughput بـ batch size يساوي 16. الـ latency للنموذج TSM8F هو 17.4 ms. لو كان الـ throughput مقلوب الـ latency لتوقعت 1000 / 17.4 = 57.5 فيديو في الثانية، لكن الورقة تذكر 77.4. الزيادة تأتي من الـ batching، وهي جوهر الشريحة 7 كله.

throughput=bTbatch,latency=Tbatch\text{throughput} = \frac{b}{T_{\text{batch}}}, \qquad \text{latency} = T_{\text{batch}}
b inputs processed together in one batch that takes T_batchb من المدخلات تُعالج معًا في دفعة واحدة تستغرق T_batch

Two designs, same processor, opposite answersتصميمان، المعالج نفسه، إجابتان متعاكستان

Slide 7 pits two designs against each other. Design 1 handles one image at a time and takes 50 ms per image. Design 2 takes four images together and finishes the whole group in 100 ms. Each image in Design 2 waits the full 100 ms, because nothing leaves the processor until the batch is done.

تضع الشريحة 7 تصميمين في مواجهة. التصميم 1 يعالج صورة واحدة في كل مرة ويستغرق 50 ms لكل صورة. التصميم 2 يأخذ أربع صور معًا وينهي المجموعة كلها في 100 ms. كل صورة في التصميم 2 تنتظر الـ 100 ms كاملة، لأن لا شيء يغادر المعالج حتى تكتمل الدفعة (batch).

DesignالتصميمHow images are handledطريقة معالجة الصورPer-image latencyالـ latency لكل صورةImages per batchعدد الصور في الدفعةBatch timeزمن الدفعةThroughputالـ throughput
Design 1التصميم 1One after anotherصورة تلو الأخرى50 ms50 ms1150 ms50 ms1 / 0.050 = 20 image/s1 / 0.050 = 20 image/s
Design 2التصميم 2Four in parallelأربع صور بالتوازي100 ms100 ms44100 ms100 ms4 / 0.100 = 40 image/s4 / 0.100 = 40 image/s
Slide 7's two designsتصميما الشريحة 7
Both lanes run for the same 200 ms. Design 1 finishes four images in four 50 ms steps. Design 2 finishes eight in two 100 ms steps, yet every one of its images waited twice as long.يعمل المساران المدة نفسها 200 ms. ينهي التصميم 1 أربع صور في أربع خطوات كل منها 50 ms. وينهي التصميم 2 ثماني صور في خطوتين كل منهما 100 ms، ومع ذلك انتظرت كل صورة من صوره ضعف المدة.

Worked exampleمثال محلول

Answering slide 7's two questionsالإجابة عن سؤالَي الشريحة 7

  1. Does higher throughput mean lower latency?هل يعني الـ throughput الأعلى latency أقل؟

    No. Design 2 has twice the throughput (40 versus 20 image/s) and twice the latency (100 versus 50 ms). Batching raised both, because throughput counts images and latency counts the wait of one image, and that image waits for its whole batch.
    لا. للتصميم 2 ضعف الـ throughput (40 مقابل 20 image/s) وضعف الـ latency (100 مقابل 50 ms). رفع الـ batching الاثنين معًا، لأن الـ throughput يعدّ الصور بينما يعدّ الـ latency انتظار صورة واحدة، وتلك الصورة تنتظر دفعتها كاملة.
  2. Does lower latency mean higher throughput?هل يعني الـ latency الأقل throughput أعلى؟

    No. Design 1 has half the latency and half the throughput. With no parallelism, a fast single lane still moves only one image per step, so a short step buys a short wait but not a high rate.
    لا. للتصميم 1 نصف الـ latency ونصف الـ throughput. من دون توازٍ، يبقى المسار الواحد السريع ينقل صورة واحدة فقط في كل خطوة، فالخطوة القصيرة تشتري انتظارًا قصيرًا لكنها لا تشتري معدلًا عاليًا.
  3. When the two coincideمتى يتطابق الاثنان

    Throughput equals 1 / latency only when the batch size is 1 and nothing is pipelined or run in parallel. Any batching or parallelism breaks the identity, always in the direction of more throughput per unit of latency.
    يساوي الـ throughput القيمة 1 / latency فقط عندما يكون الـ batch size مساويًا 1 ولا يوجد أي pipelining أو تشغيل متوازٍ. أي batching أو توازٍ يكسر هذه المساواة، ودائمًا في اتجاه throughput أكبر لكل وحدة من الـ latency.
SimulatorمحاكيLatency versus throughput under batchingزمن الاستجابة مقابل الإنتاجية مع التجميع في دفعات
images done atالصور المنجزة عند 1000 ms: 40
Latency per image100msevery image waits for its whole batch, so L = t_batchكل صورة تنتظر اكتمال دفعتها كلها، لذلك L = t_batch
Throughputالإنتاجية (throughput)40.0image/s4 / 100 ms
1 / latency10.0per sonly equals throughput when b = 1لا يساوي الإنتاجية إلا عندما b = 1

Batching multiplies throughput by b = 4 (4x the reciprocal of latency) while latency stays at 100 ms for every image. In one 1 s window this design completes 10 whole batches, 40 images. The TSM preset back-computes its batch time from the paper's 77.4 video/s at batch 16, so it illustrates the arithmetic rather than confirming it.يضاعف التجميع في دفعات الإنتاجية بمقدار b = 4 (أي 4x مقلوب زمن الاستجابة) بينما يبقى زمن الاستجابة 100 ms لكل صورة. وفي نافذة واحدة مدتها 1 s يُنجز هذا التصميم 10 دفعات كاملة، أي 40 صورة. يستنتج إعداد TSM المسبق زمن دفعته عكسيًا من القيمة 77.4 video/s الواردة في الورقة عند دفعة حجمها 16، فهو يوضح طريقة الحساب ولا يؤكد الرقم.

A second scenario for practiceسيناريو ثانٍ للتدريب

A cloud server serves the same model at two batch sizes. At batch 1 a request takes 2 ms. At batch 32 the whole batch takes 8 ms, because the GPU can spread thirty-two images over its cores far more efficiently than one.

خادم سحابي يقدّم النموذج نفسه بحجمَي batch مختلفين. عند batch يساوي 1 يستغرق الطلب 2 ms. وعند batch يساوي 32 تستغرق الدفعة كلها 8 ms، لأن الـ GPU يستطيع توزيع اثنتين وثلاثين صورة على أنويته بكفاءة أعلى بكثير من صورة واحدة.

SettingالإعدادBatch timeزمن الدفعةImages per batchعدد الصور في الدفعةLatency per imageالـ latency لكل صورةThroughputالـ throughput
Batch 1دفعة بحجم 12 ms2 ms112 ms2 ms500 image/s500 image/s
Batch 32دفعة بحجم 328 ms8 ms32328 ms8 ms4000 image/s4000 image/s
Changeالتغيّر4x longerأطول بمقدار 4x32x moreأكثر بمقدار 32x4x worseأسوأ بمقدار 4x8x betterأفضل بمقدار 8x
One server, two batch sizesخادم واحد بحجمَي batch مختلفين

Batching paid 4x latency to buy 8x throughput. Whether that is a good trade depends on who is waiting. A brake camera needs one frame answered now: latency, at batch size 1, which is why slide 5 measured that way. A data center indexing videos overnight needs clips per hour: throughput, at whatever batch size fills the GPU. Your research project sits on the first side of that line.

دفع الـ batching زيادة 4x في الـ latency ليشتري 8x في الـ throughput. هل هذه مقايضة جيدة؟ يعتمد ذلك على من ينتظر. كاميرا الفرامل تحتاج إلى إجابة عن إطار واحد الآن: أي latency، بـ batch size يساوي 1، ولهذا قاست الشريحة 5 بهذه الطريقة. أما مركز البيانات الذي يفهرس الفيديوهات طوال الليل فيحتاج إلى مقاطع في الساعة: أي throughput، بأي batch size يملأ الـ GPU. ومشروعك البحثي يقع في الجانب الأول من هذا الخط.

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

Design 2 processes 4 images in parallel in 100 ms. What are its latency and throughput?التصميم 2 يعالج 4 صور بالتوازي في 100 ms. ما الـ latency والـ throughput له؟

Recall

A design processes 8 images together every 200 ms. What are its latency and throughput, and what would the naive reciprocal give?تصميم يعالج 8 صور معًا كل 200 ms. ما الـ latency والـ throughput له، وماذا سيعطي المقلوب الساذج؟

Latency 200 ms per image. Throughput 8 / 0.2 = 40 image/s. The reciprocal 1 / 0.2 = 5 per second is wrong by the batch size.
الـ latency هو 200 ms لكل صورة. الـ throughput هو 8 / 0.2 = 40 image/s. أما المقلوب 1 / 0.2 = 5 في الثانية فخاطئ بمقدار الـ batch size.

Concept 2 left a puzzle: MACs fell 5.2x but latency fell 13.9x. To resolve it you need a model of where the milliseconds go. Watch one layer execute. The processor loads the input activations, loads the layer's weights, multiplies and accumulates, and stores the output activations. Two of those steps are arithmetic; the other two are data movement. Real hardware does not wait for one to finish before starting the other. While the multipliers chew on the current tile, the memory system is already fetching the next weights, so the two streams run side by side and the layer finishes when the slower stream finishes.

ترك المفهوم 2 لغزًا: انخفضت الـ MACs بمقدار 5.2x لكن الـ latency انخفض بمقدار 13.9x. لحله تحتاج إلى نموذج لمكان ذهاب الملّي ثواني. راقب طبقة واحدة وهي تُنفَّذ. يحمّل المعالج الـ activations الداخلة، ويحمّل أوزان الطبقة، ويضرب ويجمع (multiply and accumulate)، ثم يخزّن الـ activations الخارجة. خطوتان من هذه الخطوات حساب، والخطوتان الأخريان نقل بيانات. والعتاد الحقيقي لا ينتظر انتهاء إحداهما قبل بدء الأخرى. فبينما تعمل وحدات الضرب على الجزء (tile) الحالي، يكون نظام الذاكرة قد بدأ بالفعل في جلب الأوزان التالية، فيجري التياران جنبًا إلى جنب وتنتهي الطبقة عندما ينتهي التيار الأبطأ.

Data movement and computation run on parallel tracks. The memory track sits idle while the multipliers catch up and stores the last outputs only after the last MAC, so the layer takes as long as the busier track, which is why the estimate is a max and not a sum.يجري نقل البيانات والحساب على مسارين متوازيين. يبقى مسار الذاكرة خاملًا بينما تلحق وحدات الضرب، ولا يخزّن المخرجات الأخيرة إلا بعد آخر MAC، لذا تستغرق الطبقة مدة المسار الأكثر انشغالًا، ولهذا يكون التقدير max لا مجموعًا.
Latency≈max⁡(Tcomputation,  Tmemory)\text{Latency} \approx \max\left(T_{\text{computation}},\; T_{\text{memory}}\right)
The two overlapped streamsالتدفقان المتداخلان
Tcomputation≈number of operations in the modeloperations per second the processor can doT_{\text{computation}} \approx \frac{\text{number of operations in the model}}{\text{operations per second the processor can do}}
NN specification over hardware specificationمواصفات الشبكة العصبية مقسومة على مواصفات العتاد
Tmemory≈Tmove(activations)+Tmove(weights)T_{\text{memory}} \approx T_{\text{move}}(\text{activations}) + T_{\text{move}}(\text{weights})
Both kinds of data must cross the same memory bus, so these addكلا نوعي البيانات يجب أن يعبر ناقل الذاكرة نفسه، لذلك يُجمعان
Tmove(weights)≈model sizememory bandwidth,Tmove(activations)≈input activation size+output activation sizememory bandwidthT_{\text{move}}(\text{weights}) \approx \frac{\text{model size}}{\text{memory bandwidth}}, \qquad T_{\text{move}}(\text{activations}) \approx \frac{\text{input activation size} + \text{output activation size}}{\text{memory bandwidth}}
Bytes over bytes per secondبايتات مقسومة على بايتات في الثانية

Computation time is the operation count divided by the rate at which the processor retires operations. Memory time is the bytes that must move divided by the memory bandwidth, once for the weights and once for the activations in and out. Now look at the colour coding on the slide, because it is the sentence the exam wants. Every numerator is a property of the network: operations, model size, activation sizes. Every denominator is a property of the processor: operations per second, bandwidth. That split is the NN specification versus hardware specification distinction, and it makes efficiency a co-design problem. You change numerators by redesigning the network, denominators by choosing hardware, and latency by whichever of the two you can actually afford to change.

Computation time (زمن الحساب) هو عدد العمليات مقسومًا على المعدل الذي ينجز به المعالج العمليات. Memory time (زمن الذاكرة) هو البايتات التي يجب نقلها مقسومة على memory bandwidth، مرة للأوزان ومرة للـ activations الداخلة والخارجة. انظر الآن إلى ترميز الألوان في الشريحة، لأنه الجملة التي يريدها الاختبار. كل بسط خاصية للشبكة: العمليات، حجم النموذج، أحجام الـ activations. وكل مقام خاصية للمعالج: العمليات في الثانية، عرض النطاق. هذا الانقسام هو تمييز NN specification versus hardware specification، وهو يجعل الكفاءة مسألة تصميم مشترك (co-design). تغيّر البسوط بإعادة تصميم الشبكة، والمقامات باختيار العتاد، والـ latency بأيهما تستطيع فعلًا تحمّل تغييره.

TermالحدNumerator: NN specificationالبسط: مواصفة الشبكة (NN specification)Denominator: hardware specificationالمقام: مواصفة العتاد (hardware specification)
T_computationT_computationNumber of operations in the model (MACs, OPs: part 04)عدد العمليات في النموذج (MACs، OPs: الجزء 04)Operations per second of the processor (OPS)عدد العمليات التي ينجزها المعالج في الثانية (OPS)
T_move(weights)T_move(weights)Model size (part 02)حجم النموذج (الجزء 02)Memory bandwidthعرض نطاق الذاكرة (memory bandwidth)
T_move(activations)T_move(activations)Input activation size + output activation size (peak #activations: part 03)حجم الـ activation الداخلة + حجم الـ activation الخارجة (العدد الأقصى للـ activations: الجزء 03)Memory bandwidthعرض نطاق الذاكرة (memory bandwidth)
Where each term of the model comes from, and which later part counts itمن أين يأتي كل حد في النموذج، وأي جزء لاحق يحسبه

The same model appears in the industry literature under different names. NVIDIA's GPU performance guide states that memory time is bytes accessed divided by memory bandwidth, math time is operations divided by math bandwidth, and the longer of the two shows what limits performance: math-limited if math time is longer, memory-limited if memory time is longer. The roofline model of Williams, Waterman and Patterson (CACM 2009) writes the same idea as a rate, attainable performance = min(peak compute, bandwidth x operational intensity). The vocabulary to adopt is compute-bound when T_computation wins and memory-bound when T_memory wins. One caution before using datasheet numbers: dividing by a peak OPS figure gives a floor on T_computation, not an estimate of it, because real kernels rarely keep every multiplier busy. The measured time over that floor is the inverse of the utilisation the runtime achieved.

يظهر النموذج نفسه في أدبيات الصناعة بأسماء مختلفة. يذكر دليل أداء الـ GPU من NVIDIA أن زمن الذاكرة هو البايتات المقروءة مقسومة على عرض نطاق الذاكرة، وزمن الحساب (math time) هو العمليات مقسومة على عرض نطاق الحساب (math bandwidth)، والأطول بينهما يبيّن ما يحدّ الأداء: math-limited إذا كان زمن الحساب أطول، وmemory-limited إذا كان زمن الذاكرة أطول. ونموذج الـ roofline لـ Williams وWaterman وPatterson (CACM 2009) يكتب الفكرة نفسها في صورة معدّل: attainable performance = min(peak compute, bandwidth x operational intensity). والمصطلحات التي تعتمدها هي compute-bound عندما يكون T_computation هو الأكبر، وmemory-bound عندما يكون T_memory هو الأكبر. تحذير واحد قبل استخدام أرقام ورقة البيانات (datasheet): القسمة على رقم OPS الأقصى تعطي حدًا أدنى لـ T_computation، لا تقديرًا له، لأن الـ kernels الحقيقية نادرًا ما تُبقي كل وحدات الضرب مشغولة. والزمن المقيس مقسومًا على هذا الحد الأدنى هو مقلوب نسبة الاستغلال (utilisation) التي حققها الـ runtime.

Worked example A: a toy model and two processorsالمثال المحلول A: نموذج مبسّط ومعالجان

Worked exampleمثال محلول

Toy numbers that show the regime flippingأرقام مبسّطة تُظهر انقلاب نظام التشغيل (regime)

  1. Start on a 100 GOPS processorابدأ على معالج 100 GOPS

    The model has 1 GOP of work, so T_computation = 1e9 / 1e11 = 10 ms. Weights are 50 MB and activations in plus out are 20 MB, over 10 GB/s: T_weights = 5 ms, T_activations = 2 ms, T_memory = 7 ms. Latency ≈ max(10, 7) = 10 ms, compute-bound.
    في النموذج 1 GOP من العمل، إذن T_computation = 1e9 / 1e11 = 10 ms. الأوزان 50 MB والـ activations الداخلة والخارجة معًا 20 MB، عبر 10 GB/s: T_weights = 5 ms، T_activations = 2 ms، T_memory = 7 ms. الـ Latency ≈ max(10, 7) = 10 ms، أي compute-bound.
  2. Buy a 200 GOPS processorاشترِ معالجًا بسرعة 200 GOPS

    T_computation halves to 5 ms, but T_memory is still 7 ms. Latency ≈ max(5, 7) = 7 ms, not 5 ms. The model became memory-bound and 2 ms of the new compute sits idle every inference.
    ينخفض T_computation إلى النصف ليصبح 5 ms، لكن T_memory ما زال 7 ms. الـ Latency ≈ max(5, 7) = 7 ms، لا 5 ms. أصبح النموذج memory-bound، و2 ms من قدرة الحساب الجديدة تبقى خاملة في كل استدلال.
  3. Quantize the weights to 8-bit insteadبدلًا من ذلك، كمّم الأوزان إلى 8-bit

    Model size drops to 12.5 MB (part 02 shows why), so T_weights = 1.25 ms and T_memory = 3.25 ms. On the fast processor latency is back to max(5, 3.25) = 5 ms: the hardware upgrade only paid off once the network side shrank too.
    ينخفض حجم النموذج إلى 12.5 MB (يبيّن الجزء 02 السبب)، إذن T_weights = 1.25 ms وT_memory = 3.25 ms. على المعالج السريع يعود الـ latency إلى max(5, 3.25) = 5 ms: لم تؤتِ ترقية العتاد ثمارها إلا بعد أن صغُر جانب الشبكة أيضًا.
  4. Three configurationsثلاثة إعدادات

    ConfigurationالإعدادT_computationT_computationT_memoryT_memoryLatencyLatencyRegimeنظام التشغيل (regime)
    100 GOPS, 32-bit weightsمعالج 100 GOPS، أوزان 32-bit10 ms10 ms5 + 2 = 7 ms5 + 2 = 7 ms10 ms10 msCompute-boundCompute-bound
    200 GOPS, 32-bit weightsمعالج 200 GOPS، أوزان 32-bit5 ms5 ms5 + 2 = 7 ms5 + 2 = 7 ms7 ms7 msMemory-boundMemory-bound
    200 GOPS, 8-bit weightsمعالج 200 GOPS، أوزان 8-bit5 ms5 ms1.25 + 2 = 3.25 ms1.25 + 2 = 3.25 ms5 ms5 msCompute-boundCompute-bound
ModelنموذجLatency as max(T_computation, T_memory)زمن الاستجابة بوصفه max(T_computation, T_memory)
light symbols are NN specification (numerators), teal symbols are hardware specification (denominators)الرموز الفاتحة هي مواصفات الشبكة العصبية (البسط)، والرموز الخضراء المزرقة هي مواصفات العتاد (المقام)
T_computation = N_ops / OPS10.0 ms
T_memory = T_weights + T_activations7.00 ms
T_weights = S_w / BW = 5.00 msT_activations = (S_in + S_out) / BW = 2.00 ms
Latency estimate10.0 msmax of the two barsالقيمة العظمى بين الشريطين
Regimecompute-bounda faster processor would helpالمعالج الأسرع سيفيد
Headroom1.43xhow much T_memory could grow before it becomes the bottleneckمقدار ما يمكن أن يزيده T_memory قبل أن يصبح هو عنق الزجاجة

The two Orin presets use 43 TFLOPS dense fp16 on the Tensor Cores and 204.8 GB/s from NVIDIA's Jetson AGX Orin page, MACs and parameters from the EfficientViT paper, and a 1024 × 2048 input with 19 output classes at 2 bytes each. Dividing by a peak rate gives a floor: measured latencies were 45.9 ms and 638 ms, 3.5x and 9.4x above it, and the gap shows what a first-order model leaves out.يستخدم إعدادا Orin المسبقان قيمة 43 TFLOPS بدقة fp16 الكثيفة على Tensor Cores وقيمة 204.8 GB/s من صفحةJetson AGX Orin لدى NVIDIA، ويأخذان عدد MACs والمعاملات من ورقة EfficientViT، مع مُدخل بحجم 1024 × 2048و19 فئة في المُخرج، بمقدار 2 بايت لكل قيمة. القسمة على معدل الذروة تعطي حدًا أدنى: كانت أزمنة الاستجابة المقيسة 45.9 msو638 ms، أي أعلى منه بمقدار 3.5x و9.4x، وتكشف هذه الفجوة ما يغفله نموذج الرتبة الأولى.

Worked example B: back to slide 5المثال المحلول B: عودة إلى الشريحة 5

Worked exampleمثال محلول

Predicting the Orin numbers from public specificationsالتنبؤ بأرقام Orin من المواصفات المنشورة

  1. EfficientViT-L1, computationEfficientViT-L1، الحساب

    282 GMACs is 564 GFLOPs at two FLOPs per MAC (part 04). TensorRT runs fp16 convolutions and matrix multiplies on the Tensor Cores, which the Orin page rates at 43 dense FP16 TFLOPS (the CUDA cores alone would give 10.6), so T_computation ≈ 564 / 43 000 ≈ 13.1 ms.
    282 GMACs تساوي 564 GFLOPs باعتبار أن كل MAC تساوي اثنتين من الـ FLOPs (الجزء 04). يشغّل TensorRT عمليات الـ convolution وضرب المصفوفات بدقة fp16 على الـ Tensor Cores، التي تقدّرها صفحة Orin بـ 43 dense FP16 TFLOPS (أنوية CUDA وحدها كانت ستعطي 10.6)، إذن T_computation ≈ 564 / 43 000 ≈ 13.1 ms.
  2. EfficientViT-L1, memoryEfficientViT-L1، الذاكرة

    Weights: 40M x 2 bytes = 80 MB, over 204.8 GB/s gives 0.39 ms. Activations: input 3 x 1024 x 2048 x 2 B = 12.6 MB, output 19 x 1024 x 2048 x 2 B = 79.7 MB, sum 92.3 MB, over the same bandwidth 0.45 ms. T_memory ≈ 0.84 ms.
    الأوزان: 40M x 2 bytes = 80 MB، وعبر 204.8 GB/s تعطي 0.39 ms. الـ activations: المُدخل 3 x 1024 x 2048 x 2 B = 12.6 MB، والمُخرج 19 x 1024 x 2048 x 2 B = 79.7 MB، والمجموع 92.3 MB، وعبر عرض النطاق نفسه 0.45 ms. T_memory ≈ 0.84 ms.
  3. Compare with measurementقارن بالقياس

    Prediction max(13.1, 0.84) ≈ 13.1 ms, strongly compute-bound. Measured: 45.9 ms, 3.5x above the floor, so the runtime achieved roughly 29% of the Tensor Core peak. That is a normal utilisation for a real network: kernel launches, layers too small to fill the GPU, and intermediate activations moving between layers all sit outside the formula.
    التنبؤ max(13.1, 0.84) ≈ 13.1 ms، أي compute-bound بوضوح. المقيس: 45.9 ms، أي 3.5x فوق الحد الأدنى، فالـ runtime حقق تقريبًا 29% من ذروة الـ Tensor Cores. هذه نسبة استغلال طبيعية لشبكة حقيقية: إطلاق الـ kernels، والطبقات الأصغر من أن تملأ الـ GPU، والـ activations الوسيطة المنتقلة بين الطبقات، كلها تقع خارج الصيغة.
  4. SegFormer-B5 by the same recipeSegFormer-B5 بالوصفة نفسها

    1460 GMACs = 2920 GFLOPs, so T_computation ≈ 2920 / 43 000 ≈ 68 ms. Weights 85M x 2 B = 170 MB give 0.83 ms; the activations are the same 0.45 ms. Prediction ≈ 68 ms. Measured: 638 ms, 9.4x above the floor, about 11% utilisation.
    1460 GMACs = 2920 GFLOPs، إذن T_computation ≈ 2920 / 43 000 ≈ 68 ms. الأوزان 85M x 2 B = 170 MB تعطي 0.83 ms؛ والـ activations هي نفسها 0.45 ms. التنبؤ ≈ 68 ms. المقيس: 638 ms، أي 9.4x فوق الحد الأدنى، ونسبة استغلال تقارب 11%.
  5. Reading the two missesقراءة الفجوتين

    Both models land above the floor, as every real model does, but SegFormer misses by almost three times as much. The crude model counts only input and output activations. SegFormer's softmax attention materializes large intermediate attention maps that the formula never sees, which is exactly the memory traffic the EfficientViT paper set out to remove. The model is a first-order floor; the size of the miss tells you which term you forgot.
    كلا النموذجين يقع فوق الحد الأدنى، كما يحدث لكل نموذج حقيقي، لكن فجوة SegFormer تقارب ثلاثة أضعاف فجوة الآخر. النموذج المبسّط لا يحسب إلا الـ activations الداخلة والخارجة. أما الـ softmax attention في SegFormer فيُنشئ خرائط attention وسيطة كبيرة في الذاكرة لا تراها الصيغة أبدًا، وهذه بالضبط حركة الذاكرة التي سعت ورقة EfficientViT إلى إزالتها. النموذج حد أدنى من الرتبة الأولى؛ وحجم الفجوة يخبرك أي حد نسيته.
  6. Floor versus measurementالحد الأدنى مقابل القياس

    ModelالنموذجT_computationT_computationT_memoryT_memoryFloorالحد الأدنىMeasuredالمقيسMiss, utilisationالفجوة، نسبة الاستغلال
    EfficientViT-L1EfficientViT-L1564 / 43 000 = 13.1 ms564 / 43 000 = 13.1 ms0.39 + 0.45 = 0.84 ms0.39 + 0.45 = 0.84 ms13.1 ms13.1 ms45.9 ms45.9 ms3.5x, about 29%3.5x، قرابة 29%
    SegFormer-B5SegFormer-B52920 / 43 000 = 68 ms2920 / 43 000 = 68 ms0.83 + 0.45 = 1.3 ms0.83 + 0.45 = 1.3 ms68 ms68 ms638 ms638 ms9.4x, about 11%9.4x، قرابة 11%

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

A model is memory-bound on its current processor. Which change is guaranteed to reduce its latency under the max model?نموذج memory-bound على معالجه الحالي. أي تغيير مضمون أن يخفض الـ latency له وفق نموذج الـ max؟

Recall

Write the latency model with its sub-formulas and label every term NN or hardware.اكتب نموذج الـ latency مع صيغه الفرعية وصنّف كل حد على أنه NN أو hardware.

Latency ≈ max(T_computation, T_memory). T_computation ≈ operations in the model (NN) / operations per second (hardware). T_memory ≈ model size (NN) / bandwidth (hardware) + (input + output activation size) (NN) / bandwidth (hardware).
Latency ≈ max(T_computation, T_memory). T_computation ≈ عدد العمليات في النموذج (NN) / العمليات في الثانية (hardware). T_memory ≈ حجم النموذج (NN) / عرض النطاق (hardware) + (حجم الـ activation الداخلة + الخارجة) (NN) / عرض النطاق (hardware).

Recall

Why max and not plus?لماذا max وليس جمعًا؟

Data movement and computation overlap on real hardware: the next weights load while the current ones are being multiplied. The layer therefore finishes when the slower of the two streams finishes, which is the max.
نقل البيانات والحساب يتداخلان على العتاد الحقيقي: الأوزان التالية تُحمَّل بينما تُضرب الأوزان الحالية. لذلك تنتهي الطبقة عندما ينتهي الأبطأ من التيارين، وهذا هو الـ max.

Recall

In the SegFormer versus EfficientViT comparison, MACs drop 5.2x but latency drops 13.9x. What does that tell you?في مقارنة SegFormer مع EfficientViT تنخفض الـ MACs بمقدار 5.2x لكن الـ latency ينخفض بمقدار 13.9x. ماذا يخبرك ذلك؟

MACs are only the numerator of T_computation. SegFormer's attention moves large intermediate activations, so a memory term the simple model ignores inflates its latency, and operation count alone under-predicts it.
الـ MACs ليست إلا بسط T_computation. الـ attention في SegFormer ينقل activations وسيطة كبيرة، فيتضخم الـ latency بسبب حد ذاكرة يتجاهله النموذج المبسّط، ويصبح عدد العمليات وحده تنبؤًا أقل من الحقيقة.

The third cost is energy, and slide 9 settles where it goes with one table. In a 45 nm process at 0.9 V, a 32-bit integer add costs 0.1 pJ and a 32-bit integer multiply 3.1 pJ. Reading one 32-bit word from DRAM costs 640 pJ. The multiply is the most expensive arithmetic on the list, and the memory access is two hundred times dearer than it.

التكلفة الثالثة هي الطاقة، والشريحة 9 تحسم أين تذهب بجدول واحد. في تقنية تصنيع 45 nm عند 0.9 V، تكلف عملية جمع صحيحة بطول 32-bit مقدار 0.1 pJ، وعملية ضرب صحيحة بطول 32-bit مقدار 3.1 pJ. وقراءة كلمة واحدة بطول 32-bit من الـ DRAM تكلف 640 pJ. الضرب هو أغلى عملية حسابية في القائمة، والوصول إلى الذاكرة أغلى منه مئتي مرة.

Rough energy per operation, 45 nm, 0.9 V, 32-bit (slide 9, after Horowitz 2014 as tabulated by Han et al. 2015)الطاقة التقريبية لكل عملية، 45 nm، 0.9 V، 32-bit (الشريحة 9، عن Horowitz 2014 كما جدولها Han et al. 2015)

int ADD
0.1 pJ0.1 pJ
float ADD
0.9 pJ0.9 pJ
Register file accessالوصول إلى ملف السجلات (register file)
1 pJ1 pJ
int MULT
3.1 pJ3.1 pJ
float MULT
3.7 pJ3.7 pJ
SRAM cache accessالوصول إلى ذاكرة SRAM المخبئية (cache)
5 pJ5 pJ
DRAM memory accessالوصول إلى ذاكرة DRAM
640 pJ640 pJ
On a log axis each grid step is ten times more energy. The six arithmetic and on-chip bars fit in the first two decades; DRAM sits two more decades out from SRAM (640 / 5 = 128x) and almost four decades from the int ADD bar.على محور لوغاريتمي، كل خطوة في الشبكة تعني طاقة أكبر بعشرة أضعاف. الأعمدة الستة للحساب وللذاكرة داخل الشريحة تقع في أول مرتبتين عشريتين؛ أما DRAM فتبعد مرتبتين إضافيتين عن SRAM (640 / 5 = 128x) وقرابة أربع مراتب عن عمود int ADD.

Three ratios from that table are worth memorizing. DRAM over integer multiply is 640 / 3.1 ≈ 206, which the slide rounds to 200x. DRAM over SRAM is 640 / 5 = 128, so keeping data on-chip is worth two orders of magnitude on its own. And float add over integer add is 0.9 / 0.1 = 9, a seed for quantization: the same arithmetic in integers is nine times cheaper per add. Han et al. summarize the whole table in one line, that memory access is three orders of magnitude more energy expensive than simple arithmetic (640 / 0.1 = 6400).

ثلاث نسب من هذا الجدول تستحق الحفظ. الـ DRAM مقابل الضرب الصحيح هي 640 / 3.1 ≈ 206، وتقرّبها الشريحة إلى 200x. الـ DRAM مقابل SRAM هي 640 / 5 = 128، فإبقاء البيانات داخل الشريحة (on-chip) يساوي وحده مرتبتين عشريتين. والجمع العشري مقابل الجمع الصحيح هو 0.9 / 0.1 = 9، وهي بذرة للـ quantization: الحساب نفسه بالأعداد الصحيحة أرخص تسع مرات لكل عملية جمع. يلخص Han et al. الجدول كله في سطر واحد: الوصول إلى الذاكرة أغلى في الطاقة من الحساب البسيط بثلاث مراتب عشرية (640 / 0.1 = 6400).

The chain the slide writes across the top follows directly: data movement means more memory references, and more memory references mean more energy. Horowitz's own framing in the ISSCC 2014 talk is that a DRAM access at 1 to 2 nJ is a couple of orders of magnitude above a cache access or a functional operation at around 10 pJ, and that even with better I/O circuits DRAM will stay expensive, around 0.6 nJ per 8 bytes, because requests and data still have to travel a long physical distance. The slide's 640 pJ per 32-bit word is about half of the 1.3 nJ per 64-bit access in his Figure 1.1.9, as the provenance note below explains.

السلسلة التي تكتبها الشريحة في أعلاها تتبع مباشرة: data movement (نقل البيانات) يعني مراجع ذاكرة أكثر، ومراجع الذاكرة الأكثر تعني طاقة أكثر. إطار Horowitz نفسه في محاضرة ISSCC 2014 هو أن الوصول إلى الـ DRAM بتكلفة 1 إلى 2 nJ يزيد بمرتبتين عشريتين تقريبًا على الوصول إلى الـ cache أو على عملية وظيفية بتكلفة نحو 10 pJ، وأن الـ DRAM سيبقى مكلفًا حتى مع دوائر إدخال وإخراج (I/O) أفضل، بنحو 0.6 nJ لكل 8 بايتات، لأن الطلبات والبيانات ما زالت مضطرة لقطع مسافة فيزيائية طويلة. ورقم الشريحة 640 pJ لكل كلمة 32-bit يساوي تقريبًا نصف 1.3 nJ لكل وصول 64-bit في الشكل 1.1.9 عنده، كما تشرح ملاحظة المصدر أدناه.

The log axis of the slide's chart is not decoration either. On a linear axis the six small bars would be invisible next to DRAM; a 0.1 pJ bar next to a 640 pJ bar is a pixel next to a page. Only a logarithmic axis can show both, and it is the honest way to read any table whose entries span four decades.

المحور اللوغاريتمي في رسم الشريحة ليس زخرفة كذلك. على محور خطي ستختفي الأعمدة الستة الصغيرة بجانب الـ DRAM؛ فعمود 0.1 pJ بجانب عمود 640 pJ مثل بكسل بجانب صفحة. وحده المحور اللوغاريتمي يستطيع إظهار الاثنين، وهو الطريقة الأمينة لقراءة أي جدول تمتد قيمه عبر أربع مراتب عشرية.

Worked example: AlexNet's energy billمثال محلول: فاتورة الطاقة لـ AlexNet

Worked exampleمثال محلول

Arithmetic versus weight fetch for one AlexNet inferenceالحساب مقابل جلب الأوزان في استدلال واحد لـ AlexNet

  1. Arithmetic in 32-bit integersالحساب بأعداد صحيحة 32-bit

    AlexNet runs 724M MACs per image (part 04). Each is one multiply and one add: 724e6 x (3.1 + 0.1) pJ = 2.32 mJ.
    AlexNet ينفّذ 724M MACs لكل صورة (الجزء 04). كل واحدة منها ضرب واحد وجمع واحد: 724e6 x (3.1 + 0.1) pJ = 2.32 mJ.
  2. Weights read once from DRAMقراءة الأوزان مرة واحدة من DRAM

    AlexNet has 61M parameters (part 02). Reading each once as a 32-bit word: 61e6 x 640 pJ = 39.0 mJ. That is 16.8x the arithmetic, before a single activation has moved.
    لـ AlexNet 61M parameters (الجزء 02). قراءة كل منها مرة واحدة ككلمة 32-bit: 61e6 x 640 pJ = 39.0 mJ. أي 16.8x من الحساب، قبل أن تتحرك activation واحدة.
  3. Two alternativesبديلان

    Keep the same weights in SRAM: 61e6 x 5 pJ = 0.31 mJ, cheaper than the arithmetic. Or keep DRAM but use float arithmetic: 724e6 x (3.7 + 0.9) = 3.33 mJ, still twelve times below the DRAM bill.
    احتفظ بالأوزان نفسها في الـ SRAM: 61e6 x 5 pJ = 0.31 mJ، وهذا أرخص من الحساب. أو أبقِ الـ DRAM لكن استخدم الحساب العشري: 724e6 x (3.7 + 0.9) = 3.33 mJ، وما زال أقل من فاتورة الـ DRAM باثنتي عشرة مرة.
  4. Two orders of magnitude versus a factor of twoمرتبتان عشريتان مقابل عامل اثنين

    Changing where the weights live changes the total by two orders of magnitude; changing how the arithmetic is done changes it by less than a factor of two.
    تغيير مكان الأوزان يغيّر المجموع بمرتبتين عشريتين؛ أما تغيير طريقة إجراء الحساب فيغيّره بأقل من الضعف.
  5. The bill, line by lineالفاتورة، بندًا بندًا

    ItemالبندCountالعددEnergy eachالطاقة لكل عمليةTotalالمجموع
    Integer MACs (multiply + add)عمليات MAC صحيحة (ضرب + جمع)724M724M3.1 + 0.1 = 3.2 pJ3.1 + 0.1 = 3.2 pJ2.32 mJ2.32 mJ
    Weights read once from DRAMالأوزان مقروءة مرة واحدة من DRAM61M61M640 pJ640 pJ39.0 mJ39.0 mJ
    Same weights read from SRAMالأوزان نفسها مقروءة من SRAM61M61M5 pJ5 pJ0.31 mJ0.31 mJ
    Float MACs instead of integerعمليات MAC عشرية بدلًا من الصحيحة724M724M3.7 + 0.9 = 4.6 pJ3.7 + 0.9 = 4.6 pJ3.33 mJ3.33 mJ
CalculatorحاسبةEnergy of one inference, operation by operationطاقة استدلال واحد، عملية بعملية
1,000,000 × 0.1 pJ
100 nJ
0 × 0.9 pJ
0.0 pJ
0 × 1 pJ
0.0 pJ
1,000,000 × 3.1 pJ
3.10 µJ
0 × 3.7 pJ
0.0 pJ
100,000 × 5 pJ
500 nJ
10,000 × 640 pJ
6.40 µJ
bars use a log scale from 1 pJ to the largest row, so equal steps are ten times more energyتستخدم الأشرطة مقياسًا لوغاريتميًا من 1 pJ حتى أكبر صف، فكل خطوة متساوية تعني طاقة أكبر بعشر مرات
linear share of the total: light is arithmetic, teal is memory accessالحصة الخطية من المجموع: الفاتح هو العمليات الحسابية، والأخضر المزرق هو الوصول إلى الذاكرة
Total energy10.1 µJ
Arithmetic3.20 µJadds and multipliesعمليات الجمع والضرب
Memory access6.90 µJDRAM alone: 63% of totalDRAM وحدها: 63% من المجموع

Per-operation costs are the 45 nm, 0.9 V figures from slide 9 (Horowitz 2014, as tabulated by Han et al. 2015). The AlexNet presets use 724M MACs as one multiply plus one add each, and one fetch per weight for its 61M weights.تكاليف كل عملية هي أرقام 45 nm و0.9 V من الشريحة 9 (Horowitz 2014، كما جدولها Han et al. 2015). تستخدم إعداداتAlexNet المسبقة 724M عملية MAC، تُحسب كل منها عملية ضرب واحدة وعملية جمع واحدة، وجلبًا واحدًا لكل وزن من أوزانها البالغة 61M.

Every efficiency technique later in the course can be read as a way of cutting that memory line. Keep weights in on-chip SRAM, which is 128x cheaper per access than DRAM. Reuse each fetched value as many times as possible before it leaves the chip; Horowitz notes that efficient designs complete on the order of a thousand operations per DRAM fetch. And make the model small enough to fit on-chip in the first place, which is what model size in part 02, peak activations in part 03, and pruning and quantization later in the course are all about.

كل تقنية كفاءة لاحقة في المقرر يمكن قراءتها على أنها طريقة لخفض بند الذاكرة هذا. احتفظ بالأوزان في الـ SRAM داخل الشريحة، فهي أرخص بمقدار 128x لكل وصول من الـ DRAM. أعد استخدام كل قيمة مجلوبة أكبر عدد ممكن من المرات قبل أن تغادر الشريحة؛ ويذكر Horowitz أن التصاميم الكفؤة تنجز نحو ألف عملية لكل جلب من الـ DRAM. واجعل النموذج صغيرًا بما يكفي ليتسع داخل الشريحة من الأساس، وهذا هو موضوع model size في الجزء 02، وpeak activations في الجزء 03، والـ pruning والـ quantization لاحقًا في المقرر.

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

In the 45 nm table, how does one 32-bit DRAM access compare with one 32-bit integer multiply?في جدول 45 nm، كيف يقارن وصول واحد بطول 32-bit إلى DRAM بعملية ضرب صحيحة واحدة بطول 32-bit؟

Recall

Quote the DRAM to integer multiply energy ratio, the numbers behind it, and the process conditions.اذكر نسبة طاقة DRAM إلى الضرب الصحيح، والأرقام التي خلفها، وظروف تقنية التصنيع.

640 pJ / 3.1 pJ ≈ 206, quoted as 200x. Conditions: 45 nm, 0.9 V, 32-bit operations, Horowitz ISSCC 2014 via Han et al. 2015.
640 pJ / 3.1 pJ ≈ 206، وتُذكر على أنها 200x. الظروف: 45 nm، و0.9 V، وعمليات 32-bit، من Horowitz ISSCC 2014 عبر Han et al. 2015.

Recall

For one AlexNet inference, how does the energy of fetching every weight once from DRAM compare with the energy of all its integer MACs?في استدلال واحد لـ AlexNet، كيف تقارن طاقة جلب كل وزن مرة واحدة من DRAM بطاقة كل عمليات MAC الصحيحة فيه؟

61M x 640 pJ = 39 mJ for the weights against 724M x 3.2 pJ = 2.3 mJ for the arithmetic, about 17x. Moving the weights to SRAM drops the fetch cost to 0.31 mJ.
61M x 640 pJ = 39 mJ للأوزان مقابل 724M x 3.2 pJ = 2.3 mJ للحساب، أي قرابة 17x. نقل الأوزان إلى الـ SRAM يخفض تكلفة الجلب إلى 0.31 mJ.

Recapالخلاصة

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

  • Three goals (smaller, faster, greener) map to three costs (storage, latency, energy). All three are driven by two resources, computation and memory, so the metrics split into memory-related and computation-related.ثلاثة أهداف (أصغر، أسرع، أكثر خضرة) تقابلها ثلاث تكاليف (التخزين، الـ latency، الطاقة). والثلاث كلها يحركها مَوردان فقط هما الحساب والذاكرة، لذلك تنقسم المقاييس إلى مقاييس متعلقة بالذاكرة ومقاييس متعلقة بالحساب.
  • Latency is the delay for one task. SegFormer-B5 takes 638 ms and EfficientViT-L1 takes 45.9 ms on Jetson AGX Orin with TensorRT, fp16, batch size 1: a 13.9x gap at equal or better mIoU.الـ latency هو التأخير لمهمة واحدة. يستغرق SegFormer-B5 مدة 638 ms ويستغرق EfficientViT-L1 مدة 45.9 ms على Jetson AGX Orin مع TensorRT وfp16 وbatch size يساوي 1: فارق 13.9x مع mIoU مساوٍ أو أفضل.
  • A latency number without hardware, runtime, precision and batch size is not comparable to any other.رقم latency لا يذكر العتاد والـ runtime والدقة العددية والـ batch size لا يمكن مقارنته بأي رقم آخر.
  • Throughput is items per second. Batching raises it (Design 2: 40 image/s) while raising latency (100 ms). Throughput equals 1 / latency only with batch size 1 and no parallelism.الـ throughput هو عدد العناصر في الثانية. الـ batching يرفعه (التصميم 2: 40 image/s) ويرفع معه الـ latency (100 ms). لا يساوي الـ throughput مقلوب الـ latency (1 / latency) إلا مع batch size يساوي 1 ومن دون أي توازي.
  • Latency ≈ max(T_computation, T_memory) because data movement and computation overlap. Numerators are NN specification, denominators are hardware specification.Latency ≈ max(T_computation, T_memory) لأن نقل البيانات والحساب يتداخلان زمنيًا. البسوط مواصفات الشبكة (NN)، والمقامات مواصفات العتاد.
  • A faster processor helps only when compute-bound. A memory-bound model needs smaller weights, smaller activations or more bandwidth.المعالج الأسرع يفيد فقط عندما يكون النموذج compute-bound. أما النموذج الـ memory-bound فيحتاج أوزانًا أصغر أو activations أصغر أو عرض نطاق (bandwidth) أكبر.
  • At 45 nm and 0.9 V: int ADD 0.1 pJ, int MULT 3.1 pJ, SRAM 5 pJ, DRAM 640 pJ. One DRAM access costs about 206 int multiplies, and a float ADD costs 9 int ADDs.عند 45 nm و0.9 V: int ADD تكلف 0.1 pJ، وint MULT تكلف 3.1 pJ، وSRAM تكلف 5 pJ، وDRAM تكلف 640 pJ. وصول واحد إلى DRAM يكلف قرابة 206 عملية ضرب صحيحة، وfloat ADD واحدة تكلف 9 أضعاف int ADD.
  • Fetching AlexNet's 61M weights once from DRAM (39 mJ) costs about 17 times its 724M MACs of integer arithmetic (2.3 mJ).جلب أوزان AlexNet البالغة 61M مرة واحدة من DRAM (39 mJ) يكلف قرابة 17 ضعف حساباتها الصحيحة البالغة 724M MACs (2.3 mJ).

Sourcesالمصادر

Part 02: Parameters and model sizeالمعاملات وحجم النموذج

Counting weights for linear, convolution, grouped and depthwise layers, totaling AlexNet to 61M parameters, and converting parameter count into storage at different bit widths.عدّ الأوزان في الطبقات الخطية والالتفافية والمجمّعة والعمقية، وجمعها في AlexNet لتبلغ 61M معاملًا، وتحويل عدد المعاملات إلى مساحة تخزين عند عروض بت مختلفة.

4 concepts4 مفاهيم, ، slides 10-17الشرائح 10-17

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

Every result you will meet later in this course, from pruning AlexNet "9x smaller" to Deep Compression's "35x" storage cut, is a ratio of two numbers computed the way this part teaches. Before you can judge pruning, quantization or an architecture search, you must be able to count what a network stores, and the same count decides whether a model fits into a microcontroller's flash.

Lecture 02 taught how linear, convolution, grouped and depthwise layers compute their outputs. This part does not repeat that. It asks one narrower question of each layer: how many numbers does it keep? Four short formulas answer it, AlexNet gives them a real workout, and one multiplication by the bit width turns the answer into megabytes. Along the way you will find a typo on the slide, a hidden value to compute yourself, and a simulator you can use to check every parameter and model-size number here and the MAC and FLOP numbers in part 04.

كل نتيجة ستقابلها لاحقًا في هذا المقرر، من تقليم AlexNet ليصبح "أصغر 9x" إلى خفض التخزين "35x" في Deep Compression، هي نسبة بين عددين يُحسبان بالطريقة التي يعلّمها هذا الجزء. قبل أن تستطيع الحكم على التقليم (pruning) أو التكميم (quantization) أو البحث عن المعمارية، يجب أن تعرف كيف تعدّ ما تخزّنه الشبكة، وهذا العدد نفسه هو ما يقرّر هل يتّسع النموذج في ذاكرة الفلاش (flash) لمتحكّم دقيق (microcontroller) أم لا.

علّمت المحاضرة 02 كيف تحسب الطبقات الخطية وطبقات الالتفاف والمجمّعة والعمقية مخرجاتها. هذا الجزء لا يكرّر ذلك، بل يطرح على كل طبقة سؤالًا أضيق: كم عددًا تحتفظ به؟ أربع صيغ قصيرة تجيب عن السؤال، و AlexNet يمنحها تمرينًا حقيقيًا، وضربة واحدة في عرض البت تحوّل الجواب إلى ميغابايت. وفي الطريق ستجد خطأ مطبعيًا في الشريحة، وقيمة مخفية عليك حسابها بنفسك، ومحاكيًا يمكنك استخدامه للتحقق من كل رقم للمعاملات ولحجم النموذج هنا، ومن أرقام MAC و FLOP في الجزء 04.

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

  1. Define #Parameters as the element count of all weight tensors and explain why n, h and w never enter it.تعريف عدد المعاملات (#Parameters) بأنه عدد عناصر جميع موترات الأوزان، وشرح لماذا لا تدخل فيه n و h و w أبدًا.
  2. Write and derive the four counting formulas (linear, convolution, grouped, depthwise) and justify the single division by g.كتابة صيغ العدّ الأربع (الخطية، والالتفاف، والمجمّع، والعمقي) واشتقاقها، وتبرير القسمة الواحدة على g.
  3. Total AlexNet layer by layer, identify where 96 percent of the weights sit, and spot the slide's conv1 typo.جمع معاملات AlexNet طبقة بطبقة، وتحديد أين تقع 96 بالمئة من الأوزان، واكتشاف الخطأ المطبعي في conv1 على الشريحة.
  4. Convert a parameter count to model size at any bit width in bits, KB and MB, stating the decimal convention.تحويل عدد المعاملات إلى حجم نموذج عند أي عرض بت بالبت و KB و MB، مع ذكر الاصطلاح العشري المستخدم.
  5. Predict how architecture choices (groups, depthwise filters, linear layer width) move model size before touching hardware.توقّع كيف تغيّر قرارات المعمارية (المجموعات، والمرشّحات العمقية، وعرض الطبقة الخطية) حجم النموذج قبل لمس العتاد.

Start with the smallest network worth drawing: five inputs x0 to x4 and three outputs y0 to y2, every input wired to every output. Count the arrows. Each input sends one arrow to each output, so there are 3 × 5 = 15 arrows, labelled from w00 up to w42. Each arrow carries exactly one learned number, its weight. That is the whole idea of the number of parameters: it is the synapse count, the number of weights, and for this layer it is 15.

ابدأ بأصغر شبكة تستحق الرسم: خمسة مُدخلات من x0 إلى x4 وثلاثة مخرجات من y0 إلى y2، وكل مُدخل موصول بكل مخرج. عُدّ الأسهم. كل مُدخل يرسل سهمًا واحدًا إلى كل مخرج، فيكون عدد الأسهم 3 × 5 = 15، مُرقّمة من w00 حتى w42. كل سهم يحمل عددًا متعلَّمًا واحدًا بالضبط، هو وزنه. هذه هي الفكرة كلها في عدد المعاملات (number of parameters): إنه عدد المشابك (synapses)، أي عدد الأوزان، وفي هذه الطبقة يساوي 15.

Five inputs fully wired to three outputs: 15 arrows, 15 weights, c_o · c_iخمسة مُدخلات موصولة بالكامل بثلاثة مخرجات: 15 سهمًا، 15 وزنًا، أي c_o · c_i

Now look at the same layer the way a framework stores it. The inputs for a batch are a matrix X of shape n × c_i, the weights are a matrix W of shape c_o × c_i, and the outputs come out as Y = X · W^T of shape n × c_o. The fifteen arrows have become the fifteen elements of W. This is the second, equivalent definition the slides give: #Parameters is the number of elements in the network's weight tensors. For one linear layer that is the product of its two dimensions.

انظر الآن إلى الطبقة نفسها كما يخزّنها إطار العمل (framework). مُدخلات الدفعة مصفوفة X شكلها n × c_i، والأوزان مصفوفة W شكلها c_o × c_i، والمخرجات تخرج على شكل Y = X · W^T بالشكل n × c_o. الأسهم الخمسة عشر أصبحت العناصر الخمسة عشر في W. هذا هو التعريف الثاني المكافئ الذي تعطيه الشرائح: #Parameters هو عدد العناصر في موترات الأوزان (weight tensors) في الشبكة. وفي طبقة خطية (linear layer) واحدة يساوي ذلك حاصل ضرب بُعديها.

#Paramslinear=co⋅ci\#\text{Params}_{\text{linear}} = c_o \cdot c_i
One weight per input and output pair, bias ignoredوزن واحد لكل زوج من المدخل والمخرج، مع تجاهل الانحياز

One thing in that matrix picture deserves a second look. The batch size n appears in X and in Y, but never in W. Feed the layer one image or a thousand and the weight matrix is the same c_o × c_i block. Parameter count is a property of the model, not of the data flowing through it. Hold on to this observation: it returns in the next concept for spatial size, and it is the reason parameters and MACs behave so differently in part 04.

شيء واحد في صورة المصفوفات هذه يستحق نظرة ثانية. حجم الدفعة (batch size) n يظهر في X وفي Y، لكنه لا يظهر أبدًا في W. أدخِل إلى الطبقة صورة واحدة أو ألف صورة، وستبقى مصفوفة الأوزان هي الكتلة نفسها c_o × c_i. عدد المعاملات خاصية للنموذج، لا للبيانات التي تمرّ عبره. احتفظ بهذه الملاحظة: ستعود في المفهوم التالي مع الحجم المكاني، وهي سبب اختلاف سلوك المعاملات عن MACs اختلافًا كبيرًا في الجزء 04.

The slides define the count for the "given" (slide 10) or "entire" (slide 11) neural network; both mean the same thing, the sum over every layer. They also say bias is ignored, and this lecture keeps that convention throughout. In PyTorch a linear layer stores a bias vector of length c_o and a convolution stores one bias per output channel, so the true count is larger by c_o per layer (PyTorch Conv2d documentation). Next to c_o · c_i that is a rounding error, which is why the lecture drops it.

تعرّف الشرائح العدد للشبكة العصبية "المعطاة" (given، الشريحة 10) أو "الكاملة" (entire، الشريحة 11)؛ والمعنى واحد في الحالتين، وهو المجموع على كل الطبقات. وتقول الشرائح أيضًا إن الانحياز (bias) مُهمَل، وهذه المحاضرة تلتزم بهذا الاصطلاح من أولها إلى آخرها. في PyTorch تخزّن الطبقة الخطية متجه انحياز طوله c_o، ويخزّن الالتفاف انحيازًا واحدًا لكل قناة مخرج، فيكون العدد الحقيقي أكبر بمقدار c_o لكل طبقة (توثيق Conv2d في PyTorch). وبجانب c_o · c_i هذا فرق تقريب لا يُذكر، ولهذا تُسقطه المحاضرة.

Notation used on slides 11 to 14 and in the rest of this lectureالرموز المستخدمة في الشرائح من 11 إلى 14 وفي بقية هذه المحاضرة

n
Batch size, the number of inputs processed togetherحجم الدفعة، أي عدد المُدخلات التي تُعالَج معًا
c_i, c_o
Input and output channels (for a linear layer, input and output features)قنوات المُدخل والمخرج (وفي الطبقة الخطية: خصائص features المُدخل والمخرج)
h_i, h_o
Input and output heightارتفاع المُدخل والمخرج
w_i, w_o
Input and output widthعرض المُدخل والمخرج
k_h, k_w
Kernel height and widthارتفاع النواة (kernel) وعرضها
g
Number of groups in a grouped convolutionعدد المجموعات في الالتفاف المجمّع

Layer mechanics live in lecture 02آلية عمل الطبقات موجودة في المحاضرة 02

How a linear layer, a convolution, padding and stride, grouped and depthwise convolution and pooling produce their outputs, including the output-shape arithmetic behind numbers like 96 × 55 × 55, was covered in lecture 02: neurons and linear layers, convolution layers, padding, stride and grouped convolution and pooling.

كيف تُنتج الطبقة الخطية، والالتفاف، والحشو (padding) والخطوة (stride)، والالتفاف المجمّع والعمقي، والتجميع (pooling) مخرجاتها، بما في ذلك حساب شكل المخرج الذي يقف خلف أرقام مثل 96 × 55 × 55، كل ذلك شُرح في المحاضرة 02: العصبونات والطبقات الخطية، طبقات الالتفاف، الحشو والخطوة والالتفاف المجمّع، و التجميع.

Recall

A linear layer maps 512 inputs to 256 outputs. How many parameters does it have, ignoring bias, and does the batch size change that number?طبقة خطية تحوّل 512 مُدخلًا إلى 256 مخرجًا. كم معاملًا فيها مع إهمال الانحياز، وهل يغيّر حجم الدفعة هذا العدد؟

256 × 512 = 131,072. The batch size changes nothing: n never appears in a weight tensor.
256 × 512 = 131,072. حجم الدفعة لا يغيّر شيئًا: n لا تظهر أبدًا في موتّر أوزان.

Take AlexNet's third convolution: 384 filters, each looking at 256 input channels through a 3 × 3 window. One filter is a small brick of numbers, 256 deep and 3 × 3 across, so it holds 256 · 9 = 2,304 weights. There are 384 such bricks, so the layer holds 384 · 2,304 = 884,736 weights. That is the whole convolution formula for the #Parameters, read off the shape of the weight tensor.

خُذ الالتفاف الثالث في AlexNet: 384 مرشّحًا (filter)، كل منها ينظر إلى 256 قناة مُدخل عبر نافذة 3 × 3. المرشّح الواحد قالب صغير من الأعداد، عمقه 256 وعرضه 3 × 3، فيحمل 256 · 9 = 2,304 وزنًا. وهناك 384 قالبًا من هذا النوع، فتحمل الطبقة 384 · 2,304 = 884,736 وزنًا. هذه هي صيغة #Parameters كاملة لطبقة convolution (الالتفاف)، تُقرأ مباشرة من شكل موتّر الأوزان.

#Paramsconv=co⋅ci⋅kh⋅kw\#\text{Params}_{\text{conv}} = c_o \cdot c_i \cdot k_h \cdot k_w
c_o filters, each c_i × k_h × k_wc_o من المرشحات، كل منها c_i × k_h × k_w

Notice what is missing. The input is 256 × 13 × 13 and the output is 384 × 13 × 13, yet 13 appears nowhere in the count. A convolution slides the same brick to every output position, so the weights are shared across space. Goodfellow, Bengio and Courville make the point with an edge detector: a two-element kernel applied to a 320 × 280 image stores two parameters, where a dense matrix doing the same job would store billions (Goodfellow et al., section 9.2). The kernel size and the channel counts set the parameter count; the output height and width do not. Part 04 will show that MACs do multiply by h_o · w_o, because the brick is reapplied at every position, and that is the single most important difference between the two metrics.

لاحظ ما هو غائب. المُدخل 256 × 13 × 13 والمخرج 384 × 13 × 13، ومع ذلك لا يظهر 13 في أي مكان من العدد. الالتفاف يُزلق القالب نفسه إلى كل موضع في المخرج، فالأوزان مشتركة عبر المكان. ويوضّح Goodfellow و Bengio و Courville هذه النقطة بكاشف حواف: نواة من عنصرين تُطبَّق على صورة 320 × 280 تخزّن معاملين فقط، بينما مصفوفة كثيفة تؤدي العمل نفسه ستخزّن مليارات (Goodfellow et al.، القسم 9.2). kernel size (حجم النواة) وأعداد القنوات هي التي تحدّد عدد المعاملات؛ أما ارتفاع المخرج وعرضه (output height and width) فلا. وسيُظهر الجزء 04 أن MACs تُضرب فعلًا في h_o · w_o، لأن القالب يُعاد تطبيقه في كل موضع، وهذا هو الفرق الأهم على الإطلاق بين المقياسين.

Grouped convolution: derive it, do not memorize itالالتفاف المجمّع: اشتقّه ولا تحفظه

A grouped convolution with g groups cuts the c_i input channels into g slices of c_i/g and the c_o output channels into g slices of c_o/g, then runs g ordinary convolutions side by side, each reading only its own input slice and writing only its own output slice. So count one group with the formula you already have: (c_o/g) · (c_i/g) · k_h · k_w. There are g groups, so multiply by g. One factor of g cancels.

grouped convolution (الالتفاف المجمّع) ذو g groups يقطع قنوات المُدخل c_i إلى g شرائح، في كل منها c_i/g، ويقطع قنوات المخرج c_o إلى g شرائح، في كل منها c_o/g، ثم يشغّل g التفافات عادية جنبًا إلى جنب، كل منها يقرأ شريحة المُدخل الخاصة به فقط ويكتب في شريحة المخرج الخاصة به فقط. إذن عُدّ مجموعة واحدة بالصيغة التي تعرفها: (c_o/g) · (c_i/g) · k_h · k_w. وهناك g مجموعات، فاضرب في g. فيُحذف أحد عاملَي g.

#Paramsgrouped=cog⋅cig⋅khkw⋅g=co⋅ci⋅kh⋅kwg\#\text{Params}_{\text{grouped}} = \frac{c_o}{g}\cdot\frac{c_i}{g}\cdot k_h k_w \cdot g = \frac{c_o \cdot c_i \cdot k_h \cdot k_w}{g}
Each group is smaller by g squared, but there are g of themكل مجموعة أصغر بمقدار g تربيع، لكن عددها g
Four filters, each eight channels deep. With g = 2 each filter keeps only its half of the channels, and the lit volume is half of the resting stack.أربعة مرشّحات، عمق كل منها ثماني قنوات. عند g = 2 يحتفظ كل مرشّح بنصف القنوات الخاص به فقط، والحجم المضيء نصف الكومة الساكنة.
Eight input and eight output channels. With g = 2 the 32 cross-group links disappear and two 4 × 4 blocks remain: 64 becomes 32, one division by g.ثماني قنوات مُدخل وثماني قنوات مخرج. عند g = 2 تختفي الوصلات الـ 32 العابرة بين المجموعتين وتبقى كتلتان 4 × 4: يصبح 64 هو 32، أي قسمة واحدة على g.

This matches what a framework actually stores. PyTorch gives Conv2d a weight of shape (out_channels, in_channels / groups, kH, kW) and requires that in_channels and out_channels are both divisible by groups (PyTorch documentation). The historical reason AlexNet has g = 2 in three of its layers is hardware, not elegance: the network was trained across two GTX 580 GPUs with 3 GB each, and "the kernels of the second, fourth, and fifth convolutional layers are connected only to those kernel maps in the previous layer which reside on the same GPU" (Krizhevsky et al., 2012, section 3.5). Grouping halved the weights each GPU had to hold.

هذا يطابق ما يخزّنه إطار العمل فعلًا. يعطي PyTorch الطبقة Conv2d وزنًا شكله (out_channels, in_channels / groups, kH, kW)، ويشترط أن يقبل كل من in_channels و out_channels القسمة على groups (توثيق PyTorch). والسبب التاريخي لوجود g = 2 في ثلاث من طبقات AlexNet هو العتاد، لا الأناقة: دُرّبت الشبكة موزّعة على معالجين رسوميين GTX 580 في كل منهما 3 GB، و"أنوية طبقات الالتفاف الثانية والرابعة والخامسة متصلة فقط بخرائط الأنوية في الطبقة السابقة الموجودة على المعالج الرسومي نفسه" (Krizhevsky et al., 2012، القسم 3.5). التجميع قسم الأوزان التي يجب أن يحملها كل معالج رسومي إلى النصف.

Depthwise convolution: the limit g = c_i = c_oالالتفاف العمقي: الحالة القصوى g = c_i = c_o

Push the grouping to its extreme, one channel per group, and you get a depthwise convolution. Now g = c_i = c_o and every channel has its own private k_h × k_w filter. In the MobileNets paper this is stated directly: "the depthwise convolution applies a single filter to each input channel" (Howard et al., 2017, section 3.1). Plug g = c_i into the grouped formula and the c_i cancels.

ادفع التجميع إلى أقصاه، قناة واحدة لكل مجموعة، فتحصل على depthwise convolution (الالتفاف العمقي). الآن g = c_i = c_o، ولكل قناة مرشّحها الخاص k_h × k_w. وورقة MobileNets تقول ذلك صراحة: "الالتفاف العمقي يطبّق مرشّحًا واحدًا على كل قناة مُدخل" (Howard et al., 2017، القسم 3.1). عوّض g = c_i في صيغة المجمّع فيُحذف c_i.

#Paramsdepthwise=co⋅ci⋅kh⋅kwci=co⋅kh⋅kw\#\text{Params}_{\text{depthwise}} = \frac{c_o \cdot c_i \cdot k_h \cdot k_w}{c_i} = c_o \cdot k_h \cdot k_w
One k_h × k_w filter per channelمرشح k_h × k_w واحد لكل قناة

Worked exampleمثال محلول

One 3 × 3 layer with 256 input and 256 output channelsطبقة 3 × 3 واحدة فيها 256 قناة مُدخل و 256 قناة مخرج

  1. Standard convolutionالالتفاف العادي

    256 · 256 · 3 · 3 = 589,824 weights.
    256 · 256 · 3 · 3 = 589,824 وزنًا.
  2. Grouped with g = 2مجمّع مع g = 2

    589,824 / 2 = 294,912. Each of the two groups is a 128 → 128 convolution with 147,456 weights.
    589,824 / 2 = 294,912. كل واحدة من المجموعتين التفاف 128 → 128 فيه 147,456 وزنًا.
  3. Grouped with g = 8مجمّع مع g = 8

    589,824 / 8 = 73,728. Eight groups of 32 → 32.
    589,824 / 8 = 73,728. ثماني مجموعات، كل منها 32 → 32.
  4. Depthwise, g = 256عمقي، g = 256

    256 · 9 = 2,304. Each channel keeps one 3 × 3 filter.
    256 · 9 = 2,304. كل قناة تحتفظ بمرشّح 3 × 3 واحد.
  5. Ratio to the standard layerالنسبة إلى الطبقة العادية

    LayerالطبقةCountالعدّ#Parameters#ParametersRatioالنسبة
    Standard, g = 1عادي، g = 1256 · 256 · 9256 · 256 · 9589,824589,82411
    Grouped, g = 2مجمّع، g = 2256 · 256 · 9 / 2256 · 256 · 9 / 2294,912294,9121/21/2
    Grouped, g = 8مجمّع، g = 8256 · 256 · 9 / 8256 · 256 · 9 / 873,72873,7281/81/8
    Depthwise, g = 256عمقي، g = 256256 · 9256 · 92,3042,3041/2561/256
    LayerالطبقةCountالعدّ#Parameters#ParametersRatioالنسبة
    Standard, g = 1عادي، g = 1256 · 256 · 9256 · 256 · 9589,824589,82411
    Grouped, g = 2مجمّع، g = 2256 · 256 · 9 / 2256 · 256 · 9 / 2294,912294,9121/21/2
    Grouped, g = 8مجمّع، g = 8256 · 256 · 9 / 8256 · 256 · 9 / 873,72873,7281/81/8
    Depthwise, g = 256عمقي، g = 256256 · 9256 · 92,3042,3041/2561/256
Layerالطبقة#Parameters#ParametersDepends on h, w?يعتمد على h و w؟Depends on n?يعتمد على n؟Whyالسبب
Linearخطية (Linear)c_o · c_ic_o · c_iOnly through c_i after a flattenفقط عبر c_i بعد التسطيحNoلاEvery input feature is wired to every output feature; a flattened c_i carries h × wكل خاصية مُدخل موصولة بكل خاصية مخرج؛ و c_i المسطّحة تحمل h × w
Convolutionالتفاف (Convolution)c_o · c_i · k_h · k_wc_o · c_i · k_h · k_wNoلاNoلاc_o filters, each c_i deep and k_h × k_w widec_o مرشّحًا، عمق كل منها c_i وعرضه k_h × k_w
Grouped convolutionالتفاف مجمّع (Grouped convolution)c_o · c_i · k_h · k_w / gc_o · c_i · k_h · k_w / gNoلاNoلاg independent convolutions on c_i/g and c_o/g channelsg التفافات مستقلة على c_i/g و c_o/g قناة
Depthwise convolutionالتفاف عمقي (Depthwise convolution)c_o · k_h · k_wc_o · k_h · k_wNoلاNoلاGrouped with g = c_i = c_o, one filter per channelمجمّع مع g = c_i = c_o، مرشّح واحد لكل قناة
The four counting formulas and what they ignoreصيغ العدّ الأربع وما تتجاهله

The simulator collects all four formulas, adds the bit width from the final concept of this part and the MACs from part 04, and carries a preset for every parameterized AlexNet layer. Try conv3 now and confirm 884,736, then change h_o and watch which readouts move.

يجمع المحاكي الصيغ الأربع كلها، ويضيف عرض البت من المفهوم الأخير في هذا الجزء و MACs من الجزء 04، وفيه إعداد جاهز لكل طبقة ذات معاملات في AlexNet. جرّب conv3 الآن وتأكّد من 884,736، ثم غيّر h_o وراقب أي القراءات تتحرّك.

SimulatorمحاكيLayer metrics calculator: parameters, model size, MACsحاسبة مقاييس الطبقة: المعاملات وحجم النموذج وMACs
AlexNet presets, slide 16إعدادات AlexNet المسبقة، الشريحة 16
Parametersالمعاملات884,736weightsc_o · c_i · k_h · k_w, matches slide 16c_o · c_i · k_h · k_w، يطابق الشريحة 16
Model size (bits)حجم النموذج (بت)28.31 Mbitsparameters × 32 bitsparameters × 32 بت
Model size (MB)حجم النموذج (MB)3.539MB3,538.9 KB, decimal 10^6 as on slide 17, about 3.375 MiB3,538.9 KB، بالنظام العشري 10^6 كما في الشريحة 17، أي نحو 3.375 MiB
MACsMACs149,520,384parameters × h_o × w_o, here × 169, about 149.52 Mparameters × h_o × w_o، وهنا × 169، أي نحو 149.52 M
FLOPsFLOPs299.04 MFLOPs2 × MACs, one multiply and one add2 × MACs، عملية ضرب واحدة وعملية جمع واحدة
Reads per filterالقراءات لكل مرشح256input channelseach filter sees all of c_iكل مرشح يرى جميع قنوات c_i

Change h_o or w_o and watch MACs move while the parameter count and model size stay still. Only c_i, c_o, k_h, k_w and g touch the weight count, and only the bit width turns that count into storage.غيّر h_o أو w_o وراقب تغيّر MACs بينما يبقى عدد المعاملات وحجم النموذج ثابتين. وحدهاc_i وc_o وk_h وk_w وg تؤثر في عدد الأوزان، ووحده عرض البت يحوّل هذا العدد إلى مساحة تخزين.

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

A 3 × 3 convolution has c_i = 64 and c_o = 128. How many parameters does it have?التفاف 3 × 3 فيه c_i = 64 و c_o = 128. كم عدد معاملاته؟

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

Doubling the input resolution of a CNN does what to a convolution layer's parameter count and MAC count?ماذا تفعل مضاعفة دقة المُدخل لشبكة CNN بعدد معاملات طبقة الالتفاف وبعدد MACs فيها؟

Recall

Write the grouped-convolution count two ways and say why the division is by g, not g squared.اكتب عدد معاملات الالتفاف المجمّع بطريقتين، وقل لماذا تكون القسمة على g وليس على g تربيع.

(c_o/g) · (c_i/g) · k_h · k_w · g = c_o · c_i · k_h · k_w / g. Each group shrinks both channel dimensions by g, a factor of 1/g², but there are g such groups, which multiplies back by g.
(c_o/g) · (c_i/g) · k_h · k_w · g = c_o · c_i · k_h · k_w / g. كل مجموعة تُصغّر بُعدَي القنوات كليهما بمقدار g، أي بمعامل 1/g²، لكن هناك g مجموعات من هذا النوع، وهذا يضرب النتيجة مجددًا في g.

AlexNet is the convolutional network whose variant won the ILSVRC-2012 challenge with a top-5 test error of 15.3%, and its authors state up front that it has "60 million parameters and 650,000 neurons" (Krizhevsky et al., 2012). It is also the best possible exercise for this part: five convolutions, three of them grouped, three max-pools and three linear layers, with input 3 × 224 × 224. Walk it top to bottom and apply the right #Parameters formula at each layer. The output shapes come from lecture 02; here we only count.

AlexNet هي الشبكة الالتفافية التي فاز أحد إصداراتها بتحدي ILSVRC-2012 بخطأ اختبار top-5 قدره 15.3%، ويذكر مؤلفوها منذ البداية أن فيها "60 مليون معامل و 650,000 عصبون" (Krizhevsky et al., 2012). وهي أيضًا أفضل تمرين ممكن لهذا الجزء: خمسة التفافات، ثلاثة منها grouped (مجمّعة)، وثلاث طبقات max-pool، وثلاث linear layers (طبقات خطية)، والمُدخل 3 × 224 × 224. امشِ فيها من الأعلى إلى الأسفل وطبّق صيغة #Parameters المناسبة عند كل طبقة. أشكال المخرجات تأتي من المحاضرة 02؛ وهنا نعدّ فقط.

Worked exampleمثال محلول

Counting AlexNet, part one: the convolutionsعدّ معاملات AlexNet، الجزء الأول: طبقات الالتفاف

  1. conv1: 11 × 11, 96 channels, stride 4, pad 2conv1: نواة 11 × 11، و 96 قناة، وخطوة 4، وحشو 2

    Output 96 × 55 × 55. Weights 96 · 3 · 11 · 11 = 34,848. The stride and padding set the 55, but they do not touch the count.
    المخرج 96 × 55 × 55. الأوزان 96 · 3 · 11 · 11 = 34,848. الخطوة والحشو يحدّدان الـ 55، لكنهما لا يمسّان العدد.
  2. maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2

    Output 96 × 27 × 27. Pooling takes a maximum; it learns nothing and stores nothing: 0 parameters.
    المخرج 96 × 27 × 27. التجميع يأخذ القيمة العظمى؛ فلا يتعلّم شيئًا ولا يخزّن شيئًا: 0 معاملات.
  3. conv2: 5 × 5, 256 channels, pad 2, groups 2conv2: نواة 5 × 5، و 256 قناة، وحشو 2، ومجموعتان

    Output 256 × 27 × 27. Grouped, so 256 · 96 · 5 · 5 / 2 = 307,200.
    المخرج 256 × 27 × 27. مجمّع، إذن 256 · 96 · 5 · 5 / 2 = 307,200.
  4. maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2

    Output 256 × 13 × 13, 0 parameters.
    المخرج 256 × 13 × 13، و 0 معاملات.
  5. conv3: 3 × 3, 384 channels, pad 1conv3: نواة 3 × 3، و 384 قناة، وحشو 1

    Output 384 × 13 × 13. 384 · 256 · 3 · 3 = 884,736.
    المخرج 384 × 13 × 13. 384 · 256 · 3 · 3 = 884,736.
  6. conv4: 3 × 3, 384 channels, pad 1, groups 2conv4: نواة 3 × 3، و 384 قناة، وحشو 1، ومجموعتان

    Output 384 × 13 × 13. 384 · 384 · 3 · 3 / 2 = 663,552.
    المخرج 384 × 13 × 13. 384 · 384 · 3 · 3 / 2 = 663,552.
  7. conv5: 3 × 3, 256 channels, pad 1, groups 2conv5: نواة 3 × 3، و 256 قناة، وحشو 1، ومجموعتان

    Output 256 × 13 × 13. The slide hides this one behind a question mark. Compute it before you reveal the answer below.
    المخرج 256 × 13 × 13. الشريحة تُخفي هذه القيمة خلف علامة استفهام. احسبها قبل أن تكشف الجواب أدناه.

Recall

conv5: 3 × 3 kernels, 256 output channels, 384 input channels, groups 2. How many parameters?conv5: أنوية 3 × 3، و 256 قناة مخرج، و 384 قناة مُدخل، ومجموعتان. كم عدد المعاملات؟

256 · 384 · 9 / 2 = 442,368. Without the grouping it would be 884,736, the same as conv3.
256 · 384 · 9 / 2 = 442,368. ولولا التجميع لكان 884,736، أي مثل conv3 تمامًا.

Worked exampleمثال محلول

Counting AlexNet, part two: the linear layers and the totalعدّ معاملات AlexNet، الجزء الثاني: الطبقات الخطية والمجموع

  1. maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2

    Output 256 × 6 × 6, 0 parameters. This map is flattened into a vector of 256 · 6 · 6 = 9,216 values.
    المخرج 256 × 6 × 6، و 0 معاملات. هذه الخريطة تُسطَّح (flatten) إلى متجه فيه 256 · 6 · 6 = 9,216 قيمة.
  2. fc6: linear, 4096 outputsfc6: خطية، 4096 مخرجًا

    4096 · 9,216 = 37,748,736. One layer, and already more than half of the network.
    4096 · 9,216 = 37,748,736. طبقة واحدة، وهي وحدها أكثر من نصف الشبكة.
  3. fc7: linear, 4096 outputsfc7: خطية، 4096 مخرجًا

    4096 · 4096 = 16,777,216.
    4096 · 4096 = 16,777,216.
  4. fc8: linear, 1000 outputsfc8: خطية، 1000 مخرج

    1000 · 4096 = 4,096,000, one output per ImageNet class.
    1000 · 4096 = 4,096,000، مخرج واحد لكل صنف في ImageNet.
  5. Totalالمجموع

    2,332,704 in the convolutions plus 58,621,952 in the linear layers gives 60,954,656, about 61M, which is what the slide and the paper both round to.
    2,332,704 في طبقات الالتفاف زائد 58,621,952 في الطبقات الخطية يعطي 60,954,656، أي نحو 61M، وهو ما تقرّب إليه الشريحة والورقة كلتاهما.
LayerالطبقةOutput C × H × Wالمخرج C × H × WCountالعدّ#Parameters#ParametersShareالنسبة
Imageالصورة3 × 224 × 2243 × 224 × 22400
conv1, 11 × 11, 96 ch, stride 4, pad 2conv1، نواة 11 × 11، و 96 قناة، وخطوة 4، وحشو 296 × 55 × 5596 × 55 × 5596 · 3 · 11 · 1196 · 3 · 11 · 1134,84834,8480.06%0.06%
maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 296 × 27 × 2796 × 27 × 2700
conv2, 5 × 5, 256 ch, pad 2, groups 2conv2، نواة 5 × 5، و 256 قناة، وحشو 2، ومجموعتان256 × 27 × 27256 × 27 × 27256 · 96 · 5 · 5 / 2256 · 96 · 5 · 5 / 2307,200307,2000.50%0.50%
maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2256 × 13 × 13256 × 13 × 1300
conv3, 3 × 3, 384 ch, pad 1conv3، نواة 3 × 3، و 384 قناة، وحشو 1384 × 13 × 13384 × 13 × 13384 · 256 · 3 · 3384 · 256 · 3 · 3884,736884,7361.45%1.45%
conv4, 3 × 3, 384 ch, pad 1, groups 2conv4، نواة 3 × 3، و 384 قناة، وحشو 1، ومجموعتان384 × 13 × 13384 × 13 × 13384 · 384 · 3 · 3 / 2384 · 384 · 3 · 3 / 2663,552663,5521.09%1.09%
conv5, 3 × 3, 256 ch, pad 1, groups 2conv5، نواة 3 × 3، و 256 قناة، وحشو 1، ومجموعتان256 × 13 × 13256 × 13 × 13256 · 384 · 3 · 3 / 2256 · 384 · 3 · 3 / 2442,368442,3680.73%0.73%
maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2256 × 6 × 6256 × 6 × 600
fc6, linear 4096fc6، خطية 4096409640964096 · (256 · 6 · 6)4096 · (256 · 6 · 6)37,748,73637,748,73661.93%61.93%
fc7, linear 4096fc7، خطية 4096409640964096 · 40964096 · 409616,777,21616,777,21627.52%27.52%
fc8, linear 1000fc8، خطية 1000100010001000 · 40961000 · 40964,096,0004,096,0006.72%6.72%
Five convolutionsطبقات الالتفاف الخمس2,332,7042,332,7043.83%3.83%
Three linear layersالطبقات الخطية الثلاث58,621,95258,621,95296.17%96.17%
Totalالمجموع60,954,65660,954,656100%100%
AlexNet parameters per layer, with the corrected conv1 value and each layer's share of the totalمعاملات AlexNet لكل طبقة، مع قيمة conv1 المصحّحة ونسبة كل طبقة من المجموع

Where the weights actually areأين توجد الأوزان فعلًا

The share column is the real lesson. The five convolutions together hold 2.3M weights, under 4 percent. The three linear layers hold 58.6M, about 96 percent, and fc6 alone holds 62 percent. Krizhevsky and colleagues knew this: a footnote in the paper says that "most of the net's parameters are in the first fully-connected layer" (Krizhevsky et al., 2012). Every input of fc6 is wired to every one of its 4,096 outputs, and it has 9,216 inputs, so the dense wiring the convolutions avoided comes back in one enormous matrix.

عمود النسبة هو الدرس الحقيقي. طبقات الالتفاف الخمس مجتمعة تحمل 2.3M وزن، أي أقل من 4 بالمئة. والطبقات الخطية الثلاث تحمل 58.6M، أي نحو 96 بالمئة، و fc6 وحدها تحمل 62 بالمئة. وكان Krizhevsky وزملاؤه يعرفون ذلك: حاشية في الورقة تقول إن "معظم معاملات الشبكة موجودة في الطبقة الأولى الموصولة بالكامل" (Krizhevsky et al., 2012). كل مُدخل في fc6 موصول بكل واحد من مخرجاتها الـ 4,096، ولها 9,216 مُدخلًا، فيعود التوصيل الكثيف الذي تجنّبته طبقات الالتفاف في مصفوفة واحدة ضخمة.

There is a subtle exception hiding in that 9,216. The previous concept said parameter counts do not depend on input resolution, and for every convolution that is true. But fc6 takes the flattened 256 × 6 × 6 map as its c_i, and the 6 × 6 came from 224 × 224 through the strides and pools. Feed AlexNet a larger image and fc6 would need a larger weight matrix. The first linear layer after a flatten is the one place where resolution leaks into the parameter count. Global average pooling, introduced in Network in Network, removes that leak: it averages each channel to one number, and "there is no parameter to optimize" in it (Lin, Chen and Yan, 2014), so the classifier input no longer grows with the image.

هناك استثناء دقيق مختبئ في ذلك الرقم 9,216. قال المفهوم السابق إن عدد المعاملات لا يعتمد على دقة المُدخل (resolution)، وهذا صحيح لكل طبقة التفاف. لكن fc6 تأخذ الخريطة المسطّحة 256 × 6 × 6 على أنها c_i، والـ 6 × 6 جاءت من 224 × 224 عبر الخطوات وطبقات التجميع. أدخِل إلى AlexNet صورة أكبر وستحتاج fc6 إلى مصفوفة أوزان أكبر. الطبقة الخطية الأولى بعد التسطيح هي المكان الوحيد الذي تتسرّب فيه الدقة إلى عدد المعاملات. والتجميع المتوسط الشامل (global average pooling)، الذي قُدّم في Network in Network، يسدّ هذا التسرّب: فهو يحسب متوسط كل قناة في عدد واحد، و"لا يوجد فيه أي معامل يُحسَّن" (Lin, Chen and Yan, 2014)، فلا يعود مُدخل المصنِّف يكبر مع الصورة.

This table is the starting line for two results you will study later. Han and colleagues pruned AlexNet "from 61 million to 6.7 million" parameters with no loss of accuracy, a 9x reduction (Han et al., 2015), and Deep Compression then cut its storage "from 240MB to 6.9MB" (Han, Mao and Dally, 2016). Both papers attack exactly the layers this table flags: when 96 percent of the weights sit in three linear layers, that is where pruning pays.

هذا الجدول هو خط البداية لنتيجتين ستدرسهما لاحقًا. قلّم Han وزملاؤه AlexNet "من 61 مليونًا إلى 6.7 مليون" معامل دون أي خسارة في الدقة، أي تقليص 9x (Han et al., 2015)، ثم خفّض Deep Compression تخزينها "من 240MB إلى 6.9MB" (Han, Mao and Dally, 2016). الورقتان كلتاهما تهاجمان بالضبط الطبقات التي يشير إليها هذا الجدول: حين تقع 96 بالمئة من الأوزان في ثلاث طبقات خطية، فهناك يؤتي التقليم ثماره.

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

In AlexNet, which group of layers holds most of the 61M parameters?في AlexNet، أي مجموعة من الطبقات تحمل معظم المعاملات البالغة 61M؟

Recall

Which AlexNet layers hold about 96 percent of the parameters, and why is fc6 so large?أي طبقات AlexNet تحمل نحو 96 بالمئة من المعاملات، ولماذا fc6 كبيرة إلى هذا الحد؟

The three linear layers fc6, fc7 and fc8, with 58.6M of 61M. fc6 is the largest because its input is the flattened 256 × 6 × 6 = 9,216 map, fully wired to 4,096 outputs.
الطبقات الخطية الثلاث fc6 و fc7 و fc8، بـ 58.6M من أصل 61M. و fc6 هي الأكبر لأن مُدخلها هو الخريطة المسطّحة 256 × 6 × 6 = 9,216، الموصولة بالكامل بـ 4,096 مخرجًا.

A parameter count is a number of boxes. To know how much storage the boxes take, you need the size of each box. Store AlexNet's 61M weights as 32-bit floating point and each weight takes 4 bytes, so the whole network takes 61M × 4 B = 244 MB. Store the same weights as 8-bitintegers and the same network takes 61 MB. Nothing about the architecture changed; only the bit width did.

عدد المعاملات هو عدد صناديق. ولتعرف كم من التخزين تأخذ الصناديق، تحتاج إلى حجم كل صندوق. خزّن أوزان AlexNet البالغة 61M بصيغة الفاصلة العائمة 32-bit فيأخذ كل وزن 4 bytes، فتأخذ الشبكة كلها 61M × 4 B = 244 MB. وخزّن الأوزان نفسها أعدادًا صحيحة 8-bit فتأخذ الشبكة نفسها 61 MB. لم يتغيّر شيء في المعمارية؛ الذي تغيّر هو bit width (عرض البت) فقط.

Model Size=#Parameters⋅Bit Width\text{Model Size} = \#\text{Parameters} \cdot \text{Bit Width}
Valid when the whole network uses one data type. The result is in bits.صالحة حين تستخدم الشبكة كلها نوع بيانات واحدًا. والنتيجة بالبت.

That is the definition of model size: the storage needed for the weights of the network, the #Parameters times the bits each one occupies. The slide lists MB, KB and bits as the common units, and the formula as written produces bits. To reach bytes divide by 8, and to reach megabytes divide by another 10^6. The slide is explicit that its megabyte is decimal, 244 × 10^6 bytes. For a single layer, kilobytes are the natural unit: AlexNet's conv3 holds 884,736 weights, which at 8 bits is 884,736 bytes, or 884.7 KB (divide bytes by 10^3), and at 32 bits 3,538.9 KB.

هذا هو تعريف model size (حجم النموذج): التخزين المطلوب لأوزان الشبكة، أي #Parameters مضروبًا في عدد البتات التي يشغلها كل منها. تذكر الشريحة MB و KB والبت بوصفها الوحدات الشائعة، والصيغة كما هي مكتوبة تنتج بتات. للوصول إلى البايت اقسم على 8، وللوصول إلى الميغابايت اقسم مرة أخرى على 10^6. والشريحة صريحة في أن الميغابايت عندها عشري، أي 244 × 10^6 بايت. أما للطبقة الواحدة فالكيلوبايت هو الوحدة الطبيعية: الطبقة conv3 في AlexNet تحمل 884,736 وزنًا، وهذا عند 8 بت يساوي 884,736 بايت، أي 884.7 KB (اقسم البايتات على 10^3)، وعند 32 بت يساوي 3,538.9 KB.

SizeMB=#Parameters⋅bits per weight8⋅106\text{Size}_{\text{MB}} = \frac{\#\text{Parameters} \cdot \text{bits per weight}}{8 \cdot 10^{6}}
Bits to decimal megabytesمن البتات إلى الميغابايت العشرية
The same 61M weights on four shelves. Each halving of the bit width halves the shelf, and int8 lands at a quarter of fp32.الأوزان نفسها البالغة 61M على أربعة رفوف. كل تنصيف لعرض البت ينصّف الرف، فيصل int8 إلى ربع fp32.
Data typeنوع البياناتSize in bitsالحجم بالبتSize (decimal MB)الحجم (MB عشرية)Slide roundingتقريب الشريحة
32-bit float (fp32)فاصلة عائمة 32-bit (fp32)1,950.5 Mbit1,950.5 Mbit243.8 MB243.8 MB244 MB244 MB
16-bit (fp16 or bfloat16)16-bit (fp16 أو bfloat16)975.3 Mbit975.3 Mbit121.9 MB121.9 MB122 MB122 MB
8-bit integer (int8)عدد صحيح 8-bit (int8)487.6 Mbit487.6 Mbit61.0 MB61.0 MB61 MB61 MB
4-bit integer (int4)عدد صحيح 4-bit (int4)243.8 Mbit243.8 Mbit30.5 MB30.5 MB30.5 MB30.5 MB
1-bit (binary weights)1-bit (أوزان ثنائية)61.0 Mbit61.0 Mbit7.6 MB7.6 MB7.6 MB7.6 MB
AlexNet model size at each bit width, using the exact count 60,954,656. The last column is what the slide's rounded 61M gives.حجم نموذج AlexNet عند كل عرض بت، باستخدام العدد الدقيق 60,954,656. والعمود الأخير هو ما يعطيه الرقم المقرّب 61M في الشريحة.

The formula assumes one data type for the whole network. Mixed-precision models break that assumption, and then the size is a sum over layers, each layer's count times its own bit width. That is the form you need when, for example, the first and last layers stay at 8 bits while the middle is pushed to 4.

تفترض الصيغة نوع بيانات واحدًا للشبكة كلها. النماذج مختلطة الدقة (mixed-precision) تكسر هذا الافتراض، وعندها يكون الحجم مجموعًا على الطبقات، عدد كل طبقة مضروبًا في عرض البت الخاص بها. وهذه هي الصيغة التي تحتاجها حين تبقى الطبقتان الأولى والأخيرة مثلًا عند 8 bits بينما يُدفع الوسط إلى 4.

Model Size=∑l#Paramsl⋅bitsl\text{Model Size} = \sum_{l} \#\text{Params}_l \cdot \text{bits}_l
Mixed precision: sum per layerالدقة المختلطة: اجمع لكل طبقة

Why the multiplication matters for this courseلماذا يهمّ هذا الضرب في هذا المقرر

The parameter count is fixed once the architecture is chosen, but the bit width is a knob. The lecture on quantization turns that knob, and this formula says exactly what it buys: every halving of the bit width halves the model size with no change to the architecture. Deep Compression combined pruning, quantization and Huffman coding to take AlexNet "from 240MB to 6.9MB" (Han, Mao and Dally, 2016); their 240 MB starting point is, up to rounding, the 244 MB computed in this concept. Part 03 shows a microcontroller SRAM budget of 256 kB (the MCU constraint, a budget for activations). The slides do not give a flash size, but a typical MCU of this class, such as the STM32F746 used by MCUNet (Lin et al., NeurIPS 2020), has about 1 MB of flash, where the weights would live; even 8-bitAlexNet at 61 MB is about 60 times too large for that flash, which is why edge deployment starts with counting.

عدد المعاملات ثابت بمجرد اختيار المعمارية، لكن عرض البت مقبض قابل للتدوير. محاضرة التكميم تدير هذا المقبض، وهذه الصيغة تقول بالضبط ما الذي تشتريه: كل تنصيف لعرض البت ينصّف حجم النموذج دون أي تغيير في المعمارية. جمع Deep Compression بين التقليم والتكميم وترميز Huffman لينقل AlexNet "من 240MB إلى 6.9MB" (Han, Mao and Dally, 2016)؛ ونقطة بدايتهم 240 MB هي، بعد التقريب، الرقم 244 MB المحسوب في هذا المفهوم. يعرض الجزء 03 ميزانية SRAM لمتحكّم دقيق قدرها 256 kB (MCU constraint، وهي ميزانية للتنشيطات). الشرائح لا تعطي حجم ذاكرة الفلاش، لكن متحكّمًا نموذجيًا من هذه الفئة، مثل STM32F746 الذي يستخدمه MCUNet (Lin et al., NeurIPS 2020)، فيه نحو 1 MB من flash، وهي المكان الذي ستعيش فيه الأوزان؛ وحتى AlexNet بدقة 8-bit عند 61 MB أكبر من تلك الفلاش بنحو 60 مرة، ولهذا يبدأ النشر على الأطراف (edge deployment) بالعدّ.

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

A 10M-parameter model stored entirely in 16-bit numbers occupies how much storage?نموذج فيه 10M معامل مخزّن بالكامل بأعداد 16-bit، كم يشغل من التخزين؟

Recall

61M parameters at 8 bits: what is the model size in MB, and in bits?61M معامل عند 8 بت: ما حجم النموذج بوحدة MB، وبالبت؟

61 MB, that is 61 × 10^6 bytes, because at 8 bits each weight is exactly one byte. In bits it is 61M × 8 = 488 × 10^6 bits.
61 MB، أي 61 × 10^6 بايت، لأن كل وزن عند 8 bits يساوي بايتًا واحدًا بالضبط. وبالبت يساوي 61M × 8 = 488 × 10^6 بت.

Recapالخلاصة

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

  • #Parameters is the number of elements in the weight tensors of the whole network. Bias is ignored in this lecture.عدد المعاملات (#Parameters) هو عدد العناصر في موترات الأوزان للشبكة كاملة. الانحياز (bias) مُهمَل في هذه المحاضرة.
  • Linear c_o · c_i. Convolution c_o · c_i · k_h · k_w. Grouped divides that by g. Depthwise is c_o · k_h · k_w.الخطية c_o · c_i. الالتفاف c_o · c_i · k_h · k_w. المجمّع يقسم ذلك على g. والعمقي c_o · k_h · k_w.
  • Parameter count ignores batch size and spatial size. MACs (part 04) do not, because every filter is reapplied at every output position.عدد المعاملات لا يتأثر بحجم الدفعة ولا بالحجم المكاني. أما MACs (الجزء 04) فتتأثر، لأن كل مرشّح يُعاد تطبيقه عند كل موضع في المخرج.
  • AlexNet has 60,954,656 parameters, about 61M. conv1 is 34,848 (the slide prints 24,848) and the hidden conv5 is 442,368.في AlexNet يوجد 60,954,656 معاملًا، أي نحو 61M. الطبقة conv1 فيها 34,848 (والشريحة تطبع 24,848)، والطبقة conv5 المخفية فيها 442,368.
  • The three linear layers hold about 96 percent (58.6M). fc6 alone is 37.7M because its input is the flattened 256 × 6 × 6 map.الطبقات الخطية الثلاث تحمل نحو 96 بالمئة (58.6M). الطبقة fc6 وحدها 37.7M لأن مُدخلها هو الخريطة المسطّحة 256 × 6 × 6.
  • Pooling layers have zero parameters.طبقات التجميع (pooling) معاملاتها صفر.
  • Model size = #Parameters × bit width. 61M weights are 244 MB at 32-bit, 122 MB at 16-bit and 61 MB at 8-bit, in decimal MB.حجم النموذج = #Parameters × عرض البت. 61M وزن تساوي 244 MB عند 32-bit، و 122 MB عند 16-bit، و 61 MB عند 8-bit، بوحدات MB العشرية.

Sourcesالمصادر

Part 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.

4 concepts4 مفاهيم, ، slides 18-22الشرائح 18-22

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المصادر

Part 04: MACs, FLOPs and operationsعمليات MAC وFLOP والعمليات

Counting computation as multiply-accumulate operations for matrix products and each layer type, AlexNet's 724M MACs, and converting to FLOPs, FLOPS, OPs and OPS.عدّ الحساب بوصفه عمليات ضرب وتجميع (MAC) لضرب المصفوفات ولكل نوع من الطبقات، و724M عملية MAC في AlexNet، والتحويل إلى FLOPs وFLOPS وOPs وOPS.

5 concepts5 مفاهيم, ، slides 23-29الشرائح 23-29

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

Part 02 measured what a network stores and part 03 what it must hold in memory. This part measures what it must compute. That number is the numerator of computation time in the Latency model of slide 8, the figure every accelerator datasheet is quoted against, and the figure you will report for any model in the research project.

The counting unit is the multiply-accumulate, and everything else is bookkeeping on top of it: how many MACs a matrix product costs, how many each layer type costs, what AlexNet adds up to, and how the count becomes FLOPs, OPs and finally milliseconds on a specific chip. The surprise waiting at the centre of the part is that the layers holding almost all of AlexNet's weights do almost none of its arithmetic. Every exam question on efficiency metrics circles back to that inversion.

قاس الجزء 02 ما تخزّنه الشبكة، وقاس الجزء 03 ما يجب أن تحمله في الذاكرة. أما هذا الجزء فيقيس ما يجب أن تحسبه. هذا الرقم هو بسط زمن الحساب computation time في نموذج latency (زمن التأخير) في الشريحة 8، وهو الرقم الذي تُقاس عليه كل نشرة بيانات (datasheet) لأي مسرّع، والرقم الذي ستذكره لأي نموذج في مشروع البحث.

وحدة العدّ هي عملية الضرب والتجميع (multiply-accumulate)، وكل ما عداها حسابات مبنية فوقها: كم MAC يكلف ضرب المصفوفات، وكم يكلف كل نوع من الطبقات، وما مجموع AlexNet، وكيف يتحول العدد إلى FLOPs وOPs ثم إلى ملّي ثوانٍ على شريحة محددة. المفاجأة التي تنتظرك في قلب هذا الجزء أن الطبقات التي تحمل تقريبًا كل أوزان AlexNet لا تؤدي تقريبًا أي شيء من حساباته. كل سؤال امتحان عن مقاييس الكفاءة يعود إلى هذا الانقلاب.

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

  1. Define a multiply-accumulate operation and count MACs for a matrix-vector product and a matrix-matrix product.تعرّف عملية الضرب والتجميع (multiply-accumulate) وتحسب عدد MACs لضرب مصفوفة في متجه ولضرب مصفوفة في مصفوفة.
  2. Fill the MACs table for linear, convolution, grouped and depthwise layers, and explain why conv MACs equal parameters times h_o·w_o.تملأ جدول MACs للطبقات الخطية والالتفافية والمجمّعة (grouped) والعمقية (depthwise)، وتشرح لماذا تساوي MACs الالتفاف عدد المعاملات مضروبًا في h_o·w_o.
  3. Reproduce AlexNet's 724M MACs layer by layer and explain why convolutions dominate compute while linear layers dominate parameters.تعيد حساب الـ 724M MACs في AlexNet طبقةً طبقة، وتشرح لماذا تهيمن طبقات الالتفاف على الحساب بينما تهيمن الطبقات الخطية على المعاملات.
  4. Convert MACs to FLOPs and OPs, and tell a count (FLOPs, OPs) apart from a rate (FLOPS, OPS).تحوّل MACs إلى FLOPs و OPs، وتميّز العدد (FLOPs و OPs) عن المعدل (FLOPS و OPS).
  5. Estimate T_computation for a model on a device from the model's OPs and the device's OPS.تقدّر T_computation لنموذج على جهاز معين من عدد OPs للنموذج ومن OPS للجهاز.

Start with one neuron that has four inputs. Its output before the activation function is w₁x₁ + w₂x₂ + w₃x₃ + w₄x₄. A processor evaluates that as four identical steps: multiply a weight by an input, add the product to a running total. Each step is one multiply-accumulate operation, and the whole of neural network inference is this step repeated a few hundred million times.

ابدأ بعصبون واحد له أربعة مدخلات. مخرجه قبل دالة التفعيل هو w₁x₁ + w₂x₂ + w₃x₃ + w₄x₄. يحسب المعالج ذلك في أربع خطوات متطابقة: اضرب وزنًا في مدخل، ثم أضف الناتج إلى مجموع جارٍ. كل خطوة هي عملية ضرب وتجميع واحدة (multiply-accumulate operation)، والاستدلال (inference) في الشبكة العصبية بأكمله هو هذه الخطوة مكررة بضع مئات من ملايين المرات.

a←a+b⋅ca \leftarrow a + b \cdot c
One MAC: multiply b by c and accumulate into aعملية MAC واحدة: اضرب b في c وراكم الناتج في a

The definition is standard well beyond this course. The Wikipedia entry on the multiply-accumulate operation gives exactly a ← a + (b × c), and notes that IEEE 754-2008 defines a fused multiply-add that performs the whole step with a single rounding. Sze, Chen, Yang and Emer call the MAC "the fundamental component" of DNN processing in their survey of efficient DNN hardware. Counting MACs is therefore counting what the hardware executes, not an abstraction layered over it.

هذا التعريف معتمد على نطاق أوسع بكثير من هذا المقرر. مدخل ويكيبيديا عن عملية الضرب والتجميع يعطي بالضبط a ← a + (b × c)، ويذكر أن IEEE 754-2008 يعرّف عملية ضرب وجمع مدمجة (fused multiply-add) تنفّذ الخطوة كلها بتقريب واحد فقط. ويصف Sze وChen وYang وEmer عملية MAC بأنها "المكوّن الأساسي" (the fundamental component) لمعالجة الشبكات العصبية العميقة (DNN) في مسحهم لعتاد DNN الكفء. لذلك فإن عدّ MACs هو عدّ لما ينفّذه العتاد فعلًا، وليس تجريدًا مبنيًا فوقه.

From one neuron to a matrix-vector productمن عصبون واحد إلى ضرب مصفوفة في متجه

A layer of m neurons that each read the same n inputs is a matrix-vector multiplication: an m × n weight matrix times an n-vector. Each of the m outputs needs n MACs, one per weight in its row, so the product costs m · n MACs. Notice that this is also the number of weights. In a matrix-vector product, every weight is touched exactly once.

طبقة من m عصبون يقرأ كل منها المدخلات نفسها البالغ عددها n هي ضرب مصفوفة في متجه (matrix-vector multiplication): مصفوفة أوزان حجمها m × n مضروبة في متجه طوله n. كل مخرج من المخرجات الـ m يحتاج n من MACs، واحدة لكل وزن في صفه، لذا يكلف الضرب m · n من MACs. لاحظ أن هذا هو أيضًا عدد الأوزان. في ضرب مصفوفة في متجه، يُستخدم كل وزن مرة واحدة بالضبط.

Feed the layer a batch of inputs at once and the vector becomes a matrix. The slide renames the dimensions here: the input count becomes k and the letter n is reused for the batch, the number of columns of B. The result is a general matrix-matrix multiplication: A of size m × k times B of size k × n gives C of size m × n. Every one of the m · n cells of C is a dot product over the shared dimension k, so it costs k MACs, and the whole product costs m · n · k.

إذا أدخلت إلى الطبقة دفعة (batch) من المدخلات مرة واحدة يصبح المتجه مصفوفة. هنا تعيد الشريحة تسمية الأبعاد: عدد المدخلات يصبح k، ويُعاد استخدام الحرف n للدفعة، أي عدد أعمدة B. الناتج هو الضرب العام لمصفوفة في مصفوفة (general matrix-matrix multiplication): A بحجم m × k مضروبة في B بحجم k × n تعطي C بحجم m × n. كل خلية من خلايا C البالغ عددها m · n هي ضرب نقطي (dot product) على البعد المشترك k، فتكلف k من MACs، ويكلف الضرب كله m · n · k.

MACsMV=m⋅nMACsGEMM=m⋅n⋅k\text{MACs}_{MV} = m \cdot n \qquad \text{MACs}_{GEMM} = m \cdot n \cdot k
Rows times columns for a vector, rows times columns times the shared dimension for a matrixالصفوف في الأعمدة للمتجه، والصفوف في الأعمدة في البعد المشترك للمصفوفة
Each cell of C is one dot product over the shared dimension: k pairs, k MACs. There are m × n such cells.كل خلية في C هي ضرب نقطي واحد على البعد المشترك: k من الأزواج، أي k من MACs. وهناك m × n خلية من هذا النوع.

Dimensions used in the GEMM countالأبعاد المستخدمة في عدّ GEMM

m
Rows of A and of C: the number of outputs (neurons)صفوف A وصفوف C: عدد المخرجات (العصبونات)
k
Columns of A and rows of B: the shared dimension summed over, the number of inputs per neuronأعمدة A وصفوف B: البعد المشترك الذي يُجمع عليه، أي عدد المدخلات لكل عصبون
n
Columns of B and of C: the number of input vectors, the batchأعمدة B وأعمدة C: عدد متجهات المدخل، أي الدفعة (batch)

Why a whole network reduces to thisلماذا تُختزل الشبكة كلها إلى هذا

A Linear layer is literally a matrix-vector product at batch size one and a GEMM for a batch. Convolution looks different but is executed the same way: frameworks reorder the input patches into columns (the im2col trick described in the CS231n notes) so that a convolution becomes one large GEMM, and the MobileNets paper points out that this is why convolutions can ride on "highly optimized general matrix multiply (GEMM) functions". NVIDIA's performance guide states the cost of a GEMM as 2 · M · N · K floating point operations because "each FMA is 2 operations, a multiply and an add", which previews the FLOP conversion at the end of this part. Once you can count MACs for a matrix product, you can count them for every layer.

الطبقة الخطية (linear layer) هي حرفيًا ضرب مصفوفة في متجه عندما يكون حجم الدفعة (batch size) واحدًا، وهي GEMM عند وجود دفعة. الالتفاف يبدو مختلفًا لكنه يُنفّذ بالطريقة نفسها: تعيد أطر العمل ترتيب رقع المدخل (patches) في أعمدة (حيلة im2col الموصوفة في ملاحظات CS231n) فيصبح الالتفاف عملية GEMM واحدة كبيرة، وتشير ورقة MobileNets إلى أن هذا هو سبب قدرة الالتفاف على الاعتماد على "دوال ضرب المصفوفات العامة (GEMM) المحسّنة للغاية". ويذكر دليل الأداء من NVIDIA أن كلفة GEMM هي 2 · M · N · K عملية فاصلة عائمة لأن "كل FMA عبارة عن عمليتين، ضرب وجمع"، وهذا تمهيد لتحويل FLOP في آخر هذا الجزء. بمجرد أن تستطيع عدّ MACs لضرب المصفوفات، تستطيع عدّها لكل طبقة.

Recall

Write the MAC operation, and state how many MACs an m × n matrix times an n-vector costs.اكتب عملية MAC، واذكر كم MAC يكلف ضرب مصفوفة m × n في متجه طوله n.

a ← a + b · c. The product costs m · n MACs, one for every weight in the matrix.
a ← a + b · c. يكلف الضرب m · n من MACs، واحدة لكل وزن في المصفوفة.

Take the first layer of AlexNet. One filter spans all 3 input channels with an 11 × 11 window, so it holds 3 × 11 × 11 = 363 weights. Producing a single output pixel is one dot product of those 363 weights against a patch of the image: 363 MACs. But the filter does not produce one pixel. It slides to every one of the 55 × 55 = 3,025 output positions, and there are 96 filters. So the layer costs 363 × 3,025 × 96 = 105,415,200 MACs from only 34,848 weights.

خذ الطبقة الأولى من AlexNet. يمتد المرشح (filter) الواحد على قنوات المدخل الـ 3 كلها بنافذة 11 × 11، فيحمل 3 × 11 × 11 = 363 وزنًا. إنتاج بكسل واحد في المخرج هو ضرب نقطي واحد لهذه الأوزان الـ 363 مع رقعة من الصورة: 363 من MACs. لكن المرشح لا ينتج بكسلًا واحدًا، بل ينزلق إلى كل موضع من مواضع المخرج البالغة 55 × 55 = 3,025، وهناك 96 مرشحًا. لذا تكلف الطبقة 363 × 3,025 × 96 = 105,415,200 من MACs انطلاقًا من 34,848 وزنًا فقط.

That example contains the whole rule. In a convolution layer every weight is applied once per output position, so the MAC count is the parameter count from part 02 multiplied by the output height and width:

هذا المثال يحتوي القاعدة كلها. في طبقة الالتفاف (convolution layer) يُطبَّق كل وزن مرة واحدة لكل موضع في المخرج، لذا فإن عدد MACs هو عدد المعاملات (parameter count) من الجزء 02 مضروبًا في ارتفاع المخرج وعرضه (output height and width):

MACsconv=co⋅ci⋅kh⋅kw⏟parameters⋅ho⋅wo\text{MACs}_{conv} = \underbrace{c_o \cdot c_i \cdot k_h \cdot k_w}_{\text{parameters}} \cdot h_o \cdot w_o
Parameters times output positions, at batch size n = 1 and ignoring biasالمعاملات مضروبة في مواضع المخرج، بحجم دفعة n = 1 ومع تجاهل الانحياز
Nine weights stamped onto twenty-five output positions: 225 MACs from 9 parameters. The filter is reused, never copied.تسعة أوزان تُطبع على خمسة وعشرين موضعًا في المخرج: 225 من MACs انطلاقًا من 9 معاملات. المرشح يُعاد استخدامه ولا يُنسخ أبدًا.

The other rows of the slide's table follow from the same idea. A Linear layer has h_o = w_o = 1, so its MACs equal its parameters, c_o · c_i. A Grouped convolution splits the channels into g groups, so each filter sees only c_i / g input channels and both parameters and MACs are divided by g. A Depthwise convolution is the extreme case g = c_i = c_o: each filter sees exactly one channel, so c_i disappears from the formula entirely and only c_o · k_h · k_w weights remain, each still applied h_o · w_o times.

بقية صفوف جدول الشريحة تنتج من الفكرة نفسها. الطبقة الخطية (linear layer) لها h_o = w_o = 1، لذا فإن MACs فيها تساوي معاملاتها، c_o · c_i. والالتفاف المجمّع (grouped convolution) يقسم القنوات إلى g groups (أي g من المجموعات)، فلا يرى كل مرشح إلا c_i / g من قنوات المدخل، وتُقسم المعاملات وMACs كلاهما على g. أما الالتفاف العمقي (depthwise convolution) فهو الحالة القصوى g = c_i = c_o: كل مرشح يرى قناة واحدة بالضبط، فيختفي c_i من الصيغة تمامًا ولا يبقى إلا c_o · k_h · k_w وزنًا، كل منها لا يزال يُطبَّق h_o · w_o مرة.

LayerالطبقةParametersالمعاملاتMACsMACsMACs per parameterMACs لكل معامل
Linearخطية (Linear)c_o · c_ic_o · c_ic_o · c_ic_o · c_i11
Convolutionالتفاف (Convolution)c_o · c_i · k_h · k_wc_o · c_i · k_h · k_wc_o · c_i · k_h · k_w · h_o · w_oc_o · c_i · k_h · k_w · h_o · w_oh_o · w_oh_o · w_o
Grouped convolutionالتفاف مجمّع (Grouped convolution)c_o · c_i · k_h · k_w / gc_o · c_i · k_h · k_w / gc_o · c_i · k_h · k_w · h_o · w_o / gc_o · c_i · k_h · k_w · h_o · w_o / gh_o · w_oh_o · w_o
Depthwise convolutionالتفاف عمقي (Depthwise convolution)c_o · k_h · k_wc_o · k_h · k_wc_o · k_h · k_w · h_o · w_oc_o · k_h · k_w · h_o · w_oh_o · w_oh_o · w_o
Parameters and MACs per layer type (batch size n = 1, bias ignored)المعاملات و MACs لكل نوع من الطبقات (حجم الدفعة n = 1، مع إهمال الانحياز)

How a convolution slides, how padding and stride set the output size, and how groups partition channels were covered in lecture 02, part 02 and part 03. This part only counts, and it takes the kernel size and output shape of each layer as given.

كيف ينزلق الالتفاف، وكيف يحدد الحشو (padding) والخطوة (stride) حجم المخرج، وكيف تقسّم المجموعات القنوات، كل ذلك شُرح في المحاضرة 02، الجزء 02 والجزء 03. هذا الجزء يكتفي بالعدّ، ويأخذ حجم النواة (kernel size) وشكل المخرج لكل طبقة كمعطيات.

Why this mirrors part 02لماذا يعكس هذا الجزء صورة الجزء 02

Goodfellow, Bengio and Courville explain parameter sharing in one sentence that is the whole story of this part: sharing "does not affect the runtime of forward propagation, it is still O(k × n), but it does further reduce the storage requirements of the model to k parameters". Convolution saves storage by reusing weights, and reusing weights is exactly what makes the arithmetic grow. That is why parts 02 and 04 are mirror images. The layers cheapest to store are the most expensive to run.

يشرح Goodfellow وBengio وCourville مشاركة المعاملات (parameter sharing) في جملة واحدة هي قصة هذا الجزء كلها: المشاركة "لا تؤثر في زمن تشغيل الانتشار الأمامي، فهو لا يزال O(k × n)، لكنها تقلل أكثر متطلبات تخزين النموذج إلى k من المعاملات". الالتفاف يوفّر التخزين بإعادة استخدام الأوزان، وإعادة استخدام الأوزان هي بالضبط ما يجعل الحسابات تكبر. لهذا فإن الجزأين 02 و04 صورتان متعاكستان. الطبقات الأرخص تخزينًا هي الأغلى تشغيلًا.

Worked example: the MobileNet block that makes depthwise worth itمثال محلول: كتلة MobileNet التي تجعل الالتفاف العمقي (depthwise) مجديًا

The MobileNets paper replaces one standard 3 × 3 convolution with a depthwise 3 × 3 followed by a pointwise 1 × 1. Its equations 4 and 5 give the depthwise and separable costs, and dividing by the standard cost gives the ratio 1 / N + 1 / D_K², where N is the number of output channels and D_K the kernel size. The formulas in the table let you reproduce that with real numbers.

تستبدل ورقة MobileNets التفافًا قياسيًا واحدًا 3 × 3 بالتفاف عمقي 3 × 3 يتبعه التفاف نقطي (pointwise) 1 × 1. معادلتاها 4 و5 تعطيان كلفة الالتفاف العمقي وكلفة الالتفاف المنفصل (separable)، والقسمة على كلفة الالتفاف القياسي تعطي النسبة 1 / N + 1 / D_K²، حيث N عدد قنوات المخرج وD_K حجم النواة. الصيغ في الجدول تتيح لك إعادة إنتاج ذلك بأرقام حقيقية.

Worked exampleمثال محلول

Standard versus depthwise separable at 112 × 112, 32 to 64 channelsالالتفاف القياسي مقابل الالتفاف العمقي المنفصل عند 112 × 112، من 32 إلى 64 قناة

  1. Standard 3 × 3 convolutionالتفاف قياسي 3 × 3

    64 × 32 × 3 × 3 × 112 × 112 = 231,211,008 MACs.
    64 × 32 × 3 × 3 × 112 × 112 = 231,211,008 من MACs.
  2. Depthwise 3 × 3التفاف عمقي 3 × 3

    32 × 3 × 3 × 112 × 112 = 3,612,672 MACs. No c_i factor.
    32 × 3 × 3 × 112 × 112 = 3,612,672 من MACs. لا يوجد عامل c_i.
  3. Pointwise 1 × 1التفاف نقطي 1 × 1

    64 × 32 × 1 × 1 × 112 × 112 = 25,690,112 MACs. A convolution with k = 1.
    64 × 32 × 1 × 1 × 112 × 112 = 25,690,112 من MACs. إنه التفاف فيه k = 1.
  4. Ratioالنسبة

    (3,612,672 + 25,690,112) / 231,211,008 = 0.1267, and 1 / 64 + 1 / 9 = 0.1267.
    (3,612,672 + 25,690,112) / 231,211,008 = 0.1267، و1 / 64 + 1 / 9 = 0.1267.
  5. Resultالنتيجة

    About 7.9× fewer MACs at 64 output channels, approaching the paper's "between 8 to 9 times less computation" as the channel count grows and the 1 / D_K² = 1 / 9 pointwise term is all that remains.
    عدد MACs أقل بنحو 7.9× عند 64 قناة مخرج، ويقترب من عبارة الورقة "حساب أقل بما بين 8 و9 مرات" كلما زاد عدد القنوات ولم يبق إلا حد الالتفاف النقطي 1 / D_K² = 1 / 9.

The calculator below lets you scrub every symbol in the table. Its FLOPs, OPs and hardware rows are explained in the last concept of this part; for now watch how parameters and MACs move apart as the output size grows.

تتيح لك الحاسبة أدناه تحريك كل رمز في الجدول. صفوف FLOPs وOPs والعتاد فيها تُشرح في المفهوم الأخير من هذا الجزء؛ أما الآن فراقب كيف يتباعد عدد المعاملات وعدد MACs كلما كبر حجم المخرج.

SimulatorمحاكيLayer compute calculator: MACs, FLOPs and computation timeحاسبة حوسبة الطبقة: MACs وFLOPs وزمن الحساب
Layer presetsإعدادات الطبقة المسبقة
k_h = k_wKernel sizeحجم النواة
Parameters34,848weightsc_o · c_i · k_h · k_w
MACsMACs105,415,200(105.42 M)parameters × h_o × w_o = parameters × 3,025
MACs per parameter3,025uses per weightequal to h_o × w_oيساوي h_o × w_o
FLOPs210.83 MFLOPs2 × MACs: one multiply and one add2 × MACs: عملية ضرب واحدة وعملية جمع واحدة
OPs210.83 MOPssame count for integer weights and activationsالعدد نفسه للأوزان والتنشيطات الصحيحة
Share of AlexNet14.6% of 724M MACsthis one layer against the whole networkهذه الطبقة وحدها مقارنةً بالشبكة كلها
Hardware: processor throughputالعتاد: إنتاجية المعالج100.0 GOPS
10^11.0 OPS

illustrative mobile GPU-class figureرقم توضيحي بمستوى معالجات GPU المحمولة

T computation2.11msOPs of the layer ÷ OPS of the processorOPs الطبقة ÷ OPS المعالج
Reading210.83 MOPs ÷ 100.0 GOPSthe numerator is a count, the denominator a rateالبسط عدد، والمقام معدل

Drag h_o = w_o and watch MACs climb while parameters stay put: a convolution reuses every weight at every output position. Switch to linear and the two counts collapse into one. The hardware box only divides: the same layer takes 1000× longer at 1 GFLOPS than at 1 TFLOPS, and marketing peak TOPS are rarely sustained, so treat the time as a floor.اسحب h_o = w_o وراقب ارتفاع MACs بينما تبقى المعاملات ثابتة: فالالتفاف يعيد استخدام كل وزن عند كل موضع في المُخرج. انتقل إلى الطبقة الخطية فيندمج العددان في عدد واحد. صندوق العتاد يقسم فقط: الطبقة نفسها تستغرق وقتًا أطول بمقدار 1000× عند 1 GFLOPSمقارنةً بـ1 TFLOPS، وقيم TOPS التسويقية القصوى نادرًا ما تتحقق باستمرار، فتعامل مع الزمن على أنه حد أدنى.

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

A grouped convolution has c_i = 96, c_o = 256, a 5 × 5 kernel, a 27 × 27 output and g = 2. How many MACs does it perform?التفاف مجمّع فيه c_i = 96 و c_o = 256، ونواة 5 × 5، ومخرج 27 × 27، و g = 2. كم عدد MACs التي ينفّذها؟

Recall

A grouped convolution has 384 filters over 384 input channels, a 3 × 3 kernel, a 13 × 13 output and g = 2. How many MACs, how many parameters, and what is their ratio?التفاف مجمّع فيه 384 مرشحًا على 384 قناة مدخل، ونواة 3 × 3، ومخرج 13 × 13، و g = 2. كم عدد MACs، وكم عدد المعاملات، وما النسبة بينهما؟

MACs: 384 · 384 · 9 · 169 / 2 = 112,140,288. Parameters: 384 · 384 · 9 / 2 = 663,552. The ratio is 169 = 13 × 13, one use of each weight per output position. This is AlexNet conv4.
MACs: 384 · 384 · 9 · 169 / 2 = 112,140,288. المعاملات: 384 · 384 · 9 / 2 = 663,552. النسبة هي 169 = 13 × 13، أي استخدام واحد لكل وزن لكل موضع في المخرج. هذه هي conv4 في AlexNet.

Slide 25 hands you the eight weighted layers of AlexNet with their output shapes and an empty MACs column. Slide 26 fills it in, except for one layer hidden behind a question mark. Work through the column yourself before you read the answers. Every entry is the formula from the previous concept with the layer's numbers substituted, and the pooling layers contribute nothing because they hold no weights.

تعطيك الشريحة 25 الطبقات الثماني ذات الأوزان في AlexNet مع أشكال مخرجاتها وعمود MACs فارغ. وتملؤه الشريحة 26، باستثناء طبقة واحدة مخفية خلف علامة استفهام. احسب العمود بنفسك قبل أن تقرأ الإجابات. كل خانة هي الصيغة من المفهوم السابق بعد التعويض بأرقام الطبقة، وطبقات التجميع (pooling) لا تضيف شيئًا لأنها لا تحمل أوزانًا.

Worked exampleمثال محلول

AlexNet, layer by layer (batch size 1, bias ignored)AlexNet طبقةً طبقة (حجم الدفعة 1، مع إهمال الانحياز)

  1. conv1: 11 × 11, 3 to 96 channels, output 55 × 55conv1: نواة 11 × 11، من 3 إلى 96 قناة، المخرج 55 × 55

    96 × 3 × 11 × 11 × 55 × 55 = 105,415,200
    96 × 3 × 11 × 11 × 55 × 55 = 105,415,200
  2. conv2: 5 × 5, 96 to 256 channels, groups 2, output 27 × 27conv2: نواة 5 × 5، من 96 إلى 256 قناة، مجموعتان، المخرج 27 × 27

    256 × 96 × 5 × 5 × 27 × 27 / 2 = 223,948,800. Without the groups it would be 447,897,600; g = 2 halves it.
    256 × 96 × 5 × 5 × 27 × 27 / 2 = 223,948,800. بدون المجموعات سيكون العدد 447,897,600؛ وg = 2 ينصّفه.
  3. conv3: 3 × 3, 256 to 384 channels, output 13 × 13conv3: نواة 3 × 3، من 256 إلى 384 قناة، المخرج 13 × 13

    384 × 256 × 3 × 3 × 13 × 13 = 149,520,384
    384 × 256 × 3 × 3 × 13 × 13 = 149,520,384
  4. conv4: 3 × 3, 384 to 384 channels, groups 2, output 13 × 13conv4: نواة 3 × 3، من 384 إلى 384 قناة، مجموعتان، المخرج 13 × 13

    384 × 384 × 3 × 3 × 13 × 13 / 2 = 112,140,288
    384 × 384 × 3 × 3 × 13 × 13 / 2 = 112,140,288
  5. conv5: 3 × 3, 384 to 256 channels, groups 2, output 13 × 13conv5: نواة 3 × 3، من 384 إلى 256 قناة، مجموعتان، المخرج 13 × 13

    This is the entry the slide hides. Compute it before revealing.

    Recall

    conv5 MACs?كم عدد MACs في conv5؟

    256 × 384 × 3 × 3 × 13 × 13 / 2 = 74,760,192
    256 × 384 × 3 × 3 × 13 × 13 / 2 = 74,760,192
    هذه هي الخانة التي تخفيها الشريحة. احسبها قبل كشف الإجابة.

    Recall

    conv5 MACs?كم عدد MACs في conv5؟

    256 × 384 × 3 × 3 × 13 × 13 / 2 = 74,760,192
    256 × 384 × 3 × 3 × 13 × 13 / 2 = 74,760,192
  6. fc6: 256 × 6 × 6 = 9,216 inputs to 4,096 outputsfc6: من 256 × 6 × 6 = 9,216 مدخلًا إلى 4,096 مخرجًا

    4,096 × 9,216 = 37,748,736. The flattened pooling output is the input vector.
    4,096 × 9,216 = 37,748,736. مخرج طبقة التجميع بعد تسطيحه (flatten) هو متجه المدخل.
  7. fc7: 4,096 to 4,096fc7: من 4,096 إلى 4,096

    4,096 × 4,096 = 16,777,216
    4,096 × 4,096 = 16,777,216
  8. fc8: 4,096 to 1,000 classesfc8: من 4,096 إلى 1,000 فئة

    1,000 × 4,096 = 4,096,000
    1,000 × 4,096 = 4,096,000
  9. Totalالمجموع

    724,406,816 MACs, the slide's "724M in total". Sze et al. list the same figure, 724M MACs and 61M weights, in their survey table.
    724,406,816 من MACs، وهو "724M في المجموع" في الشريحة. ويذكر Sze وزملاؤه الرقم نفسه، 724M من MACs و61M من الأوزان، في جدول مسحهم.

Put the parameters next to the MACsضع المعاملات بجانب MACs

The total is not the interesting number. The interesting number appears when you place the parameter counts from part 02 beside the MACs and add a column for their ratio, which by the previous concept is just h_o · w_o.

المجموع ليس الرقم المثير للاهتمام. الرقم المثير يظهر عندما تضع أعداد المعاملات من الجزء 02 بجانب MACs وتضيف عمودًا للنسبة بينهما، وهي بحسب المفهوم السابق مجرد h_o · w_o.

LayerالطبقةShapeالشكلParametersالمعاملاتMACsMACsMACs per parameterMACs لكل معامل
conv1conv196 × 3 × 11 × 11, out 55 × 5596 × 3 × 11 × 11، المخرج 55 × 5534,84834,848105,415,200105,415,2003,0253,025
conv2conv2256 × 96 × 5 × 5 / 2, out 27 × 27256 × 96 × 5 × 5 / 2، المخرج 27 × 27307,200307,200223,948,800223,948,800729729
conv3conv3384 × 256 × 3 × 3, out 13 × 13384 × 256 × 3 × 3، المخرج 13 × 13884,736884,736149,520,384149,520,384169169
conv4conv4384 × 384 × 3 × 3 / 2, out 13 × 13384 × 384 × 3 × 3 / 2، المخرج 13 × 13663,552663,552112,140,288112,140,288169169
conv5conv5256 × 384 × 3 × 3 / 2, out 13 × 13256 × 384 × 3 × 3 / 2، المخرج 13 × 13442,368442,36874,760,19274,760,192169169
fc6fc64096 × (256 × 6 × 6)4096 × (256 × 6 × 6)37,748,73637,748,73637,748,73637,748,73611
fc7fc74096 × 40964096 × 409616,777,21616,777,21616,777,21616,777,21611
fc8fc81000 × 40961000 × 40964,096,0004,096,0004,096,0004,096,00011
conv1 to conv5من conv1 إلى conv5five convolutionsخمس طبقات التفاف2,332,704 (3.8%)2,332,704 (3.8%)665,784,864 (91.9%)665,784,864 (91.9%)
fc6 to fc8من fc6 إلى fc8three linear layersثلاث طبقات خطية58,621,952 (96.2%)58,621,952 (96.2%)58,621,952 (8.1%)58,621,952 (8.1%)
Totalالمجموعeight weighted layersثماني طبقات ذات أوزان60,954,65660,954,656724,406,816724,406,816
AlexNet: parameters and MACs per layerAlexNet: المعاملات و MACs لكل طبقة
The last column of the table, drawn. Each layer's slice of the MAC bar is its slice of the parameter bar stretched by h_o · w_o: conv1 grows 3,025 times, the linear layers not at all, so the convolution segments take over the lower bar.العمود الأخير من الجدول مرسومًا. نصيب كل طبقة من شريط MACs هو نصيبها من شريط المعاملات بعد مطّه بمقدار h_o · w_o: تكبر conv1 بمقدار 3,025 مرة، ولا تكبر الطبقات الخطية إطلاقًا، فتستولي مقاطع الالتفاف على الشريط السفلي.

The five convolutions hold 2,332,704 parameters, under 4% of the network, yet perform 665,784,864 MACs, 92% of the compute. The three linear layers hold 58,621,952 parameters, 96% of the network, yet perform only 58,621,952 MACs, 8% of the compute. The last column explains it: each conv1 weight is used 3,025 times, each conv3 to conv5 weight 169 times, each linear weight once. Large spatial maps early in the network make convolution compute heavy. Large c_i · c_o products late in the network make the linear layers parameter heavy.

الالتفافات الخمسة تحمل 2,332,704 من المعاملات، أي أقل من 4% من الشبكة، ومع ذلك تنفّذ 665,784,864 من MACs، أي 92% من الحساب. والطبقات الخطية الثلاث تحمل 58,621,952 من المعاملات، أي 96% من الشبكة، ومع ذلك لا تنفّذ إلا 58,621,952 من MACs، أي 8% من الحساب. العمود الأخير يفسّر ذلك: كل وزن في conv1 يُستخدم 3,025 مرة، وكل وزن في conv3 إلى conv5 يُستخدم 169 مرة، وكل وزن خطي مرة واحدة. الخرائط المكانية الكبيرة في بداية الشبكة تجعل الالتفاف ثقيلًا في الحساب. وحواصل الضرب الكبيرة c_i · c_o في نهاية الشبكة تجعل الطبقات الخطية ثقيلة في المعاملات.

InteractiveتفاعليWhere AlexNet spends parameters versus computeأين ينفق AlexNet المعاملات مقابل الحوسبة
total 60,954,656 weightsالمجموع 60,954,656 وزن
fc6fc7fc8
conv1 to conv5conv1 إلى conv53.8%2.3M
fc6 to fc8fc6 إلى fc896.2%58.6M

Flip the toggle and watch which single layer is largest. Under parameters it is fc6, about 62% of the network on its own. Under MACs it is conv2, about 31%, while conv1, invisible on the parameter bar at 0.06%, grows to 15% because its 34,848 weights each fire 3,025 times. Each layer's exact count is listed below the bar.بدّل المفتاح وراقب أي طبقة منفردة هي الأكبر. عند قياس المعاملات تكون fc6، وحدها نحو 62% من الشبكة. وعند قياس MACsتكون conv2، نحو 31%، بينما conv1 التي لا تكاد تظهر على شريط المعاملات عند 0.06% تكبر إلى 15%لأن كل وزن من أوزانها البالغة 34,848 يُستخدم 3,025 مرة. العدد الدقيق لكل طبقة مُدرج تحت الشريط.

  • conv134,848 (0.1%)
  • conv2307,200 (0.5%)
  • conv3884,736 (1.5%)
  • conv4663,552 (1.1%)
  • conv5442,368 (0.7%)
  • fc637,748,736 (61.9%)
  • fc716,777,216 (27.5%)
  • fc84,096,000 (6.7%)

What this means for making a model fasterماذا يعني هذا لجعل النموذج أسرع

The inversion sets the strategy for the rest of the course. Pruning or quantizing the linear layers shrinks Model size dramatically, because that is where the weights are, but it barely changes the MAC count and therefore barely changes computation time. To make inference faster you must attack the convolutions: fewer channels, grouped or depthwise filters as in MobileNet, or a lower input resolution so that h_o · w_o shrinks in every layer at once. The pruning lecture that follows will keep returning to this table, and when you profile a model for the research project, report both columns, not one.

هذا الانقلاب يحدد الاستراتيجية لبقية المقرر. التقليم (pruning) أو التكميم (quantization) للطبقات الخطية يقلّص حجم النموذج (model size) بشكل كبير، لأن الأوزان موجودة هناك، لكنه بالكاد يغيّر عدد MACs، وبالتالي بالكاد يغيّر زمن الحساب (computation time). لجعل الاستدلال أسرع يجب أن تستهدف الالتفافات: قنوات أقل، أو مرشحات مجمّعة أو عمقية كما في MobileNet، أو دقة مدخل أقل بحيث يصغر h_o · w_o في كل الطبقات دفعة واحدة. محاضرة التقليم التالية ستعود إلى هذا الجدول مرارًا، وعندما تحلّل نموذجًا في مشروع البحث، اذكر العمودين معًا لا عمودًا واحدًا.

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

Pruning removes 90 percent of the weights in AlexNet's fc6 and fc7. Roughly how much does the network's total MAC count fall?يزيل التقليم 90 بالمئة من الأوزان في fc6 و fc7 من AlexNet. بكم ينخفض تقريبًا العدد الكلي لـ MACs في الشبكة؟

Recall

Which AlexNet layer family holds most of the parameters, which performs most of the MACs, and why?أي عائلة طبقات في AlexNet تحمل معظم المعاملات، وأيها تنفّذ معظم MACs، ولماذا؟

The linear layers hold about 96% of the 61M parameters. The convolutions perform about 92% of the 724M MACs. Convolution MACs equal parameters times h_o · w_o, which is 3,025 for conv1 and 1 for every linear layer.
الطبقات الخطية تحمل نحو 96% من المعاملات البالغة 61M. والالتفافات تنفّذ نحو 92% من MACs البالغة 724M. MACs الالتفاف تساوي المعاملات مضروبة في h_o · w_o، وهو 3,025 في conv1 و1 في كل طبقة خطية.

A MAC hides two arithmetic operations: a multiply and an add. When each is counted as one floating point operation, one MAC is two FLOPs, and AlexNet's 724,406,816 MACs become 1,448,813,632 FLOPs. The slide rounds that to 1.4G; the exact figure is 1.449G, and showing the factor of two matters more than the rounding.

تخفي عملية MAC عمليتين حسابيتين: ضربًا وجمعًا. عندما تُعدّ كل منهما عملية فاصلة عائمة (floating point operation) واحدة، تصبح عملية MAC الواحدة اثنتين من FLOPs، وتصبح MACs في AlexNet البالغة 724,406,816 عددًا قدره 1,448,813,632 من FLOPs. تقرّب الشريحة ذلك إلى 1.4G؛ والرقم الدقيق 1.449G، وإظهار معامل الاثنين أهم من التقريب.

FLOPs=2⋅MACs\text{FLOPs} = 2 \cdot \text{MACs}
One multiply plus one add per MACضرب واحد وجمع واحد لكل MAC

The count on its own says nothing about time. To get time you need to know how fast a processor can drain it, and that is a different quantity with an almost identical name. FLOPS, with a capital S, is floating point operations per second: a throughput that belongs to the hardware. FLOPs, with a lowercase s, is a plural count that belongs to the model. TOP500 ranks supercomputers by exactly this rate, reporting Linpack results as Rmax and Rpeak in Flop/s, and NVIDIA's GPU performance background guide writes the time a math-limited kernel takes as its number of operations divided by the processor's math bandwidth, which is FLOPs over FLOPS.

العدد وحده لا يقول شيئًا عن الزمن. لتحصل على الزمن تحتاج أن تعرف مدى سرعة المعالج في استهلاكه، وهذه كمية مختلفة باسم شبه مطابق. FLOPS، بحرف S كبير، هي عدد عمليات الفاصلة العائمة في الثانية: معدل إنتاجية (throughput) يخص العتاد. أما FLOPs، بحرف s صغير، فهي عدد بصيغة الجمع يخص النموذج. تصنّف قائمة TOP500 الحواسيب الفائقة بهذا المعدل بالضبط، إذ تذكر نتائج Linpack على شكل Rmax وRpeak بوحدة Flop/s، ويكتب دليل خلفية أداء وحدات GPU من NVIDIA زمن النواة (kernel) المحدودة بالحساب على أنه عدد عملياتها مقسومًا على عرض النطاق الحسابي للمعالج، أي FLOPs على FLOPS.

FLOPS=FLOPssecondTcomputation=FLOPs of the modelFLOPS of the processor\text{FLOPS} = \frac{\text{FLOPs}}{\text{second}} \qquad T_{computation} = \frac{\text{FLOPs of the model}}{\text{FLOPS of the processor}}
A rate is a count per second; dividing a count by a rate gives a timeالمعدل عدد في الثانية؛ وقسمة العدد على المعدل تعطي زمنًا
The hopper holds a count, the pipe drains at a rate, and the clock reads the quotient: 1.449 G FLOPs through 100 GFLOPS takes 14.5 ms.القمع يحمل عددًا، والأنبوب يُفرغه بمعدل، والساعة تقرأ خارج القسمة: 1.449 G FLOPs عبر 100 GFLOPS تستغرق 14.5 ms.

This is the computation time term of the latency model on slide 8, where the numerator is a network specification and the denominator a hardware specification. Run AlexNet's count through a few devices and the scale of the answer changes by orders of magnitude while the model does not change at all.

هذا هو حد زمن الحساب (computation time) في نموذج التأخير في الشريحة 8، حيث البسط هو مواصفة الشبكة (network specification) والمقام مواصفة العتاد. مرّر عدد AlexNet عبر عدة أجهزة وسترى أن مقياس الإجابة يتغير بعدة مراتب عشرية بينما لا يتغير النموذج إطلاقًا.

ProcessorالمعالجCount ÷ rateالعدد ÷ المعدلT_computationT_computation
1 GFLOPS1 GFLOPS1.449 × 10^9 / 10^91.449 × 10^9 / 10^91,449 ms1,449 ms
100 GFLOPS100 GFLOPS1.449 × 10^9 / 10^111.449 × 10^9 / 10^1114.5 ms14.5 ms
1 TFLOPS1 TFLOPS1.449 × 10^9 / 10^121.449 × 10^9 / 10^121.45 ms1.45 ms
67 TOPS (Orin Nano, INT8 sparse peak)67 TOPS (Orin Nano، القيمة القصوى المتفرقة بدقة INT8)1.449 × 10^9 / 6.7 × 10^131.449 × 10^9 / 6.7 × 10^130.022 ms0.022 ms
AlexNet's 1.449G FLOPs on different processors; the last row treats the count as OPs, as slide 28 allows for an INT8 modelعدد FLOPs في AlexNet البالغ 1.449G على معالجات مختلفة؛ الصف الأخير يعامل العدد على أنه OPs، كما تسمح الشريحة 28 لنموذج INT8

OPs: the same count for any data typeOPs: العدد نفسه لأي نوع من البيانات

The F in FLOP assumes floating point arithmetic. A quantized network, the kind you deploy on a microcontroller or an INT8 accelerator, stores weights and activations at an Bit width of 8 bits and multiplies integers. Those are not floating point operations, but they are still operations, and there are still two per MAC. The lecture therefore generalizes: an operation (OP) is one multiply or add of any data type, AlexNet is 724M × 2 = 1.4G OPs, and OPS is operations per second. The layer compute calculator in the second concept of this part uses OPS in its hardware box for exactly this reason: the formula for time does not care whether the operands were floats.

حرف F في FLOP يفترض حسابًا بالفاصلة العائمة. الشبكة المكمّمة، من النوع الذي تنشره على متحكم دقيق (microcontroller) أو على مسرّع INT8، تخزّن الأوزان والتنشيطات (activations) بعرض بت (bit-width) قدره 8 بتات وتضرب أعدادًا صحيحة. هذه ليست عمليات فاصلة عائمة، لكنها لا تزال عمليات، ولا يزال هناك اثنتان منها لكل MAC. لذلك تعمّم المحاضرة: العملية (operation (OP)) هي ضرب أو جمع واحد لأي نوع من البيانات، وAlexNet يساوي 724M × 2 = 1.4G من OPs، وOPS هي عدد العمليات في الثانية. حاسبة حساب الطبقة في المفهوم الثاني من هذا الجزء تستخدم OPS في خانة العتاد لهذا السبب بالضبط: صيغة الزمن لا تهتم بما إذا كانت المُعامَلات الحسابية (operands) أعدادًا عائمة.

OPS=OPssecond\text{OPS} = \frac{\text{OPs}}{\text{second}}
The general form; FLOPS is the floating point special caseالصيغة العامة؛ وFLOPS حالة خاصة للفاصلة العائمة
TermالمصطلحKindالنوعMeaningالمعنىBelongs toيخص
MACMACCountعددOne multiply accumulated into a sumعملية ضرب واحدة تُجمع في مجموع جارٍA model, per inferenceالنموذج، لكل استدلال
FLOPFLOPCountعددOne floating point multiply or add; a MAC is twoعملية ضرب أو جمع واحدة بالفاصلة العائمة، وعملية MAC تساوي اثنتينA model, per inferenceالنموذج، لكل استدلال
OPOPCountعددOne operation of any data type; a MAC is twoعملية واحدة لأي نوع من البيانات، وعملية MAC تساوي اثنتينA model, per inferenceالنموذج، لكل استدلال
FLOPSFLOPSRateمعدلFloating point operations per secondعدد عمليات الفاصلة العائمة في الثانيةA processorالمعالج
OPSOPSRateمعدلOperations per second, any data typeعدد العمليات في الثانية، لأي نوع من البياناتA processorالمعالج
Five terms, two kinds of quantityخمسة مصطلحات، ونوعان من الكميات

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

A model needs 3.6G FLOPs and a chip sustains 400 GFLOPS. What is T_computation?يحتاج نموذج 3.6G FLOPs، وتحافظ شريحة على 400 GFLOPS. ما قيمة T_computation؟

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

Why does the lecture generalize from FLOPs to OPs?لماذا تعمّم المحاضرة من FLOPs إلى OPs؟

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

A datasheet advertises a board at 67 TOPS. What kind of quantity is that?تعلن نشرة بيانات أن لوحة ما تصل إلى 67 TOPS. ما نوع هذه الكمية؟

Recall

State the rule that turns a MAC count into a FLOP count and a FLOP count into a time, then apply it: AlexNet on a 1 TFLOPS processor.اذكر القاعدة التي تحوّل عدد MACs إلى عدد FLOPs، وعدد FLOPs إلى زمن، ثم طبّقها: AlexNet على معالج سرعته 1 TFLOPS.

FLOPs = 2 × MACs, and T_computation = FLOPs / FLOPS. 2 × 724,406,816 = 1,448,813,632 FLOPs; 1.449 × 10^9 / 10^12 = 1.45 ms.
FLOPs = 2 × MACs، وT_computation = FLOPs / FLOPS. 2 × 724,406,816 = 1,448,813,632 من FLOPs؛ 1.449 × 10^9 / 10^12 = 1.45 ms.

Recall

Why use OPs rather than FLOPs for an INT8 model, and how many OPs per MAC?لماذا نستخدم OPs بدلًا من FLOPs لنموذج INT8، وكم OP لكل MAC؟

Its multiplies and adds act on integers, so they are not floating point operations. OPs counts operations of any data type. There are still two per MAC, one multiply and one add.
عمليات الضرب والجمع فيه تعمل على أعداد صحيحة، لذا فهي ليست عمليات فاصلة عائمة. OPs تعدّ العمليات لأي نوع من البيانات. ولا يزال هناك اثنتان لكل MAC: ضرب واحد وجمع واحد.

Slide 29 lists the ten works the lecture draws on. Six were used directly in this lecture's counting and examples; the other four appear here as context for the architectures and normalization layers of lecture 02 and the pruning lectures ahead.

تسرد الشريحة 29 الأعمال العشرة التي تستند إليها المحاضرة. ستة منها استُخدمت مباشرة في العدّ والأمثلة في هذه المحاضرة؛ والأربعة الأخرى تظهر هنا كسياق للبنى المعمارية وطبقات التطبيع (normalization) في المحاضرة 02 ولمحاضرات التقليم القادمة.

Used in this lectureمستخدمة في هذه المحاضرة

Context for other lecturesسياق لمحاضرات أخرى

Recapالخلاصة

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

  • A MAC is a <- a + b·c. A matrix-vector product costs m·n MACs and a matrix-matrix product m·n·k MACs.عملية MAC هي a <- a + b·c. ضرب مصفوفة في متجه يكلف m·n من MACs، وضرب مصفوفة في مصفوفة يكلف m·n·k من MACs.
  • Linear: c_o·c_i. Convolution: c_o·c_i·k_h·k_w·h_o·w_o. Grouped: divide by g. Depthwise: c_o·k_h·k_w·h_o·w_o, with no c_i.الخطية: c_o·c_i. الالتفاف: c_o·c_i·k_h·k_w·h_o·w_o. المجمّع (grouped): اقسم على g. العمقي (depthwise): c_o·k_h·k_w·h_o·w_o، بدون c_i.
  • Convolution MACs equal parameters times h_o·w_o. Linear MACs equal parameters.MACs الالتفاف تساوي عدد المعاملات مضروبًا في h_o·w_o. و MACs الطبقة الخطية تساوي عدد معاملاتها.
  • AlexNet performs 724,406,816 MACs. Its convolutions hold 3.8 percent of the parameters but perform 92 percent of the MACs; the linear layers are the reverse.ينفّذ AlexNet عدد 724,406,816 من MACs. طبقات الالتفاف فيه تحمل 3.8 بالمئة من المعاملات لكنها تنفّذ 92 بالمئة من MACs، والطبقات الخطية عكس ذلك تمامًا.
  • One MAC is two FLOPs, so AlexNet is about 1.45G FLOPs, which the slide rounds to 1.4G.عملية MAC الواحدة تساوي اثنتين من FLOPs، لذا يحتاج AlexNet نحو 1.45G FLOPs، وتقرّبها الشريحة إلى 1.4G.
  • FLOPs and OPs are counts that belong to a model. FLOPS and OPS are rates that belong to a processor. T_computation = OPs / OPS.FLOPs و OPs أعداد تخص النموذج. أما FLOPS و OPS فمعدلات تخص المعالج. T_computation = OPs / OPS.
  • OPs generalizes FLOPs to quantized integer networks; the count per MAC stays two.تعمّم OPs مفهوم FLOPs على الشبكات المكمّمة التي تعمل بالأعداد الصحيحة، ويبقى العدد لكل MAC اثنين.
  • Conventions differ: many papers and tools report MACs under the label FLOPs. Check before comparing numbers.الاصطلاحات تختلف: كثير من الأوراق والأدوات تذكر MACs تحت اسم FLOPs. تحقّق من ذلك قبل مقارنة الأرقام.

Sourcesالمصادر