Majid Al-RaimiQuantization (I)التكميم (Quantization) الجزء الأول

COE 592Lecture 5.1المحاضرة 5.1

Quantization (I)التكميم (Quantization) الجزء الأول

Why fewer bits save energy, how integers, fixed point and IEEE floating point (FP32, FP16, BF16, FP8, FP4) encode numbers, what quantization is, and K-means weight quantization with codebooks, centroid fine-tuning, Huffman coding and the Deep Compression pipeline.لماذا توفر البتات الأقل الطاقة، وكيف تمثل الأعداد الصحيحة والفاصلة الثابتة والفاصلة العائمة وفق IEEE (FP32 وFP16 وBF16 وFP8 وFP4) الأعداد، وما التكميم، وتكميم الأوزان بـK-means مع دفاتر الترميز (codebooks) والضبط الدقيق للمراكز (centroids) وترميز Huffman وخط عمل Deep Compression.

Partsالأجزاء
8
Conceptsالمفاهيم
29
Slidesالشرائح
45
Readingمدة القراءة
174 min174 دقيقة
Understoodما فهمته
0/29 conceptsمفاهيم
Read the full guideاقرأ الشرح الكاملEvery part on one long page: 8 parts, 29 concepts, about 174 min.كل الأجزاء في صفحة واحدة طويلة: 8 أجزاء، و29 مفهومًا، في نحو 174 دقيقة.

AOverviewنظرة عامة

This lecture asks one question and spends 45 slides answering it: how few bits can a neural network weight use before the network stops working? It starts from the energy bill of arithmetic and memory, rebuilds every number format you will meet in embedded ML from the bits up, and ends with K-means weight quantization and the Deep Compression pipeline that shrinks AlexNet from 240 MB to 6.9 MB with no accuracy loss.

تطرح هذه المحاضرة سؤالًا واحدًا وتقضي 45 شريحة في الإجابة عنه: ما أقل عدد من البتات يمكن أن يستخدمه وزن (weight) في الشبكة العصبية قبل أن تتوقف الشبكة عن العمل؟ تبدأ من فاتورة الطاقة للعمليات الحسابية والذاكرة، ثم تبني من مستوى البت كل صيغة أرقام ستقابلها في تعلم الآلة المدمج (embedded ML)، وتنتهي بتكميم الأوزان باستخدام K-means وبخط Deep Compression الذي يصغّر AlexNet من 240 MB إلى 6.9 MB دون أي خسارة في الدقة.

As a PhD student you need it for three reasons. The COE 592 midterm asks for exact numbers here: integer ranges, FP32 extreme values, FP16 and BF16 widths, K-means storage ratios and centroid updates, all computed by hand. Any model you deploy on a microcontroller or an accelerator for your research has to pass through a quantization step, and choosing the format is a design decision you must be able to defend. And this lecture sets up lecture 05-2, where linear quantization, r = S(q - Z), replaces float arithmetic with integer arithmetic.

كطالب دكتوراه تحتاجها لثلاثة أسباب. اختبار منتصف الفصل في COE 592 يطلب أرقامًا دقيقة هنا: مدى الأعداد الصحيحة، والقيم القصوى في FP32، وعرض حقول FP16 وBF16، ونسب التخزين في K-means وتحديث المراكز (centroids)، وكلها تُحسب يدويًا. وأي نموذج تنشره على متحكم دقيق (microcontroller) أو مسرّع (accelerator) في بحثك لا بد أن يمر بخطوة تكميم (quantization)، واختيار الصيغة قرار تصميمي يجب أن تستطيع الدفاع عنه. وأخيرًا تمهّد هذه المحاضرة للمحاضرة 05-2، حيث يحل التكميم الخطي (linear quantization)، r = S(q - Z)، محل الحساب العشري بحساب صحيح (integer arithmetic).

From the energy price of bits to what K-means quantization really saves at runtimeمن ثمن البتات في الطاقة إلى ما يوفره تكميم K-means فعلًا وقت التشغيل

The path has six stops. Energy explains why fewer bits matter: an 8-bit integer add costs 30x less than a 32-bit float add, and a DRAM read costs hundreds of times more than either. Formats cover integers, fixed point, FP32 and the low-bit floats FP16, BF16, FP8 and FP4. Quantization defines the mapping onto a discrete set and its error. K-means picks the shared values, stores small indexes plus a codebook, and fine-tunes the centroids. Compression chains pruning, quantization and Huffman coding. Runtime shows the catch: K-means indexes are decoded back to floats, so the arithmetic never gets cheaper.

للمسار ست محطات. الطاقة تشرح لماذا يهم تقليل البتات: جمع عدد صحيح بطول 8-bit أرخص بـ 30x من جمع عدد عشري بطول 32-bit، وقراءة واحدة من DRAM تكلف مئات أضعاف أي منهما. الصيغ تغطي الأعداد الصحيحة، والفاصلة الثابتة (fixed point)، وFP32، والصيغ العشرية منخفضة البتات FP16 وBF16 وFP8 وFP4. التكميم يعرّف التحويل إلى مجموعة منفصلة (discrete set) والخطأ الناتج عنه. K-means يختار القيم المشتركة، ويخزن فهارس (indexes) صغيرة مع كتاب رموز (codebook)، ثم يضبط المراكز بالتدريب الدقيق (fine-tuning). الضغط يربط التقليم (pruning) والتكميم وترميز Huffman في سلسلة واحدة. ووقت التشغيل يكشف المشكلة: فهارس K-means تُفك مرة أخرى إلى أعداد عشرية، لذلك لا يصبح الحساب نفسه أرخص أبدًا.

Success looks likeعلامات النجاح

  • Quote the 45 nm energy table and compute the 30x add and roughly 16x multiply ratios, and explain why DRAM access dominates both.أن تذكر جدول الطاقة عند 45 nm وتحسب نسبة 30x للجمع ونحو 16x للضرب، وتشرح لماذا يطغى الوصول إلى DRAM على كليهما.
  • Write the n-bit range of unsigned, sign-magnitude and two's complement integers, encode -49 by hand, and decode a fixed-point number as an integer times 2^-4.أن تكتب مدى الأعداد الصحيحة ذات n بت في صيغ unsigned وsign-magnitude وtwo's complement، وترمّز -49 يدويًا، وتفك عددًا بفاصلة ثابتة على أنه عدد صحيح مضروب في 2^-4.
  • Encode and decode FP32 values, and derive the smallest subnormal 2^-149, the smallest normal 2^-126 and the largest normal (2 - 2^-23) × 2^127.أن ترمّز قيم FP32 وتفكها، وتشتق أصغر قيمة دون الطبيعية (subnormal) 2^-149، وأصغر قيمة طبيعية 2^-126، وأكبر قيمة طبيعية (2 - 2^-23) × 2^127.
  • Rank FP32, FP16, BF16, FP8 E4M3 and E5M2 by range and precision from their field widths, and list the FP4 value grids.أن ترتب FP32 وFP16 وBF16 وFP8 E4M3 وE5M2 حسب المدى والدقة انطلاقًا من عرض حقولها، وتسرد شبكات القيم في FP4.
  • Quantize a weight matrix with K-means, compute the storage ratio 32M / (NM + 32 × 2^N), and fine-tune the centroids by grouping and summing gradients.أن تكمّم مصفوفة أوزان باستخدام K-means، وتحسب نسبة التخزين 32M / (NM + 32 × 2^N)، وتضبط المراكز بالتدريب الدقيق عن طريق تجميع التدرجات (gradients) حسب الفهرس وجمعها.
  • Explain the Deep Compression pipeline and its 35x to 49x results, and why SqueezeNet plus Deep Compression reaches 510x.أن تشرح خط Deep Compression ونتائجه من 35x إلى 49x، ولماذا يصل SqueezeNet مع Deep Compression إلى 510x.
  • Fill the storage and computation table for floating point, K-means and linear quantization, and say why K-means saves storage but not arithmetic.أن تملأ جدول التخزين والحساب للفاصلة العائمة (floating point) وK-means والتكميم الخطي، وتقول لماذا يوفر K-means في التخزين لا في الحساب.

How to study this lectureكيف تذاكر هذه المحاضرة

  1. Choose your route. Read the full guide in one sitting for the big picture, or take one part at a time when you want depth. Parts 1 to 3 are number formats; parts 4 to 8 are quantization.اختر طريقك. اقرأ الدليل الكامل في جلسة واحدة لترى الصورة الكبيرة، أو خذ جزءًا واحدًا في كل مرة عندما تريد العمق. الأجزاء من 1 إلى 3 عن صيغ الأرقام، والأجزاء من 4 إلى 8 عن التكميم.
  2. Redo every worked example on paper before reading its result. Format questions are won by procedure, and the procedure only sticks when your own hand writes the bits.أعد حل كل مثال محلول على الورق قبل أن تقرأ نتيجته. أسئلة الصيغ تُكسب بالإجراء، والإجراء لا يثبت إلا عندما تكتب البتات بيدك.
  3. Answer each recall prompt in your head before you reveal it, and read every quiz explanation even when you are right.أجب عن كل سؤال استرجاع في ذهنك قبل أن تكشف الإجابة، واقرأ شرح كل سؤال في الاختبار القصير حتى عندما تكون إجابتك صحيحة.
  4. Play with the explorers: flip FP32 bits, switch FP4 formats, change K-means cluster counts. Predict the readout before you click.جرّب الأدوات التفاعلية: اقلب بتات FP32، وبدّل بين صيغ FP4، وغيّر عدد العناقيد (clusters) في K-means. توقّع النتيجة قبل أن تضغط.
  5. Mark a concept as understood only when you could explain it to a classmate without looking. Unmarked concepts show you where to return.لا تعلّم على مفهوم بأنك فهمته إلا عندما تستطيع شرحه لزميل دون أن تنظر. المفاهيم غير المعلّمة تريك أين يجب أن تعود.
  6. Use the reference sheet the night before the exam: it collects every table, formula and slide mistake in one printable page.استخدم ورقة المرجع في الليلة التي تسبق الامتحان: فهي تجمع كل جدول وكل معادلة وكل خطأ في الشرائح في صفحة واحدة قابلة للطباعة.

Sourcesالمصادر

BThe 8 partsالأجزاء الـ8

  1. 01Why fewer bits, integers and fixed pointلماذا بتات أقل، والأعداد الصحيحة والفاصلة الثابتةLower bit-width means cheaper arithmetic and memory traffic. Covers unsigned, sign-magnitude and two's complement integers, then fixed-point numbers as an integer times a power-of-two scale.عرض بت أقل يعني حسابًا وحركة ذاكرة أرخص. يغطي الأعداد الصحيحة بلا إشارة، وبالإشارة والمقدار (sign-magnitude)، والمتمم الثنائي (two's complement)، ثم أعداد الفاصلة الثابتة بوصفها عددًا صحيحًا مضروبًا في مقياس هو قوة للعدد 2.3 concepts3 مفاهيمSlides 1-6الشرائح 1-618 min18 دقيقة
    1. 1.1Why fewer bits: the energy price of arithmetic and memoryلماذا بتات أقل: ثمن الطاقة في الحساب والذاكرة
    2. 1.2Three ways to store an integer in n bitsثلاث طرق لتخزين عدد صحيح في n بت
    3. 1.3Fixed point: an integer with an implicit scaleالـ fixed point: عدد صحيح بمقياس ضمني
  2. 02IEEE 754 FP32: normals, subnormals, zero, infinity and NaNIEEE 754 FP32: القيم العادية وتحت العادية والصفر واللانهاية وNaNHow a 32-bit float splits into sign, 8-bit biased exponent and 23-bit fraction, how to encode a value, why zero and subnormals need a special rule, the smallest and largest representable values, and the exponent-255 codes for infinity and NaN.كيف ينقسم العدد العائم ذو 32 بت إلى إشارة وأس منحاز (biased exponent) من 8 بت وكسر من 23 بت، وكيف نرمّز قيمة، ولماذا يحتاج الصفر والقيم تحت العادية (subnormals) قاعدة خاصة، وأصغر وأكبر القيم القابلة للتمثيل، ورموز الأس 255 للانهاية وNaN.4 concepts4 مفاهيمSlides 7-15الشرائح 7-1524 min24 دقيقة
    1. 2.1Sign, biased exponent and fraction: encoding a number in FP32الإشارة والأس المتحيّز والكسر: ترميز عدد بصيغة FP32
    2. 2.2Zero and subnormals: dropping the implicit 1الصفر والـ subnormals: التخلي عن الـ 1 الضمني
    3. 2.3The edges of the subnormal range and the seam at 2^-126حدود مدى الـ subnormals ونقطة الالتقاء عند 2^-126
    4. 2.4Infinity, NaN and the full FP32 mapاللانهاية (Infinity) وNaN والخريطة الكاملة لصيغة FP32
  3. 03FP16, 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.4 concepts4 مفاهيمSlides 16-20الشرائح 16-2024 min24 دقيقة
    1. 3.1Exponent buys range, fraction buys precision: FP32, FP16 and BF16الأس يشتري المدى والكسر يشتري الدقة: FP32 وFP16 وBF16
    2. 3.2Decoding FP16 and encoding BF16 by handفك ترميز FP16 وترميز BF16 يدويًا
    3. 3.3FP8: E4M3 for the forward pass, E5M2 for gradientsFP8: صيغة E4M3 للمرور الأمامي وE5M2 للتدرجات
    4. 3.4Four bits, four grids: INT4, E1M2, E2M1 and E3M0أربع بتات وأربع شبكات قيم: INT4 وE1M2 وE2M1 وE3M0
  4. 04What quantization is and the three main approachesما التكميم والمناهج الثلاثة الرئيسيةQuantization maps a continuous or large set of values onto a small discrete set, leaving a quantization error. Introduces the three neural network approaches (K-means, linear, binary/ternary) and the storage versus computation table, using the 4x4 running example matrix.التكميم يربط مجموعة قيم متصلة أو كبيرة بمجموعة منفصلة صغيرة، تاركًا خطأ تكميم. يقدم المناهج الثلاثة في الشبكات العصبية (K-means والخطي والثنائي/الثلاثي) وجدول التخزين مقابل الحساب، باستخدام مصفوفة المثال الجاري 4x4.3 concepts3 مفاهيمSlides 21-25الشرائح 21-2518 min18 دقيقة
    1. 4.1Quantization: a large set squeezed into a discrete oneالتكميم: ضغط مجموعة كبيرة من القيم في مجموعة منفصلة
    2. 4.2Three ways to quantize a network, judged by storage and computationثلاث طرق لتكميم الشبكة، تُقارَن بالتخزين والحساب
    3. 4.3The running example: sixteen FP32 weights and the idea of sharingالمثال المستمر: ستة عشر وزنًا بصيغة FP32 وفكرة المشاركة
  5. 05K-means weight quantization: codebook, storage and centroid fine-tuningتكميم الأوزان بـK-means: دفتر الترميز والتخزين وضبط المراكزCluster weights with K-means, store 2-bit cluster indexes plus a 32-bit codebook, compute the compression ratio, and fine-tune centroids by grouping gradients by cluster index and summing them.جمّع الأوزان بـK-means، وخزّن فهارس عناقيد من 2 بت مع دفتر ترميز (codebook) من 32 بت، واحسب نسبة الضغط، واضبط المراكز (centroids) بتجميع التدرجات حسب فهرس العنقود وجمعها.4 concepts4 مفاهيمSlides 26-29الشرائح 26-2924 min24 دقيقة
    1. 5.1Clustering the weights: K-means picks a few shared valuesتجميع الأوزان: K-means يختار بضع قيم مشتركة
    2. 5.2Index map, codebook and the storage billخريطة الفهارس ودفتر الرموز (codebook) وفاتورة التخزين
    3. 5.3Why a shared centroid collects the gradients of all its copiesلماذا يجمع المركز المشترك gradients كل نسخه
    4. 5.4Group, reduce, update: fine-tuning the codebookجمّع، اختزل، حدّث: ضبط دفتر الرموز (fine-tuning)
  6. 06How far can we compress: accuracy curves, weight histograms and bit choiceإلى أي حد نضغط: منحنيات الدقة ومدرجات الأوزان واختيار البتاتReads the AlexNet accuracy versus model-size curves for pruning only, quantization only and both combined, shows weights becoming discrete after quantization and shifting slightly after retraining, and how many bits conv and FC layers need.يقرأ منحنيات الدقة مقابل حجم النموذج في AlexNet للتقليم وحده والتكميم وحده والاثنين معًا، ويبين أن الأوزان تصبح منفصلة بعد التكميم وتنزاح قليلًا بعد إعادة التدريب، وكم بتًا تحتاج طبقات الالتفاف (conv) والطبقات المتصلة كليًا (FC).3 concepts3 مفاهيمSlides 30-36الشرائح 30-3618 min18 دقيقة
    1. 6.1Reading the accuracy curves: pruning and quantization are complementaryقراءة منحنيات الدقة: الـ pruning والـ quantization يكمّل كل منهما الآخر
    2. 6.2What quantization does to the weight histogram, and what retraining changesماذا يفعل الـ quantization بالـ histogram الخاص بالأوزان، وما الذي يغيّره الـ retraining
    3. 6.3How many bits per weight: conv layers need 4, FC layers need 2كم bit لكل وزن: طبقات الـ conv تحتاج 4 وطبقات الـ FC تحتاج 2
  7. 07Huffman coding, the Deep Compression pipeline and SqueezeNetترميز Huffman وخط عمل Deep Compression وSqueezeNetVariable-length Huffman codes exploit the uneven usage of the 32 effective weights. Deep Compression chains pruning, K-means quantization and Huffman coding for 35x to 49x reduction with no accuracy loss, and stacks on SqueezeNet for a 510x smaller model than AlexNet.رموز Huffman متغيرة الطول تستغل الاستخدام غير المتساوي للأوزان الفعلية الـ32. يربط Deep Compression التقليم وتكميم K-means وترميز Huffman لتخفيض من 35x إلى 49x دون فقدان في الدقة، ويُطبَّق فوق SqueezeNet لنموذج أصغر بـ510x من AlexNet.5 concepts5 مفاهيمSlides 37-41الشرائح 37-4130 min30 دقيقة
    1. 7.1Huffman coding: short codes for the weights the network uses mostترميز Huffman: رموز قصيرة للأوزان التي تستخدمها الشبكة أكثر من غيرها
    2. 7.2The Deep Compression pipeline: fewer weights, fewer bits, shorter codesخط معالجة Deep Compression: أوزان أقل، وبتات أقل، ورموز أقصر
    3. 7.3Results on six networks, and why compact networks compress lessالنتائج على ست شبكات، ولماذا تنضغط الشبكات المدمجة أقل
    4. 7.4SqueezeNet's fire module: squeeze the channels before the 3x3 filtersالـ fire module في SqueezeNet: اضغط القنوات قبل مرشحات 3x3
    5. 7.5Deep Compression on SqueezeNet: two orthogonal savings that multiply to 510xDeep Compression على SqueezeNet: توفيران متعامدان يتضاعفان إلى 510x
  8. 08What K-means saves at runtime, and the road to linear quantizationماذا يوفر K-means وقت التشغيل، والطريق إلى التكميم الخطيAt inference the indexes are decoded through the codebook back to float weights, so K-means quantization saves only storage; computation and memory access stay floating point. Linear quantization stores integers and computes in integers, which is the next lecture.عند الاستدلال تُفك الفهارس عبر دفتر الترميز إلى أوزان عائمة، لذا يوفر تكميم K-means التخزين فقط؛ أما الحساب والوصول إلى الذاكرة فيبقيان بالفاصلة العائمة. التكميم الخطي يخزّن أعدادًا صحيحة ويحسب بأعداد صحيحة، وهو موضوع المحاضرة التالية.3 concepts3 مفاهيمSlides 42-45الشرائح 42-4518 min18 دقيقة
    1. 8.1Decode, then compute in float: what K-means really saves at runtimeفك الترميز ثم الحساب بالفاصلة العائمة: ما الذي يوفّره K-means فعلًا وقت التشغيل
    2. 8.2Integer weights and integer arithmetic: the linear quantization previewأوزان صحيحة وحساب بالأعداد الصحيحة: لمحة عن التكميم الخطي
    3. 8.3Where the numbers in this lecture come fromمن أين تأتي أرقام هذه المحاضرة

CGlossary and referenceالمصطلحات والمرجع