Majid Al-RaimiLinear Quantization: From Codebooks to an Affine Gridالتكميم الخطي: من دفاتر الترميز إلى شبكة أفينية (affine)

COE 592Lecture 5.2المحاضرة 5.2Part 01الجزء 01

Linear Quantization: From Codebooks to an Affine Gridالتكميم الخطي: من دفاتر الترميز إلى شبكة أفينية (affine)

Places linear quantization next to K-means quantization by asking what is stored and what is computed in each. It then works the lecture's 4x4 FP32 weight matrix through 2-bit linear quantization from start to finish: integer codes, zero point -1, scale 1.07, reconstruction and quantization error.يضع التكميم الخطي بجانب تكميم K-means بالسؤال عما يُخزَّن وما يُحسَب في كل منهما. ثم يحل مصفوفة أوزان FP32 بحجم 4x4 من المحاضرة عبر تكميم خطي بـ2 بت من البداية إلى النهاية: الرموز الصحيحة، ونقطة الصفر -1، والمقياس 1.07، وإعادة البناء، وخطأ التكميم.

Conceptsالمفاهيم
3
Slidesالشرائح
1-4
Readingمدة القراءة
18 min18 دقيقة
Understoodما فهمته
0/3 conceptsمفاهيم

Why this part mattersلماذا يهمّ هذا الجزء

Lecture 05-1 ended with K-means quantization: cluster the weights, store a tiny integer index per weight, and keep a small table of float centroids. The model shrinks, but the arithmetic never leaves floating point. This part introduces the scheme that fixes that: Linear quantization, which stores integers and also computes with them.

Three ideas build on each other. First, the storage and computation table that frames the whole lecture. Second, the forward step that turns FP32 weights into 2-bit codes on an evenly spaced grid. Third, the backward step that rebuilds approximate weights and measures what was lost. Every number in the running example is worked out below, because the midterm is likely to hand you S and Z and ask you to do exactly this by hand.

انتهت المحاضرة 05-1 بتكميم K-means: نجمّع الأوزان في عناقيد (clusters)، ونخزّن لكل وزن فهرسًا صحيحًا صغيرًا، ونحتفظ بجدول صغير من المراكز (centroids) العشرية. يصغر حجم النموذج، لكن الحساب لا يغادر الأعداد العشرية (floating point) أبدًا. يقدّم هذا الجزء الطريقة التي تعالج ذلك: linear quantization، التي تخزّن أعدادًا صحيحة وتحسب بها أيضًا.

ثلاث أفكار يبني بعضها على بعض. الأولى: جدول التخزين والحساب الذي يؤطّر المحاضرة كلها. الثانية: الخطوة الأمامية التي تحوّل أوزان FP32 إلى رموز بطول 2 بت على شبكة متساوية التباعد. الثالثة: الخطوة العكسية التي تعيد بناء أوزان تقريبية وتقيس ما فُقد. كل رقم في المثال المستمر محسوب بالتفصيل أدناه، لأن امتحان منتصف الفصل على الأرجح سيعطيك S وZ ويطلب منك أن تفعل هذا بالضبط يدويًا.

By the end you canفي نهاية هذا الجزء ستستطيع

  1. Fill in the storage and computation table for FP32, K-means and linear quantization, and justify every cell.تملأ جدول التخزين والحساب لكل من FP32 وتكميم K-means والتكميم الخطي (linear quantization)، وتبرّر كل خانة فيه.
  2. Explain why an affine grid lets a layer compute with integers while a K-means codebook does not.تشرح لماذا تسمح شبكة affine للطبقة بأن تحسب بالأعداد الصحيحة (integers)، بينما لا يسمح بذلك codebook الخاص بـ K-means.
  3. Quantize a value with q = clamp(round(r / S) + Z, q_min, q_max) and read its 2-bit two's complement code.تكمّم قيمة باستخدام q = clamp(round(r / S) + Z, q_min, q_max) وتقرأ رمزها بطول 2 بت بصيغة two's complement.
  4. Reconstruct weights with S(q - Z), compute the error matrix, and check every error against the S/2 bound.تعيد بناء الأوزان باستخدام S(q - Z)، وتحسب مصفوفة الخطأ، وتتحقق من أن كل خطأ لا يتجاوز الحد S/2.

Take the 4×4 weight matrix from last lecture and quantize it two ways to 2 bits. With K-means, each weight becomes an index between 0 and 3, and a four-entry table says index 3 means 2.00, index 2 means 1.50, and so on. With linear quantization, each weight becomes a signed code between -2 and 1, and one formula, (q - (-1)) × 1.07, turns any code back into a real number. Both versions use 2 bits per weight. The difference shows up only when the layer runs.

خذ مصفوفة الأوزان 4×4 من المحاضرة السابقة وكمّمها بطريقتين إلى 2 بت. مع K-means، يصبح كل وزن فهرسًا بين 0 و3، وجدول من أربعة مدخلات يقول إن الفهرس 3 يعني 2.00، والفهرس 2 يعني 1.50، وهكذا. ومع التكميم الخطي، يصبح كل وزن رمزًا بإشارة (signed) بين -2 و1، وصيغة واحدة هي (q - (-1)) × 1.07 تعيد أي رمز إلى عدد حقيقي. كلا النسختين تستخدم 2 بت لكل وزن. الفرق لا يظهر إلا عندما تعمل الطبقة.

The table that frames the lectureالجدول الذي يؤطّر المحاضرة

Slide 2 compares the schemes on two rows: what sits in memory, and what kind of arithmetic the processor performs. Learn it as a grid, because it is an easy short-answer question.

تقارن الشريحة 2 بين الطرق على صفّين: ما الذي يقبع في الذاكرة، وأي نوع من الحساب ينفّذه المعالج. احفظه على شكل شبكة، لأنه سؤال سهل من نوع الإجابة القصيرة.

SchemeالطريقةStorageالتخزينComputationالحساب
FP32 (no quantization)FP32Floating-point weightsأعداد عشرية (floating-point) للأوزانFloating-point arithmeticحساب بالأعداد العشرية (floating-point)
K-means-basedK-meansInteger indices plus a floating-point codebookفهارس صحيحة (integer indices) مع codebook من الأعداد العشريةFloating-point arithmeticحساب بالأعداد العشرية (floating-point)
Linearخطي (Linear)Integer weights (plus one S and one Z per tensor)أوزان صحيحة (مع S واحدة وZ واحدة لكل tensor)Integer arithmeticحساب بالأعداد الصحيحة (integer)
Binary/TernaryBinary/TernaryCovered in a later lectureيُشرح في محاضرة لاحقةCovered in a later lectureيُشرح في محاضرة لاحقة
Storage and computation for each quantization scheme (slide 2)التخزين والحساب لكل طريقة تكميم (الشريحة 2)

K-means-based quantization wins on storage but not on compute. Before a multiply can happen, every index must be looked up in the Codebook, and what comes out is a float. The multiply-accumulate (MAC) that follows is a floating-point MAC, exactly as in the original model. K-means saves memory and memory traffic, and that is all it saves.

K-means quantization يتفوق في التخزين لكن ليس في الحساب. قبل أن يحدث أي ضرب، يجب البحث عن كل فهرس في الـ codebook، وما يخرج من هذا البحث عدد عشري (float). وعملية الضرب والجمع التراكمي (MAC) التي تليه هي MAC عشرية، تمامًا كما في النموذج الأصلي. K-means يوفّر الذاكرة وحركة البيانات من الذاكرة وإليها، وهذا كل ما يوفّره.

Linear quantization changes the second row too. The reason is algebraic. Every real value in a tensor is written as r = S(q - Z) with the same Scale (S) S and the same Zero point (Z) Z. In a dot product of weights and inputs, the common scale factors out of the whole sum:

التكميم الخطي يغيّر الصف الثاني أيضًا. والسبب جبري. كل قيمة حقيقية في الـ tensor تُكتب على الصورة r = S(q - Z) بنفس الـ scale S ونفس الـ zero point Z. وفي حاصل الضرب النقطي (dot product) بين الأوزان والمدخلات، يخرج الـ scale المشترك عاملًا من المجموع كله:

∑krW,k rX,k=SWSX∑k(qW,k−ZW)(qX,k−ZX)\sum_k r_{W,k}\, r_{X,k} = S_W S_X \sum_k (q_{W,k} - Z_W)(q_{X,k} - Z_X)
One scale per tensor pulls out of the sum, leaving integer arithmetic insideمقياس واحد لكل موتر يخرج من المجموع، فيبقى الحساب بالأعداد الصحيحة في الداخل

Everything inside the sum is integer subtraction, multiplication and addition. The floats S_W S_X are applied once per output, not once per MAC, and part 3 shows that even that single rescale can be done as an integer multiply and a bit shift. That is what makes Integer-only inference possible. A codebook gives no such shortcut: its centroids are arbitrary floats like 2.00, 1.50, 0.00 and -1.00, with no common factor and offset to pull out.

كل ما داخل المجموع عمليات طرح وضرب وجمع على أعداد صحيحة. أما القيم العشرية S_W S_X فتُطبَّق مرة واحدة لكل مخرج، لا مرة لكل MAC، والجزء 3 يبيّن أن حتى إعادة التحجيم (rescale) الوحيدة هذه يمكن تنفيذها بضرب صحيح وإزاحة بتات (bit shift). وهذا ما يجعل integer-only inference ممكنًا. أما الـ codebook فلا يقدّم اختصارًا كهذا: مراكزه أعداد عشرية عشوائية مثل 2.00 و1.50 و0.00 و-1.00، ليس لها عامل مشترك ولا إزاحة (offset) مشتركة يمكن إخراجهما.

Both schemes store 2-bit codes. K-means turns them into floats before the MAC; linear keeps the MAC in integersكلتا الطريقتين تخزّن رموزًا بطول 2 بت. K-means يحوّلها إلى أعداد عشرية قبل الـ MAC، بينما التكميم الخطي يُبقي الـ MAC في الأعداد الصحيحة
QuestionالسؤالK-means-basedK-meansLinearالخطي (Linear)
How a code becomes a real valueكيف يتحول الرمز إلى قيمة حقيقيةTable lookup: r = codebook[i]بحث في جدول: r = codebook[i]Formula: r = S(q - Z)صيغة: r = S(q - Z)
Where the levels sitأين تقع المستوياتAnywhere: centroids follow the dataفي أي مكان: المراكز (centroids) تتبع البياناتOn a uniform grid of step Sعلى شبكة منتظمة خطوتها S
Extra storageالتخزين الإضافيOne float per centroidعدد عشري (float) واحد لكل مركزOne float S and one integer Zعدد عشري واحد S وعدد صحيح واحد Z
Multiply-accumulateالضرب والجمع التراكمي (MAC)Floating-point MACs on looked-up valuesعمليات MAC عشرية على القيم المستخرجة من الجدولInteger MACs on the codes, one rescale at the endعمليات MAC صحيحة على الرموز، وإعادة تحجيم (rescale) واحدة في النهاية
Needs a floating-point unit at runtimeيحتاج وحدة أعداد عشرية (floating-point unit) وقت التشغيلYesنعمNo, the rescale can be fixed-pointلا، يمكن تنفيذ إعادة التحجيم بالفاصلة الثابتة (fixed-point)
Best atيتفوق فيAccuracy at very low bit widths for storageالدقة عند أعداد بتات منخفضة جدًا لأجل التخزينSpeed and energy on integer hardwareالسرعة والطاقة على العتاد الصحيح (integer hardware)
K-means versus linear quantizationتكميم K-means مقابل التكميم الخطي

Storage also differs a little, even at the same bit width. For the 16 example weights:

ويختلف التخزين قليلًا أيضًا، حتى عند نفس عدد البتات (bit width). بالنسبة لأوزان المثال الـ 16:

SchemeالطريقةCodesالرموزSide informationالمعلومات الجانبيةTotalالمجموع
FP32FP3216 × 32 = 512 bits16 × 32 = 512 bitsnothingلا شيء512 bits512 bits
K-means, 4 clustersK-means بأربعة عناقيد (clusters)16 × 2 = 32 bits16 × 2 = 32 bits4 × 32 = 128 bits (codebook)4 × 32 = 128 bits (codebook)160 bits160 bits
Linear, 2 bitsخطي (Linear) بطول 2 بت16 × 2 = 32 bits16 × 2 = 32 bits32 bits (S) + 2 bits (Z)32 bits (S) + 2 bits (Z)66 bits66 bits
Bits needed for the 4×4 example matrixعدد البتات اللازمة لمصفوفة المثال 4×4

For a real layer with millions of weights the side information is negligible in both schemes, so the storage row is roughly a tie. The computation row decides the matter. Integer adders and multipliers cost far less energy and silicon area than floating-point ones (recall the energy table from Lecture 05-1), and many microcontrollers and NPUs have no floating-point unit at all.

في طبقة حقيقية فيها ملايين الأوزان تكون المعلومات الجانبية (side information) مهملة في الطريقتين، لذا يكون صف التخزين متعادلًا تقريبًا. صف الحساب هو الذي يحسم الأمر. وحدات الجمع والضرب الصحيحة تستهلك طاقة ومساحة سيليكون أقل بكثير من نظيراتها العشرية (تذكّر جدول الطاقة من المحاضرة 05-1)، وكثير من المتحكمات الدقيقة (microcontrollers) ووحدات NPU لا تحتوي على وحدة أعداد عشرية (floating-point unit) أصلًا.

Quick checkاختبار سريع

A K-means quantized layer must run on a microcontroller with no floating-point unit. What is the problem?طبقة مكمّمة بـ K-means يجب أن تعمل على متحكم دقيق (microcontroller) لا يحتوي على وحدة أعداد عشرية (floating-point unit). ما المشكلة؟

Recall

Without looking, fill in the storage and computation cells for FP32, K-means and linear quantization.دون النظر، املأ خانات التخزين والحساب لكل من FP32 وK-means والتكميم الخطي.

FP32 stores floating-point weights and computes in floating point. K-means stores integer weights (indices) plus a floating-point codebook, and still computes in floating point. Linear stores integer weights and computes with integer arithmetic. Binary/Ternary is left for a later lecture.
FP32 يخزّن أوزانًا عشرية ويحسب بالأعداد العشرية. K-means يخزّن أوزانًا صحيحة (فهارس) مع codebook عشري، وما زال يحسب بالأعداد العشرية. التكميم الخطي يخزّن أوزانًا صحيحة ويحسب بحساب الأعداد الصحيحة. أما Binary/Ternary فمتروك لمحاضرة لاحقة.

Start with one weight, 1.48, and a ruler whose marks are 1.07 apart. Divide by the mark spacing: 1.48 / 1.07 = 1.383. The nearest whole mark is 1. Now shift every mark number down by one so that the mark sitting on real zero is called -1. The weight lands on mark 1 + (-1) = 0. That is the whole of the forward step.

ابدأ بوزن واحد، 1.48، ومسطرة تفصل بين علاماتها مسافة 1.07. اقسم على المسافة بين العلامات: 1.48 / 1.07 = 1.383. أقرب علامة صحيحة هي 1. الآن أنقص رقم كل علامة بواحد بحيث تُسمّى العلامة الواقعة على الصفر الحقيقي -1. يقع الوزن على العلامة 1 + (-1) = 0. وهذه هي الخطوة الأمامية بكاملها.

The affine mappingالتحويل الـ affine

Linear quantization ties every integer code q to a real value r with an Affine mapping: a multiply by a scale plus a fixed offset.

يربط التكميم الخطي كل رمز صحيح q بقيمة حقيقية r عبر affine mapping: ضرب في scale مع إزاحة (offset) ثابتة.

r=S (q−Z)r = S\,(q - Z)
The affine mapping from integer code to real valueالتحويل الأفيني من الرمز الصحيح إلى القيمة الحقيقية
  • Scale (S) S is a floating-point number: the real distance between two neighbouring codes. In the example S = 1.07.الـ scale S عدد عشري (floating-point): هو المسافة الحقيقية بين رمزين متجاورين. في المثال S = 1.07.
  • Zero point (Z) Z is an integer of the same type as the codes: the code that means real 0, since S(Z - Z) = 0 for any S. In the example Z = -1; part 2 derives it as round(q_min - r_min / S) = round(-2 + 1.08 / 1.07) = round(-0.99) = -1.الـ zero point Z عدد صحيح من نفس نوع الرموز: هو الرمز الذي يعني الصفر الحقيقي 0، لأن S(Z - Z) = 0 لأي قيمة S. في المثال Z = -1، والجزء 2 يشتقّه على أنه round(q_min - r_min / S) = round(-2 + 1.08 / 1.07) = round(-0.99) = -1.
  • The codes are signed Bit width (N) integers. With N = 2, q_min and q_max are -2 and 1, so there are only 4 levels.الرموز أعداد صحيحة بإشارة (signed) بعدد بتات يحدده الـ bit width. عندما N = 2، تكون قيمتا q_min, q_max هما -2 و1، فلا يوجد إلا 4 مستويات.

The mapping is called affine rather than linear in the strict sense because of the offset Z. Its job is to give real zero an exact code. Zero padding, ReLU outputs and pruned weights are all exactly zero, and Jacob et al. require that they quantize with no error at all. Part 2 shows how to derive S and Z from r_min and r_max. Here, take them as given.

يُسمّى هذا التحويل affine وليس خطيًا (linear) بالمعنى الدقيق بسبب الإزاحة Z. مهمتها أن تعطي الصفر الحقيقي رمزًا دقيقًا (exact code). الحشو بالأصفار (zero padding) ومخرجات ReLU والأوزان المقلّمة (pruned) كلها أصفار تامة، وJacob et al. يشترطون أن تُكمَّم دون أي خطأ على الإطلاق. الجزء 2 يبيّن كيف نشتق S وZ من r_min, r_max. هنا، اعتبرهما معطيين.

Each 2-bit code maps to one real level; the zero point -1 maps exactly to real 0كل رمز بطول 2 بت يقابل مستوى حقيقيًا واحدًا، والـ zero point وقيمته -1 يقابل الصفر الحقيقي 0 بدقة تامة

The forward formula the slides leave outالصيغة الأمامية التي تتجاهلها الشرائح

Slide 4 shows the weights turning into codes, but it never writes down how. Inverting r = S(q - Z) gives q = r / S + Z, which is usually not a whole number. So round it, and clamp anything that falls outside the integer range:

تعرض الشريحة 4 الأوزان وهي تتحول إلى رموز، لكنها لا تكتب أبدًا كيف يحدث ذلك. عكس الصيغة r = S(q - Z) يعطي q = r / S + Z، وهي في العادة ليست عددًا صحيحًا. لذا نقرّبها (round)، ونقصّ (clamp) أي قيمة تقع خارج مدى الأعداد الصحيحة:

q=clamp⁡ ⁣(round⁡ ⁣(rS)+Z,  qmin⁡,  qmax⁡)q = \operatorname{clamp}\!\left(\operatorname{round}\!\left(\tfrac{r}{S}\right) + Z,\; q_{\min},\; q_{\max}\right)
Quantize: the forward step that produces each stored codeالتكميم: الخطوة الأمامية التي تنتج كل رمز مخزن

Rounding r / S before adding Z works because Z is already an integer, and it keeps real 0 landing exactly on code Z. This is the form given in the Nagel et al. white paper on neural network quantization.

تقريب r / S قبل إضافة Z يصحّ لأن Z عدد صحيح أصلًا، وهو يضمن أن يقع الصفر الحقيقي 0 تمامًا على الرمز Z. هذه هي الصيغة الواردة في الورقة البيضاء (white paper) لـ Nagel et al. عن تكميم الشبكات العصبية.

Worked exampleمثال محلول

Quantize three weights with S = 1.07 and Z = -1كمّم ثلاثة أوزان باستخدام S = 1.07 وZ = -1

  1. r = 1.48r = 1.48

    1.48 / 1.07 = 1.383, which rounds to 1. Add Z: 1 + (-1) = 0. Inside [-2, 1], so q = 0, stored as binary 00.
    1.48 / 1.07 = 1.383، ويُقرَّب إلى 1. أضف Z: 1 + (-1) = 0. القيمة داخل [-2, 1]، إذن q = 0، ويُخزَّن بالثنائي 00.
  2. r = -0.98r = -0.98

    -0.98 / 1.07 = -0.916, which rounds to -1. Add Z: -2. Stored as 10.
    -0.98 / 1.07 = -0.916، ويُقرَّب إلى -1. أضف Z: -2. ويُخزَّن على أنه 10.
  3. r = 2.12, the largest weightr = 2.12، أكبر وزن

    2.12 / 1.07 = 1.981, which rounds to 2. Add Z: 1, exactly q_max. Stored as 01. No clamping needed.
    2.12 / 1.07 = 1.981، ويُقرَّب إلى 2. أضف Z: 1، وهي بالضبط q_max. ويُخزَّن على أنه 01. لا حاجة إلى القصّ (clamping).
  4. Resultالنتيجة

    Doing this for all 16 weights gives the code matrix on slide 4: [[1, -2, 0, -1], [-1, -1, -2, 1], [-2, 1, -1, -2], [1, -1, 0, 0]].
    تطبيق ذلك على الأوزان الـ 16 كلها يعطي مصفوفة الرموز في الشريحة 4: [[1, -2, 0, -1], [-1, -1, -2, 1], [-2, 1, -1, -2], [1, -1, 0, 0]].

Reading the 2-bit codesقراءة الرموز بطول 2 بت

The codes are stored in Two's complement. With two bits the leading bit carries weight -2 and the trailing bit carries +1, which gives the table on slide 4. The slides colour each level: blue for 1, pink for 0, orange for -1, green for -2. The simulator below also gives each of the four levels its own colour, using the site palette, with a legend under the matrices.

تُخزَّن الرموز بصيغة two's complement. مع بتين، يحمل البت الأول وزن -2 ويحمل البت الأخير وزن +1، وهذا يعطي الجدول في الشريحة 4. تلوّن الشرائح كل مستوى: الأزرق لـ 1، والوردي لـ 0، والبرتقالي لـ -1، والأخضر لـ -2. والمحاكي أدناه يعطي كذلك كل مستوى من المستويات الأربعة لونه الخاص، باستخدام ألوان الموقع، مع مفتاح ألوان تحت المصفوفات.

BinaryالثنائيCode qالرمز qReal value S(q - Z)القيمة الحقيقية S(q - Z)
010111(1 + 1) × 1.07 = 2.14(1 + 1) × 1.07 = 2.14
000000(0 + 1) × 1.07 = 1.07(0 + 1) × 1.07 = 1.07
1111-1-1(-1 + 1) × 1.07 = 0(-1 + 1) × 1.07 = 0
1010-2-2(-2 + 1) × 1.07 = -1.07(-2 + 1) × 1.07 = -1.07
2-bit two's complement codes and the real value each one stands forرموز two's complement بطول 2 بت والقيمة الحقيقية التي يمثّلها كل منها

Recall

Quantize r = -0.14 and r = 1.87 with S = 1.07 and Z = -1. Give the codes in decimal and binary.كمّم r = -0.14 وr = 1.87 باستخدام S = 1.07 وZ = -1. أعطِ الرموز بالنظامين العشري والثنائي.

-0.14 / 1.07 = -0.131 rounds to 0, so q = -1, binary 11. 1.87 / 1.07 = 1.748 rounds to 2, so q = 1, binary 01.
-0.14 / 1.07 = -0.131 يُقرَّب إلى 0، إذن q = -1، وبالثنائي 11. و1.87 / 1.07 = 1.748 يُقرَّب إلى 2، إذن q = 1، وبالثنائي 01.

Recall

Why must the zero point Z be an integer while the scale S is a float?لماذا يجب أن يكون الـ zero point Z عددًا صحيحًا بينما الـ scale S عدد عشري؟

Z is itself one of the codes: the integer that stands for real 0. Keeping it an integer means zero is represented exactly and the q - Z terms stay integer arithmetic. S is a real step size, so it must be able to take any positive value.
Z هو نفسه أحد الرموز: العدد الصحيح الذي يمثّل الصفر الحقيقي 0. إبقاؤه عددًا صحيحًا يعني أن الصفر يُمثَّل بدقة، وأن حدود q - Z تبقى حسابًا صحيحًا. أما S فهو حجم خطوة حقيقي، لذا يجب أن يستطيع أخذ أي قيمة موجبة.

Slide 4 turns 16 weights into codes, then back into numbers. What comes back has only four distinct values: -1.07, 0, 1.07 and 2.14. The weights 1.48, 1.53 and 1.49 all come back as 1.07. That collapse is the price of using 2 bits.

تحوّل الشريحة 4 الأوزان الـ 16 إلى رموز، ثم تعيدها إلى أعداد. ما يعود ليس فيه إلا أربع قيم مختلفة: -1.07 و0 و1.07 و2.14. الأوزان 1.48 و1.53 و1.49 كلها تعود على أنها 1.07. هذا الانهيار هو ثمن استخدام 2 بت.

Reconstruct with the affine mapإعادة البناء بالتحويل الـ affine

The Reconstructed weights are just S(q - Z) applied to each Quantized value (q). With Z = -1, that is (q + 1) × 1.07:

الـ reconstructed weights ما هي إلا S(q - Z) مطبّقة على كل quantized value. ومع Z = -1، تصبح (q + 1) × 1.07:

  • q = 1 gives 2.14, and q = 0 gives 1.07.q = 1 يعطي 2.14، وq = 0 يعطي 1.07.
  • q = -1 gives 0 exactly, and q = -2 gives -1.07.q = -1 يعطي 0 بالضبط، وq = -2 يعطي -1.07.

The reconstructed matrix is [[2.14, -1.07, 1.07, 0], [0, 0, -1.07, 2.14], [-1.07, 2.14, 0, -1.07], [2.14, 0, 1.07, 1.07]]. In a real integer pipeline this matrix is never built in memory. It is the meaning of the codes, which is what we compare against the original to measure the damage.

المصفوفة المعاد بناؤها هي [[2.14, -1.07, 1.07, 0], [0, 0, -1.07, 2.14], [-1.07, 2.14, 0, -1.07], [2.14, 0, 1.07, 1.07]]. في خط معالجة صحيح حقيقي (integer pipeline) لا تُبنى هذه المصفوفة في الذاكرة أبدًا. إنها معنى الرموز، وهي ما نقارنه بالأصل لقياس حجم الضرر.

Quantization error and the S/2 boundخطأ التكميم والحد S/2

The Quantization error of each cell is the original minus the reconstruction, e = r - S(q - Z):

الـ quantization error لكل خانة هو القيمة الأصلية ناقص القيمة المعاد بناؤها، e = r - S(q - Z):

E=[−0.050.090.410.090.05−0.14−0.01−0.020.16−0.2200.04−0.2700.460.42]E = \begin{bmatrix} -0.05 & 0.09 & 0.41 & 0.09 \\ 0.05 & -0.14 & -0.01 & -0.02 \\ 0.16 & -0.22 & 0 & 0.04 \\ -0.27 & 0 & 0.46 & 0.42 \end{bmatrix}
Error matrix r - S(q - Z), matching slide 4مصفوفة الخطأ r - S(q - Z)، مطابقةً للشريحة 4

Round-to-nearest picks the closest level, and neighbouring levels are S apart, so a value that is not clipped is never more than half a step from its level. For every such value, |e| ≤ S/2 = 0.535. The worst cell is 1.53: it divides to 1.430, rounds down to code 0, and comes back as 1.07, an error of 0.46. That is close to the bound, because 1.53 sits near the midpoint 1.605 between 1.07 and 2.14. The two zeros have error 0, as promised by the exact zero point.

التقريب إلى الأقرب (round-to-nearest) يختار أقرب مستوى، والمستويات المتجاورة تفصل بينها مسافة S، لذا فالقيمة التي لم تُقصّ (not clipped) لا تبعد أبدًا عن مستواها أكثر من نصف خطوة. لكل قيمة كهذه، |e| ≤ S/2 = 0.535. أسوأ خانة هي 1.53: قسمتها تعطي 1.430، فتُقرَّب للأسفل إلى الرمز 0، وتعود على أنها 1.07، بخطأ قدره 0.46. وهذا قريب من الحد، لأن 1.53 تقع قرب نقطة المنتصف 1.605 بين 1.07 و2.14. والصفران خطؤهما 0، كما يضمن الـ zero point الدقيق.

∣e∣≤S2for S(qmin⁡−Z)−S2≤r≤S(qmax⁡−Z)+S2|e| \le \tfrac{S}{2} \quad \text{for } S(q_{\min} - Z) - \tfrac{S}{2} \le r \le S(q_{\max} - Z) + \tfrac{S}{2}
Rounding error bound for values that are not clippedحد خطأ التقريب للقيم غير المقصوصة

More than half a step beyond either end level, clamping takes over and the error grows with the distance. Nagel et al. call this clipping error, as opposed to rounding error. A wider range means less clipping but a larger S and more rounding error. Choosing the range is the central trade-off in linear quantization.

عندما تبتعد القيمة أكثر من نصف خطوة خارج أيّ من المستويين الطرفيين، يتولى القصّ (clamping) الأمر ويكبر الخطأ مع المسافة. يسمّي Nagel et al. هذا خطأ القصّ (clipping error)، في مقابل خطأ التقريب (rounding error). المدى الأوسع يعني قصًّا أقل، لكنه يعني S أكبر وخطأ تقريب أكبر. اختيار المدى هو المفاضلة (trade-off) المحورية في التكميم الخطي.

Each weight snaps to the nearest of four levels; the error bars are the quantization error, all shorter than S/2كل وزن ينجذب إلى أقرب المستويات الأربعة، وأشرطة الخطأ هي خطأ التكميم، وكلها أقصر من S/2

Why the top level is 2.14 and not 2.12لماذا المستوى الأعلى 2.14 وليس 2.12

The largest weight is r_max = 2.12, yet the top level is 2.14, and the bottom level -1.07 sits just above r_min = -1.08. Two roundings cause this. The exact scale is 3.20 / 3 = 1.0667, which the slides round to 1.07. The exact offset would be -0.99, but the zero point must be an integer so that real zero stays exact, so it is rounded to -1. Together these shift and stretch the grid slightly, so it covers [-1.07, 2.14] instead of [-1.08, 2.12]. The cost is tiny: -1.08 still reconstructs with an error of only -0.01, well within S/2.

أكبر وزن هو r_max = 2.12، ومع ذلك فالمستوى الأعلى هو 2.14، والمستوى الأدنى -1.07 يقع فوق r_min = -1.08 بقليل. السبب عمليتا تقريب. الـ scale الدقيق هو 3.20 / 3 = 1.0667، وتقرّبه الشرائح إلى 1.07. والإزاحة الدقيقة كانت ستكون -0.99، لكن الـ zero point يجب أن يكون عددًا صحيحًا ليبقى الصفر الحقيقي دقيقًا، لذا يُقرَّب إلى -1. معًا، تُزيح هاتان العمليتان الشبكة وتمطّانها قليلًا، فتغطي [-1.07, 2.14] بدلًا من [-1.08, 2.12]. والكلفة ضئيلة: -1.08 ما زالت تُعاد بناؤها بخطأ قدره -0.01 فقط، وهو أقل بكثير من S/2.

Try it at other bit widthsجرّبه بأعداد بتات أخرى

The simulator below runs the same matrix through the same formulas. At 2 bits it reproduces slide 4 cell for cell. Slide up to 8 bits, the INT8 format used in deployment, and watch S shrink by roughly half per extra bit, the levels multiply, and the error heatmap fade.

المحاكي أدناه يمرّر المصفوفة نفسها عبر الصيغ نفسها. عند 2 بت يعيد إنتاج الشريحة 4 خانةً بخانة. حرّك المؤشر حتى 8 بت، وهي صيغة INT8 المستخدمة في النشر (deployment)، وراقب S وهي تتقلص إلى النصف تقريبًا مع كل بت إضافي، والمستويات وهي تتضاعف، وخريطة الخطأ الحرارية (heatmap) وهي تبهت.

SimulatorمحاكيLinear quantizer: one 4x4 weight matrix at any bit widthالمكمِّم الخطي: مصفوفة أوزان 4x4 واحدة بأي عرض بتات
Weights rFP32, coloured by assigned levelFP32، ملوّنة حسب المستوى المُسنَد
2.09
-0.98
1.48
0.09
0.05
-0.14
-1.08
2.12
-0.91
1.92
0
-1.03
1.87
0
1.53
1.49
Codes q2-bit signed int in [-2, 1]عدد صحيح بإشارة بطول 2 بت ضمن [-2, 1]
1
-2
0
-1
-1
-1
-2
1
-2
1
-1
-2
1
-1
0
0
Reconstruction S(q - Z)FP32, only on the grid of step SFP32، على شبكة خطوتها S فقط
2.14
-1.07
1.07
0
0
0
-1.07
2.14
-1.07
2.14
0
-1.07
2.14
0
1.07
1.07
Error r - S(q - Z)shade grows toward |e| = S/2يشتد اللون كلما اقترب من |e| = S/2
-0.05
0.09
0.41
0.09
0.05
-0.14
-0.01
-0.02
0.16
-0.22
0
0.04
-0.27
0
0.46
0.42
01 = 100 = 011 = -110 = -2
Scale S1.07FP32(r_max - r_min) / (q_max - q_min)
Zero point Z-1int2round(q_min - r_min / S)
Max |error|0.460S/2 = 0.535within the S/2 boundضمن الحد S/2
Weight storage32bits vs 512plus one S and one Z for the tensorيُضاف إليها S واحد وZ واحد للموتر كله

Each code is q = clamp(round(r / S) + Z, q_min, q_max). The range is [-1.08, 2.12], and S is rounded up to three significant figures, which is how the slide gets 1.07 at 2 bits. Clipped values this run: 0. Hover a cell for its exact numbers.يُحسب كل رمز بالعلاقة q = clamp(round(r / S) + Z, q_min, q_max). المدى هو [-1.08, 2.12]، وتُقرَّب S إلى الأعلى لثلاثة أرقام معنوية، وبهذا تحصل الشريحة على 1.07 عند 2 بت. عدد القيم المقصوصة في هذا التشغيل: 0. مرّر المؤشر فوق أي خلية لترى أرقامها الدقيقة.

Quick checkاختبار سريع

With S = 1.07 and Z = -1, what does the weight 1.53 reconstruct to?مع S = 1.07 وZ = -1، ما القيمة التي يُعاد بناء الوزن 1.53 إليها؟

Quick checkاختبار سريع

What is the largest possible error for an in-range weight when S = 1.07?ما أكبر خطأ ممكن لوزن داخل المدى عندما S = 1.07؟

Recall

Why does the reconstructed matrix contain only four distinct values, and what are they?لماذا لا تحتوي المصفوفة المعاد بناؤها إلا على أربع قيم مختلفة، وما هي؟

With 2 bits there are only 4 codes, -2 to 1, and each maps to exactly one value S(q - Z): -1.07, 0, 1.07 and 2.14.
مع 2 بت لا يوجد إلا 4 رموز، من -2 إلى 1، وكل رمز يقابل قيمة واحدة بالضبط S(q - Z): -1.07 و0 و1.07 و2.14.

Recapالخلاصة

If you remember nothing elseإن لم تتذكر شيئاً آخر فتذكّر هذا

  • FP32 stores floats and computes in floats. K-means stores integer indices plus a float codebook, but still computes in floats. Linear quantization stores integers and computes with integers.FP32 يخزّن أعدادًا عشرية (floats) ويحسب بها. K-means يخزّن فهارس صحيحة (integer indices) مع codebook من الأعداد العشرية، لكنه ما زال يحسب بالأعداد العشرية. التكميم الخطي يخزّن أعدادًا صحيحة ويحسب بالأعداد الصحيحة.
  • Linear quantization is an affine map r = S(q - Z): S is a float step size, Z is an integer code that stands for real 0.التكميم الخطي هو تحويل affine صيغته r = S(q - Z): حيث S حجم خطوة عشري (float)، وZ رمز صحيح يمثّل الصفر الحقيقي.
  • Because one S and one Z serve a whole tensor, S factors out of every dot product, leaving pure integer multiply-accumulates. A codebook of arbitrary centroids has no such common factor.لأن قيمة S واحدة وقيمة Z واحدة تخدمان الـ tensor كله، تخرج S عاملًا مشتركًا من كل حاصل ضرب نقطي (dot product)، فلا يبقى إلا عمليات ضرب وجمع تراكمي (MAC) صحيحة خالصة. أما codebook من مراكز (centroids) عشوائية فليس له عامل مشترك كهذا.
  • Forward: q = clamp(round(r / S) + Z, q_min, q_max). Backward: r ≈ S(q - Z). The slides only show the backward direction.الاتجاه الأمامي: q = clamp(round(r / S) + Z, q_min, q_max). الاتجاه العكسي: r ≈ S(q - Z). الشرائح تعرض الاتجاه العكسي فقط.
  • Running example: 2-bit codes in [-2, 1], Z = -1, S = 1.07. The 16 weights collapse onto four values: -1.07, 0, 1.07 and 2.14.المثال المستمر: رموز بطول 2 بت في المدى [-2, 1]، وZ = -1، وS = 1.07. تنهار الأوزان الـ 16 إلى أربع قيم فقط: -1.07 و0 و1.07 و2.14.
  • Error = r - S(q - Z). For any in-range value it is at most S/2 = 0.535 in size. The worst cell in the example is 1.53, with error 0.46.الخطأ = r - S(q - Z). لأي قيمة داخل المدى لا يتجاوز حجمه S/2 = 0.535. أسوأ خانة في المثال هي 1.53، وخطؤها 0.46.
  • The top level 2.14 sits above r_max = 2.12 because Z was rounded to an integer (so 0 stays exact) and S was rounded up to 1.07.المستوى الأعلى 2.14 يقع فوق r_max = 2.12 لأن Z قُرّبت إلى عدد صحيح (ليبقى الصفر دقيقًا)، ولأن S قُرّبت للأعلى إلى 1.07.

Sourcesالمصادر