COE 592Lecture 5.1المحاضرة 5.1Part 03الجزء 03
FP16, BF16, FP8 and FP4: trading range for precisionFP16 وBF16 وFP8 وFP4: مقايضة المدى بالدقة
Exponent width sets range and fraction width sets precision. Compares FP32, FP16 and BF16, works the two conversion examples, then FP8 (E4M3 and E5M2) and the four-bit formats INT4, E1M2, E2M1 and E3M0.عرض الأس يحدد المدى وعرض الكسر يحدد الدقة. يقارن FP32 وFP16 وBF16، ويحل مثالي التحويل، ثم FP8 (E4M3 وE5M2) والصيغ ذات الأربع بتات INT4 وE1M2 وE2M1 وE3M0.
- Conceptsالمفاهيم
- 4
- Slidesالشرائح
- 16-20
- Readingمدة القراءة
- 24 min24 دقيقة
Why this part mattersلماذا يهمّ هذا الجزء
Part 2 took FP32 apart. This part shrinks it. Every format here keeps the same recipe of sign, biased exponent and fraction, and only changes how many bits each field gets. Once you see that the exponent field buys range and the fraction field buys precision, every format from 32 bits down to 4 becomes a single design decision you can reason about.
This matters well beyond the exam. Mixed precision training on GPUs and TPUs runs in FP16 or BF16, the newest accelerators train and serve large models in FP8, and FP4 is now a real deployment format for weights. For the midterm, expect three kinds of questions: decode a bit pattern, encode a decimal, and justify which format suits which tensor. All three are worked below.
فكّك الجزء الثاني صيغة FP32، وهذا الجزء يصغّرها. كل صيغة هنا تحتفظ بالوصفة نفسها: بت إشارة، وأس مُزاح (biased exponent)، وكسر (fraction)، ولا يتغير سوى عدد البتات المخصص لكل حقل. حين ترى أن حقل الأس يشتري المدى (range) وأن حقل الكسر يشتري الدقة (precision)، تصبح كل صيغة من 32 بتًا نزولًا إلى 4 بتات قرار تصميم واحدًا تستطيع التفكير فيه.
أهمية هذا تتجاوز الامتحان بكثير. التدريب بالدقة المختلطة (mixed precision training) على وحداتGPU وTPU يعمل بصيغة FP16 أو BF16، وأحدث المسرّعات تدرّب النماذج الكبيرة وتخدمها بصيغة FP8، وأصبحت FP4 صيغة نشر حقيقية للأوزان. في امتحان منتصف الفصل توقّع ثلاثة أنواع من الأسئلة: فك ترميز نمط بتات، وترميز رقم عشري، وتبرير أي صيغة تناسب أي موتّر (tensor). الأنواع الثلاثة محلولة بالتفصيل أدناه.
By the end you canفي نهاية هذا الجزء ستستطيع
- State the rule: exponent width sets range, fraction width sets precision, and use it to rank any float format.تذكر القاعدة: عرض الأس (exponent) يحدد المدى (range)، وعرض الكسر (fraction) يحدد الدقة (precision)، وتستخدمها لترتيب أي صيغة فاصلة عائمة.
- Recall the sign, exponent and fraction widths and the bias of FP32, FP16, BF16, FP8 E4M3 and FP8 E5M2.تتذكر عرض حقول الإشارة والأس والكسر وقيمة الإزاحة (bias) في FP32 وFP16 وBF16 وFP8 E4M3 وFP8 E5M2.
- Decode an FP16 bit pattern to decimal and encode a decimal into BF16 by hand.تفك ترميز نمط بتات FP16 إلى رقم عشري، وترمّز رقمًا عشريًا في BF16 يدويًا.
- Compute the largest normal, smallest normal and smallest subnormal of a format from its widths.تحسب أكبر قيمة عادية (normal) وأصغر قيمة عادية وأصغر قيمة دون عادية (subnormal) لأي صيغة انطلاقًا من عرض حقولها.
- Explain why BF16 is preferred for training, and why E4M3 serves the forward pass and E5M2 the gradients.تشرح لماذا تُفضَّل BF16 في التدريب، ولماذا تخدم E4M3 المرور الأمامي (forward pass) وتخدم E5M2 التدرجات (gradients).
- List the eight positive values of INT4, E1M2, E2M1 and E3M0, and spot the slide's E2M1 mistake.تسرد القيم الموجبة الثماني في INT4 وE1M2 وE2M1 وE3M0، وتكتشف خطأ الشريحة في E2M1.
Try storing 100,000 and 1000.5 in two different 16-bit formats. FP16 cannot hold 100,000 at all: its largest finite value is 65504, so the number overflows to infinity. BF16 stores it easily, rounding it to 99,840. Now flip the test. FP16 stores 1000.5 exactly (its steps near 1000 are 0.5 wide), while BF16 rounds it to 1000 because its steps near 1000 are a full 4 wide. Same 16 bits, opposite strengths.
جرّب تخزين 100,000 و1000.5 في صيغتين مختلفتين بطول 16 بتًا. لا تستطيعFP16 أن تحمل 100,000 أصلًا: أكبر قيمة منتهية فيها هي 65504، فيتجاوز الرقم المدى (overflow) ويصبح ما لا نهاية. أما BF16 فتخزنه بسهولة وتقرّبه إلى 99,840. الآن اعكس الاختبار. تخزن FP16 القيمة 1000.5 بدقة تامة (خطواتها قرب 1000 عرضها 0.5)، بينما تقرّبها BF16 إلى 1000 لأن خطواتها قرب 1000 عرضها 4 كاملة. البتات نفسها، 16 بتًا، ونقاط القوة متعاكسة.
The rule on the slideالقاعدة في الشريحة
Every Floating-point number number is stored the way Part 2 showed for FP32: a sign bit, an exponent field with a bias, and a fraction. A normal value is
كل رقم floating-point يُخزَّن بالطريقة التي عرضها الجزء الثاني لصيغة FP32: بت إشارة، وحقل أس مع إزاحة (bias)، وكسر. والقيمة العادية (normal) هي
The exponent picks which power of two the number lives near, so more exponent bits reach more powers of two: that is range. The fraction picks a position between two neighbouring powers of two, so more fraction bits slice that interval finer: that is precision. This is the slide headline, Range vs precision: exponent width gives range, fraction width gives precision.
يختار الأس قوة العدد 2 التي يقع الرقم قربها، لذلك تصل بتات الأس الإضافية إلى قوى أكثر للعدد 2: وهذا هو المدى (range). ويختار الكسر موضعًا بين قوتين متجاورتين للعدد 2، لذلك تقسّم بتات الكسر الإضافية هذه الفترة إلى شرائح أدق: وهذه هي الدقة (precision). هذا هو عنوان الشريحة، range vs precision: عرض الأس يعطي المدى، وعرض الكسر يعطي الدقة.
FP16 and BF16 spend the same 16 bits differentlyصيغتا FP16 وBF16 تنفقان البتات الـ16 نفسها بطريقتين مختلفتين
FP16, the IEEE 754 half precision format, keeps 5 exponent bits and 10 fraction bits with bias 15. BF16 (Brain Float), designed at Google for TPUs, keeps all 8 of FP32’s exponent bits with bias 127 and only 7 fraction bits. The Exponent bias follows the same formula everywhere, 2^(e-1) - 1, so you never need to memorize it: e = 5 gives 15, e = 8 gives 127.
صيغة FP16، وهي صيغة نصف الدقة (half precision) في IEEE 754، تحتفظ بـ5 بتات للأس و10 بتات للكسر مع إزاحة 15. أما BF16، التي صممتها Google لوحدات TPU، فتحتفظ ببتات الأس الثماني كلها من FP32 مع إزاحة 127، و7 بتات فقط للكسر. وتتبع exponent bias الصيغة نفسها في كل مكان، 2^(e-1) - 1، فلا تحتاج إلى حفظها: e = 5 تعطي 15، وe = 8 تعطي 127.
| FP32FP32 | FP16FP16 | BF16BF16 | |
|---|---|---|---|
| Sign / exponent / fractionالإشارة / الأس / الكسر | 1 / 8 / 231 / 8 / 23 | 1 / 5 / 101 / 5 / 10 | 1 / 8 / 71 / 8 / 7 |
| Biasالإزاحة (bias) | 127127 | 1515 | 127127 |
| Largest normalأكبر قيمة عادية | ≈ 3.40 × 10^38≈ 3.40 × 10^38 | 6550465504 | ≈ 3.39 × 10^38≈ 3.39 × 10^38 |
| Smallest normalأصغر قيمة عادية | 2^-126 ≈ 1.18 × 10^-382^-126 ≈ 1.18 × 10^-38 | 2^-14 ≈ 6.10 × 10^-52^-14 ≈ 6.10 × 10^-5 | 2^-126 ≈ 1.18 × 10^-382^-126 ≈ 1.18 × 10^-38 |
| Smallest subnormalأصغر قيمة دون عادية | 2^-1492^-149 | 2^-24 ≈ 5.96 × 10^-82^-24 ≈ 5.96 × 10^-8 | 2^-1332^-133 |
| Gap after 1.0 (2^-fraction bits)الفجوة بعد 1.0 (2^-عدد بتات الكسر) | 2^-23 ≈ 1.2 × 10^-72^-23 ≈ 1.2 × 10^-7 | 2^-10 ≈ 0.000982^-10 ≈ 0.00098 | 2^-7 ≈ 0.00782^-7 ≈ 0.0078 |
| Gap near 1000الفجوة قرب 1000 | ≈ 0.00006≈ 0.00006 | 0.50.5 | 44 |
The FP16 limits come straight from the widths. The largest exponent field that is not reserved is 11110 = 30, so the top power is 2^(30 - 15) = 2^15, and a full fraction of ten ones gives (2 - 2^-10) × 2^15 = 65504. The smallest normal uses exponent field 1: 2^(1 - 15) = 2^-14. The smallest Subnormal number puts a single 1 in the last fraction bit: 2^-10 × 2^-14 = 2^-24.
حدود FP16 تأتي مباشرة من عرض الحقول. أكبر قيمة لحقل الأس غير محجوزة هي 11110 = 30، فتكون أعلى قوة 2^(30 - 15) = 2^15، وكسر كامل من عشرة آحاد يعطي (2 - 2^-10) × 2^15 = 65504. أصغر قيمة عادية تستخدم حقل أس يساوي 1: 2^(1 - 15) = 2^-14. وأصغر subnormal number تضع 1 واحدًا في آخر بت من الكسر: 2^-10 × 2^-14 = 2^-24.
Worked exampleمثال محلول
FP16 range from the bit widthsمدى FP16 انطلاقًا من عرض البتات
Largest normalأكبر قيمة عادية
Exponent field 11110 (all ones is reserved for INF and NaN), fraction all ones: (1 + 1023/1024) × 2^15 = 65504.حقل الأس 11110 (الآحاد كلها محجوزة لـINF وNaN)، والكسر كله آحاد: (1 + 1023/1024) × 2^15 = 65504.Smallest normalأصغر قيمة عادية
Exponent field 00001, fraction zero: 1.0 × 2^(1 - 15) = 2^-14 ≈ 6.10 × 10^-5.حقل الأس 00001، والكسر صفر: 1.0 × 2^(1 - 15) = 2^-14 ≈ 6.10 × 10^-5.Smallest subnormalأصغر قيمة دون عادية
Exponent field 00000, fraction 0000000001: 2^-10 × 2^(1 - 15) = 2^-24 ≈ 5.96 × 10^-8.حقل الأس 00000، والكسر 0000000001: 2^-10 × 2^(1 - 15) = 2^-24 ≈ 5.96 × 10^-8.Resultالنتيجة
FP16 spans roughly 6 × 10^-8 to 6.5 × 10^4. FP32 and BF16 span roughly 10^-38 to 3.4 × 10^38 for normals.تمتد FP16 تقريبًا من 6 × 10^-8 إلى 6.5 × 10^4. أما FP32 وBF16 فتمتدان تقريبًا من 10^-38 إلى 3.4 × 10^38 للقيم العادية.
Why BF16 won for trainingلماذا فازت BF16 في التدريب
Training produces values with a huge dynamic range. Activations can be large, while many gradients are tiny, often below FP16’s 2^-24. NVIDIA reports that for the Multibox SSD detector, 31% of activation gradient values became zero when cast to FP16. The FP16 fix is loss scaling: multiply the loss by a large constant so the gradients shift up into FP16’s range, then divide the update back down, while watching for overflow. BF16 removes the problem at its root. Because its exponent is FP32’s exponent, every FP32 magnitude lands in a BF16 binade too (only a sliver just below FP32’s maximum can round up to infinity), and tiny gradients stay representable. Google describes BF16 as close to a drop-in replacement for FP32 with no loss scaling, and notes that networks are more sensitive to exponent size than to fraction size. The price is precision: about two to three significant decimal digits.
ينتج التدريب قيمًا ذات مدى ديناميكي هائل. قد تكون التنشيطات (activations) كبيرة، بينما كثير من التدرجات (gradients) صغير جدًا، وغالبًا أصغر من 2^-24 في FP16. تذكر NVIDIA أنه في كاشف Multibox SSD أصبحت 31% من قيم تدرجات التنشيط أصفارًا عند تحويلها إلى FP16. الحل في FP16 هو تحجيم الخسارة (loss scaling): تضرب الخسارة في ثابت كبير لترتفع التدرجات إلى داخل مدى FP16، ثم تقسم التحديث ليعود إلى حجمه، مع مراقبة تجاوز المدى (overflow). أما BF16 فتزيل المشكلة من جذورها. لأن أسّها هو أس FP32 نفسه، فكل مقدار في FP32 يقع أيضًا في binade من BF16 (باستثناء شريحة رفيعة تحت أكبر قيمة في FP32 مباشرة قد تُقرَّب إلى ما لا نهاية)، وتبقى التدرجات الصغيرة قابلة للتمثيل. تصف Google صيغة BF16 بأنها بديل شبه مباشر لـFP32 دون الحاجة إلى loss scaling، وتذكر أن الشبكات أكثر حساسية لحجم الأس منها لحجم الكسر. والثمن هو الدقة: نحو رقمين إلى ثلاثة أرقام عشرية معنوية.
Recall
Without looking: what are the widths and bias of FP16 and of BF16, and which one can store 100,000?دون أن تنظر: ما عرض الحقول وقيمة الإزاحة في FP16 وفي BF16، وأيهما تستطيع تخزين 100,000؟
FP16 is 1/5/10 with bias 15; BF16 is 1/8/7 with bias 127. Only BF16 can store 100,000, because FP16 tops out at 65504.
FP16 هي 1/5/10 بإزاحة 15؛ وBF16 هي 1/8/7 بإزاحة 127. وحدها BF16 تستطيع تخزين 100,000، لأن أقصى قيمة في FP16 هي 65504.
Quick checkاختبار سريع
Why is BF16 usually preferred over FP16 for training neural networks?لماذا تُفضَّل BF16 عادةً على FP16 في تدريب الشبكات العصبية؟
Exams love two directions: “here are 16 bits, what number is it?” and “here is a number, what are the bits?” Both reuse the FP32 procedure from Part 2. Only the field widths and the bias change.
تحب الامتحانات اتجاهين: «هذه 16 بتًا، ما الرقم الذي تمثله؟» و«هذا رقم، ما بتاته؟» كلاهما يعيد استخدام إجراء FP32 من الجزء الثاني. لا يتغير سوى عرض الحقول وقيمة الإزاحة.
Bits to decimal: FP16من البتات إلى العشري: FP16
Worked exampleمثال محلول
Decode the FP16 pattern 1 10001 1100000000 (slide 17)فك ترميز نمط FP16 الآتي 1 10001 1100000000 (الشريحة 17)
Split the fieldsافصل الحقول
Sign 1, exponent 10001 (5 bits), fraction 1100000000 (10 bits). Bias is 15.الإشارة 1، والأس 10001 (5 بتات)، والكسر 1100000000 (10 بتات). والإزاحة 15.Signالإشارة
The sign bit is 1, so the number is negative.بت الإشارة 1، إذن الرقم سالب.Exponentالأس
10001₂ = 16 + 1 = 17. It is neither 00000 nor 11111, so this is a Normal number. Unbiased exponent 17 - 15 = 2.10001₂ = 16 + 1 = 17. ليس 00000 ولا 11111، إذن هذا normal number. الأس بعد إزالة الإزاحة 17 - 15 = 2.Fractionالكسر
The Fraction (mantissa, significand) bits sit after the binary point: 0.11₂ = 1/2 + 1/4 = 0.75. Add the hidden leading one: 1.75.بتات fraction (mantissa) تقع بعد الفاصلة الثنائية: 0.11₂ = 1/2 + 1/4 = 0.75. أضف الواحد المخفي في البداية: 1.75.Combineاجمع النتيجة
-(1 + 0.75) × 2^2 = -1.75 × 4.-(1 + 0.75) × 2^2 = -1.75 × 4.Resultالنتيجة
-7.0-7.0
Decimal to bits: BF16من العشري إلى البتات: BF16
Worked exampleمثال محلول
Encode 2.5 in BF16 (slide 18)ترميز 2.5 في BF16 (الشريحة 18)
Normalizeالتطبيع
Write the value as 1.f × 2^k: 2.5 = 1.25 × 2^1, so k = 1 and f = 0.25.اكتب القيمة على الصورة 1.f × 2^k: 2.5 = 1.25 × 2^1، إذن k = 1 وf = 0.25.Signالإشارة
Positive, so the sign bit is 0.موجب، إذن بت الإشارة 0.Exponent fieldحقل الأس
Add the bias: 1 + 127 = 128 = 10000000₂ (8 bits).أضف الإزاحة: 1 + 127 = 128 = 10000000₂ (8 بتات).Fraction fieldحقل الكسر
0.25 = 0.01₂, so the 7 fraction bits are 0100000.0.25 = 0.01₂، إذن بتات الكسر السبع هي 0100000.Resultالنتيجة
0 10000000 0100000, which is 0x4020.0 10000000 0100000، أي 0x4020.
Compare this with FP32. In FP32, 2.5 is 0 10000000 01000000000000000000000, which is 0x40200000. The BF16 code is exactly the top 16 bits. That is the whole BF16 conversion story: keep the top half of an FP32 word and round the dropped half to nearest, ties to even. The exponent field is copied as is (rounding can only carry into it), which is why conversion from FP32 practically never overflows.
قارن هذا بـFP32. في FP32 تكون 2.5 هي 0 10000000 01000000000000000000000، أي 0x40200000. رمز BF16 هو بالضبط أعلى 16 بتًا. وهذه هي قصة تحويل BF16 كلها: احتفظ بالنصف الأعلى من كلمة FP32 وقرّب النصف المحذوف إلى الأقرب، مع كسر التعادل نحو الزوجي (ties to even). يُنسخ حقل الأس كما هو (التقريب لا يفعل إلا أن يحمل إليه)، ولهذا لا يكاد التحويل من FP32 يتجاوز المدى أبدًا.
Bits read sign, exponent, fraction. Rounding is round to nearest, ties to even. Overflow follows each format: IEEE-style formats go to infinity, while E4M3 has no infinity, so it is shown saturating to 448 (the OCP spec also allows mapping it to NaN).تُقرأ البتات بالترتيب: الإشارة ثم الأس ثم الكسر. التقريب إلى الأقرب، وعند التعادل إلى الزوجي (round to nearest, ties to even). ويتبع الفيض overflow قاعدة كل صيغة: الصيغ على نمط IEEE تذهب إلى اللانهاية، أما E4M3 فلا لانهاية فيها، لذلك تظهر وهي تتشبع عند 448 (وتسمح مواصفة OCP أيضا بربطها بـ NaN).
Recall
Encode -7 in BF16 and in FP16. How do the two codes differ?رمّز -7 في BF16 وفي FP16. كيف يختلف الرمزان؟
-7 = -1.75 × 2^2. BF16: sign 1, exponent 2 + 127 = 129 = 10000001, fraction 1100000, so 1 10000001 1100000. FP16: exponent 2 + 15 = 17 = 10001, fraction 1100000000, so 1 10001 1100000000. Same sign and fraction prefix; only the exponent width and bias differ.
-7 = -1.75 × 2^2. في BF16: الإشارة 1، والأس 2 + 127 = 129 = 10000001، والكسر 1100000، فيكون 1 10000001 1100000. وفي FP16: الأس 2 + 15 = 17 = 10001، والكسر 1100000000، فيكون 1 10001 1100000000. الإشارة وبداية الكسر متطابقتان؛ ولا يختلف إلا عرض الأس وقيمة الإزاحة.
Quick checkاختبار سريع
What decimal value does the FP16 pattern 0 10010 0100000000 represent?ما القيمة العشرية التي يمثلها نمط FP16 التالي: 0 10010 0100000000؟
Halve the width again and the range versus precision tension becomes impossible to ignore. With only 8 bits, a single format cannot serve both weights, which want fine steps, and gradients, which swing over many orders of magnitude. So NVIDIA, Arm and Intel proposed two FP8 formats and use each where it fits.
نصّف العرض مرة أخرى فيصبح التوتر بين المدى والدقة مستحيل التجاهل. مع 8 بتات فقط، لا تستطيع صيغة واحدة أن تخدم الأوزان (weights)، التي تريد خطوات دقيقة، والتدرجات، التي تتأرجح عبر رتب كثيرة من المقادير. لذلك اقترحت NVIDIA وArm وIntel صيغتين لـFP8، وتُستخدم كل منهما حيث تناسب.
Two splits of eight bitsتقسيمان للبتات الثماني
FP8 E4M3 spends 4 bits on the exponent and 3 on the mantissa, bias 7. FP8 E5M2 spends 5 on the exponent and 2 on the mantissa, bias 15: it is literally FP16 with the low 8 fraction bits cut off, the same relationship BF16 has with FP32.
تنفق FP8 E4M3 4 بتات على الأس و3 على الجزء العشري (mantissa)، بإزاحة 7. وتنفق FP8 E5M2 5 على الأس و2 على الجزء العشري، بإزاحة 15: إنها حرفيًا FP16 بعد قطع بتات الكسر الثماني الدنيا، وهي العلاقة نفسها التي تربط BF16 بـFP32.
| E4M3E4M3 | E5M2E5M2 | |
|---|---|---|
| Exponent / mantissa bitsعدد بتات الأس / الجزء العشري | 4 / 34 / 3 | 5 / 25 / 2 |
| Biasالإزاحة (bias) | 77 | 1515 |
| Infinityما لا نهاية (Infinity) | Noneلا يوجد | S.11111.00S.11111.00 |
| NaNNaN | S.1111.111 onlyS.1111.111 فقط | S.11111.01, .10, .11S.11111.01, .10, .11 |
| Largest normalأكبر قيمة عادية | S.1111.110 = 1.75 × 2^8 = 448S.1111.110 = 1.75 × 2^8 = 448 | S.11110.11 = 1.75 × 2^15 = 57344S.11110.11 = 1.75 × 2^15 = 57344 |
| Smallest normalأصغر قيمة عادية | 2^-6 = 0.0156252^-6 = 0.015625 | 2^-14 ≈ 6.1 × 10^-52^-14 ≈ 6.1 × 10^-5 |
| Smallest subnormalأصغر قيمة دون عادية | 2^-9 ≈ 0.001952^-9 ≈ 0.00195 | 2^-16 ≈ 1.5 × 10^-52^-16 ≈ 1.5 × 10^-5 |
| Typical tensorالموتّر المعتاد | Weights and activations, forward passالأوزان والتنشيطات، المرور الأمامي | Gradients, backward passالتدرجات، المرور الخلفي |
E4M3 reclaims the special codesصيغة E4M3 تستعيد الرموز الخاصة
In IEEE style, an exponent field of all ones is reserved for Infinity and NaN. In a format with only 16 exponent codes, losing a whole binade to special values is expensive. This is the waste flagged at the end of Part 2. E4M3 drops infinity entirely and keeps just one NaN pattern per sign, S.1111.111. Every other code with exponent 1111 becomes an ordinary normal number.
على طريقة IEEE، حقل الأس المكون من آحاد كلها محجوز لـINF وNaN. وفي صيغة ليس فيها سوى 16 رمزًا للأس، خسارة binade كامل لصالح القيم الخاصة مكلفة. وهذا هو الهدر الذي أُشير إليه في نهاية الجزء الثاني. تتخلى E4M3 عن ما لا نهاية تمامًا وتحتفظ بنمط NaN واحد فقط لكل إشارة، S.1111.111. وكل رمز آخر أسّه 1111 يصبح رقمًا عاديًا (normal).
Worked exampleمثال محلول
Where 448 and 57344 come fromمن أين تأتي 448 و57344
E4M3 largest normalأكبر قيمة عادية في E4M3
S.1111.110: exponent 15 - 7 = 8, mantissa 0.110₂ = 0.75. Value 1.75 × 2^8 = 448.S.1111.110: الأس 15 - 7 = 8، والجزء العشري 0.110₂ = 0.75. القيمة 1.75 × 2^8 = 448.What IEEE rules would have givenما كانت قواعد IEEE ستعطيه
With exponent 1111 reserved, the top would be S.1110.111 = 1.875 × 2^7 = 240. Reclaiming the codes adds seven magnitudes, 256 through 448, and nearly doubles the range.لو كان الأس 1111 محجوزًا لكانت القمة S.1110.111 = 1.875 × 2^7 = 240. استعادة هذه الرموز تضيف سبعة مقادير، من 256 إلى 448، وتكاد تضاعف المدى.E5M2 largest normalأكبر قيمة عادية في E5M2
Exponent 11111 stays reserved, so the top is S.11110.11: exponent 30 - 15 = 15, mantissa 0.75. Value 1.75 × 2^15 = 57344.يبقى الأس 11111 محجوزًا، فتكون القمة S.11110.11: الأس 30 - 15 = 15، والجزء العشري 0.75. القيمة 1.75 × 2^15 = 57344.Resultالنتيجة
E5M2 reaches about 128× further than E4M3 (57344 versus 448) and about 128× smaller on the subnormal side (2^-16 versus 2^-9), but each binade has only 4 steps instead of 8.تصل E5M2 إلى مدى أبعد بنحو 128× من E4M3 (57344 مقابل 448)، وإلى قيم أصغر بنحو 128× في الجانب دون العادي (2^-16 مقابل 2^-9)، لكن كل binade فيها لا يحوي سوى 4 خطوات بدلًا من 8.
Which tensor gets which formatأي موتّر يأخذ أي صيغة
The FP8 paper by Micikevicius and colleagues recommends E4M3 for weight and activation tensors and E5M2 for gradient tensors. The reasoning follows the rule. In the forward pass, weights and activations of one layer sit in a fairly narrow band and benefit from the extra mantissa bit. In the backward pass, gradients vary over many orders of magnitude, and losing small ones to underflow or large ones to overflow hurts more than coarse steps. In practice each tensor also carries a per-tensor scale factor so its values land inside the format’s range, which is the same idea as loss scaling.
توصي ورقة FP8 التي كتبها Micikevicius وزملاؤه باستخدام E4M3 لموتّرات الأوزان والتنشيطات، وE5M2 لموتّرات التدرجات. والتعليل يتبع القاعدة. في المرور الأمامي (forward pass) تقع أوزان طبقة واحدة وتنشيطاتها في نطاق ضيق نسبيًا وتستفيد من بت الجزء العشري الإضافي. وفي المرور الخلفي (backward pass) تتفاوت التدرجات عبر رتب كثيرة من المقادير، وخسارة الصغيرة منها بسبب underflow أو الكبيرة بسبب overflow تضر أكثر من الخطوات الخشنة. وعمليًا يحمل كل موتّر أيضًا معامل تحجيم خاصًا به (per-tensor scale factor) لتقع قيمه داخل مدى الصيغة، وهي الفكرة نفسها التي يقوم عليها loss scaling.
Recall
Derive the largest E4M3 normal and explain why it is not 240.اشتق أكبر قيمة عادية في E4M3 واشرح لماذا ليست 240.
S.1111.110 = 1.75 × 2^(15 - 7) = 448. It would be 240 under IEEE rules, because the all-ones exponent would be reserved for INF and NaN. E4M3 keeps only S.1111.111 as NaN and uses the rest of that binade for normal values.
S.1111.110 = 1.75 × 2^(15 - 7) = 448. كانت ستكون 240 وفق قواعد IEEE، لأن الأس المكوّن من آحاد كلها كان سيُحجز لـINF وNaN. تحتفظ E4M3 بـS.1111.111 وحده كـNaN وتستخدم بقية ذلك الـbinade للقيم العادية.
Quick checkاختبار سريع
Which FP8 format should store gradients in the backward pass, and why?أي صيغة FP8 يجب أن تخزّن التدرجات في المرور الخلفي، ولماذا؟
With 4 bits there are only 16 codes, 8 of them non-negative. That is small enough to list every value, and the lists make the range versus precision rule visible at a glance.
مع 4 بتات لا يوجد سوى 16 رمزًا، منها 8 غير سالبة. وهذا عدد صغير بما يكفي لسرد كل قيمة، والقوائم تجعل قاعدة المدى مقابل الدقة مرئية من نظرة واحدة.
The same eight codes, four waysالرموز الثمانية نفسها بأربع طرق
INT4 treats the bits as a Two's complement integer: 0 to 7 and -1 to -8, evenly spaced. The FP4 (E1M2, E2M1, E3M0) variants instead keep a sign bit and split the remaining three between exponent and mantissa, using the same bias formula 2^(e-1) - 1, which gives 0 for E1M2, 1 for E2M1 and 3 for E3M0. None of them reserves any code for INF or NaN: with so few codes, every pattern must be a number.
تعامل INT4 البتات كعدد صحيح بنظام two's complement: من 0 إلى 7 ومن -1 إلى -8، بتباعد متساوٍ. أما متغيرات FP4 فتحتفظ ببت إشارة وتقسم البتات الثلاثة الباقية بين الأس والجزء العشري، مستخدمة صيغة الإزاحة نفسها 2^(e-1) - 1، التي تعطي 0 لـE1M2 و1 لـE2M1 و3 لـE3M0. ولا تحجز أي منها رمزًا لـINF أو NaN: مع هذا العدد القليل من الرموز، يجب أن يكون كل نمط رقمًا.
| Formatالصيغة | Biasالإزاحة | Codes 0000 to 0111الرموز من 0000 إلى 0111 | Spacingالتباعد |
|---|---|---|---|
| INT4INT4 | noneلا يوجد | 0, 1, 2, 3, 4, 5, 6, 70, 1, 2, 3, 4, 5, 6, 7 | Even, step 1متساوٍ، بخطوة 1 |
| E1M2E1M2 | 00 | 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.50, 0.5, 1, 1.5, 2, 2.5, 3, 3.5 | Even, step 0.5متساوٍ، بخطوة 0.5 |
| E2M1E2M1 | 11 | 0, 0.5, 1, 1.5, 2, 3, 4, 60, 0.5, 1, 1.5, 2, 3, 4, 6 | Step doubles every two codesتتضاعف الخطوة كل رمزين |
| E3M0E3M0 | 33 | 0, 0.25, 0.5, 1, 2, 4, 8, 160, 0.25, 0.5, 1, 2, 4, 8, 16 | Every code doublesكل رمز يضاعف القيمة |
Decoding every E2M1 codeفك ترميز كل رمز في E2M1
E2M1 is the one that matters most in practice: it is the FP4 element type in the OCP microscaling (MX) specification and in NVIDIA Blackwell’s FP4. Its fields are one sign bit, two exponent bits and one mantissa bit, with bias 1. Exponent 00 means subnormal, value 0.m × 2^(1 - 1); any other exponent is normal, value 1.m × 2^(E - 1).
E2M1 هي الأهم عمليًا: إنها نوع العنصر في FP4 ضمن مواصفة OCP للتحجيم الدقيق (microscaling, MX) وفي FP4 الخاصة بـNVIDIA Blackwell. حقولها بت إشارة واحد، وبتا أس، وبت واحد للجزء العشري، بإزاحة 1. الأس 00 يعني قيمة دون عادية، قيمتها 0.m × 2^(1 - 1)؛ وأي أس آخر يعني قيمة عادية، قيمتها 1.m × 2^(E - 1).
| Codeالرمز | Kindالنوع | Formulaالصيغة الحسابية | Valueالقيمة |
|---|---|---|---|
| 0 00 00 00 0 | subnormalدون عادية | 0.0 × 2^(1-1)0.0 × 2^(1-1) | 00 |
| 0 00 10 00 1 | subnormalدون عادية | 0.5 × 2^(1-1)0.5 × 2^(1-1) | 0.50.5 |
| 0 01 00 01 0 | normalعادية | 1.0 × 2^(1-1)1.0 × 2^(1-1) | 11 |
| 0 01 10 01 1 | normalعادية | 1.5 × 2^(1-1)1.5 × 2^(1-1) | 1.51.5 |
| 0 10 00 10 0 | normalعادية | 1.0 × 2^(2-1)1.0 × 2^(2-1) | 22 |
| 0 10 10 10 1 | normalعادية | 1.5 × 2^(2-1)1.5 × 2^(2-1) | 33 |
| 0 11 00 11 0 | normalعادية | 1.0 × 2^(3-1)1.0 × 2^(3-1) | 44 |
| 0 11 10 11 1 | normalعادية | 1.5 × 2^(3-1)1.5 × 2^(3-1) | 66 |
The other two rows on the slide check out. E1M2 with bias 0: 0001 is subnormal, 0.25 × 2^(1 - 0) = 0.5, and 0111 is (1 + 0.75) × 2^(1 - 0) = 3.5. Its values are exactly INT4 times 0.5, so E1M2 is really a fixed-point format in disguise. E3M0 with bias 3 has no mantissa at all: 0001 is 2^(1 - 3) = 0.25 and 0111 is 2^(7 - 3) = 16. Every value is a power of two.
الصفان الآخران في الشريحة صحيحان. E1M2 بإزاحة 0: الرمز 0001 دون عادي، 0.25 × 2^(1 - 0) = 0.5، والرمز 0111 هو (1 + 0.75) × 2^(1 - 0) = 3.5. قيمها هي بالضبط قيم INT4 مضروبة في 0.5، لذلك فإن E1M2 في الحقيقة صيغة فاصلة ثابتة (fixed-point) متنكرة. وE3M0 بإزاحة 3 ليس فيها جزء عشري إطلاقًا: الرمز 0001 هو 2^(1 - 3) = 0.25 والرمز 0111 هو 2^(7 - 3) = 16. كل قيمة فيها قوة للعدد 2.
Positive half only; setting the sign bit mirrors every value. Bias: 1. Spacing: step doubles every two codes. Largest value: 6.يظهر النصف الموجب فقط، وتفعيل بت الإشارة يعكس كل قيمة. الانحياز bias: 1. التباعد: تتضاعف الخطوة كل رمزين. أكبر قيمة: 6.
What the four grids teachما الذي تعلّمه الشبكات الأربع
- Mantissa bits give even spacing. INT4 and E1M2 are uniform grids; the only difference is the scale.بتات الجزء العشري تعطي تباعدًا متساويًا. INT4 وE1M2 شبكتان منتظمتان؛ والفرق الوحيد بينهما هو المقياس.
- Exponent bits give logarithmic spacing. E3M0 doubles at every step, so it reaches 16 but jumps from 8 to 16 with nothing in between.بتات الأس تعطي تباعدًا لوغاريتميًا. E3M0 تتضاعف عند كل خطوة، لذلك تصل إلى 16 لكنها تقفز من 8 إلى 16 دون أي قيمة بينهما.
- E2M1 is the compromise: two values per binade, reaching 6, dense near zero and coarse near the top. That shape matches the bell-shaped distribution of neural network weights, which is why it is the practical FP4.E2M1 هي الحل الوسط: قيمتان في كل binade، وتصل إلى 6، كثيفة قرب الصفر وخشنة قرب القمة. هذا الشكل يطابق التوزيع الجرسي لأوزان الشبكات العصبية، ولهذا هي صيغة FP4 العملية.
Recall
List the eight non-negative E2M1 values and say which two are subnormal.اسرد القيم الثماني غير السالبة في E2M1 وحدد أي اثنتين منها دون عادية.
0, 0.5, 1, 1.5, 2, 3, 4, 6. The codes with exponent 00, namely 0 and 0.5, are subnormal.
0, 0.5, 1, 1.5, 2, 3, 4, 6. الرمزان اللذان أسّهما 00، أي 0 و0.5، هما القيمتان دون العاديتين.
Quick checkاختبار سريع
In FP4 E2M1 (bias 1), what value does the code 0111 represent?في صيغة FP4 E2M1 (الإزاحة 1)، ما القيمة التي يمثلها الرمز 0111؟
Recapالخلاصة
If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا
- Exponent width sets range, fraction width sets precision. Total width only says how many codes you get.عرض الأس يحدد المدى، وعرض الكسر يحدد الدقة. أما العرض الكلي فيخبرك فقط بعدد الرموز (codes) المتاحة.
- Bias = 2^(e-1) - 1: 127 for FP32 and BF16, 15 for FP16 and E5M2, 7 for E4M3.الإزاحة bias = 2^(e-1) - 1: تساوي 127 في FP32 وBF16، و15 في FP16 وE5M2، و7 في E4M3.
- FP16 (5/10): max 65504, min normal 2^-14, min subnormal 2^-24. Small gradients underflow, so FP16 training needs loss scaling.FP16 (5/10): أكبر قيمة 65504، وأصغر قيمة عادية 2^-14، وأصغر قيمة دون عادية 2^-24. التدرجات الصغيرة تنزل تحت المدى (underflow)، لذلك يحتاج تدريب FP16 إلى loss scaling.
- BF16 (8/7): same exponent, same range as FP32. It is the top 16 bits of FP32 after rounding, so conversion keeps FP32's range.BF16 (8/7): الأس نفسه والمدى نفسه كما في FP32. هي أعلى 16 بتًا من FP32 بعد التقريب، لذلك يحافظ التحويل على مدى FP32.
- Decode: value = (-1)^S × (1 + fraction) × 2^(exponent - bias). FP16 1 10001 1100000000 = -1.75 × 4 = -7.فك الترميز: القيمة = (-1)^S × (1 + fraction) × 2^(exponent - bias). النمط FP16 1 10001 1100000000 = -1.75 × 4 = -7.
- Encode: normalize to 1.f × 2^k, store k + bias, store f. 2.5 = 1.25 × 2^1 gives BF16 0 10000000 0100000.الترميز: حوّل الرقم إلى الصورة 1.f × 2^k، واخزن k + bias في الأس، واخزن f في الكسر. 2.5 = 1.25 × 2^1 يعطي في BF16 النمط 0 10000000 0100000.
- E4M3 drops INF and keeps a single NaN pattern, reclaiming exponent 1111 for normals: max 1.75 × 2^8 = 448. Forward weights and activations.تتخلى E4M3 عن INF وتحتفظ بنمط NaN واحد فقط، فتستعيد الأس 1111 للقيم العادية: أكبر قيمة 1.75 × 2^8 = 448. تُستخدم للأوزان والتنشيطات في المرور الأمامي.
- E5M2 keeps IEEE INF and NaN: max 1.75 × 2^15 = 57344. Backward gradients.تحتفظ E5M2 بقيم INF وNaN على طريقة IEEE: أكبر قيمة 1.75 × 2^15 = 57344. تُستخدم للتدرجات في المرور الخلفي.
- FP4 positive grids: INT4 0..7, E1M2 0..3.5 in 0.5 steps, E2M1 {0, 0.5, 1, 1.5, 2, 3, 4, 6}, E3M0 {0, 0.25, ..., 16}. No INF, no NaN.شبكات القيم الموجبة في FP4: INT4 من 0 إلى 7، وE1M2 من 0 إلى 3.5 بخطوة 0.5، وE2M1 هي {0, 0.5, 1, 1.5, 2, 3, 4, 6}، وE3M0 هي {0, 0.25, ..., 16}. لا INF ولا NaN.
Sourcesالمصادر
- FP8 Formats for Deep Learning (Micikevicius et al., 2022)PaperarXiv 2209.05433, NVIDIA, Arm and IntelE4M3 and E5M2 definitions, the 448 maximum from reclaimed codes, and E4M3 for weights and activations, E5M2 for gradients.تعريفا E4M3 وE5M2، والحد الأقصى 448 الناتج عن استعادة الرموز الخاصة، واستخدام E4M3 للأوزان والتنشيطات وE5M2 للتدرجات.(opens in a new tab)(يفتح في تبويب جديد)
- OCP Microscaling Formats (MX) Specification v1.0DocsOpen Compute ProjectEncoding tables for FP8 E4M3 and E5M2 (max, min normal, min subnormal) and FP4 E2M1 (bias 1, max 6.0, no INF or NaN).جداول الترميز لصيغتي FP8 E4M3 وE5M2 (أكبر قيمة، وأصغر قيمة عادية، وأصغر قيمة دون عادية) ولصيغة FP4 E2M1 (الإزاحة 1، وأكبر قيمة 6.0، ولا INF ولا NaN).(opens in a new tab)(يفتح في تبويب جديد)
- BFloat16: The secret to high performance on Cloud TPUsArticleGoogle CloudBF16 layout, same dynamic range as FP32, no loss scaling needed, smaller multipliers.تخطيط BF16، والمدى الديناميكي نفسه كما في FP32، فلا حاجة إلى loss scaling، ودوائر ضرب أصغر.(opens in a new tab)(يفتح في تبويب جديد)
- Train With Mixed PrecisionDocsNVIDIA Deep Learning Performance documentationFP16 range (max 65504, min subnormal 2^-24), gradient underflow and loss scaling.مدى FP16 (أكبر قيمة 65504، وأصغر قيمة دون عادية 2^-24)، ونزول التدرجات تحت المدى (underflow)، وloss scaling.(opens in a new tab)(يفتح في تبويب جديد)
- Mixed Precision Training (Micikevicius et al., ICLR 2018)PaperarXiv 1710.03740Training in IEEE half precision with an FP32 master copy and loss scaling.التدريب بنصف الدقة وفق IEEE مع نسخة رئيسية (master copy) بصيغة FP32 وloss scaling.(opens in a new tab)(يفتح في تبويب جديد)