<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Quantization on 笔记本子</title><link>https://tianshihao.github.io/tags/quantization/</link><description>Recent content in Quantization on 笔记本子</description><generator>Hugo -- gohugo.io</generator><language>cn</language><copyright>Copyright © 2026</copyright><lastBuildDate>Tue, 25 Aug 2026 13:36:10 +0800</lastBuildDate><atom:link href="https://tianshihao.github.io/tags/quantization/index.xml" rel="self" type="application/rss+xml"/><item><title>Quantization in Depth 1</title><link>https://tianshihao.github.io/posts/quantization-in-depth/</link><pubDate>Tue, 25 Aug 2026 13:36:10 +0800</pubDate><guid>https://tianshihao.github.io/posts/quantization-in-depth/</guid><description>&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Quantization&lt;/strong&gt;: Store the parameters of the model in lower precision&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Knowledge Distillation&lt;/strong&gt;: Train a smaller model (student) using a original model (instructor)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pruning&lt;/strong&gt;: Remove connections (weights) from the model&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;FP32&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sign: 1 bit&lt;/li&gt;
&lt;li&gt;Exponent (range): 8 bits&lt;/li&gt;
&lt;li&gt;Fraction (precision): 23 bits&lt;/li&gt;
&lt;li&gt;Total: 32 bits&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="quantization"&gt;Quantization&lt;/h2&gt;
&lt;p&gt;Quantization refers to the process of mapping a large set to a smaller set of values. For example, mapping the continuous range of real numbers [-234.1, 251.51] to a discrete set of integers [-128, 127].&lt;/p&gt;
&lt;h2 id="neural-network-quantization"&gt;Neural Network Quantization&lt;/h2&gt;
&lt;p&gt;You can quantize:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The weights: Neural network parameters&lt;/li&gt;
&lt;li&gt;The Activations: Values that propagate through the layers of the neural network&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you quantize the NN after it has been quantized, you are doing &lt;strong&gt;post training quantization&lt;/strong&gt;(PTQ)&lt;/p&gt;
&lt;h2 id="advantages-of-quantization"&gt;Advantages of Quantization&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Smaller model&lt;/li&gt;
&lt;li&gt;Speed gains
&lt;ul&gt;
&lt;li&gt;Memory bandwidth: Less data to transfer&lt;/li&gt;
&lt;li&gt;Faster operations
&lt;ul&gt;
&lt;li&gt;GEMM: General Matrix Multiply&lt;/li&gt;
&lt;li&gt;GEMV: General Matrix Vector Multiplication&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="challenges-of-quantization"&gt;Challenges of Quantization&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Quantization error&lt;/li&gt;
&lt;li&gt;Retraining (Quantization Aware Training, QAT)&lt;/li&gt;
&lt;li&gt;Limited hardware support&lt;/li&gt;
&lt;li&gt;Calibration dataset needed&lt;/li&gt;
&lt;li&gt;Packing/unpacking&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="linear-quantization"&gt;Linear Quantization&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Idea: linear mapping&lt;/li&gt;
&lt;li&gt;Formula:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;$$
r=s(q-z)
$$&lt;/p&gt;
&lt;p&gt;$s$ is the scale factor, $z$ is the zero point, $r$ is the real value (e.g. in FP32), $q$ is the quantized value (e.g. in INT8).&lt;/p&gt;
&lt;p&gt;&lt;img src="images/linear-quantization.png" alt="Linear Quantization"&gt;&lt;/p&gt;
&lt;p&gt;Example with $s=2$, and $z=0$:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We got $r=2(q-0)=2q$&lt;/li&gt;
&lt;li&gt;For $q=10$, we have $r=20$&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>