Lecture 03 · NN Efficiency Metrics

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

Latency vs throughput

Latency مقابل throughput

Slides 1–7 الشرائح 1–7 P1 MUST

Latency is how long one item waits. Throughput is how many items finish per second. They are different questions, and batching trades one for the other.

Latency هو كم ينتظر عنصر واحد. وthroughput هو كم عنصراً ينتهي في الثانية. هما سؤالان مختلفان، وbatching يبادل أحدهما بالآخر.

The map: three goals, three costs, two resources

الخريطة: ثلاثة أهداف، وثلاث تكاليف، وموردان

Slide 4 is the map for the whole lecture. We want models that are smaller, faster and greener. Each goal is measured by a cost: smaller means less storage, faster means lower latency, greener means less energy. In the middle of the triangle sit the two resources that drive all three costs: computation and memory.

الشريحة 4 هي خريطة المحاضرة كلها. نريد نماذج أصغر (smaller) وأسرع (faster) وأكثر خضرة (greener). كل هدف يقاس بتكلفة: الأصغر يعني storage أقل، والأسرع يعني latency أقل، والأخضر يعني energy أقل. وفي وسط المثلث المورِدان اللذان يحرّكان التكاليف الثلاث: computation وmemory.

That split gives the two families of metrics in the slide's red box. Memory-related: #parameters, model size, total/peak #activations. Computation-related: MAC, FLOP and FLOPS, OP and OPS. Latency and throughput (slide 2's outline ends with them) are the measured outcomes those metrics try to predict.

هذا التقسيم يعطي عائلتي المقاييس في المربع الأحمر في الشريحة. المقاييس المرتبطة بالذاكرة (memory-related): #parameters وmodel size وtotal/peak #activations. والمرتبطة بالحساب (computation-related): MAC وFLOP وFLOPS وOP وOPS. أما latency وthroughput فهما النتائج المقاسة التي تحاول هذه المقاييس التنبؤ بها.

GoalالهدفCostالتكلفةDriven byيحرّكهاMetrics that predict itالمقاييس التي تتنبأ بها
SmallerأصغرStorageالتخزين (storage)Memoryالذاكرة#parameters, model size#parameters، وحجم النموذج (model size)
FasterأسرعLatencyLatencyComputation and memoryالحساب والذاكرةMACs, OPs, model size, peak #activationsMACs، وOPs، وحجم النموذج، وذروة عدد التنشيطات (peak #activations)
Greenerأكثر خضرةEnergyالطاقة (energy)Computation and memoryالحساب والذاكرةMACs, OPs, and above all bytes movedMACs، وOPs، وقبل كل شيء عدد البايتات المنقولة

يربط هذا الجدول كل هدف بتكلفته وبالمورد الذي يحرّكها وبالمقاييس التي تتنبأ بها. لاحظ أن storage يعتمد على الذاكرة وحدها، بينما latency وenergy يعتمدان على الحساب والذاكرة معاً، ولهذا لا يكفي عدّ العمليات لتقدير السرعة أو الطاقة.

Latency: the delay for one task

Latency: زمن التأخير لمهمة واحدة

Slide 5 defines latency as the time to complete one specific task, measured in seconds or milliseconds. Its example is semantic segmentation on a city street: SegFormer takes 638 ms at 82.4 mIoU, EfficientViT takes 46 ms (45.9 ms in the paper) at 82.7 mIoU. That is 638 / 45.9 ≈ 13.9x faster with slightly better accuracy.

تعرّف الشريحة 5 الـ latency بأنه الزمن اللازم لإنجاز مهمة واحدة محددة، ويقاس بالثواني أو الميلي ثانية. المثال هو semantic segmentation لشارع: SegFormer يستغرق 638 ms بدقة 82.4 mIoU، وEfficientViT يستغرق 46 ms (45.9 ms في الورقة) بدقة 82.7 mIoU. أي أسرع بحوالي 638 / 45.9 ≈ 13.9x مع دقة أفضل قليلاً.

The footnote matters as much as the numbers: measured on NVIDIA Jetson AGX Orin with TensorRT, fp16, batch size 1. Change any of those four and the milliseconds change. A latency number is only comparable to another latency number measured under the same hardware, runtime, precision and batch size.

الحاشية مهمة بقدر الأرقام: measured on NVIDIA Jetson AGX Orin with TensorRT, fp16, batch size 1. غيّر أياً من هذه الأربعة وتتغير الميلي ثواني. رقم latency لا يقارَن إلا برقم آخر قيس على نفس العتاد (hardware) ونفس runtime ونفس الدقة العددية (precision) ونفس batch size.

Memory trickحيلة للحفظ

"H R P B": Hardware, Runtime, Precision, Batch. Four conditions travel with every latency number.

"H R P B": العتاد، runtime، الدقة، batch. أربعة شروط ترافق كل رقم latency.

On slide 5 they are Orin, TensorRT, fp16, 1. If an exam asks "what is missing to compare these two latencies?", the answer is whichever of the four is not stated.

في الشريحة 5 هي: Orin، TensorRT، fp16، 1. إذا سألك الامتحان "ما الذي ينقص لمقارنة هذين الرقمين؟" فالجواب هو أي واحد من الأربعة لم يُذكر.

Throughput: the rate at which data is processed

Throughput: معدل معالجة البيانات

Slide 6 defines throughput as the rate of processing: items completed per unit time (images/s, videos/s, tokens/s). Its example: one system processes 6.1 videos/s, another 77.4 videos/s. Throughput says nothing about how long any single video waited. It only counts completions per second.

تعرّف الشريحة 6 الـ throughput بأنه معدل المعالجة: عدد العناصر المنجزة في وحدة الزمن (images/s، videos/s، tokens/s). المثال: نظام يعالج 6.1 videos/s وآخر 77.4 videos/s. الـ throughput لا يخبرك كم انتظر أي فيديو بمفرده، بل يعدّ الإنجازات في الثانية فقط.

Latency

Latency

Unit: seconds per item. Lower is better.

الوحدة: ثانية لكل عنصر. الأقل أفضل.

Question it answers: how long from input arriving to output ready, for this item?

السؤال الذي يجيب عنه: كم من لحظة وصول المُدخل إلى جاهزية المُخرج، لـهذا العنصر؟

Matters for: real-time and interactive systems (a car, a camera, a voice assistant).

مهم في: الأنظمة اللحظية والتفاعلية (سيارة، كاميرا، مساعد صوتي).

Throughput

Throughput

Unit: items per second. Higher is better.

الوحدة: عنصر في الثانية. الأعلى أفضل.

Question it answers: how much work does the system finish per second, in total?

السؤال الذي يجيب عنه: كم من العمل ينجز النظام في الثانية إجمالاً؟

Matters for: offline and server workloads (labelling a dataset, serving many users).

مهم في: الأحمال غير اللحظية وأحمال الخوادم (وسم dataset، خدمة مستخدمين كثيرين).

DESIGN 1 · ONE AT A TIME DESIGN 2 · FOUR IN PARALLEL 0 50 100 ms img 1 · 50 ms img 2 · 50 ms 2 images done in 100 ms latency = 50 ms throughput = 1 / 0.05 = 20 img/s 0 100 ms img 1 · 100 ms img 2 · 100 ms img 3 · 100 ms img 4 · 100 ms 4 images done in 100 ms latency = 100 ms · throughput = 4 / 0.1 = 40 img/s THE PUNCHLINE OF SLIDE 7 Design 2 has 2x the throughput AND 2x the latency. Design 1 has the lower latency but the lower throughput. Neither metric implies the other.
Slide 7's two designs. Throughput counts completions per second across all lanes. Latency is the wait of one image inside its own lane.
التصميمان في الشريحة 7. الـ throughput يعدّ الإنجازات في الثانية عبر كل المسارات. أما الـ latency فهو زمن انتظار صورة واحدة داخل مسارها.

Slide 7: does one imply the other?

الشريحة 7: هل يستلزم أحدهما الآخر؟

Slide 7 asks two questions and answers both with the same pair of designs:

تطرح الشريحة 7 سؤالين وتجيب عنهما بنفس التصميمين:

  1. Does higher throughput mean lower latency? No. Design 2 has the higher throughput (40 vs 20 img/s) but the higher latency (100 vs 50 ms). It wins on throughput by processing four images at once, while each individual image takes longer.

    هل throughput الأعلى يعني latency أقل؟ لا. التصميم 2 لديه throughput أعلى (40 مقابل 20 img/s) لكن latency أعلى (100 مقابل 50 ms). يفوز بالـ throughput لأنه يعالج أربع صور معاً، بينما تستغرق كل صورة وحدها وقتاً أطول.

  2. Does lower latency mean higher throughput? No. Design 1 has the lower latency (50 ms) but only half the throughput. It finishes each image quickly but handles only one at a time.

    هل latency الأقل يعني throughput أعلى؟ لا. التصميم 1 لديه latency أقل (50 ms) لكن نصف الـ throughput فقط. ينهي كل صورة بسرعة، لكنه يعالج صورة واحدة في كل مرة.

  3. When are they tied? Only when exactly one item is in flight at a time (batch size 1, no parallelism, no pipelining). Then throughput = 1 / latency. Design 1 is that case: 1 / 0.05 s = 20 img/s.

    متى يرتبطان؟ فقط عندما يكون هناك عنصر واحد قيد المعالجة في كل لحظة (batch size 1، بلا توازي ولا pipelining). عندها throughput = 1 / latency. التصميم 1 هو هذه الحالة: 1 / 0.05 s = 20 img/s.

throughput = items in flightlatency    (steady state, each batch finishes before the next starts)

Batching is why GPUs are throughput machines: a bigger batch fills more of the hardware so total images per second rises, but every image in the batch waits for the whole batch to finish, so latency rises too. Embedded and real-time systems usually run batch 1 because they care about one frame's deadline.

الـ batching هو سبب كون GPUs آلات throughput: batch أكبر يملأ جزءاً أكبر من العتاد فيرتفع عدد الصور في الثانية إجمالاً، لكن كل صورة في الـ batch تنتظر انتهاء الـ batch كله، فيرتفع الـ latency أيضاً. الأنظمة المدمجة واللحظية تعمل غالباً بـ batch 1 لأن ما يهمها هو موعد إطار واحد.

Worked exampleمثال محلول

Recompute slide 7, then extend it to a batched server.

أعد حساب الشريحة 7، ثم وسّعها إلى خادم يستخدم batching.

Design 1: one image at a time, 50 ms each
  latency    = 50 ms
  throughput = 1 image / 0.050 s = 20 image/s

Design 2: four images in parallel, 100 ms each
  latency    = 100 ms        (each image still waits its full 100 ms)
  throughput = 4 images / 0.100 s = 40 image/s

Server: batch 1 takes 2 ms, batch 32 takes 8 ms
  batch 1 :  latency 2 ms,  throughput 1 / 0.002  =  500 image/s
  batch 32:  latency 8 ms,  throughput 32 / 0.008 = 4000 image/s
  batching: latency 4x worse, throughput 8x better

And slide 5's pair at batch 1: SegFormer 1 / 0.638 s ≈ 1.6 image/s, EfficientViT 1 / 0.0459 s ≈ 21.8 image/s. At batch 1 the throughput ratio equals the latency ratio, 13.9x.

وثنائي الشريحة 5 عند batch 1: SegFormer يعطي 1 / 0.638 s ≈ 1.6 image/s، وEfficientViT يعطي 1 / 0.0459 s ≈ 21.8 image/s. عند batch 1 تكون نسبة الـ throughput مساوية لنسبة الـ latency، أي 13.9x.

Exam trapفخ امتحاني

Never compute latency as 1 / throughput unless batch is 1 with no parallelism. For Design 2, 1 / 40 = 25 ms is wrong: no image ever finishes in 25 ms. The 25 ms is the average gap between completions, not the wait of any image.

لا تحسب الـ latency على أنه 1 / throughput إلا إذا كان batch يساوي 1 بلا توازي. في التصميم 2، الناتج 1 / 40 = 25 ms خطأ: لا توجد صورة تنتهي في 25 ms. الرقم 25 ms هو متوسط الفاصل بين الإنجازات، وليس زمن انتظار أي صورة.

"Faster" is ambiguous. If a question says one model is faster, ask: lower latency or higher throughput? Slide 7 exists to show these can point in opposite directions.

كلمة "أسرع" غامضة. إذا قال السؤال إن نموذجاً أسرع، فاسأل: latency أقل أم throughput أعلى؟ الشريحة 7 موجودة لتبيّن أنهما قد يشيران إلى اتجاهين متعاكسين.

Check yourself

اختبر نفسك

A system processes 8 images in parallel, and each image takes 200 ms. What are its latency and throughput?

نظام يعالج 8 صور بالتوازي، وكل صورة تستغرق 200 ms. ما قيمتا latency وthroughput؟

C. Each image waits its full 200 ms, so latency is 200 ms. Eight images complete every 0.2 s, so throughput is 8 / 0.2 = 40 image/s. A divides latency by the parallelism, which is the 1 / throughput trap. B forgets the parallelism. D treats the eight lanes as if they ran one after another.

C. كل صورة تنتظر 200 ms كاملة، لذا فالـ latency يساوي 200 ms. وتكتمل ثماني صور كل 0.2 s، لذا فالـ throughput يساوي 8 / 0.2 = 40 image/s. الخيار A يقسم الـ latency على درجة التوازي، وهذا هو فخ 1 / throughput. والخيار B ينسى التوازي. والخيار D يعامل المسارات الثمانية كأنها تعمل واحداً تلو الآخر.

Slide 7's Design 2 has higher throughput than Design 1. What does that tell you about its latency?

التصميم 2 في الشريحة 7 لديه throughput أعلى من التصميم 1. ماذا يخبرك ذلك عن latency الخاص به؟

A. Higher throughput comes from processing four images at once, while each image takes 100 ms instead of 50. B applies throughput = 1 / latency, which only holds at batch 1 with no parallelism. C and D have no basis in the slide.

A. الـ throughput الأعلى يأتي من معالجة أربع صور في وقت واحد، بينما تستغرق كل صورة 100 ms بدلاً من 50. الخيار B يطبّق throughput = 1 / latency، وهي لا تصح إلا عند batch 1 بلا توازٍ. والخياران C وD لا أساس لهما في الشريحة.

Slide 5 reports SegFormer 638 ms and EfficientViT 46 ms. Which information is essential before comparing these to a latency from another paper?

تذكر الشريحة 5 أن SegFormer يستغرق 638 ms وEfficientViT يستغرق 46 ms. ما المعلومة الضرورية قبل مقارنتهما برقم latency من ورقة أخرى؟

D. Latency depends on the device, the inference engine, the numeric precision and the batch size, and the slide's footnote states all four. Accuracy (A) is a separate axis. Training details (B, C) do not affect inference time.

D. الـ latency يعتمد على الجهاز، ومحرك الاستدلال (inference engine)، والدقة العددية، وbatch size، وحاشية الشريحة تذكر الأربعة كلها. والدقة (A) محور منفصل. وتفاصيل التدريب (B وC) لا تؤثر في زمن الاستدلال.

A server moves from batch 1 (2 ms) to batch 32 (8 ms). What happens?

خادم ينتقل من batch 1 (2 ms) إلى batch 32 (8 ms). ماذا يحدث؟

B. Every image now waits for the 8 ms batch instead of 2 ms, so latency is 4x worse. Throughput goes from 1 / 0.002 = 500 to 32 / 0.008 = 4000 image/s, 8x better. This is exactly the batching trade-off: throughput up, latency up. D forgets that 32 images finish per batch.

B. كل صورة تنتظر الآن الـ batch البالغ 8 ms بدلاً من 2 ms، لذا يسوء الـ latency بمقدار 4x. ويرتفع الـ throughput من 1 / 0.002 = 500 إلى 32 / 0.008 = 4000 image/s، أي أفضل بـ 8x. هذه بالضبط مقايضة الـ batching: يرتفع الـ throughput ويرتفع الـ latency معه. والخيار D ينسى أن 32 صورة تنتهي في كل batch.