Lecture 03 · NN Efficiency Metrics
المحاضرة 03 · مقاييس كفاءة الشبكات العصبية
AlexNet parameters and model size
معاملات AlexNet وحجم النموذج
P1 MUSTEight layers with weights, four formulas, one multiplication by the bit width. This is the table the exam hands you in a new disguise, so fill it once by hand.
ثماني طبقات فيها أوزان، وأربع معادلات، وعملية ضرب واحدة في bit width. هذا هو الجدول الذي سيعطيك إياه الاختبار بشكل جديد، فاملأه بيدك مرة واحدة على الأقل.
The four parameter formulas
صيغ المعاملات الأربع
Slide 15 puts these next to the AlexNet table. Bias is ignored throughout, exactly as the slide says. ci is input channels, co is output channels, kh × kw is the kernel, g is the number of groups.
تضع الشريحة 15 هذه المعادلات بجانب جدول AlexNet. ويُهمَل bias في كل الحسابات كما تقول الشريحة. وci هو عدد قنوات الدخل، وco عدد قنوات الخرج، وkh × kw حجم kernel، وg عدد المجموعات.
| Layerالطبقة | #Parameters#Parameters |
|---|---|
| Linearخطية (linear) | co · ci |
| Convolutionالتفاف (convolution) | co · ci · kh · kw |
| Grouped convolutionالتفاف مجمّع (grouped convolution) | co · ci · kh · kw / g |
| Depthwise convolutionالتفاف عمقي (depthwise convolution) | co · kh · kw |
الجدول يلخّص المعادلات الأربع: linear هو co · ci، وconvolution يضيف حجم kernel، وgrouped convolution يقسم على g، وdepthwise يحذف ci تماماً لأن كل قناة لها filter واحد خاص بها.
Notice what is missing: the spatial size of the feature map. A conv layer's weights do not care whether the image is 13 × 13 or 224 × 224. Only channels and kernel size count. Pooling layers have no weights at all.
لاحظ ما هو غائب: الحجم المكاني للـ feature map. فأوزان طبقة conv لا تهتم إن كانت الصورة 13 × 13 أو 224 × 224، فقط عدد القنوات وحجم kernel هما المهمان. أما طبقات pooling فليس فيها أوزان إطلاقاً.
The full AlexNet table, by hand
جدول AlexNet كاملاً، باليد
Input 3 × 224 × 224. Shapes are C × H × W after each layer. Groups 2 means split into two halves, which divides the parameters by 2.
الدخل 3 × 224 × 224. والأشكال مكتوبة C × H × W بعد كل طبقة. وgroups 2 تعني التقسيم إلى نصفين، فتنقسم الأوزان على 2.
| Layerالطبقة | Outputالخرج | Arithmeticالحساب | #Parameters#Parameters |
|---|---|---|---|
| conv1 11×11, 96, stride 4, pad 2conv1 11×11، 96، خطوة 4، حشو 2 | 96×55×55 | 96 × 3 × 11 × 11 | 34,848 |
| maxpool 3×3, s2maxpool 3×3، s2 | 96×27×27 | no weightsبلا أوزان | 0 |
| conv2 5×5, 256, pad 2, groups 2conv2 5×5، 256، حشو 2، مجموعتان | 256×27×27 | 256 × 96 × 5 × 5 / 2 | 307,200 |
| maxpool 3×3, s2maxpool 3×3، s2 | 256×13×13 | no weightsبلا أوزان | 0 |
| conv3 3×3, 384, pad 1conv3 3×3، 384، حشو 1 | 384×13×13 | 384 × 256 × 3 × 3 | 884,736 |
| conv4 3×3, 384, pad 1, groups 2conv4 3×3، 384، حشو 1، مجموعتان | 384×13×13 | 384 × 384 × 3 × 3 / 2 | 663,552 |
| conv5 3×3, 256, pad 1, groups 2conv5 3×3، 256، حشو 1، مجموعتان | 256×13×13 | 256 × 384 × 3 × 3 / 2 | 442,368 |
| maxpool 3×3, s2maxpool 3×3، s2 | 256×6×6 | no weightsبلا أوزان | 0 |
| fc6 linear 4096fc6 خطية 4096 | 4096 | 4096 × (256 × 6 × 6) | 37,748,736 |
| fc7 linear 4096fc7 خطية 4096 | 4096 | 4096 × 4096 | 16,777,216 |
| fc8 linear 1000fc8 خطية 1000 | 1000 | 1000 × 4096 | 4,096,000 |
| Totalالمجموع | conv 2,332,704 + FC 58,621,952conv 2,332,704 + FC 58,621,952 | 60,954,656 |
الجدول يمر على طبقات AlexNet واحدة واحدة: conv1 يساوي 34,848، وconv2 يساوي 307,200، وconv3 يساوي 884,736، وconv4 يساوي 663,552، وconv5 يساوي 442,368، وطبقات maxpool صفر. ثم fc6 وحدها 37,748,736، وfc7 يساوي 16,777,216، وfc8 يساوي 4,096,000. والمجموع 60,954,656، أي حوالي 61M كما تقول الشريحة.
في الرسم أشرطة أفقية بمقياس خطي لعدد الأوزان في كل طبقة. أشرطة طبقات conv الخمس تكاد لا تُرى، ومجموعها 2.33M، أي 3.8% فقط. أما طبقات FC الثلاث فمجموعها 58.6M، أي 96.2%، وfc6 وحدها تحمل 61.9% لأنها تربط كل قيمة من القيم المسطّحة البالغة 9,216 بكل واحد من المخارج 4,096.
Why fc6 is so heavy
لماذا fc6 ثقيلة إلى هذا الحد
After the last maxpool the tensor is 256 × 6 × 6. A linear layer cannot take a 3-D tensor, so it is flattened into a vector of 256 × 6 × 6 = 9,216 values. That 9,216 is ci for fc6. Multiply by co = 4,096 and you get 37,748,736.
بعد آخر maxpool يكون الـ tensor بشكل 256 × 6 × 6. والطبقة الخطية لا تقبل tensor ثلاثي الأبعاد، فيُسطَّح إلى متجه طوله 256 × 6 × 6 = 9,216. هذا الرقم 9,216 هو ci لطبقة fc6، وبضربه في co = 4,096 نحصل على 37,748,736.
A conv layer reuses one small kernel at every spatial position, so its weight count stays small. A linear layer gives every input-output pair its own weight. That is the structural reason FC dominates the parameter count.
طبقة conv تُعيد استخدام kernel صغير واحد في كل موضع مكاني، فيبقى عدد أوزانها صغيراً. أما الطبقة الخطية فتعطي كل زوج دخل وخرج وزناً خاصاً به. وهذا هو السبب البنيوي لهيمنة FC على عدد الأوزان.
The slide prints conv1 as 24,848. It is 34,848. 96 × 3 × 11 × 11 = 96 × 363 = 34,848. Slide 26 confirms it: conv1 MACs = 105,415,200 = 34,848 × 55 × 55. If an exam question is built from the slide table, compute it yourself and trust the arithmetic. Using 24,848 makes the total 60,944,656, off by exactly 10,000.
الشريحة تكتب conv1 على أنها 24,848، والصحيح 34,848. فـ 96 × 3 × 11 × 11 = 96 × 363 = 34,848. والشريحة 26 تؤكد ذلك: عدد MACs في conv1 هو 105,415,200 = 34,848 × 55 × 55. فإذا بُني سؤال على جدول الشريحة، احسبه بنفسك وثِق بالحساب. واستخدام 24,848 يجعل المجموع 60,944,656، أي أقل بـ 10,000 بالضبط.
Second trap: forgetting the /2 for groups 2. conv2 without it is 614,400, double the right answer.
الفخ الثاني: نسيان القسمة على 2 عند groups 2. فـ conv2 بدونها تساوي 614,400، أي ضعف الجواب الصحيح.
Model size = #parameters × bit width
حجم النموذج = #parameters × bit width
Model size is the storage for the weights (slide 17). If every weight uses the same data type:
حجم النموذج model size هو مساحة التخزين اللازمة للأوزان (الشريحة 17). وإذا كانت كل الأوزان من نفس نوع البيانات:
تقول المعادلة: حجم النموذج يساوي عدد الأوزان مضروباً في عدد البتات لكل وزن.
The answer comes out in bits. Divide by 8 to get bytes. 32 bits is 4 bytes, 16 bits is 2 bytes, 8 bits is 1 byte, 4 bits is half a byte.
الناتج يخرج بالبتات، فاقسم على 8 لتحصل على البايتات. 32 بت تساوي 4 بايت، و16 بت تساوي 2 بايت، و8 بت تساوي بايتاً واحداً، و4 بت نصف بايت.
AlexNet, 61M parameters (60,954,656 exactly) fp32 61M x 4 B = 244 MB (243,818,624 B) fp16 61M x 2 B = 122 MB (121,909,312 B) int8 61M x 1 B = 61 MB ( 60,954,656 B) int4 61M x 0.5 B = 30.5 MB ( 30,477,328 B) MB here is decimal, 10^6 bytes, as on slide 17 (244 x 10^6 bytes). in MiB, 243,818,624 B = 232.5 MiB. going fp32 to int8 shrinks storage exactly 4x.
المثال المحلول: AlexNet فيها 61M وزن. بدقة fp32 يكون الحجم 61M × 4 B = 244 MB، وبدقة fp16 يكون 122 MB، وبدقة int8 يكون 61 MB، وبدقة int4 يكون 30.5 MB. وMB هنا عشري، أي 106 بايت كما في الشريحة 17. والانتقال من fp32 إلى int8 يقلّص التخزين أربع مرات بالضبط.
At int8, megaparameters equal megabytes. 61M params at 8 bits is 61 MB. Every other bit width is a simple multiple of that: ×4 for fp32, ×2 for fp16, ÷2 for int4.
عند int8، عدد ملايين الأوزان يساوي عدد الميغابايتات. فـ 61M وزن بدقة 8 بت تساوي 61 MB. وكل دقة أخرى مضاعف بسيط لذلك: ×4 لـ fp32، و×2 لـ fp16، و÷2 لـ int4.
The factor of 8. 61M × 32 = 1,952M bits, which is 244 MB, not 1,952 MB. If your model size is 8 times too big, you multiplied by the bit width and forgot to convert bits to bytes. Watch the unit the question asks for: Mb (megabits) and MB (megabytes) differ by 8.
معامل الـ 8. فـ 61M × 32 = 1,952M بت، وهذا يساوي 244 MB وليس 1,952 MB. فإذا خرج حجم النموذج أكبر بثماني مرات، فقد ضربت في bit width ونسيت التحويل من البت إلى البايت. وانتبه للوحدة المطلوبة: Mb (ميغابت) وMB (ميغابايت) بينهما معامل 8.
What this tells an embedded engineer
ماذا يعني هذا لمهندس الأنظمة المدمجة
Model size decides whether the weights fit in flash. 61 MB at int8 is far beyond a microcontroller's roughly 1 MB of flash, and the target to attack is obvious from the table: the FC layers. Pruning fc6 or replacing the FC head with global average pooling removes most of the weight storage while touching only a few percent of the compute (the MACs page shows the reverse split).
حجم النموذج يحدد هل تتسع الأوزان في ذاكرة flash. و61 MB بدقة int8 أكبر بكثير من ذاكرة flash في microcontroller التي تكون حوالي 1 MB. والهدف واضح من الجدول: طبقات FC. فعمل pruning لـ fc6، أو استبدال رأس FC بـ global average pooling، يزيل معظم تخزين الأوزان مع أنه لا يلمس إلا نسبة صغيرة من الحساب (صفحة MACs تُظهر التوزيع المعكوس).
Check yourself
اختبر نفسك
A model has 25M parameters stored in fp16. What is its model size?
نموذج فيه 25M وزن مخزّنة بدقة fp16. ما حجم النموذج؟
16 bits = 2 bytes, so 25M × 2 B = 50 MB. Option A is 25M × 16 = 400M bits mislabelled as bytes, the factor-of-8 trap. Option C is fp32 and option D is int8.
16 بت تساوي 2 بايت، إذن 25M × 2 B = 50 MB. والخيار A هو 25M × 16 = 400M بت كُتبت خطأً كبايتات، وهو فخ معامل 8. والخيار C هو fp32، والخيار D هو int8.
How many weights does AlexNet's conv1 (11×11, 96 filters, 3 input channels, bias ignored) have?
كم وزناً في conv1 من AlexNet (11×11، و96 filter، و3 قنوات دخل، مع إهمال bias)؟
96 × 3 × 11 × 11 = 34,848. Option A is the slide's typo. Option B is the number of output activations (96 × 55 × 55), and option D is the MAC count (34,848 × 55 × 55). Parameters never depend on the output size.
96 × 3 × 11 × 11 = 34,848. والخيار A هو الخطأ المطبعي في الشريحة. والخيار B هو عدد activations في الخرج (96 × 55 × 55)، والخيار D هو عدد MACs (34,848 × 55 × 55). وعدد الأوزان لا يعتمد أبداً على حجم الخرج.
AlexNet's conv2 is 5×5, 96 → 256 channels, groups 2. How many parameters?
طبقة conv2 في AlexNet حجمها 5×5، من 96 إلى 256 قناة، مع groups 2. كم عدد الأوزان؟
256 × 96 × 5 × 5 / 2 = 307,200. Each group sees only 48 input channels, so each of the 256 filters is 48 × 5 × 5. Option B forgets the /g. Option C divides twice. Option D is the depthwise formula with co = 256, which does not apply.
256 × 96 × 5 × 5 / 2 = 307,200. فكل مجموعة ترى 48 قناة دخل فقط، فيكون كل filter من الـ 256 بحجم 48 × 5 × 5. والخيار B ينسى القسمة على g، والخيار C يقسم مرتين، والخيار D هو معادلة depthwise مع co = 256، وهي لا تنطبق هنا.
Roughly what share of AlexNet's parameters sits in its three fully connected layers?
تقريباً، ما نسبة أوزان AlexNet الموجودة في طبقاتها الثلاث من نوع fully connected؟
58,621,952 / 60,954,656 = 96.2%. Option C is fc6 alone (61.9%). Option A is FC's share of the MACs, the mirror image of this fact.
58,621,952 / 60,954,656 = 96.2%. والخيار C هو نسبة fc6 وحدها (61.9%). والخيار A هو نسبة FC من MACs، أي الصورة المعكوسة لهذه الحقيقة.
The input to fc6 is the 256 × 6 × 6 output of the last maxpool. Why is fc6 the single largest layer?
دخل fc6 هو خرج آخر maxpool بشكل 256 × 6 × 6. لماذا تكون fc6 أكبر طبقة منفردة؟
Linear parameters are co · ci = 4,096 × 9,216 = 37,748,736. fc7 has the same 4,096 outputs but only 4,096 inputs, so option A is wrong. Option C describes weight sharing in conv, which keeps parameters small. Activations are never counted as parameters.
أوزان الطبقة الخطية هي co · ci = 4,096 × 9,216 = 37,748,736. وfc7 لها نفس المخارج 4,096 لكن مداخلها 4,096 فقط، فالخيار A خاطئ. والخيار C يصف مشاركة الأوزان في conv، وهي التي تُبقي الأوزان قليلة. وactivations لا تُحسب أبداً ضمن الأوزان.