欢迎光临
我们一直在努力

第2章 什么是量化?用 3 个数字算懂 INT8

第2章 什么是量化?用 3 个数字算懂 INT8

2.1 从一个浮点数开始

上一章我们回答了一个问题:为什么 LLM 需要量化? 因为模型参数越来越多,FP16、BF16 等高精度表示会带来巨大的存储和显存压力。例如一个 7B 模型 7B×2 Bytes≈14GB7B \\times 2\\ \\text{Bytes} \\approx 14\\text{GB}7B×2 Bytes14GB,降到 INT8 约 7GB,降到 INT4 约 3.5GB。因此我们知道:

降低每个参数所使用的 bit 数,可以显著降低模型的存储需求。

但是一个真正的问题出现了:一个浮点数到底是怎么变成 INT8 的?

例如有一个模型参数 x=1.273x = 1.273x=1.273,我们希望把它变成一个 8-bit 的整数。难道直接 1.273→11.273 \\rightarrow 11.2731 就可以了吗?当然不行。如果直接进行整数截断:

1.273 → 1
1.823 → 1
1.999 → 1

那么大量原本不同的浮点数都会被压缩成同一个整数,信息损失会非常严重。因此,真正的量化不是简单地"Float → Int",而是:

#mermaid-svg-uOddbnx0jnWI7NZ1{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-uOddbnx0jnWI7NZ1 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-uOddbnx0jnWI7NZ1 .error-icon{fill:#552222;}#mermaid-svg-uOddbnx0jnWI7NZ1 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-uOddbnx0jnWI7NZ1 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-uOddbnx0jnWI7NZ1 .marker.cross{stroke:#333333;}#mermaid-svg-uOddbnx0jnWI7NZ1 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-uOddbnx0jnWI7NZ1 p{margin:0;}#mermaid-svg-uOddbnx0jnWI7NZ1 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-uOddbnx0jnWI7NZ1 .cluster-label text{fill:#333;}#mermaid-svg-uOddbnx0jnWI7NZ1 .cluster-label span{color:#333;}#mermaid-svg-uOddbnx0jnWI7NZ1 .cluster-label span p{background-color:transparent;}#mermaid-svg-uOddbnx0jnWI7NZ1 .label text,#mermaid-svg-uOddbnx0jnWI7NZ1 span{fill:#333;color:#333;}#mermaid-svg-uOddbnx0jnWI7NZ1 .node rect,#mermaid-svg-uOddbnx0jnWI7NZ1 .node circle,#mermaid-svg-uOddbnx0jnWI7NZ1 .node ellipse,#mermaid-svg-uOddbnx0jnWI7NZ1 .node polygon,#mermaid-svg-uOddbnx0jnWI7NZ1 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-uOddbnx0jnWI7NZ1 .rough-node .label text,#mermaid-svg-uOddbnx0jnWI7NZ1 .node .label text,#mermaid-svg-uOddbnx0jnWI7NZ1 .image-shape .label,#mermaid-svg-uOddbnx0jnWI7NZ1 .icon-shape .label{text-anchor:middle;}#mermaid-svg-uOddbnx0jnWI7NZ1 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-uOddbnx0jnWI7NZ1 .rough-node .label,#mermaid-svg-uOddbnx0jnWI7NZ1 .node .label,#mermaid-svg-uOddbnx0jnWI7NZ1 .image-shape .label,#mermaid-svg-uOddbnx0jnWI7NZ1 .icon-shape .label{text-align:center;}#mermaid-svg-uOddbnx0jnWI7NZ1 .node.clickable{cursor:pointer;}#mermaid-svg-uOddbnx0jnWI7NZ1 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-uOddbnx0jnWI7NZ1 .arrowheadPath{fill:#333333;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-uOddbnx0jnWI7NZ1 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-uOddbnx0jnWI7NZ1 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-uOddbnx0jnWI7NZ1 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-uOddbnx0jnWI7NZ1 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-uOddbnx0jnWI7NZ1 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-uOddbnx0jnWI7NZ1 .cluster text{fill:#333;}#mermaid-svg-uOddbnx0jnWI7NZ1 .cluster span{color:#333;}#mermaid-svg-uOddbnx0jnWI7NZ1 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-uOddbnx0jnWI7NZ1 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-uOddbnx0jnWI7NZ1 rect.text{fill:none;stroke-width:0;}#mermaid-svg-uOddbnx0jnWI7NZ1 .icon-shape,#mermaid-svg-uOddbnx0jnWI7NZ1 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-uOddbnx0jnWI7NZ1 .icon-shape p,#mermaid-svg-uOddbnx0jnWI7NZ1 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-uOddbnx0jnWI7NZ1 .icon-shape .label rect,#mermaid-svg-uOddbnx0jnWI7NZ1 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-uOddbnx0jnWI7NZ1 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-uOddbnx0jnWI7NZ1 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-uOddbnx0jnWI7NZ1 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

高精度浮点数

确定一个映射范围

Scale / Zero Point

低比特离散表示

这就是本章要解决的问题。


2.2 量化到底在做什么?

先暂时不考虑复杂的 LLM,只看最简单的一组数字 x=[−1.0,−0.5,0,0.5,1.0]x = [-1.0, -0.5, 0, 0.5, 1.0]x=[1.0,0.5,0,0.5,1.0],假设我们希望把它们映射到 INT8。

INT8 能够表示 [−128,127][-128, 127][128,127]。在最简单的对称量化里,我们通常把浮点范围 [−1,1][-1, 1][1,1] 映射到 [−127,127][-127, 127][127,127]。为什么不是使用 [−128,127][-128, 127][128,127]?后面讨论量化方案时我们会再解释。现在先把重点放在一个概念:Scale。

2.2.1 Scale 是什么?

假设 xmax=1x_{max} = 1xmax=1,INT8 的最大正整数 qmax=127q_{max} = 127qmax=127,那么一个很自然的 Scale 是:

s=xmaxqmax=1127≈0.007874s = \\frac{x_{max}}{q_{max}} = \\frac{1}{127} \\approx 0.007874s=qmaxxmax=12710.007874

它表示:INT8 中的一个整数单位,大约对应原始浮点空间中的 0.007874。

于是 0.50.50.5 量化后 q=round(0.50.007874)≈64q = \\text{round}\\left(\\frac{0.5}{0.007874}\\right) \\approx 64q=round(0.0078740.5)64,所以 0.5→640.5 \\rightarrow 640.564。恢复时 x^=64×0.007874≈0.504\\hat{x} = 64 \\times 0.007874 \\approx 0.504x^=64×0.0078740.504

你会发现 0.5→64→0.5040.5 \\rightarrow 64 \\rightarrow 0.5040.5640.504 发生了一点误差。这就是量化最基本的本质:

用更少的离散状态近似原来的连续数值。

2.2.2 最基本的量化公式

在最简单的对称量化中,可以使用:

xq=round(xs)\\boxed{x_q = \\text{round}\\left(\\frac{x}{s}\\right)}xq=round(sx)

恢复时:

x^=s⋅xq\\boxed{\\hat{x} = s \\cdot x_q}x^=sxq

整个过程可以表示为:

#mermaid-svg-A5VI8G3CeRX5rgou{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-A5VI8G3CeRX5rgou .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-A5VI8G3CeRX5rgou .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-A5VI8G3CeRX5rgou .error-icon{fill:#552222;}#mermaid-svg-A5VI8G3CeRX5rgou .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-A5VI8G3CeRX5rgou .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-A5VI8G3CeRX5rgou .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-A5VI8G3CeRX5rgou .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-A5VI8G3CeRX5rgou .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-A5VI8G3CeRX5rgou .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-A5VI8G3CeRX5rgou .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-A5VI8G3CeRX5rgou .marker{fill:#333333;stroke:#333333;}#mermaid-svg-A5VI8G3CeRX5rgou .marker.cross{stroke:#333333;}#mermaid-svg-A5VI8G3CeRX5rgou svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-A5VI8G3CeRX5rgou p{margin:0;}#mermaid-svg-A5VI8G3CeRX5rgou .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-A5VI8G3CeRX5rgou .cluster-label text{fill:#333;}#mermaid-svg-A5VI8G3CeRX5rgou .cluster-label span{color:#333;}#mermaid-svg-A5VI8G3CeRX5rgou .cluster-label span p{background-color:transparent;}#mermaid-svg-A5VI8G3CeRX5rgou .label text,#mermaid-svg-A5VI8G3CeRX5rgou span{fill:#333;color:#333;}#mermaid-svg-A5VI8G3CeRX5rgou .node rect,#mermaid-svg-A5VI8G3CeRX5rgou .node circle,#mermaid-svg-A5VI8G3CeRX5rgou .node ellipse,#mermaid-svg-A5VI8G3CeRX5rgou .node polygon,#mermaid-svg-A5VI8G3CeRX5rgou .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-A5VI8G3CeRX5rgou .rough-node .label text,#mermaid-svg-A5VI8G3CeRX5rgou .node .label text,#mermaid-svg-A5VI8G3CeRX5rgou .image-shape .label,#mermaid-svg-A5VI8G3CeRX5rgou .icon-shape .label{text-anchor:middle;}#mermaid-svg-A5VI8G3CeRX5rgou .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-A5VI8G3CeRX5rgou .rough-node .label,#mermaid-svg-A5VI8G3CeRX5rgou .node .label,#mermaid-svg-A5VI8G3CeRX5rgou .image-shape .label,#mermaid-svg-A5VI8G3CeRX5rgou .icon-shape .label{text-align:center;}#mermaid-svg-A5VI8G3CeRX5rgou .node.clickable{cursor:pointer;}#mermaid-svg-A5VI8G3CeRX5rgou .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-A5VI8G3CeRX5rgou .arrowheadPath{fill:#333333;}#mermaid-svg-A5VI8G3CeRX5rgou .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-A5VI8G3CeRX5rgou .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-A5VI8G3CeRX5rgou .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-A5VI8G3CeRX5rgou .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-A5VI8G3CeRX5rgou .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-A5VI8G3CeRX5rgou .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-A5VI8G3CeRX5rgou .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-A5VI8G3CeRX5rgou .cluster text{fill:#333;}#mermaid-svg-A5VI8G3CeRX5rgou .cluster span{color:#333;}#mermaid-svg-A5VI8G3CeRX5rgou div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-A5VI8G3CeRX5rgou .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-A5VI8G3CeRX5rgou rect.text{fill:none;stroke-width:0;}#mermaid-svg-A5VI8G3CeRX5rgou .icon-shape,#mermaid-svg-A5VI8G3CeRX5rgou .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-A5VI8G3CeRX5rgou .icon-shape p,#mermaid-svg-A5VI8G3CeRX5rgou .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-A5VI8G3CeRX5rgou .icon-shape .label rect,#mermaid-svg-A5VI8G3CeRX5rgou .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-A5VI8G3CeRX5rgou .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-A5VI8G3CeRX5rgou .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-A5VI8G3CeRX5rgou :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

原始浮点数 x

除以 Scale

四舍五入

量化整数 xq

乘以 Scale

近似浮点数 x̂

所以整个过程是 x→xq→x^x \\rightarrow x_q \\rightarrow \\hat{x}xxqx^,其中 x^≈x\\hat{x} \\approx xx^x,但通常 x^≠x\\hat{x} \\neq xx^=x


2.3 用 3 个数字手算 INT8

这一节我们直接手算一个完整例子。假设有三个浮点数 x=[−1.0,0.5,0.8]x = [-1.0, 0.5, 0.8]x=[1.0,0.5,0.8],最大绝对值 max⁡(∣x∣)=1.0\\max(|x|) = 1.0max(x)=1.0。我们选择 INT8 的对称范围 [−127,127][-127, 127][127,127],那么 s=1127≈0.007874s = \\frac{1}{127} \\approx 0.007874s=12710.007874

2.3.1 第一个数字:-1.0

量化:xq=round(−1.01/127)=round(−127)=−127x_q = \\text{round}\\left(\\frac{-1.0}{1/127}\\right) = \\text{round}(-127) = -127xq=round(1/1271.0)=round(127)=127,所以 −1.0→−127\\boxed{-1.0 \\rightarrow -127}1.0127

恢复:x^=−127×1127=−1.0\\hat{x} = -127 \\times \\frac{1}{127} = \\boxed{-1.0}x^=127×1271=1.0。这个数字在这里刚好可以被精确恢复。

2.3.2 第二个数字:0.5

量化:xq=round(0.51/127)=round(63.5)x_q = \\text{round}\\left(\\frac{0.5}{1/127}\\right) = \\text{round}(63.5)xq=round(1/1270.5)=round(63.5),根据具体取整规则可以得到接近 646464。于是 0.5→640.5 \\rightarrow 640.564

恢复:x^=64×1127≈0.50394\\hat{x} = 64 \\times \\frac{1}{127} \\approx 0.50394x^=64×12710.50394,所以 0.5→64→0.504\\boxed{0.5 \\rightarrow 64 \\rightarrow 0.504}0.5640.504。量化误差大约 0.504−0.5=0.0040.504 – 0.5 = 0.0040.5040.5=0.004

2.3.3 第三个数字:0.8

量化:xq=round(0.8×127)=round(101.6)=102x_q = \\text{round}(0.8 \\times 127) = \\text{round}(101.6) = 102xq=round(0.8×127)=round(101.6)=102

恢复:x^=102×1127≈0.80315\\hat{x} = 102 \\times \\frac{1}{127} \\approx 0.80315x^=102×12710.80315,因此 0.8→102→0.803\\boxed{0.8 \\rightarrow 102 \\rightarrow 0.803}0.81020.803

2.3.4 我们刚刚到底做了什么?

阶段值
原始 [−1.0,0.5,0.8][-1.0, 0.5, 0.8][1.0,0.5,0.8]
量化 [−127,64,102][-127, 64, 102][127,64,102]
恢复 [−1.0,0.504,0.803][-1.0, 0.504, 0.803][1.0,0.504,0.803]

可以看到:原始值没有被完整保留,而是被映射成有限个离散整数。 因此出现 Quantization Error(量化误差)E=x−x^E = x – \\hat{x}E=xx^


2.4 为什么 INT8 只有 256 种状态?

这是理解低比特量化非常重要的基础。8 bit 一共可以表达 28=2562^8 = 25628=256 种不同的二进制组合(00000000、00000001、……、11111111)。而 4 bit 是 24=162^4 = 1624=16

#mermaid-svg-Z29HBWm65qg9RyDM{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Z29HBWm65qg9RyDM .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Z29HBWm65qg9RyDM .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Z29HBWm65qg9RyDM .error-icon{fill:#552222;}#mermaid-svg-Z29HBWm65qg9RyDM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Z29HBWm65qg9RyDM .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Z29HBWm65qg9RyDM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Z29HBWm65qg9RyDM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Z29HBWm65qg9RyDM .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Z29HBWm65qg9RyDM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Z29HBWm65qg9RyDM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Z29HBWm65qg9RyDM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Z29HBWm65qg9RyDM .marker.cross{stroke:#333333;}#mermaid-svg-Z29HBWm65qg9RyDM svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Z29HBWm65qg9RyDM p{margin:0;}#mermaid-svg-Z29HBWm65qg9RyDM .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Z29HBWm65qg9RyDM .cluster-label text{fill:#333;}#mermaid-svg-Z29HBWm65qg9RyDM .cluster-label span{color:#333;}#mermaid-svg-Z29HBWm65qg9RyDM .cluster-label span p{background-color:transparent;}#mermaid-svg-Z29HBWm65qg9RyDM .label text,#mermaid-svg-Z29HBWm65qg9RyDM span{fill:#333;color:#333;}#mermaid-svg-Z29HBWm65qg9RyDM .node rect,#mermaid-svg-Z29HBWm65qg9RyDM .node circle,#mermaid-svg-Z29HBWm65qg9RyDM .node ellipse,#mermaid-svg-Z29HBWm65qg9RyDM .node polygon,#mermaid-svg-Z29HBWm65qg9RyDM .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Z29HBWm65qg9RyDM .rough-node .label text,#mermaid-svg-Z29HBWm65qg9RyDM .node .label text,#mermaid-svg-Z29HBWm65qg9RyDM .image-shape .label,#mermaid-svg-Z29HBWm65qg9RyDM .icon-shape .label{text-anchor:middle;}#mermaid-svg-Z29HBWm65qg9RyDM .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Z29HBWm65qg9RyDM .rough-node .label,#mermaid-svg-Z29HBWm65qg9RyDM .node .label,#mermaid-svg-Z29HBWm65qg9RyDM .image-shape .label,#mermaid-svg-Z29HBWm65qg9RyDM .icon-shape .label{text-align:center;}#mermaid-svg-Z29HBWm65qg9RyDM .node.clickable{cursor:pointer;}#mermaid-svg-Z29HBWm65qg9RyDM .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Z29HBWm65qg9RyDM .arrowheadPath{fill:#333333;}#mermaid-svg-Z29HBWm65qg9RyDM .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Z29HBWm65qg9RyDM .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Z29HBWm65qg9RyDM .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Z29HBWm65qg9RyDM .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Z29HBWm65qg9RyDM .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Z29HBWm65qg9RyDM .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Z29HBWm65qg9RyDM .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Z29HBWm65qg9RyDM .cluster text{fill:#333;}#mermaid-svg-Z29HBWm65qg9RyDM .cluster span{color:#333;}#mermaid-svg-Z29HBWm65qg9RyDM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Z29HBWm65qg9RyDM .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Z29HBWm65qg9RyDM rect.text{fill:none;stroke-width:0;}#mermaid-svg-Z29HBWm65qg9RyDM .icon-shape,#mermaid-svg-Z29HBWm65qg9RyDM .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Z29HBWm65qg9RyDM .icon-shape p,#mermaid-svg-Z29HBWm65qg9RyDM .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Z29HBWm65qg9RyDM .icon-shape .label rect,#mermaid-svg-Z29HBWm65qg9RyDM .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Z29HBWm65qg9RyDM .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Z29HBWm65qg9RyDM .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Z29HBWm65qg9RyDM :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

INT8 → 2⁸ = 256 个状态

INT4 → 2⁴ = 16 个状态

这就意味着:

INT4 比 INT8 的离散状态少得多,因此通常需要更加精细的量化策略才能控制误差。

这也是为什么真正的 LLM 4-bit 量化不会简单地做一次 float → int4,而会引入 Group-wise Quantization、Calibration、GPTQ、AWQ、NF4 等方法。

这些方法会在后续章节逐一展开:

  • Group-wise Quantization(分组量化)——第 4 章《LLM 4-bit 量化到底意味着什么?》讲 Group Size 的粒度权衡。
  • Calibration(校准)——第 3 章《PTQ:不用重新训练也能量化 LLM》讲如何用代表性数据确定量化参数。
  • GPTQ——第 5 章《GPTQ 实战》讲如何利用误差最小化把 Hugging Face 模型量化成 4-bit。
  • AWQ——第 6 章《AWQ 实战》讲为什么不是所有权重都同样重要。
  • NF4——第 7 章《NF4 与 QLoRA》讲为什么 4-bit 还能用于训练。

2.5 对称量化与非对称量化

刚才我们的例子属于 Symmetric Quantization(对称量化)。也就是说,浮点空间 [−a,a][-a, a][a,a] 映射到 [−qmax,qmax][-q_{max}, q_{max}][qmax,qmax],例如 [−1,1]→[−127,127][-1, 1] \\rightarrow [-127, 127][1,1][127,127]。它的一个重要特点是:正负两边围绕 0 对称。

2.5.1 对称量化

最简单的形式:

s=max⁡(∣x∣)qmax,xq=round(x/s),x^=s⋅xq\\boxed{s = \\frac{\\max(|x|)}{q_{max}}}, \\qquad \\boxed{x_q = \\text{round}(x/s)}, \\qquad \\boxed{\\hat{x} = s \\cdot x_q}s=qmaxmax(x),xq=round(x/s),x^=sxq

因为浮点 0 直接对应整数 0,所以实现起来比较简单。

2.5.2 非对称量化

假设数据不是围绕 0 对称,而是 [0.2,0.5,1.0,2.0][0.2, 0.5, 1.0, 2.0][0.2,0.5,1.0,2.0]。这时如果强行使用 [−2,2][-2, 2][2,2] 这样的对称范围,就可能浪费大量表示空间(例如数据根本没有负值)。

于是可以使用 Asymmetric Quantization(非对称量化)。基本思路是把 [xmin,xmax][x_{min}, x_{max}][xmin,xmax] 映射到 [qmin,qmax][q_{min}, q_{max}][qmin,qmax],此时通常引入 Zero Point。


2.6 Zero Point 是什么?

非对称量化中,一个常见公式是:

xq=round(xs)+z\\boxed{x_q = \\text{round}\\left(\\frac{x}{s}\\right) + z}xq=round(sx)+z

其中 sss 是 Scale,zzz 是 Zero Point。Zero Point 的作用可以简单理解成:

告诉我们:浮点数中的 0,在整数空间里对应哪个整数。

2.6.1 为什么需要 Zero Point?

假设浮点范围 [0,10][0, 10][0,10],而整数范围 [−128,127][-128, 127][128,127]。如果直接做对称映射,会浪费一部分负数区间,因为原数据根本没有负数。非对称量化可以把整个整数区间更充分地用于 [0,10][0, 10][0,10] 这样的数据:

#mermaid-svg-QnNkwbz5qfsZNmiv{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-QnNkwbz5qfsZNmiv .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-QnNkwbz5qfsZNmiv .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-QnNkwbz5qfsZNmiv .error-icon{fill:#552222;}#mermaid-svg-QnNkwbz5qfsZNmiv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-QnNkwbz5qfsZNmiv .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-QnNkwbz5qfsZNmiv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-QnNkwbz5qfsZNmiv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-QnNkwbz5qfsZNmiv .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-QnNkwbz5qfsZNmiv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-QnNkwbz5qfsZNmiv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-QnNkwbz5qfsZNmiv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-QnNkwbz5qfsZNmiv .marker.cross{stroke:#333333;}#mermaid-svg-QnNkwbz5qfsZNmiv svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-QnNkwbz5qfsZNmiv p{margin:0;}#mermaid-svg-QnNkwbz5qfsZNmiv .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-QnNkwbz5qfsZNmiv .cluster-label text{fill:#333;}#mermaid-svg-QnNkwbz5qfsZNmiv .cluster-label span{color:#333;}#mermaid-svg-QnNkwbz5qfsZNmiv .cluster-label span p{background-color:transparent;}#mermaid-svg-QnNkwbz5qfsZNmiv .label text,#mermaid-svg-QnNkwbz5qfsZNmiv span{fill:#333;color:#333;}#mermaid-svg-QnNkwbz5qfsZNmiv .node rect,#mermaid-svg-QnNkwbz5qfsZNmiv .node circle,#mermaid-svg-QnNkwbz5qfsZNmiv .node ellipse,#mermaid-svg-QnNkwbz5qfsZNmiv .node polygon,#mermaid-svg-QnNkwbz5qfsZNmiv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-QnNkwbz5qfsZNmiv .rough-node .label text,#mermaid-svg-QnNkwbz5qfsZNmiv .node .label text,#mermaid-svg-QnNkwbz5qfsZNmiv .image-shape .label,#mermaid-svg-QnNkwbz5qfsZNmiv .icon-shape .label{text-anchor:middle;}#mermaid-svg-QnNkwbz5qfsZNmiv .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-QnNkwbz5qfsZNmiv .rough-node .label,#mermaid-svg-QnNkwbz5qfsZNmiv .node .label,#mermaid-svg-QnNkwbz5qfsZNmiv .image-shape .label,#mermaid-svg-QnNkwbz5qfsZNmiv .icon-shape .label{text-align:center;}#mermaid-svg-QnNkwbz5qfsZNmiv .node.clickable{cursor:pointer;}#mermaid-svg-QnNkwbz5qfsZNmiv .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-QnNkwbz5qfsZNmiv .arrowheadPath{fill:#333333;}#mermaid-svg-QnNkwbz5qfsZNmiv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-QnNkwbz5qfsZNmiv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-QnNkwbz5qfsZNmiv .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QnNkwbz5qfsZNmiv .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-QnNkwbz5qfsZNmiv .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QnNkwbz5qfsZNmiv .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-QnNkwbz5qfsZNmiv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-QnNkwbz5qfsZNmiv .cluster text{fill:#333;}#mermaid-svg-QnNkwbz5qfsZNmiv .cluster span{color:#333;}#mermaid-svg-QnNkwbz5qfsZNmiv div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-QnNkwbz5qfsZNmiv .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-QnNkwbz5qfsZNmiv rect.text{fill:none;stroke-width:0;}#mermaid-svg-QnNkwbz5qfsZNmiv .icon-shape,#mermaid-svg-QnNkwbz5qfsZNmiv .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-QnNkwbz5qfsZNmiv .icon-shape p,#mermaid-svg-QnNkwbz5qfsZNmiv .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-QnNkwbz5qfsZNmiv .icon-shape .label rect,#mermaid-svg-QnNkwbz5qfsZNmiv .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-QnNkwbz5qfsZNmiv .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-QnNkwbz5qfsZNmiv .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-QnNkwbz5qfsZNmiv :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

Scale + Zero Point 映射

整数空间

-128 ——————— 127

浮点空间

0 ——————— 10

所以:Scale 负责"缩放",Zero Point 负责"平移"。 这是理解两者区别最简单的方法。


2.7 Per-Tensor Quantization

现在假设我们有一整个 Tensor XXX,里面有很多不同范围的数据。最简单的方式是:整个 Tensor 共用一个 Scale。

例如:

X=[−1.00.50.8−0.3]X = \\begin{bmatrix} -1.0 & 0.5 \\\\ 0.8 & -0.3 \\end{bmatrix}X=[1.00.80.50.3]

如果 max⁡(∣X∣)=1\\max(|X|) = 1max(X)=1,那么整个 Tensor 都使用 s=1127s = \\frac{1}{127}s=1271。这叫 Per-Tensor Quantization(整个 Tensor → 一个 Scale)。

2.7.1 Per-Tensor 的问题

考虑下面的数据:Channel 1 是 [-1.0, 0.8, -0.7],Channel 2 是 [0.01, 0.02, 0.03]。如果整个 Tensor 使用 max⁡(∣X∣)=1\\max(|X|) = 1max(X)=1,那么 s=1127s = \\frac{1}{127}s=1271

对于 Channel 1,这个 Scale 还比较合适。但 Channel 2 的值只有 0.01∼0.030.01 \\sim 0.030.010.03,量化后 0.01×127≈1.270.01 \\times 127 \\approx 1.270.01×1271.27,很多非常小的数可能被压缩到很少几个整数状态。于是:

小范围的数据没有充分利用 INT8 的表示空间。

这就是 Per-Tensor 的一个典型问题。


2.8 Per-Channel Quantization

解决办法是:每个 Channel 使用自己的 Scale。

#mermaid-svg-XbS71oqsJrhZRsDG{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-XbS71oqsJrhZRsDG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-XbS71oqsJrhZRsDG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-XbS71oqsJrhZRsDG .error-icon{fill:#552222;}#mermaid-svg-XbS71oqsJrhZRsDG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-XbS71oqsJrhZRsDG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-XbS71oqsJrhZRsDG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-XbS71oqsJrhZRsDG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-XbS71oqsJrhZRsDG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-XbS71oqsJrhZRsDG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-XbS71oqsJrhZRsDG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-XbS71oqsJrhZRsDG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-XbS71oqsJrhZRsDG .marker.cross{stroke:#333333;}#mermaid-svg-XbS71oqsJrhZRsDG svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-XbS71oqsJrhZRsDG p{margin:0;}#mermaid-svg-XbS71oqsJrhZRsDG .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-XbS71oqsJrhZRsDG .cluster-label text{fill:#333;}#mermaid-svg-XbS71oqsJrhZRsDG .cluster-label span{color:#333;}#mermaid-svg-XbS71oqsJrhZRsDG .cluster-label span p{background-color:transparent;}#mermaid-svg-XbS71oqsJrhZRsDG .label text,#mermaid-svg-XbS71oqsJrhZRsDG span{fill:#333;color:#333;}#mermaid-svg-XbS71oqsJrhZRsDG .node rect,#mermaid-svg-XbS71oqsJrhZRsDG .node circle,#mermaid-svg-XbS71oqsJrhZRsDG .node ellipse,#mermaid-svg-XbS71oqsJrhZRsDG .node polygon,#mermaid-svg-XbS71oqsJrhZRsDG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-XbS71oqsJrhZRsDG .rough-node .label text,#mermaid-svg-XbS71oqsJrhZRsDG .node .label text,#mermaid-svg-XbS71oqsJrhZRsDG .image-shape .label,#mermaid-svg-XbS71oqsJrhZRsDG .icon-shape .label{text-anchor:middle;}#mermaid-svg-XbS71oqsJrhZRsDG .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-XbS71oqsJrhZRsDG .rough-node .label,#mermaid-svg-XbS71oqsJrhZRsDG .node .label,#mermaid-svg-XbS71oqsJrhZRsDG .image-shape .label,#mermaid-svg-XbS71oqsJrhZRsDG .icon-shape .label{text-align:center;}#mermaid-svg-XbS71oqsJrhZRsDG .node.clickable{cursor:pointer;}#mermaid-svg-XbS71oqsJrhZRsDG .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-XbS71oqsJrhZRsDG .arrowheadPath{fill:#333333;}#mermaid-svg-XbS71oqsJrhZRsDG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-XbS71oqsJrhZRsDG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-XbS71oqsJrhZRsDG .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XbS71oqsJrhZRsDG .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-XbS71oqsJrhZRsDG .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XbS71oqsJrhZRsDG .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-XbS71oqsJrhZRsDG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-XbS71oqsJrhZRsDG .cluster text{fill:#333;}#mermaid-svg-XbS71oqsJrhZRsDG .cluster span{color:#333;}#mermaid-svg-XbS71oqsJrhZRsDG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-XbS71oqsJrhZRsDG .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-XbS71oqsJrhZRsDG rect.text{fill:none;stroke-width:0;}#mermaid-svg-XbS71oqsJrhZRsDG .icon-shape,#mermaid-svg-XbS71oqsJrhZRsDG .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XbS71oqsJrhZRsDG .icon-shape p,#mermaid-svg-XbS71oqsJrhZRsDG .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-XbS71oqsJrhZRsDG .icon-shape .label rect,#mermaid-svg-XbS71oqsJrhZRsDG .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XbS71oqsJrhZRsDG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-XbS71oqsJrhZRsDG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-XbS71oqsJrhZRsDG :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

Tensor

Channel 1: [-1.0, 0.8, -0.7]

Channel 2: [0.01, 0.02, 0.03]

Scale 1

Scale 2

这样每个 Channel 都可以更充分地利用量化范围。因此:Per-Channel 通常能够降低量化误差,但需要保存更多 Scale。 这体现了一个量化中的基本权衡:

更细粒度→更高精度→更多辅助信息\\boxed{\\text{更细粒度} \\rightarrow \\text{更高精度} \\rightarrow \\text{更多辅助信息}}更细粒度更高精度更多辅助信息


2.9 Group-wise Quantization

如果一个 Scale 对整个 Tensor 太粗,而一个 Scale 对每个元素又太浪费,那么可以采用中间方案:Group-wise Quantization——把大量参数分成很多 Group,每个 Group 使用一个 Scale。

例如 256 个权重分成 4 组:

#mermaid-svg-1i3SHzclbuPXHFdH{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-1i3SHzclbuPXHFdH .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-1i3SHzclbuPXHFdH .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-1i3SHzclbuPXHFdH .error-icon{fill:#552222;}#mermaid-svg-1i3SHzclbuPXHFdH .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1i3SHzclbuPXHFdH .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-1i3SHzclbuPXHFdH .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1i3SHzclbuPXHFdH .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1i3SHzclbuPXHFdH .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-1i3SHzclbuPXHFdH .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1i3SHzclbuPXHFdH .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1i3SHzclbuPXHFdH .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1i3SHzclbuPXHFdH .marker.cross{stroke:#333333;}#mermaid-svg-1i3SHzclbuPXHFdH svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1i3SHzclbuPXHFdH p{margin:0;}#mermaid-svg-1i3SHzclbuPXHFdH .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-1i3SHzclbuPXHFdH .cluster-label text{fill:#333;}#mermaid-svg-1i3SHzclbuPXHFdH .cluster-label span{color:#333;}#mermaid-svg-1i3SHzclbuPXHFdH .cluster-label span p{background-color:transparent;}#mermaid-svg-1i3SHzclbuPXHFdH .label text,#mermaid-svg-1i3SHzclbuPXHFdH span{fill:#333;color:#333;}#mermaid-svg-1i3SHzclbuPXHFdH .node rect,#mermaid-svg-1i3SHzclbuPXHFdH .node circle,#mermaid-svg-1i3SHzclbuPXHFdH .node ellipse,#mermaid-svg-1i3SHzclbuPXHFdH .node polygon,#mermaid-svg-1i3SHzclbuPXHFdH .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1i3SHzclbuPXHFdH .rough-node .label text,#mermaid-svg-1i3SHzclbuPXHFdH .node .label text,#mermaid-svg-1i3SHzclbuPXHFdH .image-shape .label,#mermaid-svg-1i3SHzclbuPXHFdH .icon-shape .label{text-anchor:middle;}#mermaid-svg-1i3SHzclbuPXHFdH .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-1i3SHzclbuPXHFdH .rough-node .label,#mermaid-svg-1i3SHzclbuPXHFdH .node .label,#mermaid-svg-1i3SHzclbuPXHFdH .image-shape .label,#mermaid-svg-1i3SHzclbuPXHFdH .icon-shape .label{text-align:center;}#mermaid-svg-1i3SHzclbuPXHFdH .node.clickable{cursor:pointer;}#mermaid-svg-1i3SHzclbuPXHFdH .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-1i3SHzclbuPXHFdH .arrowheadPath{fill:#333333;}#mermaid-svg-1i3SHzclbuPXHFdH .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-1i3SHzclbuPXHFdH .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-1i3SHzclbuPXHFdH .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-1i3SHzclbuPXHFdH .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-1i3SHzclbuPXHFdH .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-1i3SHzclbuPXHFdH .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-1i3SHzclbuPXHFdH .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-1i3SHzclbuPXHFdH .cluster text{fill:#333;}#mermaid-svg-1i3SHzclbuPXHFdH .cluster span{color:#333;}#mermaid-svg-1i3SHzclbuPXHFdH div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-1i3SHzclbuPXHFdH .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-1i3SHzclbuPXHFdH rect.text{fill:none;stroke-width:0;}#mermaid-svg-1i3SHzclbuPXHFdH .icon-shape,#mermaid-svg-1i3SHzclbuPXHFdH .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-1i3SHzclbuPXHFdH .icon-shape p,#mermaid-svg-1i3SHzclbuPXHFdH .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-1i3SHzclbuPXHFdH .icon-shape .label rect,#mermaid-svg-1i3SHzclbuPXHFdH .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-1i3SHzclbuPXHFdH .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-1i3SHzclbuPXHFdH .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-1i3SHzclbuPXHFdH :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

256 个权重

Group 1: 0~63

Group 2: 64~127

Group 3: 128~191

Group 4: 192~255

Scale 1

Scale 2

Scale 3

Scale 4

这样比 Per-Tensor 更精细,同时比 Per-Element 更节省辅助信息。

2.9.1 Group Size

例如 GroupSize=128GroupSize = 128GroupSize=128 表示每 128 个连续权重使用一个量化参数组。常见的思想是:

Group SizeScale 数量精度存储开销
更多 更精细,误差更容易控制 更高
更少 量化可能更粗 更低

因此:Group Size 本身也是精度与额外开销之间的权衡。 后面学习 GPTQ、AWQ 时,你会反复看到这个参数。


2.10 为什么 LLM 特别喜欢 Group-wise Quantization?

这是因为现代 LLM 中,一个 Linear Layer 可能拥有巨大的权重矩阵 W∈Rdout×dinW \\in \\mathbb{R}^{d_{out} \\times d_{in}}WRdout×din,例如 4096×110084096 \\times 110084096×11008,里面有数千万个参数。

如果所有参数共用一个 Scale,粒度太粗;但如果每个参数一个 Scale,辅助数据又会太大。所以一个常见的工程折中就是:把权重划分成 Group,每个 Group 使用自己的量化参数。

#mermaid-svg-IZiWW4uxxyrc5SDG{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-IZiWW4uxxyrc5SDG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IZiWW4uxxyrc5SDG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IZiWW4uxxyrc5SDG .error-icon{fill:#552222;}#mermaid-svg-IZiWW4uxxyrc5SDG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IZiWW4uxxyrc5SDG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IZiWW4uxxyrc5SDG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IZiWW4uxxyrc5SDG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IZiWW4uxxyrc5SDG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IZiWW4uxxyrc5SDG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IZiWW4uxxyrc5SDG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IZiWW4uxxyrc5SDG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IZiWW4uxxyrc5SDG .marker.cross{stroke:#333333;}#mermaid-svg-IZiWW4uxxyrc5SDG svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IZiWW4uxxyrc5SDG p{margin:0;}#mermaid-svg-IZiWW4uxxyrc5SDG .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-IZiWW4uxxyrc5SDG .cluster-label text{fill:#333;}#mermaid-svg-IZiWW4uxxyrc5SDG .cluster-label span{color:#333;}#mermaid-svg-IZiWW4uxxyrc5SDG .cluster-label span p{background-color:transparent;}#mermaid-svg-IZiWW4uxxyrc5SDG .label text,#mermaid-svg-IZiWW4uxxyrc5SDG span{fill:#333;color:#333;}#mermaid-svg-IZiWW4uxxyrc5SDG .node rect,#mermaid-svg-IZiWW4uxxyrc5SDG .node circle,#mermaid-svg-IZiWW4uxxyrc5SDG .node ellipse,#mermaid-svg-IZiWW4uxxyrc5SDG .node polygon,#mermaid-svg-IZiWW4uxxyrc5SDG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-IZiWW4uxxyrc5SDG .rough-node .label text,#mermaid-svg-IZiWW4uxxyrc5SDG .node .label text,#mermaid-svg-IZiWW4uxxyrc5SDG .image-shape .label,#mermaid-svg-IZiWW4uxxyrc5SDG .icon-shape .label{text-anchor:middle;}#mermaid-svg-IZiWW4uxxyrc5SDG .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-IZiWW4uxxyrc5SDG .rough-node .label,#mermaid-svg-IZiWW4uxxyrc5SDG .node .label,#mermaid-svg-IZiWW4uxxyrc5SDG .image-shape .label,#mermaid-svg-IZiWW4uxxyrc5SDG .icon-shape .label{text-align:center;}#mermaid-svg-IZiWW4uxxyrc5SDG .node.clickable{cursor:pointer;}#mermaid-svg-IZiWW4uxxyrc5SDG .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-IZiWW4uxxyrc5SDG .arrowheadPath{fill:#333333;}#mermaid-svg-IZiWW4uxxyrc5SDG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-IZiWW4uxxyrc5SDG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-IZiWW4uxxyrc5SDG .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IZiWW4uxxyrc5SDG .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-IZiWW4uxxyrc5SDG .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IZiWW4uxxyrc5SDG .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-IZiWW4uxxyrc5SDG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-IZiWW4uxxyrc5SDG .cluster text{fill:#333;}#mermaid-svg-IZiWW4uxxyrc5SDG .cluster span{color:#333;}#mermaid-svg-IZiWW4uxxyrc5SDG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-IZiWW4uxxyrc5SDG .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-IZiWW4uxxyrc5SDG rect.text{fill:none;stroke-width:0;}#mermaid-svg-IZiWW4uxxyrc5SDG .icon-shape,#mermaid-svg-IZiWW4uxxyrc5SDG .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IZiWW4uxxyrc5SDG .icon-shape p,#mermaid-svg-IZiWW4uxxyrc5SDG .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-IZiWW4uxxyrc5SDG .icon-shape .label rect,#mermaid-svg-IZiWW4uxxyrc5SDG .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IZiWW4uxxyrc5SDG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-IZiWW4uxxyrc5SDG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-IZiWW4uxxyrc5SDG :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

巨大的权重矩阵

切成很多 Group

每个 Group 一个 Scale

4-bit / 8-bit 权重

这也是为什么在后续学习 LLM 量化算法时,group_size 会成为非常重要的配置参数。


2.11 Python 手写一个最简单的量化器

理解完公式以后,我们直接写代码。下面用 NumPy 实现最简单的对称量化。

import numpy as np

def quantize_symmetric(x):
"""
最简单的对称量化。
这里以 INT8 为例。
"""

x = np.asarray(x, dtype=np.float32)

qmax = 127

# 计算最大绝对值
max_abs = np.max(np.abs(x))

# 防止输入全为 0 时除零
if max_abs == 0:
scale = 1.0
else:
scale = max_abs / qmax

# Quantize
x_q = np.round(x / scale).astype(np.int8)

return x_q, scale

再实现反量化:

def dequantize(x_q, scale):
"""
将量化后的整数恢复为近似浮点数。
"""

return x_q.astype(np.float32) * scale

测试:

x = np.array(
[1.0, 0.5, 0.0, 0.5, 0.8],
dtype=np.float32
)

x_q, scale = quantize_symmetric(x)
x_hat = dequantize(x_q, scale)

print("Original:")
print(x)

print("\\nQuantized:")
print(x_q)

print("\\nScale:")
print(scale)

print("\\nDequantized:")
print(x_hat)

print("\\nError:")
print(x x_hat)


2.12 运行结果应该怎么看?

你应该重点观察四个东西:Original、Quantized、Scale、Dequantized。它们分别对应量化流程的各个阶段:

#mermaid-svg-tvu6RGExiOIwrk9e{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-tvu6RGExiOIwrk9e .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-tvu6RGExiOIwrk9e .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-tvu6RGExiOIwrk9e .error-icon{fill:#552222;}#mermaid-svg-tvu6RGExiOIwrk9e .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-tvu6RGExiOIwrk9e .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-tvu6RGExiOIwrk9e .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-tvu6RGExiOIwrk9e .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-tvu6RGExiOIwrk9e .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-tvu6RGExiOIwrk9e .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-tvu6RGExiOIwrk9e .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-tvu6RGExiOIwrk9e .marker{fill:#333333;stroke:#333333;}#mermaid-svg-tvu6RGExiOIwrk9e .marker.cross{stroke:#333333;}#mermaid-svg-tvu6RGExiOIwrk9e svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-tvu6RGExiOIwrk9e p{margin:0;}#mermaid-svg-tvu6RGExiOIwrk9e .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-tvu6RGExiOIwrk9e .cluster-label text{fill:#333;}#mermaid-svg-tvu6RGExiOIwrk9e .cluster-label span{color:#333;}#mermaid-svg-tvu6RGExiOIwrk9e .cluster-label span p{background-color:transparent;}#mermaid-svg-tvu6RGExiOIwrk9e .label text,#mermaid-svg-tvu6RGExiOIwrk9e span{fill:#333;color:#333;}#mermaid-svg-tvu6RGExiOIwrk9e .node rect,#mermaid-svg-tvu6RGExiOIwrk9e .node circle,#mermaid-svg-tvu6RGExiOIwrk9e .node ellipse,#mermaid-svg-tvu6RGExiOIwrk9e .node polygon,#mermaid-svg-tvu6RGExiOIwrk9e .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-tvu6RGExiOIwrk9e .rough-node .label text,#mermaid-svg-tvu6RGExiOIwrk9e .node .label text,#mermaid-svg-tvu6RGExiOIwrk9e .image-shape .label,#mermaid-svg-tvu6RGExiOIwrk9e .icon-shape .label{text-anchor:middle;}#mermaid-svg-tvu6RGExiOIwrk9e .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-tvu6RGExiOIwrk9e .rough-node .label,#mermaid-svg-tvu6RGExiOIwrk9e .node .label,#mermaid-svg-tvu6RGExiOIwrk9e .image-shape .label,#mermaid-svg-tvu6RGExiOIwrk9e .icon-shape .label{text-align:center;}#mermaid-svg-tvu6RGExiOIwrk9e .node.clickable{cursor:pointer;}#mermaid-svg-tvu6RGExiOIwrk9e .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-tvu6RGExiOIwrk9e .arrowheadPath{fill:#333333;}#mermaid-svg-tvu6RGExiOIwrk9e .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-tvu6RGExiOIwrk9e .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-tvu6RGExiOIwrk9e .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-tvu6RGExiOIwrk9e .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-tvu6RGExiOIwrk9e .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-tvu6RGExiOIwrk9e .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-tvu6RGExiOIwrk9e .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-tvu6RGExiOIwrk9e .cluster text{fill:#333;}#mermaid-svg-tvu6RGExiOIwrk9e .cluster span{color:#333;}#mermaid-svg-tvu6RGExiOIwrk9e div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-tvu6RGExiOIwrk9e .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-tvu6RGExiOIwrk9e rect.text{fill:none;stroke-width:0;}#mermaid-svg-tvu6RGExiOIwrk9e .icon-shape,#mermaid-svg-tvu6RGExiOIwrk9e .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-tvu6RGExiOIwrk9e .icon-shape p,#mermaid-svg-tvu6RGExiOIwrk9e .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-tvu6RGExiOIwrk9e .icon-shape .label rect,#mermaid-svg-tvu6RGExiOIwrk9e .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-tvu6RGExiOIwrk9e .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-tvu6RGExiOIwrk9e .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-tvu6RGExiOIwrk9e :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

原始数据

量化

低比特整数

反量化

近似原始数据

也就是 x→xq→x^x \\rightarrow x_q \\rightarrow \\hat{x}xxqx^。最后比较 x−x^x – \\hat{x}xx^,就能够看到:量化到底产生了多少误差。


2.13 量化模型里存的到底是什么?

看到这里,很多人会产生一个非常自然的疑问:

量化之后,模型权重变成整数了吗?那计算时用这么大的整数,会不会让 Softmax 之类的运算爆炸?

这是理解量化时最关键、也最容易卡住的一点。答案要分成两个层面来看:存储态和计算态。

2.13.1 存储态:整数 + Scale

量化模型在磁盘和显存里保存的,确实是低比特整数(INT8 是 [−128,127][-128, 127][128,127],INT4 是 [−8,7][-8, 7][8,7])。但它从来不是只存整数——每个整数(或每组整数)还配套保存了对应的 Scale(必要时还有 Zero Point)。也就是说,权重是以"整数 + Scale"这一对的形式存下来的:

#mermaid-svg-r3fd2vX5ziEdoLKB{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-r3fd2vX5ziEdoLKB .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-r3fd2vX5ziEdoLKB .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-r3fd2vX5ziEdoLKB .error-icon{fill:#552222;}#mermaid-svg-r3fd2vX5ziEdoLKB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-r3fd2vX5ziEdoLKB .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-r3fd2vX5ziEdoLKB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-r3fd2vX5ziEdoLKB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-r3fd2vX5ziEdoLKB .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-r3fd2vX5ziEdoLKB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-r3fd2vX5ziEdoLKB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-r3fd2vX5ziEdoLKB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-r3fd2vX5ziEdoLKB .marker.cross{stroke:#333333;}#mermaid-svg-r3fd2vX5ziEdoLKB svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-r3fd2vX5ziEdoLKB p{margin:0;}#mermaid-svg-r3fd2vX5ziEdoLKB .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-r3fd2vX5ziEdoLKB .cluster-label text{fill:#333;}#mermaid-svg-r3fd2vX5ziEdoLKB .cluster-label span{color:#333;}#mermaid-svg-r3fd2vX5ziEdoLKB .cluster-label span p{background-color:transparent;}#mermaid-svg-r3fd2vX5ziEdoLKB .label text,#mermaid-svg-r3fd2vX5ziEdoLKB span{fill:#333;color:#333;}#mermaid-svg-r3fd2vX5ziEdoLKB .node rect,#mermaid-svg-r3fd2vX5ziEdoLKB .node circle,#mermaid-svg-r3fd2vX5ziEdoLKB .node ellipse,#mermaid-svg-r3fd2vX5ziEdoLKB .node polygon,#mermaid-svg-r3fd2vX5ziEdoLKB .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-r3fd2vX5ziEdoLKB .rough-node .label text,#mermaid-svg-r3fd2vX5ziEdoLKB .node .label text,#mermaid-svg-r3fd2vX5ziEdoLKB .image-shape .label,#mermaid-svg-r3fd2vX5ziEdoLKB .icon-shape .label{text-anchor:middle;}#mermaid-svg-r3fd2vX5ziEdoLKB .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-r3fd2vX5ziEdoLKB .rough-node .label,#mermaid-svg-r3fd2vX5ziEdoLKB .node .label,#mermaid-svg-r3fd2vX5ziEdoLKB .image-shape .label,#mermaid-svg-r3fd2vX5ziEdoLKB .icon-shape .label{text-align:center;}#mermaid-svg-r3fd2vX5ziEdoLKB .node.clickable{cursor:pointer;}#mermaid-svg-r3fd2vX5ziEdoLKB .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-r3fd2vX5ziEdoLKB .arrowheadPath{fill:#333333;}#mermaid-svg-r3fd2vX5ziEdoLKB .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-r3fd2vX5ziEdoLKB .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-r3fd2vX5ziEdoLKB .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-r3fd2vX5ziEdoLKB .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-r3fd2vX5ziEdoLKB .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-r3fd2vX5ziEdoLKB .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-r3fd2vX5ziEdoLKB .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-r3fd2vX5ziEdoLKB .cluster text{fill:#333;}#mermaid-svg-r3fd2vX5ziEdoLKB .cluster span{color:#333;}#mermaid-svg-r3fd2vX5ziEdoLKB div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-r3fd2vX5ziEdoLKB .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-r3fd2vX5ziEdoLKB rect.text{fill:none;stroke-width:0;}#mermaid-svg-r3fd2vX5ziEdoLKB .icon-shape,#mermaid-svg-r3fd2vX5ziEdoLKB .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-r3fd2vX5ziEdoLKB .icon-shape p,#mermaid-svg-r3fd2vX5ziEdoLKB .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-r3fd2vX5ziEdoLKB .icon-shape .label rect,#mermaid-svg-r3fd2vX5ziEdoLKB .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-r3fd2vX5ziEdoLKB .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-r3fd2vX5ziEdoLKB .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-r3fd2vX5ziEdoLKB :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

原始权重 0.7321

量化: round(0.7321 / Scale) = 93

存储态: 整数 93 + Scale 0.00787

整数 93 只是压缩后的存储形态,它本身不代表 0.73 这个数值,必须配合 Scale 才有意义。

2.13.2 计算态:先反量化回小数

真正参与矩阵乘法之前,会先做一步反量化(Dequantization),把整数乘回 Scale,还原成接近原始 FP16 的小数:

w^=wint×s=93×0.00787≈0.732\\hat{w} = w_{int} \\times s = 93 \\times 0.00787 \\approx 0.732w^=wint×s=93×0.007870.732

这正是 2.11 里 dequantize(x_q, scale) 做的事情。所以:

#mermaid-svg-MjXoZ8LUL50OkUYQ{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-MjXoZ8LUL50OkUYQ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MjXoZ8LUL50OkUYQ .error-icon{fill:#552222;}#mermaid-svg-MjXoZ8LUL50OkUYQ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MjXoZ8LUL50OkUYQ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MjXoZ8LUL50OkUYQ .marker.cross{stroke:#333333;}#mermaid-svg-MjXoZ8LUL50OkUYQ svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MjXoZ8LUL50OkUYQ p{margin:0;}#mermaid-svg-MjXoZ8LUL50OkUYQ .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-MjXoZ8LUL50OkUYQ .cluster-label text{fill:#333;}#mermaid-svg-MjXoZ8LUL50OkUYQ .cluster-label span{color:#333;}#mermaid-svg-MjXoZ8LUL50OkUYQ .cluster-label span p{background-color:transparent;}#mermaid-svg-MjXoZ8LUL50OkUYQ .label text,#mermaid-svg-MjXoZ8LUL50OkUYQ span{fill:#333;color:#333;}#mermaid-svg-MjXoZ8LUL50OkUYQ .node rect,#mermaid-svg-MjXoZ8LUL50OkUYQ .node circle,#mermaid-svg-MjXoZ8LUL50OkUYQ .node ellipse,#mermaid-svg-MjXoZ8LUL50OkUYQ .node polygon,#mermaid-svg-MjXoZ8LUL50OkUYQ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MjXoZ8LUL50OkUYQ .rough-node .label text,#mermaid-svg-MjXoZ8LUL50OkUYQ .node .label text,#mermaid-svg-MjXoZ8LUL50OkUYQ .image-shape .label,#mermaid-svg-MjXoZ8LUL50OkUYQ .icon-shape .label{text-anchor:middle;}#mermaid-svg-MjXoZ8LUL50OkUYQ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-MjXoZ8LUL50OkUYQ .rough-node .label,#mermaid-svg-MjXoZ8LUL50OkUYQ .node .label,#mermaid-svg-MjXoZ8LUL50OkUYQ .image-shape .label,#mermaid-svg-MjXoZ8LUL50OkUYQ .icon-shape .label{text-align:center;}#mermaid-svg-MjXoZ8LUL50OkUYQ .node.clickable{cursor:pointer;}#mermaid-svg-MjXoZ8LUL50OkUYQ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-MjXoZ8LUL50OkUYQ .arrowheadPath{fill:#333333;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MjXoZ8LUL50OkUYQ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MjXoZ8LUL50OkUYQ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-MjXoZ8LUL50OkUYQ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MjXoZ8LUL50OkUYQ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-MjXoZ8LUL50OkUYQ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MjXoZ8LUL50OkUYQ .cluster text{fill:#333;}#mermaid-svg-MjXoZ8LUL50OkUYQ .cluster span{color:#333;}#mermaid-svg-MjXoZ8LUL50OkUYQ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MjXoZ8LUL50OkUYQ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-MjXoZ8LUL50OkUYQ rect.text{fill:none;stroke-width:0;}#mermaid-svg-MjXoZ8LUL50OkUYQ .icon-shape,#mermaid-svg-MjXoZ8LUL50OkUYQ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MjXoZ8LUL50OkUYQ .icon-shape p,#mermaid-svg-MjXoZ8LUL50OkUYQ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-MjXoZ8LUL50OkUYQ .icon-shape .label rect,#mermaid-svg-MjXoZ8LUL50OkUYQ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MjXoZ8LUL50OkUYQ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-MjXoZ8LUL50OkUYQ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-MjXoZ8LUL50OkUYQ :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

存储态: 整数 93

反量化: 93 × Scale

计算态: 小数 0.732

参与矩阵乘 / 进入后续层

网络里实际流动、参与运算的,始终是正常范围的浮点数,不是那些整数。

2.13.3 那 Softmax 会爆炸吗?

不会。原因有两个:

  • 权重不会直接进 Softmax,而且进计算前已经被还原成小数。 Softmax 作用在 attention 分数(QK⊤/dQK^\\top / \\sqrt{d}QK/d)上,参与计算的权重此时已经反量化回 0.7 这种正常数值,整数 93、-8 只是躺在显存里的存储形态,不会出现在计算里。

  • Softmax 本身自带防溢出机制。 标准实现都会先减去最大值再取指数:softmax(x)=softmax(x−max⁡(x))\\text{softmax}(x) = \\text{softmax}(x – \\max(x))softmax(x)=softmax(xmax(x)),即使输入偏大也不会溢出。这在量化之前就是常规做法。

  • 一句话记住:

    量化模型存的是"整数 + Scale"这一对。整数是压缩后的存储形态,计算时先反量化成小数,网络里流动的仍是正常浮点数——所以 Softmax 看到的从来不是那些整数。

    也正因为如此,Scale 的选择才如此重要:它直接决定了"整数 × Scale"能把原始权重还原得多准。后面第 4 章的 Group-wise、第 5 章的 GPTQ、第 6 章的 AWQ,本质上都是在想办法让这个还原更接近原始权重,从而把量化误差压到最小。


    2.14 衡量量化误差

    一个最简单的方法是 MAE(Mean Absolute Error,平均绝对误差):

    MAE=1N∑i∣xi−x^i∣MAE = \\frac{1}{N} \\sum_i |x_i – \\hat{x}_i|MAE=N1ixix^i

    Python:

    mae = np.mean(np.abs(x x_hat))

    print("MAE:", mae)

    也可以计算 MSE:

    MSE=1N∑i(xi−x^i)2MSE = \\frac{1}{N} \\sum_i (x_i – \\hat{x}_i)^2MSE=N1i(xix^i)2

    Python:

    mse = np.mean((x x_hat) ** 2)

    print("MSE:", mse)

    这样我们就拥有了一个非常基础的量化实验:

    #mermaid-svg-X1HASBJSCkW1kBCN{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-X1HASBJSCkW1kBCN .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-X1HASBJSCkW1kBCN .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-X1HASBJSCkW1kBCN .error-icon{fill:#552222;}#mermaid-svg-X1HASBJSCkW1kBCN .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-X1HASBJSCkW1kBCN .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-X1HASBJSCkW1kBCN .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-X1HASBJSCkW1kBCN .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-X1HASBJSCkW1kBCN .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-X1HASBJSCkW1kBCN .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-X1HASBJSCkW1kBCN .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-X1HASBJSCkW1kBCN .marker{fill:#333333;stroke:#333333;}#mermaid-svg-X1HASBJSCkW1kBCN .marker.cross{stroke:#333333;}#mermaid-svg-X1HASBJSCkW1kBCN svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-X1HASBJSCkW1kBCN p{margin:0;}#mermaid-svg-X1HASBJSCkW1kBCN .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-X1HASBJSCkW1kBCN .cluster-label text{fill:#333;}#mermaid-svg-X1HASBJSCkW1kBCN .cluster-label span{color:#333;}#mermaid-svg-X1HASBJSCkW1kBCN .cluster-label span p{background-color:transparent;}#mermaid-svg-X1HASBJSCkW1kBCN .label text,#mermaid-svg-X1HASBJSCkW1kBCN span{fill:#333;color:#333;}#mermaid-svg-X1HASBJSCkW1kBCN .node rect,#mermaid-svg-X1HASBJSCkW1kBCN .node circle,#mermaid-svg-X1HASBJSCkW1kBCN .node ellipse,#mermaid-svg-X1HASBJSCkW1kBCN .node polygon,#mermaid-svg-X1HASBJSCkW1kBCN .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-X1HASBJSCkW1kBCN .rough-node .label text,#mermaid-svg-X1HASBJSCkW1kBCN .node .label text,#mermaid-svg-X1HASBJSCkW1kBCN .image-shape .label,#mermaid-svg-X1HASBJSCkW1kBCN .icon-shape .label{text-anchor:middle;}#mermaid-svg-X1HASBJSCkW1kBCN .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-X1HASBJSCkW1kBCN .rough-node .label,#mermaid-svg-X1HASBJSCkW1kBCN .node .label,#mermaid-svg-X1HASBJSCkW1kBCN .image-shape .label,#mermaid-svg-X1HASBJSCkW1kBCN .icon-shape .label{text-align:center;}#mermaid-svg-X1HASBJSCkW1kBCN .node.clickable{cursor:pointer;}#mermaid-svg-X1HASBJSCkW1kBCN .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-X1HASBJSCkW1kBCN .arrowheadPath{fill:#333333;}#mermaid-svg-X1HASBJSCkW1kBCN .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-X1HASBJSCkW1kBCN .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-X1HASBJSCkW1kBCN .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-X1HASBJSCkW1kBCN .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-X1HASBJSCkW1kBCN .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-X1HASBJSCkW1kBCN .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-X1HASBJSCkW1kBCN .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-X1HASBJSCkW1kBCN .cluster text{fill:#333;}#mermaid-svg-X1HASBJSCkW1kBCN .cluster span{color:#333;}#mermaid-svg-X1HASBJSCkW1kBCN div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-X1HASBJSCkW1kBCN .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-X1HASBJSCkW1kBCN rect.text{fill:none;stroke-width:0;}#mermaid-svg-X1HASBJSCkW1kBCN .icon-shape,#mermaid-svg-X1HASBJSCkW1kBCN .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-X1HASBJSCkW1kBCN .icon-shape p,#mermaid-svg-X1HASBJSCkW1kBCN .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-X1HASBJSCkW1kBCN .icon-shape .label rect,#mermaid-svg-X1HASBJSCkW1kBCN .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-X1HASBJSCkW1kBCN .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-X1HASBJSCkW1kBCN .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-X1HASBJSCkW1kBCN :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    原始数据

    INT8 Quantization

    Dequantization

    计算 MAE / MSE


    2.15 为什么量化误差不能只看一个数字?

    假设我们有两个量化方法:方法 A 的 MAE = 0.001,方法 B 的 MAE = 0.002。是不是 A 一定比 B 好?

    对于一个普通 Tensor,也许 A 的误差确实更小。但对于 LLM:

    单纯看某一层权重的平均误差并不能完全预测最终模型效果。

    原因是误差会在多层传播:

    #mermaid-svg-wNykUckqco7JldMZ{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wNykUckqco7JldMZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wNykUckqco7JldMZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wNykUckqco7JldMZ .error-icon{fill:#552222;}#mermaid-svg-wNykUckqco7JldMZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wNykUckqco7JldMZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wNykUckqco7JldMZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wNykUckqco7JldMZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wNykUckqco7JldMZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wNykUckqco7JldMZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wNykUckqco7JldMZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wNykUckqco7JldMZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wNykUckqco7JldMZ .marker.cross{stroke:#333333;}#mermaid-svg-wNykUckqco7JldMZ svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wNykUckqco7JldMZ p{margin:0;}#mermaid-svg-wNykUckqco7JldMZ .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-wNykUckqco7JldMZ .cluster-label text{fill:#333;}#mermaid-svg-wNykUckqco7JldMZ .cluster-label span{color:#333;}#mermaid-svg-wNykUckqco7JldMZ .cluster-label span p{background-color:transparent;}#mermaid-svg-wNykUckqco7JldMZ .label text,#mermaid-svg-wNykUckqco7JldMZ span{fill:#333;color:#333;}#mermaid-svg-wNykUckqco7JldMZ .node rect,#mermaid-svg-wNykUckqco7JldMZ .node circle,#mermaid-svg-wNykUckqco7JldMZ .node ellipse,#mermaid-svg-wNykUckqco7JldMZ .node polygon,#mermaid-svg-wNykUckqco7JldMZ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wNykUckqco7JldMZ .rough-node .label text,#mermaid-svg-wNykUckqco7JldMZ .node .label text,#mermaid-svg-wNykUckqco7JldMZ .image-shape .label,#mermaid-svg-wNykUckqco7JldMZ .icon-shape .label{text-anchor:middle;}#mermaid-svg-wNykUckqco7JldMZ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wNykUckqco7JldMZ .rough-node .label,#mermaid-svg-wNykUckqco7JldMZ .node .label,#mermaid-svg-wNykUckqco7JldMZ .image-shape .label,#mermaid-svg-wNykUckqco7JldMZ .icon-shape .label{text-align:center;}#mermaid-svg-wNykUckqco7JldMZ .node.clickable{cursor:pointer;}#mermaid-svg-wNykUckqco7JldMZ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wNykUckqco7JldMZ .arrowheadPath{fill:#333333;}#mermaid-svg-wNykUckqco7JldMZ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wNykUckqco7JldMZ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wNykUckqco7JldMZ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wNykUckqco7JldMZ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wNykUckqco7JldMZ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wNykUckqco7JldMZ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wNykUckqco7JldMZ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wNykUckqco7JldMZ .cluster text{fill:#333;}#mermaid-svg-wNykUckqco7JldMZ .cluster span{color:#333;}#mermaid-svg-wNykUckqco7JldMZ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wNykUckqco7JldMZ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wNykUckqco7JldMZ rect.text{fill:none;stroke-width:0;}#mermaid-svg-wNykUckqco7JldMZ .icon-shape,#mermaid-svg-wNykUckqco7JldMZ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wNykUckqco7JldMZ .icon-shape p,#mermaid-svg-wNykUckqco7JldMZ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wNykUckqco7JldMZ .icon-shape .label rect,#mermaid-svg-wNykUckqco7JldMZ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wNykUckqco7JldMZ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wNykUckqco7JldMZ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wNykUckqco7JldMZ :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    权重误差

    Linear

    Activation

    Attention

    很多 Transformer Block

    最终输出

    一个看似很小的局部误差,可能在多层传播后产生不同程度的影响。所以真正的 LLM 量化算法不会只追求"参数数值离原参数最近",而会进一步考虑:

    哪些权重更重要?怎样的误差更容易被模型容忍?

    这就是后面 GPTQ 和 AWQ 要解决的问题。


    2.16 从简单 Round() 到真正的 LLM Quantization

    现在我们已经可以自己实现 FP → Scale → Round → INT8,但这只是最基础的量化。真实的 LLM 量化远比这个复杂。可以把整个发展路线理解成:

    #mermaid-svg-8XzpctkCIFpJAksa{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-8XzpctkCIFpJAksa .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-8XzpctkCIFpJAksa .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-8XzpctkCIFpJAksa .error-icon{fill:#552222;}#mermaid-svg-8XzpctkCIFpJAksa .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-8XzpctkCIFpJAksa .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-8XzpctkCIFpJAksa .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-8XzpctkCIFpJAksa .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-8XzpctkCIFpJAksa .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-8XzpctkCIFpJAksa .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-8XzpctkCIFpJAksa .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-8XzpctkCIFpJAksa .marker{fill:#333333;stroke:#333333;}#mermaid-svg-8XzpctkCIFpJAksa .marker.cross{stroke:#333333;}#mermaid-svg-8XzpctkCIFpJAksa svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-8XzpctkCIFpJAksa p{margin:0;}#mermaid-svg-8XzpctkCIFpJAksa .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-8XzpctkCIFpJAksa .cluster-label text{fill:#333;}#mermaid-svg-8XzpctkCIFpJAksa .cluster-label span{color:#333;}#mermaid-svg-8XzpctkCIFpJAksa .cluster-label span p{background-color:transparent;}#mermaid-svg-8XzpctkCIFpJAksa .label text,#mermaid-svg-8XzpctkCIFpJAksa span{fill:#333;color:#333;}#mermaid-svg-8XzpctkCIFpJAksa .node rect,#mermaid-svg-8XzpctkCIFpJAksa .node circle,#mermaid-svg-8XzpctkCIFpJAksa .node ellipse,#mermaid-svg-8XzpctkCIFpJAksa .node polygon,#mermaid-svg-8XzpctkCIFpJAksa .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-8XzpctkCIFpJAksa .rough-node .label text,#mermaid-svg-8XzpctkCIFpJAksa .node .label text,#mermaid-svg-8XzpctkCIFpJAksa .image-shape .label,#mermaid-svg-8XzpctkCIFpJAksa .icon-shape .label{text-anchor:middle;}#mermaid-svg-8XzpctkCIFpJAksa .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-8XzpctkCIFpJAksa .rough-node .label,#mermaid-svg-8XzpctkCIFpJAksa .node .label,#mermaid-svg-8XzpctkCIFpJAksa .image-shape .label,#mermaid-svg-8XzpctkCIFpJAksa .icon-shape .label{text-align:center;}#mermaid-svg-8XzpctkCIFpJAksa .node.clickable{cursor:pointer;}#mermaid-svg-8XzpctkCIFpJAksa .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-8XzpctkCIFpJAksa .arrowheadPath{fill:#333333;}#mermaid-svg-8XzpctkCIFpJAksa .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-8XzpctkCIFpJAksa .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-8XzpctkCIFpJAksa .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8XzpctkCIFpJAksa .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-8XzpctkCIFpJAksa .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8XzpctkCIFpJAksa .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-8XzpctkCIFpJAksa .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-8XzpctkCIFpJAksa .cluster text{fill:#333;}#mermaid-svg-8XzpctkCIFpJAksa .cluster span{color:#333;}#mermaid-svg-8XzpctkCIFpJAksa div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-8XzpctkCIFpJAksa .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-8XzpctkCIFpJAksa rect.text{fill:none;stroke-width:0;}#mermaid-svg-8XzpctkCIFpJAksa .icon-shape,#mermaid-svg-8XzpctkCIFpJAksa .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8XzpctkCIFpJAksa .icon-shape p,#mermaid-svg-8XzpctkCIFpJAksa .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-8XzpctkCIFpJAksa .icon-shape .label rect,#mermaid-svg-8XzpctkCIFpJAksa .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8XzpctkCIFpJAksa .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-8XzpctkCIFpJAksa .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-8XzpctkCIFpJAksa :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    最简单的量化x → Scale → Round → INT8

    Per-Channel每个 Channel 自己的 Scale

    Group-wise每个 Group 自己的 Scale

    GPTQ / AWQ不仅考虑数值误差,还考虑模型行为

    NF4针对神经网络权重分布设计更合适的低比特表示

    因此,本章真正想建立的不是"我会一个 NumPy 量化函数",而是:

    我知道量化的基本数学结构,知道误差从哪里来,也理解为什么真实 LLM 量化必须做得更加聪明。


    2.17 本章小结

    这一章我们从一个最简单的浮点数开始,一步一步理解了量化。最核心的公式:

    xq=round(xs),x^=s⋅xq,E=x−x^\\boxed{x_q = \\text{round}\\left(\\frac{x}{s}\\right)}, \\qquad \\boxed{\\hat{x} = s \\cdot x_q}, \\qquad \\boxed{E = x – \\hat{x}}xq=round(sx),x^=sxq,E=xx^

    2.16.1 Scale

    Scale 可以理解成浮点空间与整数空间之间的"刻度尺",例如 s=max⁡(∣x∣)qmaxs = \\frac{\\max(|x|)}{q_{max}}s=qmaxmax(x)

    2.16.2 Zero Point

    Zero Point 可以理解成浮点数 0 在整数空间中的对应位置。因此:Scale 负责缩放,Zero Point 负责平移。

    2.16.3 Quantization 粒度

    我们学习了三种典型方式:

    #mermaid-svg-gt0Ax7vWTzpJ9SCn{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .error-icon{fill:#552222;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .marker.cross{stroke:#333333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gt0Ax7vWTzpJ9SCn p{margin:0;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .cluster-label text{fill:#333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .cluster-label span{color:#333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .cluster-label span p{background-color:transparent;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .label text,#mermaid-svg-gt0Ax7vWTzpJ9SCn span{fill:#333;color:#333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .node rect,#mermaid-svg-gt0Ax7vWTzpJ9SCn .node circle,#mermaid-svg-gt0Ax7vWTzpJ9SCn .node ellipse,#mermaid-svg-gt0Ax7vWTzpJ9SCn .node polygon,#mermaid-svg-gt0Ax7vWTzpJ9SCn .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .rough-node .label text,#mermaid-svg-gt0Ax7vWTzpJ9SCn .node .label text,#mermaid-svg-gt0Ax7vWTzpJ9SCn .image-shape .label,#mermaid-svg-gt0Ax7vWTzpJ9SCn .icon-shape .label{text-anchor:middle;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .rough-node .label,#mermaid-svg-gt0Ax7vWTzpJ9SCn .node .label,#mermaid-svg-gt0Ax7vWTzpJ9SCn .image-shape .label,#mermaid-svg-gt0Ax7vWTzpJ9SCn .icon-shape .label{text-align:center;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .node.clickable{cursor:pointer;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .arrowheadPath{fill:#333333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-gt0Ax7vWTzpJ9SCn .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-gt0Ax7vWTzpJ9SCn .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-gt0Ax7vWTzpJ9SCn .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .cluster text{fill:#333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .cluster span{color:#333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-gt0Ax7vWTzpJ9SCn rect.text{fill:none;stroke-width:0;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .icon-shape,#mermaid-svg-gt0Ax7vWTzpJ9SCn .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .icon-shape p,#mermaid-svg-gt0Ax7vWTzpJ9SCn .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .icon-shape .label rect,#mermaid-svg-gt0Ax7vWTzpJ9SCn .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-gt0Ax7vWTzpJ9SCn .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-gt0Ax7vWTzpJ9SCn .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-gt0Ax7vWTzpJ9SCn :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    Per-Tensor一个 Tensor 一个 Scale

    Per-Channel一个 Channel 一个 Scale

    Group-wise一个 Group 一个 Scale

    粒度越细:通常能够获得更好的量化适应性,但也需要保存更多辅助量化参数。

    2.16.4 INT8 与 INT4

    INT8 有 28=2562^8 = 25628=256 种状态,INT4 有 24=162^4 = 1624=16 种状态。因此:

    Bit 越低,可表达的离散状态越少,对量化策略的要求越高。


    2.18 下一章:PTQ

    到现在,我们解决了"一个浮点数如何变成一个 INT8 数字?"但是对于真正的 LLM,还有一个非常大的问题:

    模型已经训练完成了,我们怎么给它做量化?

    这时候就会进入 PTQ——Post-Training Quantization:

    #mermaid-svg-gSUbyLPPZd9mfFUa{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-gSUbyLPPZd9mfFUa .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-gSUbyLPPZd9mfFUa .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-gSUbyLPPZd9mfFUa .error-icon{fill:#552222;}#mermaid-svg-gSUbyLPPZd9mfFUa .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gSUbyLPPZd9mfFUa .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-gSUbyLPPZd9mfFUa .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gSUbyLPPZd9mfFUa .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gSUbyLPPZd9mfFUa .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-gSUbyLPPZd9mfFUa .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gSUbyLPPZd9mfFUa .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gSUbyLPPZd9mfFUa .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gSUbyLPPZd9mfFUa .marker.cross{stroke:#333333;}#mermaid-svg-gSUbyLPPZd9mfFUa svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gSUbyLPPZd9mfFUa p{margin:0;}#mermaid-svg-gSUbyLPPZd9mfFUa .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-gSUbyLPPZd9mfFUa .cluster-label text{fill:#333;}#mermaid-svg-gSUbyLPPZd9mfFUa .cluster-label span{color:#333;}#mermaid-svg-gSUbyLPPZd9mfFUa .cluster-label span p{background-color:transparent;}#mermaid-svg-gSUbyLPPZd9mfFUa .label text,#mermaid-svg-gSUbyLPPZd9mfFUa span{fill:#333;color:#333;}#mermaid-svg-gSUbyLPPZd9mfFUa .node rect,#mermaid-svg-gSUbyLPPZd9mfFUa .node circle,#mermaid-svg-gSUbyLPPZd9mfFUa .node ellipse,#mermaid-svg-gSUbyLPPZd9mfFUa .node polygon,#mermaid-svg-gSUbyLPPZd9mfFUa .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-gSUbyLPPZd9mfFUa .rough-node .label text,#mermaid-svg-gSUbyLPPZd9mfFUa .node .label text,#mermaid-svg-gSUbyLPPZd9mfFUa .image-shape .label,#mermaid-svg-gSUbyLPPZd9mfFUa .icon-shape .label{text-anchor:middle;}#mermaid-svg-gSUbyLPPZd9mfFUa .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-gSUbyLPPZd9mfFUa .rough-node .label,#mermaid-svg-gSUbyLPPZd9mfFUa .node .label,#mermaid-svg-gSUbyLPPZd9mfFUa .image-shape .label,#mermaid-svg-gSUbyLPPZd9mfFUa .icon-shape .label{text-align:center;}#mermaid-svg-gSUbyLPPZd9mfFUa .node.clickable{cursor:pointer;}#mermaid-svg-gSUbyLPPZd9mfFUa .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-gSUbyLPPZd9mfFUa .arrowheadPath{fill:#333333;}#mermaid-svg-gSUbyLPPZd9mfFUa .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-gSUbyLPPZd9mfFUa .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-gSUbyLPPZd9mfFUa .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-gSUbyLPPZd9mfFUa .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-gSUbyLPPZd9mfFUa .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-gSUbyLPPZd9mfFUa .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-gSUbyLPPZd9mfFUa .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-gSUbyLPPZd9mfFUa .cluster text{fill:#333;}#mermaid-svg-gSUbyLPPZd9mfFUa .cluster span{color:#333;}#mermaid-svg-gSUbyLPPZd9mfFUa div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-gSUbyLPPZd9mfFUa .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-gSUbyLPPZd9mfFUa rect.text{fill:none;stroke-width:0;}#mermaid-svg-gSUbyLPPZd9mfFUa .icon-shape,#mermaid-svg-gSUbyLPPZd9mfFUa .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-gSUbyLPPZd9mfFUa .icon-shape p,#mermaid-svg-gSUbyLPPZd9mfFUa .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-gSUbyLPPZd9mfFUa .icon-shape .label rect,#mermaid-svg-gSUbyLPPZd9mfFUa .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-gSUbyLPPZd9mfFUa .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-gSUbyLPPZd9mfFUa .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-gSUbyLPPZd9mfFUa :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    完整训练好的 LLM

    不重新训练

    PTQ

    INT8 / INT4 量化模型

    下一章我们将正式进入 LLM Quantization 的第一个真正工程阶段:

    第3章 PTQ:不用重新训练也能量化 LLM

    届时我们会进一步回答:

    • 为什么 LLM 特别适合 Weight-only Quantization?
    • 什么是 W8A16?
    • 什么是 W4A16?
    • 为什么 Activation Quantization 更难?
    • Calibration 到底在干什么?
    • 为什么 GPTQ 和 AWQ 都属于更加高级的 PTQ 思路?
    赞(0)
    未经允许不得转载:171主机测评 » 第2章 什么是量化?用 3 个数字算懂 INT8
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址