Lecture 05-3 · Quantization III

المحاضرة 05-3 · التكميم الثالث

KL-divergence and MSE (OCTAV) clipping

القص بـ KL-divergence وبـ MSE (OCTAV)

Slides 27–31الشرائح 27–31 P2 SHOULDP2 مهم

Two more ways to choose the clip threshold from calibration data. TensorRT picks the threshold whose quantized histogram loses the least information (smallest KL divergence). OCTAV picks the threshold with the smallest mean-square error, found by Newton-Raphson.

طريقتان أخريان لاختيار حد القص من بيانات calibration. TensorRT يختار الحد الذي يفقد فيه الـ histogram المكمَّم أقل قدر من المعلومات (أصغر KL divergence). وOCTAV يختار الحد الذي يعطي أصغر mean-square error، ويجده بطريقة Newton-Raphson.

Minimize loss of information

تقليل فقدان المعلومات

Slide 27 (Migacz, 8-bit Inference with TensorRT, 2017). The idea: the integer model should encode the same information as the original floating-point model. Information loss is measured by the Kullback-Leibler divergence (relative entropy, information divergence). For two discrete distributions P and Q:

الشريحة 27 (Migacz, 8-bit Inference with TensorRT, 2017). الفكرة: النموذج الصحيح يجب أن يحمل نفس المعلومات التي يحملها النموذج العشري الأصلي. ويُقاس فقدان المعلومات بـ Kullback-Leibler divergence (ويسمى أيضاً relative entropy أو information divergence). ولتوزيعين منفصلين P وQ:

DKL(P ‖ Q) = Σi=1..N P(xi) · log( P(xi) / Q(xi) )

تقول المعادلة: نجمع على كل الخانات i حاصل ضرب P(xi) في لوغاريتم النسبة P(xi) / Q(xi).

Intuition from the slide: KL divergence measures the amount of information lost when approximating a given encoding. Here P is the reference: the FP32 activation histogram. Q is its approximation: the histogram after clipping and quantizing to 8 bits. DKL is 0 only when Q = P, and it is not symmetric: DKL(P ‖ Q) ≠ DKL(Q ‖ P) in general.

الحدس من الشريحة: KL divergence يقيس كمية المعلومات المفقودة عند تقريب ترميز معين. وهنا P هو المرجع: histogram الخاص بـ activations بدقة FP32. وQ هو التقريب: الـ histogram بعد القص وquantization إلى 8 بتات. وقيمة DKL تساوي 0 فقط عندما Q = P، وهي غير متماثلة: عموماً DKL(P ‖ Q) ≠ DKL(Q ‖ P).

How TensorRT entropy calibration uses it

كيف تستخدمه معايرة الإنتروبيا في TensorRT

For each layer, the calibration activations are collected into a fine histogram. Then for each candidate threshold T: values above T are folded into the last bin to form P, the range [0, T] is squeezed into the 8-bit levels to form Q, and DKL(P ‖ Q) is computed. The threshold with the smallest KL wins and becomes rmax. In slide 28 (ResNet-152 res4b30) the "entropy method" line sits near 40 even though activations reach about 500.

لكل طبقة تُجمع activations من بيانات calibration في histogram دقيق. ثم لكل حد مرشح T: القيم الأكبر من T تُضم إلى الخانة الأخيرة لتكوين P، ويُضغط المدى [0, T] إلى مستويات 8 بتات لتكوين Q، ثم يُحسب DKL(P ‖ Q). والحد ذو أصغر KL يفوز ويصبح rmax. وفي الشريحة 28 (ResNet-152 res4b30) يقع خط entropy method قرب 40 رغم أن القيم تصل إلى حوالي 500.

log count 0 100 200 300 400 500 KL threshold ≈ 40 sparse tail: clipped to the threshold activation value (ResNet-152 res4b30, slide 28)
Most of the probability mass sits below about 40. Covering the full range to 500 would make every 8-bit step about 12 times wider for the sake of a tail that holds almost no mass.معظم كتلة الاحتمال تقع تحت 40 تقريباً. تغطية المدى كاملاً حتى 500 ستجعل كل خطوة من خطوات الـ 8 بت أعرض بنحو 12 مرة من أجل ذيل لا يحمل تقريباً أي كتلة.

الرسم histogram بمقياس لوغاريتمي ينخفض بحدة ثم له ذيل طويل متناثر يصل إلى 500. وحد KL يقع قرب 40، وكل ما على يمينه يُقص. معظم الكتلة الاحتمالية تحت 40 تقريباً، وتغطية المدى كله حتى 500 تجعل كل خطوة 8-bit أعرض بحوالي 12 مرة من أجل ذيل لا يحمل تقريباً أي كتلة.

Slide 28's question: what happens if we choose a higher rmax? Answer: larger dynamic range → larger quantization loss. The step size S grows, and the dense region near zero, where almost all values live, gets fewer distinct levels.

سؤال الشريحة 28: ماذا يحدث لو اخترنا rmax أكبر؟ الجواب: مدى أكبر يعني خسارة quantization أكبر. فحجم الخطوة S يكبر، والمنطقة الكثيفة قرب الصفر، حيث تعيش معظم القيم، تحصل على مستويات أقل.

The four histograms on slide 29

المدرّجات التكرارية الأربعة في الشريحة 29

LayerالطبقةHistogram shapeشكل المدرّج التكراريWhere KL puts the thresholdأين يضع KL العتبة
AlexNet pool2smooth decay, then a spike at the very end (about 140)تناقص سلس، ثم قمة حادة في النهاية تماماً (نحو 140)at the end, about 140: clipping would discard the spikeفي النهاية، نحو 140: القص سيُسقط القمة
GoogLeNet inception_3a/poolhump, then a spike at the max (about 140)حدبة، ثم قمة حادة عند القيمة القصوى (نحو 140)at the end, about 140في النهاية، نحو 140
GoogLeNet inception_5a/5x5steady decay with a sparse tail to about 175تناقص منتظم مع ذيل متفرّق حتى نحو 175about 137, trims only the sparse tailنحو 137، يقص الذيل المتفرّق فقط
ResNet-152 res4b8_branch2aalmost all mass near 0, long tail to about 250كل الكتلة تقريباً قرب 0، وذيل طويل حتى نحو 250very low, about 15منخفضة جداً، نحو 15

الجدول: في AlexNet pool2 تناقص ناعم ثم قمة في النهاية (حوالي 140)، فيضع KL الحد في النهاية لأن القص سيحذف القمة. وفي GoogLeNet inception_3a/pool سنام ثم قمة عند الحد الأقصى، والحد أيضاً في النهاية. وفي GoogLeNet inception_5a/5x5 تناقص ثابت مع ذيل متناثر حتى 175 تقريباً، والحد حوالي 137. وفي ResNet-152 res4b8_branch2a كل الكتلة تقريباً قرب الصفر مع ذيل طويل حتى 250، والحد منخفض جداً حوالي 15.

The lesson: KL calibration is adaptive. It does not always clip hard. When real mass sits at the top of the range, the KL cost of clipping it is high, so the threshold stays at the max.

الدرس: KL calibration تكيّفية، ولا تقص بقوة دائماً. فعندما توجد كتلة حقيقية في أعلى المدى تكون كلفة قصها في KL عالية، فيبقى الحد عند القيمة القصوى.

Workedمثال محلول
reference FP32 histogram, 3 bins:  P  = [0.5, 0.3, 0.2]
candidate A after quantization:    Q1 = [0.4, 0.4, 0.2]
candidate B after quantization:    Q2 = [0.6, 0.3, 0.1]
(natural log, result in nats)

D(P||Q1) = 0.5 ln(0.5/0.4) + 0.3 ln(0.3/0.4) + 0.2 ln(1)
         = 0.5(0.2231) + 0.3(-0.2877) + 0
         = 0.1116 - 0.0863 = 0.0253

D(P||Q2) = 0.5 ln(0.5/0.6) + 0.3 ln(1) + 0.2 ln(0.2/0.1)
         = 0.5(-0.1823) + 0 + 0.2(0.6931)
         = -0.0912 + 0.1386 = 0.0475

pick candidate A (0.0253 < 0.0475)

asymmetry check:
D(Q1||P) = 0.4 ln(0.8) + 0.4 ln(4/3) + 0 = 0.0258  != 0.0253

المثال المحلول: الـ histogram المرجعي P = [0.5, 0.3, 0.2]، ومرشحان بعد quantization: Q1 = [0.4, 0.4, 0.2] وQ2 = [0.6, 0.3, 0.1]. باللوغاريتم الطبيعي: D(P‖Q1) = 0.1116 − 0.0863 = 0.0253، وD(P‖Q2) = −0.0912 + 0.1386 = 0.0475. إذن نختار المرشح A. وللتأكد من عدم التماثل: D(Q1‖P) = 0.0258، وهي لا تساوي 0.0253. لاحظ أن بعض الحدود سالبة، لكن المجموع لا يكون سالباً أبداً.

Minimize MSE with Newton-Raphson: OCTAV

تقليل MSE بطريقة Newton-Raphson: OCTAV

Slides 30 and 31 (Sakr et al., ICML 2022). Compare the two extremes on slide 30, a bell-shaped tensor in [−0.8, 0.8] with almost all mass in [−0.2, 0.2]:

الشريحتان 30 و31 (Sakr et al., ICML 2022). قارن بين الطرفين في الشريحة 30، لـ tensor جرسي الشكل في [−0.8, 0.8] وكل كتلته تقريباً في [−0.2, 0.2]:

ChoiceالخيارWhat goes wrongما الذي يسوء
Max-scaled quantization (clip at ±0.8)التكميم بالقيمة القصوى (القص عند ±0.8)levels are spread over the empty low-density data region, so the dense centre gets only two or three levels: large quantization noiseتتوزع المستويات على منطقة البيانات منخفضة الكثافة الفارغة، فلا يحصل المركز الكثيف إلا على مستويين أو ثلاثة: ضجيج تكميم كبير
Clipped quantization (clip near ±0.2)التكميم مع القص (القص قرب ±0.2)all levels land where the data is; the empty tails are clipped at little costكل المستويات تقع حيث توجد البيانات؛ والذيول الفارغة تُقص بكلفة قليلة

الجدول: max-scaled quantization (القص عند ±0.8) ينشر المستويات على منطقة low-density data الفارغة، فيحصل المركز الكثيف على مستويين أو ثلاثة فقط، والنتيجة large quantization noise. أما clipped quantization (القص قرب ±0.2) فيضع كل المستويات حيث توجد البيانات، ويُقص الذيلان الفارغان بكلفة قليلة.

The MSE as a function of the clipping scalar s is convex (slide 31, layer #63 of BERT-Base for 10 different inputs: every curve drops steeply, bottoms out near s ≈ 1, then rises slowly). A convex 1-D function has a single minimum, so Newton-Raphson converges to the global optimum. Because it converges in a few iterations, OCTAV finds the optimal scalars on the fly, recomputing them for each tensor during training instead of in a separate offline search.

قيمة MSE كدالة في clipping scalar s محدّبة convex (الشريحة 31، الطبقة #63 في BERT-Base لـ 10 مدخلات مختلفة: كل منحنى ينخفض بحدة ويصل إلى القاع قرب s ≈ 1 ثم يرتفع ببطء). والدالة المحدبة ذات البعد الواحد لها قاع واحد، لذلك تتقارب Newton-Raphson إلى الحل الأمثل العام. ولأنها تتقارب في خطوات قليلة، فإن OCTAV يجد القيم المثلى أثناء العمل on the fly، ويعيد حسابها لكل tensor أثناء التدريب بدلاً من بحث منفصل.

MSE 0 1 2 3 4 OCTAV optimum, s ≈ 1 s too small: heavy clipping s too large: coarse steps clipping scalar s (slide 31, BERT-Base layer #63)
One bowl, one minimum. Newton-Raphson on the derivative of this curve lands on it in a few steps.وعاء واحد ونهاية صغرى واحدة. طريقة Newton-Raphson على مشتقة هذا المنحنى تصل إليها في خطوات قليلة.

الرسم منحنى MSE محدب مقابل clipping scalar من 0 إلى 4. على اليسار انخفاض حاد لأن القص كثير، والقاع قرب 1 هو الأمثل الذي يجده OCTAV، وعلى اليمين ارتفاع بطيء لأن الخطوات تصبح خشنة. وعاء واحد وقاع واحد، وNewton-Raphson على مشتقة هذا المنحنى يصل إليه في خطوات قليلة.

For reference (from the paper; the slide only names the method), with B bits the OCTAV update is:

للمرجع (من الورقة البحثية، فالشريحة تذكر اسم الطريقة فقط)، مع B بتات يكون تحديث OCTAV:

sn+1 = E[ |X| · 1{|X| > sn} ] / ( (4−B/3) · E[ 1{|X| ≤ sn} ] + E[ 1{|X| > sn} ] )

تقول المعادلة: القيمة الجديدة لـ s تساوي متوسط |X| للقيم التي تتجاوز sn، مقسوماً على (4−B/3) مضروباً في نسبة القيم داخل المدى، زائد نسبة القيم المقصوصة. الحد الأول في المقام يمثل rounding noise والثاني يمثل clipping.

OCTAV results at int4 (slide 31)

نتائج OCTAV بدقة int4 (الشريحة 31)

NetworkالشبكةFP32OCTAV int4Dropالانخفاض
ResNet-5076.0775.840.23
MobileNet-V271.7170.880.83
BERT-Large91.0087.093.91

الجدول: ResNet-50 من 76.07 إلى 75.84 (انخفاض 0.23)، وMobileNet-V2 من 71.71 إلى 70.88 (انخفاض 0.83)، وBERT-Large من 91.00 إلى 87.09 (انخفاض 3.91). الشبكة المدمجة MobileNet-V2 تخسر أكثر من ResNet-50، وBERT-Large هو الأصعب.

Trapفخ

P is the FP32 reference, Q is the quantized approximation. The calibration objective is DKL(P ‖ Q), and KL is asymmetric, so swapping the arguments changes the number and can change which threshold wins.

P هو المرجع FP32، وQ هو التقريب المكمَّم. هدف calibration هو DKL(P ‖ Q)، وKL غير متماثل، فتبديل الطرفين يغيّر الرقم وقد يغيّر الحد الفائز.

"Larger rmax is safer" is false. It avoids clipping but makes every step coarser, so the dense region loses precision. And the KL method does not always clip low: for AlexNet pool2 it keeps the full range because of the spike at the max.

"rmax الأكبر أكثر أماناً" عبارة خاطئة. فهو يتجنب القص لكنه يجعل كل خطوة أخشن، فتخسر المنطقة الكثيفة دقتها. وطريقة KL لا تقص دائماً عند قيمة منخفضة: في AlexNet pool2 تحتفظ بالمدى كله بسبب القمة عند الحد الأقصى.

Memory hookخطّاف للذاكرة

Three ways to clip, three objectives. Mean of per-sample max (simple statistics), KL (TensorRT, keep the histogram's information), MSE (Laplace closed form or OCTAV Newton-Raphson, keep the values close).

ثلاث طرق للقص، وثلاثة أهداف. متوسط القيم القصوى لكل عينة (إحصاء بسيط)، وKL (TensorRT، يحافظ على معلومات الـ histogram)، وMSE (صيغة Laplace أو OCTAV بـ Newton-Raphson، يحافظ على قرب القيم).

Check yourself

اختبر نفسك

In TensorRT entropy calibration, which distribution plays the role of P in DKL(P ‖ Q)?

في TensorRT entropy calibration، أي توزيع يمثل P في DKL(P ‖ Q)؟

P is the original encoding and Q is the approximation; KL measures the information lost when Q stands in for P.

P هو الترميز الأصلي وQ هو التقريب، وKL يقيس المعلومات المفقودة عندما يحل Q محل P.

P = [0.5, 0.5] and Q = [0.25, 0.75]. Using the natural log, what is DKL(P ‖ Q)?

P = [0.5, 0.5] وQ = [0.25, 0.75]. باستخدام اللوغاريتم الطبيعي، ما قيمة DKL(P ‖ Q)؟

0.5 ln 2 + 0.5 ln(2/3) = 0.3466 − 0.2027 = 0.1438. Option B is DKL(Q ‖ P) = 0.1308, the reversed order. KL is never negative, which rules out D.

0.5 ln 2 + 0.5 ln(2/3) = 0.3466 − 0.2027 = 0.1438. والخيار B هو DKL(Q ‖ P) = 0.1308، أي الترتيب المعكوس. وKL لا يكون سالباً أبداً، فالخيار D مستبعد.

What happens if you choose a higher rmax than the KL-optimal threshold?

ماذا يحدث لو اخترت rmax أكبر من الحد الأمثل حسب KL؟

Slide 28: larger dynamic range → larger quantization loss. Fewer levels land where most values live.

الشريحة 28: مدى أكبر يعني خسارة quantization أكبر، لأن عدداً أقل من المستويات يقع حيث تعيش معظم القيم.

Why can OCTAV find the MSE-optimal clipping scalar reliably and on the fly?

لماذا يستطيع OCTAV إيجاد clipping scalar الأمثل لـ MSE بشكل موثوق وأثناء العمل؟

Slide 31: MSE is convex and OCTAV converges to the global optimum. Max-scaling (option C) is exactly what slide 30 shows producing large quantization noise.

الشريحة 31: MSE محدّب وOCTAV يتقارب إلى الأمثل العام. وmax-scaling (الخيار C) هو بالضبط ما تُظهر الشريحة 30 أنه يسبب quantization noise كبيراً.

With OCTAV int4, which network loses the most accuracy relative to FP32?

مع OCTAV int4، أي شبكة تخسر أكبر قدر من الدقة مقارنة بـ FP32؟

Drops are 3.91 for BERT-Large, 0.23 for ResNet-50 and 0.83 for MobileNet-V2.

الانخفاض 3.91 لـ BERT-Large، و0.23 لـ ResNet-50، و0.83 لـ MobileNet-V2.