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 دقائق
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في نهاية هذا الجزء ستستطيع
- Place any efficiency metric in the memory or computation group and say which of storage, latency and energy it predicts.تضع أي مقياس كفاءة في مجموعة الذاكرة أو مجموعة الحساب، وتحدد أيّ التكاليف الثلاث يتنبأ بها: التخزين أو الـ latency أو الطاقة.
- Define latency and throughput, and compute both for a batched design.تعرّف الـ latency والـ throughput، وتحسب كليهما لتصميم يستخدم الـ batching.
- Explain with numbers why higher throughput does not imply lower latency, and lower latency does not imply higher throughput.تشرح بالأرقام لماذا لا يعني الـ throughput الأعلى latency أقل، ولا يعني الـ latency الأقل throughput أعلى.
- 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.
- 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.
| Goalالهدف | Costالتكلفة | Resourceالمورد | Metrics that predict itالمقاييس التي تتنبأ به |
|---|---|---|---|
| Smallerأصغر | Storageالتخزين | Memoryالذاكرة | #parameters, model sizeعدد المعاملات (#parameters)، وحجم النموذج |
| Fasterأسرع | Latencyالـ latency | Computation and memoryالحساب والذاكرة | MACs, OPs, model size, peak #activationsMACs، وOPs، وحجم النموذج، والعدد الأقصى للـ activations |
| Greenerأكثر خضرة | Energyالطاقة | Computation and memoryالحساب والذاكرة | MACs, OPs, and above all the bytes movedMACs وOPs، وقبل كل شيء البايتات المنقولة |
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.سمِّ مجموعتَي مقاييس الكفاءة، واذكر أعضاء كل منهما، وقل أي تكلفة تتنبأ بها كل مجموعة.
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النموذج | mIoUmIoU | Parametersالمعاملات | MACsMACs | Orin latencyالـ latency على Orin | A100 throughputالـ throughput على A100 |
|---|---|---|---|---|---|
| SegFormer-B5SegFormer-B5 | 82.482.4 | 85M85M | 1460G1460G | 638 ms638 ms | 12 image/s12 image/s |
| EfficientViT-L1EfficientViT-L1 | 82.782.7 | 40M40M | 282G282G | 45.9 ms45.9 ms | 122 image/s122 image/s |
| Ratioالنسبة | +0.3+0.3 | 2.1x fewerأقل بمقدار 2.1x | 5.2x fewerأقل بمقدار 5.2x | 13.9x fasterأسرع بمقدار 13.9x | 10x moreأكثر بمقدار 10x |
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 لأرقامها.
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 كله.
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التصميم 1 | One after anotherصورة تلو الأخرى | 50 ms50 ms | 11 | 50 ms50 ms | 1 / 0.050 = 20 image/s1 / 0.050 = 20 image/s |
| Design 2التصميم 2 | Four in parallelأربع صور بالتوازي | 100 ms100 ms | 44 | 100 ms100 ms | 4 / 0.100 = 40 image/s4 / 0.100 = 40 image/s |
Worked exampleمثال محلول
Answering slide 7's two questionsالإجابة عن سؤالَي الشريحة 7
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 انتظار صورة واحدة، وتلك الصورة تنتظر دفعتها كاملة.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. من دون توازٍ، يبقى المسار الواحد السريع ينقل صورة واحدة فقط في كل خطوة، فالخطوة القصيرة تشتري انتظارًا قصيرًا لكنها لا تشتري معدلًا عاليًا.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.
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دفعة بحجم 1 | 2 ms2 ms | 11 | 2 ms2 ms | 500 image/s500 image/s |
| Batch 32دفعة بحجم 32 | 8 ms8 ms | 3232 | 8 ms8 ms | 4000 image/s4000 image/s |
| Changeالتغيّر | 4x longerأطول بمقدار 4x | 32x moreأكثر بمقدار 32x | 4x worseأسوأ بمقدار 4x | 8x betterأفضل بمقدار 8x |
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 له، وماذا سيعطي المقلوب الساذج؟
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) الحالي، يكون نظام الذاكرة قد بدأ بالفعل في جلب الأوزان التالية، فيجري التياران جنبًا إلى جنب وتنتهي الطبقة عندما ينتهي التيار الأبطأ.
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_computation | Number 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) |
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)
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.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 من قدرة الحساب الجديدة تبقى خاملة في كل استدلال.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: لم تؤتِ ترقية العتاد ثمارها إلا بعد أن صغُر جانب الشبكة أيضًا.Three configurationsثلاثة إعدادات
Configurationالإعداد T_computationT_computation T_memoryT_memory LatencyLatency Regimeنظام التشغيل (regime) 100 GOPS, 32-bit weightsمعالج 100 GOPS، أوزان 32-bit 10 ms10 ms 5 + 2 = 7 ms5 + 2 = 7 ms 10 ms10 ms Compute-boundCompute-bound 200 GOPS, 32-bit weightsمعالج 200 GOPS، أوزان 32-bit 5 ms5 ms 5 + 2 = 7 ms5 + 2 = 7 ms 7 ms7 ms Memory-boundMemory-bound 200 GOPS, 8-bit weightsمعالج 200 GOPS، أوزان 8-bit 5 ms5 ms 1.25 + 2 = 3.25 ms1.25 + 2 = 3.25 ms 5 ms5 ms Compute-boundCompute-bound
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 من المواصفات المنشورة
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.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.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 الوسيطة المنتقلة بين الطبقات، كلها تقع خارج الصيغة.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%.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 إلى إزالتها. النموذج حد أدنى من الرتبة الأولى؛ وحجم الفجوة يخبرك أي حد نسيته.Floor versus measurementالحد الأدنى مقابل القياس
Modelالنموذج T_computationT_computation T_memoryT_memory Floorالحد الأدنى Measuredالمقيس Miss, utilisationالفجوة، نسبة الاستغلال EfficientViT-L1EfficientViT-L1 564 / 43 000 = 13.1 ms564 / 43 000 = 13.1 ms 0.39 + 0.45 = 0.84 ms0.39 + 0.45 = 0.84 ms 13.1 ms13.1 ms 45.9 ms45.9 ms 3.5x, about 29%3.5x، قرابة 29% SegFormer-B5SegFormer-B5 2920 / 43 000 = 68 ms2920 / 43 000 = 68 ms 0.83 + 0.45 = 1.3 ms0.83 + 0.45 = 1.3 ms 68 ms68 ms 638 ms638 ms 9.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.
Recall
Why max and not plus?لماذا 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. ماذا يخبرك ذلك؟
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
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
Arithmetic in 32-bit integersالحساب بأعداد صحيحة 32-bit
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 واحدة.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 باثنتي عشرة مرة.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.تغيير مكان الأوزان يغيّر المجموع بمرتبتين عشريتين؛ أما تغيير طريقة إجراء الحساب فيغيّره بأقل من الضعف.The bill, line by lineالفاتورة، بندًا بندًا
Itemالبند Countالعدد Energy eachالطاقة لكل عملية Totalالمجموع Integer MACs (multiply + add)عمليات MAC صحيحة (ضرب + جمع) 724M724M 3.1 + 0.1 = 3.2 pJ3.1 + 0.1 = 3.2 pJ 2.32 mJ2.32 mJ Weights read once from DRAMالأوزان مقروءة مرة واحدة من DRAM 61M61M 640 pJ640 pJ 39.0 mJ39.0 mJ Same weights read from SRAMالأوزان نفسها مقروءة من SRAM 61M61M 5 pJ5 pJ 0.31 mJ0.31 mJ Float MACs instead of integerعمليات MAC عشرية بدلًا من الصحيحة 724M724M 3.7 + 0.9 = 4.6 pJ3.7 + 0.9 = 4.6 pJ 3.33 mJ3.33 mJ
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 إلى الضرب الصحيح، والأرقام التي خلفها، وظروف تقنية التصنيع.
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 الصحيحة فيه؟
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المصادر
- EfficientViT: Multi-Scale Linear Attention for High-Resolution Dense PredictionPaperICCV 2023, Cai, Li, Hu, Gan and HanTable 3: SegFormer-B5 638 ms and EfficientViT-L1 45.9 ms on Jetson AGX Orin, TensorRT, fp16, batch 1; the 13.9x claim; Nano latencies.الجدول 3: SegFormer-B5 عند 638 ms وEfficientViT-L1 عند 45.9 ms على Jetson AGX Orin مع TensorRT وfp16 وbatch يساوي 1؛ وادعاء الـ 13.9x؛ وأرقام الـ latency على Nano.(opens in a new tab)(يفتح في تبويب جديد)
- SegFormer: Simple and Efficient Design for Semantic Segmentation with TransformersPaperNeurIPS 2021, Xie et al.Table 1a: 82.4 (single-scale) / 84.0 (multi-scale) mIoU for B5 on Cityscapes validation.الجدول 1a: قيمة mIoU تساوي 82.4 بمقياس واحد (single-scale) و84.0 بمقاييس متعددة (multi-scale) للنموذج B5 على بيانات التحقق في Cityscapes.(opens in a new tab)(يفتح في تبويب جديد)
- TSM: Temporal Shift Module for Efficient Video UnderstandingPaperICCV 2019, Lin, Gan and HanTable 5: I3D 6.1 V/s versus TSM8F 77.4 V/s on a Tesla P100; latency at batch 1, throughput at batch 16.الجدول 5: I3D عند 6.1 V/s مقابل TSM8F عند 77.4 V/s على Tesla P100؛ الـ latency بـ batch يساوي 1، والـ throughput بـ batch يساوي 16.(opens in a new tab)(يفتح في تبويب جديد)
- Learning both Weights and Connections for Efficient Neural NetworksPaperNeurIPS 2015, Han, Pool, Tran and DallyFigure 1: the seven-row 45 nm energy table on slide 9; memory access three orders of magnitude above arithmetic.الشكل 1: جدول الطاقة عند 45 nm ذو الصفوف السبعة في الشريحة 9؛ والوصول إلى الذاكرة أغلى من الحساب بثلاث مراتب عشرية.(opens in a new tab)(يفتح في تبويب جديد)
- Computing's energy problem (and what we can do about it)PaperIEEE ISSCC 2014, M. HorowitzFigure 1.1.9 arithmetic and 64-bit memory costs; DRAM at 1 to 2 nJ, about 0.6 nJ per 8 bytes even with better I/O.الشكل 1.1.9: تكاليف الحساب وتكاليف الذاكرة لكل وصول 64-bit؛ الـ DRAM من 1 إلى 2 nJ، ونحو 0.6 nJ لكل 8 بايتات حتى مع دوائر I/O أفضل.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Lecture 2: Basics of Deep LearningDocsMIT HAN Lab, Fall 2023Slides 52 to 57 are the originals of slides 4 to 9, including the overlapped load, compute and store pipeline that justifies the max.الشرائح من 52 إلى 57 هي الأصل للشرائح من 4 إلى 9، ومنها خط المعالجة (pipeline) المتداخل للتحميل والحساب والتخزين الذي يبرر الـ max.(opens in a new tab)(يفتح في تبويب جديد)
- Roofline: An Insightful Visual Performance Model for Multicore ArchitecturesPaperCommunications of the ACM 2009, Williams, Waterman and PattersonAttainable performance = min(peak compute, bandwidth x operational intensity), the rate form of the max model.Attainable performance = min(peak compute, bandwidth x operational intensity)، وهي صيغة المعدّل لنموذج الـ max.(opens in a new tab)(يفتح في تبويب جديد)
- GPU Performance Background User's GuideDocsNVIDIAMath time versus memory time, and math-limited versus memory-limited, defined exactly as in slide 8.زمن الحساب (math time) مقابل زمن الذاكرة (memory time)، وmath-limited مقابل memory-limited، معرّفة تمامًا كما في الشريحة 8.(opens in a new tab)(يفتح في تبويب جديد)
- Jetson AGX OrinDocsNVIDIA2048-core Ampere GPU, 64 Tensor Cores at 43 dense FP16 TFLOPS (10.6 FP16 TFLOPS on CUDA cores alone), 204.8 GB/s LPDDR5, 15 W to 60 W.GPU بمعمارية Ampere فيه 2048 نواة، و64 من الـ Tensor Cores بأداء 43 dense FP16 TFLOPS (و10.6 FP16 TFLOPS على أنوية CUDA وحدها)، وذاكرة LPDDR5 بعرض نطاق 204.8 GB/s، واستهلاك من 15 W إلى 60 W.(opens in a new tab)(يفتح في تبويب جديد)
- TensorRT Quick Start GuideDocsNVIDIAWhat TensorRT does, and that speedup depends on model, precision, batch size and GPU.ما يفعله TensorRT، وأن مقدار التسريع يعتمد على النموذج والدقة العددية والـ batch size والـ GPU.(opens in a new tab)(يفتح في تبويب جديد)
- Computer Organization and Design, RISC-V Edition, 2nd editionBookElsevier 2020, Patterson and HennessyChapter 1: response time (execution time) versus throughput (bandwidth).الفصل 1: زمن الاستجابة (response time أو execution time) مقابل الـ throughput (أو bandwidth).(opens in a new tab)(يفتح في تبويب جديد)
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في نهاية هذا الجزء ستستطيع
- Define #Parameters as the element count of all weight tensors and explain why n, h and w never enter it.تعريف عدد المعاملات (#Parameters) بأنه عدد عناصر جميع موترات الأوزان، وشرح لماذا لا تدخل فيه n و h و w أبدًا.
- Write and derive the four counting formulas (linear, convolution, grouped, depthwise) and justify the single division by g.كتابة صيغ العدّ الأربع (الخطية، والالتفاف، والمجمّع، والعمقي) واشتقاقها، وتبرير القسمة الواحدة على g.
- Total AlexNet layer by layer, identify where 96 percent of the weights sit, and spot the slide's conv1 typo.جمع معاملات AlexNet طبقة بطبقة، وتحديد أين تقع 96 بالمئة من الأوزان، واكتشاف الخطأ المطبعي في conv1 على الشريحة.
- Convert a parameter count to model size at any bit width in bits, KB and MB, stating the decimal convention.تحويل عدد المعاملات إلى حجم نموذج عند أي عرض بت بالبت و KB و MB، مع ذكر الاصطلاح العشري المستخدم.
- 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.
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) واحدة يساوي ذلك حاصل ضرب بُعديها.
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 مخرجًا. كم معاملًا فيها مع إهمال الانحياز، وهل يغيّر حجم الدفعة هذا العدد؟
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 (الالتفاف)، تُقرأ مباشرة من شكل موتّر الأوزان.
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.
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.
Worked exampleمثال محلول
One 3 × 3 layer with 256 input and 256 output channelsطبقة 3 × 3 واحدة فيها 256 قناة مُدخل و 256 قناة مخرج
Standard convolutionالالتفاف العادي
256 · 256 · 3 · 3 = 589,824 weights.256 · 256 · 3 · 3 = 589,824 وزنًا.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 وزنًا.Grouped with g = 8مجمّع مع g = 8
589,824 / 8 = 73,728. Eight groups of 32 → 32.589,824 / 8 = 73,728. ثماني مجموعات، كل منها 32 → 32.Depthwise, g = 256عمقي، g = 256
256 · 9 = 2,304. Each channel keeps one 3 × 3 filter.256 · 9 = 2,304. كل قناة تحتفظ بمرشّح 3 × 3 واحد.Ratio to the standard layerالنسبة إلى الطبقة العادية
Layerالطبقة Countالعدّ #Parameters#Parameters Ratioالنسبة Standard, g = 1عادي، g = 1 256 · 256 · 9256 · 256 · 9 589,824589,824 11 Grouped, g = 2مجمّع، g = 2 256 · 256 · 9 / 2256 · 256 · 9 / 2 294,912294,912 1/21/2 Grouped, g = 8مجمّع، g = 8 256 · 256 · 9 / 8256 · 256 · 9 / 8 73,72873,728 1/81/8 Depthwise, g = 256عمقي، g = 256 256 · 9256 · 9 2,3042,304 1/2561/256 Layerالطبقة Countالعدّ #Parameters#Parameters Ratioالنسبة Standard, g = 1عادي، g = 1 256 · 256 · 9256 · 256 · 9 589,824589,824 11 Grouped, g = 2مجمّع، g = 2 256 · 256 · 9 / 2256 · 256 · 9 / 2 294,912294,912 1/21/2 Grouped, g = 8مجمّع، g = 8 256 · 256 · 9 / 8256 · 256 · 9 / 8 73,72873,728 1/81/8 Depthwise, g = 256عمقي، g = 256 256 · 9256 · 9 2,3042,304 1/2561/256
| Layerالطبقة | #Parameters#Parameters | Depends on h, w?يعتمد على h و w؟ | Depends on n?يعتمد على n؟ | Whyالسبب |
|---|---|---|---|---|
| Linearخطية (Linear) | c_o · c_ic_o · c_i | Only 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_w | Noلا | 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 / g | Noلا | 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_w | Noلا | Noلا | Grouped with g = c_i = c_o, one filter per channelمجمّع مع g = c_i = c_o، مرشّح واحد لكل قناة |
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 وراقب أي القراءات تتحرّك.
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 تربيع.
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، الجزء الأول: طبقات الالتفاف
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، لكنهما لا يمسّان العدد.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 معاملات.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.maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2
Output 256 × 13 × 13, 0 parameters.المخرج 256 × 13 × 13، و 0 معاملات.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.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.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 قناة مُدخل، ومجموعتان. كم عدد المعاملات؟
Worked exampleمثال محلول
Counting AlexNet, part two: the linear layers and the totalعدّ معاملات AlexNet، الجزء الثاني: الطبقات الخطية والمجموع
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 قيمة.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. طبقة واحدة، وهي وحدها أكثر من نصف الشبكة.fc7: linear, 4096 outputsfc7: خطية، 4096 مخرجًا
4096 · 4096 = 16,777,216.4096 · 4096 = 16,777,216.fc8: linear, 1000 outputsfc8: خطية، 1000 مخرج
1000 · 4096 = 4,096,000, one output per ImageNet class.1000 · 4096 = 4,096,000، مخرج واحد لكل صنف في ImageNet.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 × W | Countالعدّ | #Parameters#Parameters | Shareالنسبة |
|---|---|---|---|---|
| Imageالصورة | 3 × 224 × 2243 × 224 × 224 | 00 | ||
| conv1, 11 × 11, 96 ch, stride 4, pad 2conv1، نواة 11 × 11، و 96 قناة، وخطوة 4، وحشو 2 | 96 × 55 × 5596 × 55 × 55 | 96 · 3 · 11 · 1196 · 3 · 11 · 11 | 34,84834,848 | 0.06%0.06% |
| maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2 | 96 × 27 × 2796 × 27 × 27 | 00 | ||
| conv2, 5 × 5, 256 ch, pad 2, groups 2conv2، نواة 5 × 5، و 256 قناة، وحشو 2، ومجموعتان | 256 × 27 × 27256 × 27 × 27 | 256 · 96 · 5 · 5 / 2256 · 96 · 5 · 5 / 2 | 307,200307,200 | 0.50%0.50% |
| maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2 | 256 × 13 × 13256 × 13 × 13 | 00 | ||
| conv3, 3 × 3, 384 ch, pad 1conv3، نواة 3 × 3، و 384 قناة، وحشو 1 | 384 × 13 × 13384 × 13 × 13 | 384 · 256 · 3 · 3384 · 256 · 3 · 3 | 884,736884,736 | 1.45%1.45% |
| conv4, 3 × 3, 384 ch, pad 1, groups 2conv4، نواة 3 × 3، و 384 قناة، وحشو 1، ومجموعتان | 384 × 13 × 13384 × 13 × 13 | 384 · 384 · 3 · 3 / 2384 · 384 · 3 · 3 / 2 | 663,552663,552 | 1.09%1.09% |
| conv5, 3 × 3, 256 ch, pad 1, groups 2conv5، نواة 3 × 3، و 256 قناة، وحشو 1، ومجموعتان | 256 × 13 × 13256 × 13 × 13 | 256 · 384 · 3 · 3 / 2256 · 384 · 3 · 3 / 2 | 442,368442,368 | 0.73%0.73% |
| maxpool 3 × 3, stride 2maxpool بنافذة 3 × 3 وخطوة 2 | 256 × 6 × 6256 × 6 × 6 | 00 | ||
| fc6, linear 4096fc6، خطية 4096 | 40964096 | 4096 · (256 · 6 · 6)4096 · (256 · 6 · 6) | 37,748,73637,748,736 | 61.93%61.93% |
| fc7, linear 4096fc7، خطية 4096 | 40964096 | 4096 · 40964096 · 4096 | 16,777,21616,777,216 | 27.52%27.52% |
| fc8, linear 1000fc8، خطية 1000 | 10001000 | 1000 · 40961000 · 4096 | 4,096,0004,096,000 | 6.72%6.72% |
| Five convolutionsطبقات الالتفاف الخمس | 2,332,7042,332,704 | 3.83%3.83% | ||
| Three linear layersالطبقات الخطية الثلاث | 58,621,95258,621,952 | 96.17%96.17% | ||
| Totalالمجموع | 60,954,65660,954,656 | 100%100% |
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 كبيرة إلى هذا الحد؟
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 (عرض البت) فقط.
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.
| Data typeنوع البيانات | Size in bitsالحجم بالبت | Size (decimal MB)الحجم (MB عشرية) | Slide roundingتقريب الشريحة |
|---|---|---|---|
| 32-bit float (fp32)فاصلة عائمة 32-bit (fp32) | 1,950.5 Mbit1,950.5 Mbit | 243.8 MB243.8 MB | 244 MB244 MB |
| 16-bit (fp16 or bfloat16)16-bit (fp16 أو bfloat16) | 975.3 Mbit975.3 Mbit | 121.9 MB121.9 MB | 122 MB122 MB |
| 8-bit integer (int8)عدد صحيح 8-bit (int8) | 487.6 Mbit487.6 Mbit | 61.0 MB61.0 MB | 61 MB61 MB |
| 4-bit integer (int4)عدد صحيح 4-bit (int4) | 243.8 Mbit243.8 Mbit | 30.5 MB30.5 MB | 30.5 MB30.5 MB |
| 1-bit (binary weights)1-bit (أوزان ثنائية) | 61.0 Mbit61.0 Mbit | 7.6 MB7.6 MB | 7.6 MB7.6 MB |
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.
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، وبالبت؟
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المصادر
- ImageNet Classification with Deep Convolutional Neural NetworksPaperNeurIPS 2012, Krizhevsky, Sutskever and Hinton60 million parameters, per-layer kernel shapes, same-GPU connectivity behind groups = 2, first fully connected layer holds most parameters.60 مليون معامل، وأشكال الأنوية لكل طبقة، والاتصال داخل المعالج الرسومي نفسه الذي يقف خلف groups = 2، والطبقة الأولى الموصولة بالكامل تحمل معظم المعاملات.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.Conv2dDocsPyTorch documentationWeight shape (out_channels, in_channels / groups, kH, kW), divisibility by groups, depthwise as groups = in_channels, bias shape.شكل الوزن (out_channels, in_channels / groups, kH, kW)، وقابلية القسمة على groups، والالتفاف العمقي بوصفه groups = in_channels، وشكل الانحياز.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Fall 2024DocsMIT HAN Lab, Song HanLecture 2, Basics of Deep Learning, is the source deck for slides 10 to 17 including the conv1 typo.المحاضرة 2، Basics of Deep Learning، هي مصدر الشرائح من 10 إلى 17، بما في ذلك الخطأ المطبعي في conv1.(opens in a new tab)(يفتح في تبويب جديد)
- Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman CodingPaperICLR 2016, Han, Mao and DallyAlexNet storage reduced 35x, from 240 MB to 6.9 MB.تخزين AlexNet خُفّض 35x، من 240 MB إلى 6.9 MB.(opens in a new tab)(يفتح في تبويب جديد)
- Learning both Weights and Connections for Efficient Neural NetworksPaperNeurIPS 2015, Han, Pool, Tran and DallyAlexNet pruned 9x, from 61 million to 6.7 million parameters.AlexNet قُلّمت 9x، من 61 مليون معامل إلى 6.7 مليون.(opens in a new tab)(يفتح في تبويب جديد)
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision ApplicationsPaperarXiv 2017, Howard et al.Depthwise convolution applies a single filter per input channel; Table 2 gives parameter shares of 1 x 1, depthwise and linear layers.الالتفاف العمقي يطبّق مرشّحًا واحدًا لكل قناة مُدخل؛ والجدول 2 يعطي نسب المعاملات لطبقات 1 x 1 والطبقات العمقية والخطية.(opens in a new tab)(يفتح في تبويب جديد)
- Prefixes for binary multiplesDocsNIST1 MB = 10^6 bytes, 1 MiB = 2^20 bytes.1 MB = 10^6 بايت، و 1 MiB = 2^20 بايت.(opens in a new tab)(يفتح في تبويب جديد)
- torchvision.models.alexnetDocsPyTorch documentation61,100,840 parameters; implementation follows the One weird trick variant, not the 2012 paper.61,100,840 معاملًا؛ والتنفيذ يتبع إصدار One weird trick، لا ورقة 2012.(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, chapter 9: Convolutional NetworksBookMIT Press, Goodfellow, Bengio and CourvilleSection 9.2, parameter sharing: a two-parameter edge kernel versus a dense matrix with billions of entries.القسم 9.2، مشاركة المعاملات: نواة حواف بمعاملين مقابل مصفوفة كثيفة فيها مليارات العناصر.(opens in a new tab)(يفتح في تبويب جديد)
- MCUNet: Tiny Deep Learning on IoT DevicesPaperNeurIPS 2020, Lin et al.Runs TinyNAS networks on the STM32F746, a microcontroller with 320 kB SRAM and 1 MB flash.يشغّل شبكات TinyNAS على STM32F746، وهو متحكّم دقيق فيه 320 kB من SRAM و 1 MB من الفلاش.(opens in a new tab)(يفتح في تبويب جديد)
- Network In NetworkPaperICLR 2014, Lin, Chen and YanGlobal average pooling replaces fully connected classifier layers and has no parameters to optimize.التجميع المتوسط الشامل (global average pooling) يحلّ محل طبقات المصنِّف الموصولة بالكامل، ولا يحتوي أي معامل يُحسَّن.(opens in a new tab)(يفتح في تبويب جديد)
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في نهاية هذا الجزء ستستطيع
- 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.
- 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.
- 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 المختار.
- 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.
- 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 انتقل من مكانه دون أن يقلّصه أحد.
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-18 | 11.2 MB11.2 MB | 0.9 MB0.9 MB | No (flash and SRAM)لا (flash وSRAM) | No (flash and SRAM)لا (flash وSRAM) |
| MobileNetV2-0.75MobileNetV2-0.75 | 2.5 MB2.5 MB | 1.7 MB1.7 MB | No (flash and SRAM)لا (flash وSRAM) | No (flash and SRAM)لا (flash وSRAM) |
| MCUNet (paper, Figure 8)MCUNet (الورقة، Figure 8) | 1.9 MB1.9 MB | 0.49 MB0.49 MB | No (flash and SRAM)لا (flash وSRAM) | Yesنعم |
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).
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 فيه قليل؟
Recall
Where do weights and activations live on a microcontroller at run time, and which one sets the peak?أين تعيش الأوزان والـ activations على الـ microcontroller وقت التشغيل، وأيهما يحدد الذروة؟
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-50 | 102102 | 707707 | about 7xحوالي 7x |
| MobileNetV2-1.4MobileNetV2-1.4 | about 24حوالي 24 | about 626حوالي 626 | about 26xحوالي 26x |
| Reductionالتخفيض | 4.3x4.3x | 1.1x1.1x |
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 العائد من الطبقة التالية.
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 يضربها مرة أخرى.
| Modeالوضع | Which activations are heldأي الـ activations تُحفَظ | How manyكم عددها | Memory ruleقاعدة الذاكرة |
|---|---|---|---|
| Inferenceالـ inference | Input and output of the layer running nowالـ input والـ output للطبقة التي تعمل الآن | Two tensorsاثنان من الـ tensors | Largest 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، مضروبة في الـ batch | Sum of all activations × batch sizeمجموع كل الـ activations × الـ batch size |
Worked exampleمثال محلول
AlexNet in fp32, inference against trainingAlexNet بـ fp32: الـ inference مقابل التدريب
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 الحية في وقت واحد.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.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 أخرى.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 وقل ماذا تقارن كل واحدة منها.
Recall
Why does training need every activation while inference needs only two?لماذا يحتاج التدريب إلى كل الـ activations بينما يحتاج الـ inference إلى اثنين فقط؟
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ذاكرة الـ activation | Weight memoryذاكرة الأوزان | Dominantالمهيمن |
|---|---|---|---|
| 0 to 50 إلى 5 | 50 to 75 kB50 إلى 75 kB | about 0حوالي 0 | Activationالـ activation |
| 6 to 176 إلى 17 | 20 to 55 kB20 إلى 55 kB | about 0حوالي 0 | Activationالـ activation |
| 18 to 3018 إلى 30 | under 15 kBأقل من 15 kB | under 15 kBأقل من 15 kB | Neitherلا أحد منهما |
| 32 to 4232 إلى 42 | 5 to 10 kB5 إلى 10 kB | 35 to 75 kB35 إلى 75 kB | Weightالأوزان |
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×W | Activationsالـ activations | Weightsالأوزان | Which dominatesأيهما يهيمن |
|---|---|---|---|---|
| conv1conv1 | 96×55×5596×55×55 | 290,400290,400 | 34,84834,848 | Activations, 8xالـ activations، 8x |
| conv2conv2 | 256×27×27256×27×27 | 186,624186,624 | 307,200307,200 | Weights, 1.6xالأوزان، 1.6x |
| conv3conv3 | 384×13×13384×13×13 | 64,89664,896 | 884,736884,736 | Weights, 14xالأوزان، 14x |
| conv4conv4 | 384×13×13384×13×13 | 64,89664,896 | 663,552663,552 | Weights, 10xالأوزان، 10x |
| conv5conv5 | 256×13×13256×13×13 | 43,26443,264 | 442,368442,368 | Weights, 10xالأوزان، 10x |
| fc6fc6 | 40964096 | 4,0964,096 | 37,748,73637,748,736 | Weights, 9,216xالأوزان، 9,216x |
| fc7fc7 | 40964096 | 4,0964,096 | 16,777,21616,777,216 | Weights, 4,096xالأوزان، 4,096x |
| fc8fc8 | 10001000 | 1,0001,000 | 4,096,0004,096,000 | Weights, 4,096xالأوزان، 4,096x |
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 عالية في الطبقات الأولى، ولماذا تكون ذاكرة الأوزان عالية في الطبقات الأخيرة؟
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).
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 قيمة نفسها تتّسع أم لا.
| Layer executingالطبقة المنفَّذة | Inputالمُدخل | Outputالمُخرج | Liveالحية | At 8-bitعند 8-bit |
|---|---|---|---|---|
| 150,528 | 290,400 | 440,928 | 440.9 kB | |
| 290,400 | 69,984 | 360,384 | 360.4 kB | |
| 69,984 | 186,624 | 256,608 | 256.6 kB | |
| 186,624 | 43,264 | 229,888 | 229.9 kB | |
| 43,264 | 64,896 | 108,160 | 108.2 kB | |
| 64,896 | 64,896 | 129,792 | 129.8 kB | |
| 64,896 | 43,264 | 108,160 | 108.2 kB | |
| 43,264 | 9,216 | 52,480 | 52.5 kB | |
| 9,216 | 4,096 | 13,312 | 13.3 kB | |
| 4,096 | 4,096 | 8,192 | 8.2 kB | |
| 4,096 | 1,000 | 5,096 | 5.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
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.Sum for the totalاجمع لتحصل على العدد الكلي
932,264 values, matching the slide. This is the number that training would have to hold.932,264 قيمة، مطابقة للشريحة. هذا هو العدد الذي سيتعين على التدريب الاحتفاظ به.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
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 تصبح الأزواج ربع الذروة.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)
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؟
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؟
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المصادر
- MCUNet: Tiny Deep Learning on IoT DevicesPaperNeurIPS 2020, Lin, Chen, Lin, Cohn, Gan and Han4.6x and 1.8x, SRAM versus flash roles, MCU list with STM32F746 at 320 kB, 5.3x in Table 1, in-place depthwise convolution, 2.2x block imbalance on a 0.3x-width MobileNetV2 (Figure 11).4.6x و1.8x، دورا الـ SRAM والـ flash، قائمة الـ MCUs مع STM32F746 عند 320 kB، و5.3x في Table 1، والـ depthwise convolution من نوع in-place، وعدم توازن الـ blocks بـ 2.2x على MobileNetV2 بعرض 0.3x (Figure 11).(opens in a new tab)(يفتح في تبويب جديد)
- MCUNetV2: Memory-Efficient Patch-based Inference for Tiny Deep LearningPaperNeurIPS 2021, Lin, Chen, Cai, Gan and HanPeak = input + output rule, imbalanced MobileNetV2 profile with 1372 kB peak, 539 kB first-conv example, resolution versus channel growth. True source of the right chart on slide 18.قاعدة الذروة = input + output، ومخطط MobileNetV2 غير المتوازن بذروة 1372 kB، ومثال الـ convolution الأولى بـ 539 kB، والـ resolution مقابل نمو الـ channels. وهي المصدر الحقيقي للمخطط الأيمن في الشريحة 18.(opens in a new tab)(يفتح في تبويب جديد)
- TinyTL: Reduce Activations, Not Trainable Parameters for Efficient On-Device LearningPaperNeurIPS 2020, Cai, Gan, Zhu and Han102 MB versus 707 MB, 4.3x versus 1.1x (the slide halves the paper's batch 16 values, so it shows batch 8), equation 2 for backpropagation memory, bias-only updates, DRAM access two orders of magnitude costlier than SRAM.102 MB مقابل 707 MB، و4.3x مقابل 1.1x (الشريحة تقسم قيم الورقة عند batch 16 على اثنين، فهي تُظهر batch 8)، والمعادلة 2 لذاكرة الـ backpropagation، والتحديثات على الـ bias فقط، وأن الوصول إلى الـ DRAM أغلى من الـ SRAM بمرتبتين عشريتين.(opens in a new tab)(يفتح في تبويب جديد)
- On-Device Training Under 256KB MemoryPaperNeurIPS 2022, Lin, Zhu, Wang, Cai, Gan and HanFigure 11(a): activation and weight memory of updating each layer of MCUNet. True source of slide 20.Figure 11(a): ذاكرة الـ activation والأوزان اللازمة لتحديث كل طبقة في MCUNet. وهي المصدر الحقيقي للشريحة 20.(opens in a new tab)(يفتح في تبويب جديد)
- ImageNet Classification with Deep Convolutional Neural NetworksPaperNeurIPS 2012, Krizhevsky, Sutskever and HintonAlexNet architecture: five convolutions, max pooling, three fully connected layers, about 60M parameters.بنية AlexNet: خمس convolutions، وmax pooling، وثلاث طبقات fully connected، وحوالي 60M parameter.(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, chapter 6: Deep Feedforward NetworksBookMIT Press, Goodfellow, Bengio and Courville, 2016Section 6.5.7: hidden-layer inputs are stored until the backward pass returns, at cost O(m n_h) per layer.القسم 6.5.7: الـ inputs الخاصة بالطبقات المخفية تُخزَّن حتى يعود الـ backward pass، بتكلفة O(m n_h) لكل طبقة.(opens in a new tab)(يفتح في تبويب جديد)
- MIT 6.5940 TinyML and Efficient Deep Learning Computing, Fall 2023DocsMIT HAN LabLecture 02 slides 70 to 75 are the origin of slides 18 to 22. The 14, 16, 16, 17 axis typo is already in Figure 1 of the MCUNetV2 paper and was carried into this deck and the slide.شرائح المحاضرة 02 من 70 إلى 75 هي أصل الشرائح 18 إلى 22. خطأ المحور 14, 16, 16, 17 موجود أصلًا في Figure 1 من ورقة MCUNetV2 وانتقل إلى هذا العرض وإلى الشريحة.(opens in a new tab)(يفتح في تبويب جديد)
- Get started with LiteRT for MicrocontrollersDocsGoogle for DevelopersThe tensor arena preallocates RAM for input, output and intermediate arrays; the model ships as a byte array.الـ tensor arena تحجز مسبقًا RAM للـ input والـ output والمصفوفات الوسيطة؛ والنموذج يُشحن على شكل byte array.(opens in a new tab)(يفتح في تبويب جديد)
- STM32F746NG product pageDocsSTMicroelectronicsCortex-M7 up to 216 MHz, 1 MB flash, 320 KB SRAM; the same figures appear in MCUNet's table 1.Cortex-M7 حتى 216 MHz، و1 MB flash، و320 KB SRAM؛ والأرقام نفسها تظهر في Table 1 من MCUNet.(opens in a new tab)(يفتح في تبويب جديد)
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في نهاية هذا الجزء ستستطيع
- Define a multiply-accumulate operation and count MACs for a matrix-vector product and a matrix-matrix product.تعرّف عملية الضرب والتجميع (multiply-accumulate) وتحسب عدد MACs لضرب مصفوفة في متجه ولضرب مصفوفة في مصفوفة.
- 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.
- Reproduce AlexNet's 724M MACs layer by layer and explain why convolutions dominate compute while linear layers dominate parameters.تعيد حساب الـ 724M MACs في AlexNet طبقةً طبقة، وتشرح لماذا تهيمن طبقات الالتفاف على الحساب بينما تهيمن الطبقات الخطية على المعاملات.
- 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).
- 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) في الشبكة العصبية بأكمله هو هذه الخطوة مكررة بضع مئات من ملايين المرات.
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.
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.
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):
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المعاملات | MACsMACs | MACs per parameterMACs لكل معامل |
|---|---|---|---|
| Linearخطية (Linear) | c_o · c_ic_o · c_i | c_o · c_ic_o · c_i | 11 |
| Convolutionالتفاف (Convolution) | c_o · c_i · k_h · k_wc_o · c_i · k_h · k_w | c_o · c_i · k_h · k_w · h_o · w_oc_o · c_i · k_h · k_w · h_o · w_o | h_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 / g | c_o · c_i · k_h · k_w · h_o · w_o / gc_o · c_i · k_h · k_w · h_o · w_o / g | h_o · w_oh_o · w_o |
| Depthwise convolutionالتفاف عمقي (Depthwise convolution) | c_o · k_h · k_wc_o · k_h · k_w | c_o · k_h · k_w · h_o · w_oc_o · k_h · k_w · h_o · w_o | h_o · w_oh_o · w_o |
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 قناة
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.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.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.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.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 كلما كبر حجم المخرج.
illustrative mobile GPU-class figureرقم توضيحي بمستوى معالجات GPU المحمولة
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، وكم عدد المعاملات، وما النسبة بينهما؟
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، مع إهمال الانحياز)
conv1: 11 × 11, 3 to 96 channels, output 55 × 55conv1: نواة 11 × 11، من 3 إلى 96 قناة، المخرج 55 × 55
96 × 3 × 11 × 11 × 55 × 55 = 105,415,20096 × 3 × 11 × 11 × 55 × 55 = 105,415,200conv2: 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 ينصّفه.conv3: 3 × 3, 256 to 384 channels, output 13 × 13conv3: نواة 3 × 3، من 256 إلى 384 قناة، المخرج 13 × 13
384 × 256 × 3 × 3 × 13 × 13 = 149,520,384384 × 256 × 3 × 3 × 13 × 13 = 149,520,384conv4: 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,288384 × 384 × 3 × 3 × 13 × 13 / 2 = 112,140,288conv5: 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,192256 × 384 × 3 × 3 × 13 × 13 / 2 = 74,760,192هذه هي الخانة التي تخفيها الشريحة. احسبها قبل كشف الإجابة.Recall
conv5 MACs?كم عدد MACs في conv5؟
256 × 384 × 3 × 3 × 13 × 13 / 2 = 74,760,192256 × 384 × 3 × 3 × 13 × 13 / 2 = 74,760,192fc6: 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) هو متجه المدخل.fc7: 4,096 to 4,096fc7: من 4,096 إلى 4,096
4,096 × 4,096 = 16,777,2164,096 × 4,096 = 16,777,216fc8: 4,096 to 1,000 classesfc8: من 4,096 إلى 1,000 فئة
1,000 × 4,096 = 4,096,0001,000 × 4,096 = 4,096,000Totalالمجموع
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المعاملات | MACsMACs | MACs per parameterMACs لكل معامل |
|---|---|---|---|---|
| conv1conv1 | 96 × 3 × 11 × 11, out 55 × 5596 × 3 × 11 × 11، المخرج 55 × 55 | 34,84834,848 | 105,415,200105,415,200 | 3,0253,025 |
| conv2conv2 | 256 × 96 × 5 × 5 / 2, out 27 × 27256 × 96 × 5 × 5 / 2، المخرج 27 × 27 | 307,200307,200 | 223,948,800223,948,800 | 729729 |
| conv3conv3 | 384 × 256 × 3 × 3, out 13 × 13384 × 256 × 3 × 3، المخرج 13 × 13 | 884,736884,736 | 149,520,384149,520,384 | 169169 |
| conv4conv4 | 384 × 384 × 3 × 3 / 2, out 13 × 13384 × 384 × 3 × 3 / 2، المخرج 13 × 13 | 663,552663,552 | 112,140,288112,140,288 | 169169 |
| conv5conv5 | 256 × 384 × 3 × 3 / 2, out 13 × 13256 × 384 × 3 × 3 / 2، المخرج 13 × 13 | 442,368442,368 | 74,760,19274,760,192 | 169169 |
| fc6fc6 | 4096 × (256 × 6 × 6)4096 × (256 × 6 × 6) | 37,748,73637,748,736 | 37,748,73637,748,736 | 11 |
| fc7fc7 | 4096 × 40964096 × 4096 | 16,777,21616,777,216 | 16,777,21616,777,216 | 11 |
| fc8fc8 | 1000 × 40961000 × 4096 | 4,096,0004,096,000 | 4,096,0004,096,000 | 11 |
| conv1 to conv5من conv1 إلى conv5 | five 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 إلى fc8 | three 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,656 | 724,406,816724,406,816 |
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 في نهاية الشبكة تجعل الطبقات الخطية ثقيلة في المعاملات.
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، ولماذا؟
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، وإظهار معامل الاثنين أهم من التقريب.
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.
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 GFLOPS | 1.449 × 10^9 / 10^91.449 × 10^9 / 10^9 | 1,449 ms1,449 ms |
| 100 GFLOPS100 GFLOPS | 1.449 × 10^9 / 10^111.449 × 10^9 / 10^11 | 14.5 ms14.5 ms |
| 1 TFLOPS1 TFLOPS | 1.449 × 10^9 / 10^121.449 × 10^9 / 10^12 | 1.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^13 | 0.022 ms0.022 ms |
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) أعدادًا عائمة.
| Termالمصطلح | Kindالنوع | Meaningالمعنى | Belongs toيخص |
|---|---|---|---|
| MACMAC | Countعدد | One multiply accumulated into a sumعملية ضرب واحدة تُجمع في مجموع جارٍ | A model, per inferenceالنموذج، لكل استدلال |
| FLOPFLOP | Countعدد | One floating point multiply or add; a MAC is twoعملية ضرب أو جمع واحدة بالفاصلة العائمة، وعملية MAC تساوي اثنتين | A model, per inferenceالنموذج، لكل استدلال |
| OPOP | Countعدد | One operation of any data type; a MAC is twoعملية واحدة لأي نوع من البيانات، وعملية MAC تساوي اثنتين | A model, per inferenceالنموذج، لكل استدلال |
| FLOPSFLOPS | Rateمعدل | Floating point operations per secondعدد عمليات الفاصلة العائمة في الثانية | A processorالمعالج |
| OPSOPS | Rateمعدل | Operations per second, any data typeعدد العمليات في الثانية، لأي نوع من البيانات | A processorالمعالج |
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.
Recall
Why use OPs rather than FLOPs for an INT8 model, and how many OPs per MAC?لماذا نستخدم OPs بدلًا من FLOPs لنموذج INT8، وكم OP لكل 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مستخدمة في هذه المحاضرة
- Dumoulin and Visin, Convolution arithmetic (GitHub repository and arXiv 1603.07285): the output-shape arithmetic behind every h_o × w_o in this part.Dumoulin وVisin، Convolution arithmetic (مستودع GitHub وarXiv 1603.07285): حسابات شكل المخرج وراء كل h_o × w_o في هذا الجزء.
- Stanford CS231n, lecture 5, Image Classification with CNNs: parameter sharing, the output-size formula and im2col.Stanford CS231n، المحاضرة 5، Image Classification with CNNs: مشاركة المعاملات، وصيغة حجم المخرج، وim2col.
- Krizhevsky, Sutskever and Hinton, ImageNet Classification with Deep Convolutional Neural Networks, NeurIPS 2012: the AlexNet layers counted on slides 25 and 26.Krizhevsky وSutskever وHinton، ImageNet Classification with Deep Convolutional Neural Networks، NeurIPS 2012: طبقات AlexNet المعدودة في الشريحتين 25 و26.
- Sandler et al., MobileNetV2: Inverted Residuals and Linear Bottlenecks, CVPR 2018: the depthwise design that cuts both parameters and MACs, and the "MAdd" convention.Sandler et al.، MobileNetV2: Inverted Residuals and Linear Bottlenecks، CVPR 2018: التصميم العمقي (depthwise) الذي يخفض المعاملات وMACs معًا، واصطلاح "MAdd".
- Deng, Li, Han, Shi and Xie, Model Compression and Hardware Acceleration for Neural Networks: A Comprehensive Survey, Proceedings of the IEEE 108(4), 2020: the wider map of compression techniques that these metrics evaluate.Deng وLi وHan وShi وXie، Model Compression and Hardware Acceleration for Neural Networks: A Comprehensive Survey، Proceedings of the IEEE 108(4), 2020: الخريطة الأوسع لتقنيات الضغط التي تقيّمها هذه المقاييس.
- Song Han, MIT 6.5940 TinyML and Efficient Deep Learning Computing, lecture 2: the direct source of slides 23 to 28.Song Han، MIT 6.5940 TinyML and Efficient Deep Learning Computing، المحاضرة 2: المصدر المباشر للشرائح من 23 إلى 28.
Context for other lecturesسياق لمحاضرات أخرى
- Ioffe and Szegedy, Batch Normalization, ICML 2015.Ioffe وSzegedy، Batch Normalization، ICML 2015.
- Wu and He, Group Normalization, ECCV 2018.Wu وHe، Group Normalization، ECCV 2018.
- Simonyan and Zisserman, Very Deep Convolutional Networks for Large-Scale Image Recognition, ICLR 2015.Simonyan وZisserman، Very Deep Convolutional Networks for Large-Scale Image Recognition، ICLR 2015.
- He, Zhang, Ren and Sun, Deep Residual Learning for Image Recognition, CVPR 2016.He وZhang وRen وSun، Deep Residual Learning for Image Recognition، CVPR 2016.
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المصادر
- MIT 6.5940 Fall 2023, Lecture 2: Basics of Deep LearningVideoMIT HAN Lab, Song HanSlides 77 to 85 are the source of slides 23 to 28: MAC, MV, GEMM, per-layer MACs, AlexNet 724M, FLOP, FLOPS, OP, OPS.الشرائح من 77 إلى 85 هي مصدر الشرائح من 23 إلى 28: MAC، و MV، و GEMM، و MACs لكل طبقة، و AlexNet بعدد 724M، و FLOP، و FLOPS، و OP، و OPS.(opens in a new tab)(يفتح في تبويب جديد)
- ImageNet Classification with Deep Convolutional Neural NetworksPaperNeurIPS 2012, Krizhevsky, Sutskever and HintonAlexNet: five convolution and three linear layers, about 60M parameters.AlexNet: خمس طبقات التفاف وثلاث طبقات خطية، ونحو 60M من المعاملات.(opens in a new tab)(يفتح في تبويب جديد)
- Efficient Processing of Deep Neural Networks: A Tutorial and SurveyPaperProceedings of the IEEE, Sze, Chen, Yang and EmerMAC as the fundamental component; AlexNet at 61M weights and 724M MACs, with FC layers at 58.6M of each.MAC بوصفها المكوّن الأساسي؛ و AlexNet بعدد 61M من الأوزان و 724M من MACs، وتحمل الطبقات المتصلة كليًا (FC) 58.6M من كل منهما.(opens in a new tab)(يفتح في تبويب جديد)
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision ApplicationsPaperarXiv, Howard et al.Equations 4 and 5 give the depthwise and separable costs; the unnumbered ratio that follows is 1/N + 1/D_K^2, 8 to 9 times less computation; Mult-Adds; GEMM via im2col.المعادلتان 4 و 5 تعطيان كلفة الالتفاف العمقي (depthwise) والمنفصل (separable)؛ والنسبة غير المرقّمة التي تليهما هي 1/N + 1/D_K^2، أي حساب أقل بما بين 8 و 9 مرات؛ و Mult-Adds؛ و GEMM عبر im2col.(opens in a new tab)(يفتح في تبويب جديد)
- MobileNetV2: Inverted Residuals and Linear BottlenecksPaperCVPR 2018, Sandler et al.Operations reported as multiply-adds (MAdd).العمليات مذكورة على شكل عمليات ضرب وجمع (MAdd).(opens in a new tab)(يفتح في تبويب جديد)
- Deep Learning, chapter 9: Convolutional NetworksBookMIT Press, Goodfellow, Bengio and CourvilleSection 9.2: parameter sharing keeps forward runtime O(k x n) while cutting storage to k parameters.القسم 9.2: مشاركة المعاملات تُبقي زمن الانتشار الأمامي O(k x n) بينما تخفض التخزين إلى k من المعاملات.(opens in a new tab)(يفتح في تبويب جديد)
- Matrix Multiplication Background User's GuideDocsNVIDIA Deep Learning Performance documentationGEMM costs 2 * M * N * K FLOPs (the page itself writes FLOPS) because each FMA is two operations, a multiply and an add.كلفة GEMM هي 2 * M * N * K من FLOPs (والصفحة نفسها تكتبها FLOPS) لأن كل FMA عبارة عن عمليتين، ضرب وجمع.(opens in a new tab)(يفتح في تبويب جديد)
- GPU Performance Background User's GuideDocsNVIDIA Deep Learning Performance documentationMath time equals the number of operations divided by the processor's math bandwidth: T_computation = FLOPs / FLOPS when math limited.زمن الحساب يساوي عدد العمليات مقسومًا على عرض النطاق الحسابي للمعالج: T_computation = FLOPs / FLOPS عندما يكون الأداء محدودًا بالحساب.(opens in a new tab)(يفتح في تبويب جديد)
- torch.nn.Conv2dDocsPyTorch documentationOutput-size formula, groups semantics, groups = in_channels as depthwise convolution.صيغة حجم المخرج، ومعنى groups، و groups = in_channels بوصفها التفافًا عمقيًا (depthwise).(opens in a new tab)(يفتح في تبويب جديد)
- CS231n Convolutional Neural Networks for Visual Recognition: course notesDocsStanford UniversityParameter sharing, output-size arithmetic, AlexNet conv1 at 55 x 55 x 96, im2col.مشاركة المعاملات، وحساب حجم المخرج، و conv1 في AlexNet بحجم 55 x 55 x 96، و im2col.(opens in a new tab)(يفتح في تبويب جديد)
- TOP500 List, November 2024DocsTOP500Rmax and Rpeak columns reported in PFlop/s.عمودا Rmax و Rpeak مذكوران بوحدة PFlop/s.(opens in a new tab)(يفتح في تبويب جديد)
- fvcore FlopCountAnalysisDocsDetectron2 documentation, MetaCounts one fused multiply-add as one flop: the opposite convention to this course.تعدّ عملية الضرب والجمع المدمجة الواحدة flop واحدًا: وهو الاصطلاح المعاكس لاصطلاح هذا المقرر.(opens in a new tab)(يفتح في تبويب جديد)
- Multiply-accumulate operationArticleWikipediaDefinition a <- a + (b x c) and the IEEE 754-2008 fused multiply-add. Used only for the definition.التعريف a <- a + (b x c)، وعملية الضرب والجمع المدمجة في IEEE 754-2008. استُخدم للتعريف فقط.(opens in a new tab)(يفتح في تبويب جديد)
- Jetson modulesDocsNVIDIA DeveloperJetson Orin Nano up to 67 TOPS and AGX Orin up to 275 TOPS.Jetson Orin Nano حتى 67 TOPS، و AGX Orin حتى 275 TOPS.(opens in a new tab)(يفتح في تبويب جديد)
- Jetson Orin specificationsDocsNVIDIAThe TOPS figures are sparse INT8; dense INT8 ratings are about half, for example 170 sparse against 85 dense.أرقام TOPS هي قيم متفرقة (sparse) بدقة INT8؛ والقيم الكثيفة (dense) بدقة INT8 تقارب النصف، مثلًا 170 متفرقة مقابل 85 كثيفة.(opens in a new tab)(يفتح في تبويب جديد)
- Model Compression and Hardware Acceleration for Neural Networks: A Comprehensive SurveyPaperProceedings of the IEEE 108(4), Deng, Li, Han, Shi and XieReference 9 on slide 29.المرجع 9 في الشريحة 29.(opens in a new tab)(يفتح في تبويب جديد)
- A guide to convolution arithmetic for deep learningDocsDumoulin and Visin, GitHub and arXiv 1603.07285Reference 1 on slide 29: output-shape arithmetic.المرجع 1 في الشريحة 29: حساب شكل المخرج.(opens in a new tab)(يفتح في تبويب جديد)