Lecture 05-1 · Quantization Iالمحاضرة 05-1 · التكميم I

FP16, BF16, FP8 and FP4

FP16 وBF16 وFP8 وFP4

Slides 16–20الشرائح 16–20 P1 MUST

Every low-precision float is the same recipe as FP32 with a different split of bits. Exponent width sets the range, fraction width sets the precision, and the bias is always 2e−1 − 1 for an e-bit exponent.

كل عدد عائم منخفض الدقة هو نفس وصفة FP32 مع تقسيم مختلف للبتات. عرض الأس يحدد المدى (range)، وعرض الكسر يحدد الدقة (precision)، والإزاحة دائماً 2^(e−1) − 1 لأس من e بتات.

Exponent width → range, fraction width → precision

عرض الأس ← المدى، وعرض الكسر ← الدقة

Slides 16 and 19 line the formats up. Each extra exponent bit roughly squares the largest value (it doubles the exponent range). Each extra fraction bit halves the gap between neighbouring values. With a fixed bit budget you must trade one for the other.

تصفّ الشريحتان 16 و19 الأشكال جنباً إلى جنب. كل بت إضافي في الأس يقارب تربيع أكبر قيمة (لأنه يضاعف مدى الأس). وكل بت إضافي في الكسر ينصّف الفجوة بين القيم المتجاورة. ومع ميزانية بتات ثابتة يجب أن تبادل أحدهما بالآخر.

FP32 FP16 BF16 FP8 E4M3 FP8 E5M2 FP4 E2M1 823 510 87 43 52 21 bias 127 bias 15 · max 65504 bias 127 · same range as FP32 bias 7 · max 448 · no inf bias 15 · max 57344 · has inf bias 1 · max 6 · no inf, no NaN sign exponent fraction
All six formats at 18 px per bit. BF16 keeps FP32's exponent and cuts the fraction; FP16 cuts both.الصيغ الست كلها بمقياس 18 px لكل بت. BF16 يحتفظ بأس FP32 ويقصّ الكسر؛ وFP16 يقصّ الاثنين.
FormatالصيغةExpالأسFracالكسرTotalالمجموعBiasالإزاحة (Bias)Largest normalأكبر عدد normalNotesملاحظات
FP32 (IEEE)82332127(2 − 2−23) × 2127 ≈ 3.4 × 1038baselineخط الأساس
FP16 (IEEE half)5101615(2 − 2−10) × 215 = 65504more precision, small rangeدقة أكبر، ومدى صغير
BF16 (Google Brain)8716127(2 − 2−7) × 2127 ≈ 3.39 × 1038FP32 range, low precisionمدى FP32، ودقة منخفضة
FP8 E4M3 (Nvidia)4387S.1111.110 = 448no INF; S.1111.111 is NaNلا يوجد INF؛ وS.1111.111 هو NaN
FP8 E5M2 (Nvidia)52815S.11110.11 = 57344INF S.11111.00, NaN S.11111.XX; gradientsINF هو S.11111.00، وNaN هو S.11111.XX؛ للتدرجات (gradients)
INT4sign + 3إشارة + 34noneلا يوجد7 (min −8)uniform step 1خطوة منتظمة مقدارها 1
FP4 E2M1214160, 0.5, 1, 1.5, 2, 3, 4, 6
FP4 E3M03043160, 0.25, 0.5, 1, 2, 4, 8, 16

هذا الجدول هو كل ما تحتاجه من الشرائح 16 إلى 20. الإزاحة تُحسب دائماً بـ 2^(e−1) − 1: e = 8 يعطي 127، وe = 5 يعطي 15، وe = 4 يعطي 7، وe = 3 يعطي 3، وe = 2 يعطي 1. لاحظ أن BF16 له نفس مدى FP32 تقريباً لأن الأس نفسه، بينما أكبر قيمة في FP16 هي 65504 فقط.

FP16 vs BF16: the same 16 bits, opposite trade

FP16 مقابل BF16: البتات الـ 16 نفسها، ومقايضة معاكسة

FP16 (5 exp, 10 frac)

FP16 (5 للأس، 10 للكسر)

10 fraction bits give fine steps (about 3 decimal digits), but the 5-bit exponent caps the range at 65504 and the smallest normal at 2−14. Large activations overflow and small gradients underflow, which is why FP16 training needs loss scaling.

10 بتات كسر تعطي خطوات دقيقة (حوالي 3 أرقام عشرية)، لكن الأس ذو 5 بتات يحدّ المدى عند 65504 وأصغر عدد طبيعي عند 2^−14. التفعيلات الكبيرة تفيض (overflow) والتدرجات الصغيرة تتلاشى (underflow)، ولهذا يحتاج تدريب FP16 إلى loss scaling.

BF16 (8 exp, 7 frac)

BF16 (8 للأس، 7 للكسر)

Keeps FP32's 8-bit exponent and bias 127, so it covers the same range. Only 7 fraction bits remain (about 2 decimal digits). Converting FP32 to BF16 is just dropping the low 16 fraction bits, which makes it a drop-in training format.

يحتفظ بأس FP32 ذي 8 بتات وبإزاحة 127، فيغطي نفس المدى. ويبقى 7 بتات كسر فقط (حوالي رقمين عشريين). التحويل من FP32 إلى BF16 هو مجرد حذف 16 بتاً من أسفل الكسر، ولهذا يصلح بديلاً مباشراً في التدريب.

Worked exampleمثال محلول

Slide 17 decodes an FP16 word; slide 18 encodes 2.5 into BF16. The FP16 encoding of 2.5 is added to show how only the bias and widths change.

الشريحة 17 تفك رموز كلمة FP16، والشريحة 18 ترمّز 2.5 إلى BF16. وأضفنا ترميز 2.5 في FP16 لنبيّن أن الذي يتغير هو الإزاحة والعروض فقط.

Slide 17: FP16  1 | 10001 | 1100000000   (bias 15)
  sign      = 1 → negative
  exponent  = 10001₂ − 15 = 17 − 15 = 2
  fraction  = 0.11₂ = 0.5 + 0.25 = 0.75
  value     = −(1 + 0.75) × 2^2 = −1.75 × 4 = −7.0

Slide 18: 2.5 in BF16   (bias 127)
  2.5 = 1.25 × 2^1
  sign      = 0
  exponent  = 1 + 127 = 128 = 1000 0000₂
  fraction  = 0.25 = 0.01₂ → 0100000   (7 bits)
  word      = 0 | 1000 0000 | 010 0000

Same 2.5 in FP16   (bias 15)
  exponent  = 1 + 15 = 16 = 10000₂
  fraction  → 0100000000   (10 bits)
  word      = 0 | 10000 | 0100000000

FP8: two flavours for two jobs

FP8: نكهتان لمهمتين

Slide 19 shows Nvidia's two FP8 formats. E4M3 (bias 7) keeps 3 fraction bits for precision and is used for weights and activations in the forward pass. It gives up infinity entirely and spends only S.1111.111 on NaN, so exponent field 1111 still holds normal values. Its largest value is S.1111.110 = (1 + 6/8) × 215 − 7 = 1.75 × 256 = 448.

تعرض الشريحة 19 شكلي FP8 من Nvidia. E4M3 (إزاحة 7) يحتفظ بـ 3 بتات كسر للدقة، ويُستخدم للأوزان والتفعيلات في المرور الأمامي (forward pass). يتخلى عن اللانهاية تماماً ولا يصرف على NaN إلا S.1111.111، فيبقى حقل الأس 1111 صالحاً لقيم طبيعية. أكبر قيمة فيه S.1111.110 = (1 + 6/8) × 2^(15 − 7) = 1.75 × 256 = 448.

E5M2 (bias 15) keeps IEEE-style specials: S.11111.00 is ±INF and S.11111.XX (XX ≠ 00) is NaN. Its largest normal is S.11110.11 = 1.75 × 230 − 15 = 1.75 × 32768 = 57344. The extra exponent bit buys the wide range that gradients in the backward pass need.

E5M2 (إزاحة 15) يحتفظ بالقيم الخاصة على طريقة IEEE: S.11111.00 هو ±INF وS.11111.XX (حيث XX ≠ 00) هو NaN. أكبر قيمة طبيعية فيه S.11110.11 = 1.75 × 2^(30 − 15) = 1.75 × 32768 = 57344. بت الأس الإضافي يشتري المدى الواسع الذي تحتاجه التدرجات (gradients) في المرور الخلفي (backward pass).

4-bit: INT4 vs FP4

4-bit: INT4 مقابل FP4

Slide 20 compares 16 patterns spent four ways. INT4 gives −8 to 7 with uniform step 1. FP4 E1M2 (bias 0) gives 0 to 3.5 in steps of 0.5, still uniform. FP4 E2M1 (bias 1) gives 0, 0.5, 1, 1.5, 2, 3, 4, 6: dense near zero, sparse further out. FP4 E3M0 (bias 3) has no fraction at all, so every value is a power of two: 0, 0.25, 0.5, 1, 2, 4, 8, 16. FP4 formats have no inf and no NaN.

تقارن الشريحة 20 بين 16 نمطاً مصروفة بأربع طرق. INT4 يعطي من −8 إلى 7 بخطوة منتظمة 1. FP4 E1M2 (إزاحة 0) يعطي من 0 إلى 3.5 بخطوات 0.5، وما زال منتظماً. FP4 E2M1 (إزاحة 1) يعطي 0, 0.5, 1, 1.5, 2, 3, 4, 6: كثيف قرب الصفر ومتباعد بعيداً عنه. FP4 E3M0 (إزاحة 3) بلا كسر إطلاقاً، فكل قيمة قوة للعدد 2: 0, 0.25, 0.5, 1, 2, 4, 8, 16. أشكال FP4 ليس فيها inf ولا NaN.

INT4 FP4 E2M1 FP4 E3M0 0124 6816 0 ... 7, uniform 0, 0.5, 1, 1.5, 2, 3, 4, 6 0, 0.25, 0.5, 1, 2, 4, 8, 16
Positive halves only (each format mirrors to negatives). More exponent bits: wider range, coarser steps far from zero.النصف الموجب فقط (كل صيغة تنعكس إلى السالب). بتات أس أكثر تعني مدى أوسع وخطوات أخشن بعيداً عن الصفر.
Worked exampleمثال محلول

Build the FP4 E2M1 value list from the formula (bias = 22−1 − 1 = 1).

ابنِ قائمة قيم FP4 E2M1 من الصيغة (الإزاحة 2^(2−1) − 1 = 1).

E = 00 (subnormal): M × 0.5 × 2^(1−1)     → 0, 0.5
E = 01            : (1 + M·0.5) × 2^(1−1) → 1, 1.5
E = 10            : (1 + M·0.5) × 2^(2−1) → 2, 3
E = 11            : (1 + M·0.5) × 2^(3−1) → 4, 6
max = 0 11 1 = (1 + 0.5) × 2^2 = 6

The slide prints "(1+0.5)×23−1=1" for 0111; the arithmetic is 1.5 × 4 = 6, matching the value list on the same slide.

تطبع الشريحة "(1+0.5)×2^(3−1)=1" للنمط 0111؛ والحساب الصحيح 1.5 × 4 = 6، وهو ما يطابق قائمة القيم على نفس الشريحة.

Memory trickحيلة للحفظ

"E for Extent, M for Mesh". Exponent bits set how far the values extend; mantissa bits set how fine the mesh is. E4M3 = finer mesh (forward), E5M2 = farther extent (gradients).

"E للامتداد، M للشبكة". بتات الأس تحدد إلى أي مدى تمتد القيم، وبتات الـ mantissa تحدد مدى نعومة الشبكة. E4M3 شبكة أنعم (المرور الأمامي)، وE5M2 امتداد أبعد (التدرجات).

Exam trapفخ في الاختبار

Use the right bias. FP16 is 15, BF16 is 127, E4M3 is 7, E5M2 is 15, E2M1 is 1. Decoding slide 17's word with bias 127 gives nonsense; always compute 2e−1 − 1 from the exponent width.

استخدم الإزاحة الصحيحة. FP16 إزاحته 15، وBF16 إزاحته 127، وE4M3 إزاحته 7، وE5M2 إزاحته 15، وE2M1 إزاحته 1. فك رموز كلمة الشريحة 17 بإزاحة 127 يعطي نتيجة بلا معنى؛ احسب دائماً 2^(e−1) − 1 من عرض الأس.

BF16 is not "more precise FP16". It has fewer fraction bits (7 vs 10). What it gains is range: the same 8-bit exponent as FP32.

BF16 ليس "FP16 أدق". لديه بتات كسر أقل (7 مقابل 10). ما يكسبه هو المدى: نفس الأس ذي 8 بتات الموجود في FP32.

E4M3's max is 448, not 240. Because E4M3 has no infinity, exponent field 1111 still carries normal numbers; only mantissa 111 is taken by NaN, so the top value is 1.75 × 28. And E5M2, not E4M3, is the gradient format.

أكبر قيمة في E4M3 هي 448 وليست 240. لأن E4M3 بلا لانهاية، يبقى حقل الأس 1111 حاملاً لأعداد طبيعية، ولا يأخذ NaN إلا الـ mantissa 111، فتكون القيمة العليا 1.75 × 2^8. وE5M2 وليس E4M3 هو شكل التدرجات.

Check yourself

اختبر نفسك

What is the FP16 word 0 | 10010 | 1010000000 in decimal?

ما القيمة العشرية لكلمة FP16 التالية: 0 | 10010 | 1010000000؟

B. Exponent 100102 = 18, minus bias 15 gives 3. Fraction 0.1012 = 0.625. Value = 1.625 × 8 = 13. A uses exponent 2. C forgets the hidden 1 (0.625 × 8). D uses the FP32 bias 127.

الجواب B. الأس 100102 = 18، وبطرح الإزاحة 15 يعطي 3. الكسر 0.1012 = 0.625. القيمة = 1.625 × 8 = 13. والخيار A يستخدم الأس 2. وC ينسى الـ 1 المخفي (0.625 × 8). وD يستخدم إزاحة FP32 وهي 127.

Which 16-bit format covers the same dynamic range as FP32?

أي شكل ذي 16 بتاً يغطي نفس المدى الديناميكي لـ FP32؟

C. Range comes from exponent width, and BF16 keeps FP32's 8 exponent bits. FP16 has 5 exponent bits, so its max is 65504. Fraction bits (A) give precision, not range. INT16 tops out at 32767.

الجواب C. المدى يأتي من عرض الأس، وBF16 يحتفظ ببتات الأس الثماني في FP32. أما FP16 فله 5 بتات أس، لذلك أقصاه 65504. وبتات الكسر (A) تعطي الدقة لا المدى. وINT16 أقصاه 32767.

What is the largest normal value of FP8 E4M3, and why?

ما أكبر قيمة طبيعية في FP8 E4M3، ولماذا؟

A. (1 + 6/8) × 215 − 7 = 1.75 × 256 = 448. B uses the NaN pattern. C would be true only if E4M3 reserved exponent 1111 for INF and NaN as IEEE does, which it does not. D is the E5M2 maximum.

الجواب A. (1 + 6/8) × 215 − 7 = 1.75 × 256 = 448. والخيار B يستخدم نمط NaN. وC يصح فقط لو حجز E4M3 الأس 1111 لـ INF وNaN كما يفعل IEEE، وهو لا يفعل. وD هو أقصى قيمة في E5M2.

Slide 19 says one FP8 format is used "for gradient in the backward". Which one, and why?

تقول الشريحة 19 إن أحد شكلي FP8 يُستخدم "for gradient in the backward". أيهما، ولماذا؟

D. Gradients span many orders of magnitude, so range matters more than precision, and the extra exponent bit raises the max from 448 to 57344. C is wrong: E5M2 has fewer fraction bits (2). A and B describe E4M3, the forward-pass format.

الجواب D. التدرجات تمتد عبر رتب عشرية كثيرة، فالمدى أهم من الدقة، وبت الأس الإضافي يرفع الحد الأقصى من 448 إلى 57344. والخيار C خاطئ: E5M2 فيه بتات كسر أقل (2). وA وB يصفان E4M3، وهو صيغة forward pass.

Which list gives the non-negative values of FP4 E2M1?

أي قائمة تعطي القيم غير السالبة لـ FP4 E2M1؟

B. With bias 1 the exponents give scales 20 (subnormal and E = 1), 21 and 22, each with mantissa 1.0 or 1.5 (or 0 and 0.5 when subnormal). A is INT4. C is E1M2. D is E3M0.

الجواب B. مع إزاحة 1 تعطي الأسس المقاييس 20 (للحالة subnormal وE = 1) و21 و22، ولكل منها mantissa تساوي 1.0 أو 1.5 (أو 0 و0.5 في حالة subnormal). والخيار A هو INT4. وC هو E1M2. وD هو E3M0.