欢迎光临
我们一直在努力

第4章 LLM 4-bit 量化到底意味着什么?

第4章 LLM 4-bit 量化到底意味着什么?

本章目录:

  • 4.1 4-bit 让模型“小这么多”的原理
  • 4.2 INT4 的基本表示
  • 4.3 低到 4-bit 仍然可以工作的原因
  • 4.4 W4A16 到底是什么?
  • 4.5 第一次真实 4-bit 推理
  • 4.6 本章小结 前面第1章和第2章,我们已经回答了两个基础问题:

第一:

为什么 LLM 需要量化?

因为模型参数越来越大,FP16/BF16 权重会占用大量显存和存储空间。

第二:

量化到底做了什么?

本质上是:

C

o

n

t

i

n

u

o

u

s

 

V

a

l

u

e

D

i

s

c

r

e

t

e

 

V

a

l

u

e

\\boxed{ Continuous\\ Value \\rightarrow Discrete\\ Value }

Continuous ValueDiscrete Value

并通过:

x

q

=

r

o

u

n

d

(

x

/

s

)

x_q=round(x/s)

xq=round(x/s) 将高精度浮点数映射到低比特整数表示。

第3章进一步学习了 PTQ:

P

o

s

t

 

T

r

a

i

n

i

n

g

 

Q

u

a

n

t

i

z

a

t

i

o

n

\\boxed{ Post\\ Training\\ Quantization }

Post Training Quantization

即模型训练完成以后再进行量化。

那么一个自然的问题出现了:

到底什么叫“4-bit 量化”?

例如我们经常看到:

Llama 7B 4-bit
Qwen 7B 4-bit
GPTQ 4-bit
AWQ 4-bit
GGUF Q4

这里的:

4

b

i

t

\\boxed{4-bit}

4bit

到底意味着什么?

为什么从:

F

P

16

FP16

FP16 降低到:

I

N

T

4

INT4

INT4

以后,模型可以一下子变得这么小?

为什么 4-bit 只有:

2

4

=

16

2^4=16

24=16 种状态,却还能保存一个拥有几十亿参数的大语言模型?

更重要的是:

为什么量化到如此低的精度以后,模型还能够正常工作?

本章就从这些最基础的问题开始,一步一步把:

4

b

i

t

\\boxed{4-bit}

4bit

真正讲明白。


4.1 4-bit 让模型“小这么多”的原理

先从一个最简单的问题开始:

一个参数到底需要多少空间?

假设一个模型有:

N

N

N 个参数。

如果每个参数使用:

b

b

b 个 bit。

那么仅仅从理论上看,权重存储空间就是:

M

e

m

o

r

y

=

N

×

b

8

Memory=N\\times\\frac{b}{8}

Memory=N×8b 也就是:

M

e

m

o

r

y

B

i

t

W

i

d

t

h

\\boxed{ Memory\\propto BitWidth }

MemoryBitWidth

这就是量化能够显著压缩模型的根本原因。


4.1.1 FP32

FP32:

32

b

i

t

=

4

B

y

t

e

32bit=4Byte

32bit=4Byte 如果有:

7

B

7B

7B

参数:

7

×

10

9

×

4

7\\times10^9\\times4

7×109×4 约等于:

28

G

B

28GB

28GB

所以:

7

B

 

F

P

32

28

G

B

\\boxed{ 7B\\ FP32\\approx28GB }

7B FP3228GB

仅仅是权重。


4.1.2 FP16

FP16:

16

b

i

t

=

2

B

y

t

e

16bit=2Byte

16bit=2Byte 于是:

7

B

×

2

B

y

t

e

14

G

B

7B\\times2Byte \\approx14GB

7B×2Byte14GB

所以:

7

B

 

F

P

16

14

G

B

\\boxed{ 7B\\ FP16\\approx14GB }

7B FP1614GB

这也是为什么很多 7B 模型使用 FP16 加载以后,显存压力会明显增加。


4.1.3 INT8

INT8:

8

b

i

t

=

1

B

y

t

e

8bit=1Byte

8bit=1Byte 那么:

7

B

×

1

B

y

t

e

7

G

B

7B\\times1Byte \\approx7GB

7B×1Byte7GB

也就是:

7

B

 

I

N

T

8

7

G

B

\\boxed{ 7B\\ INT8\\approx7GB }

7B INT87GB

和 FP16 相比:

14

G

B

7

G

B

14GB\\rightarrow7GB

14GB7GB 理论上直接减少一半。


4.1.4 INT4

再进一步:

4

b

i

t

=

0.5

B

y

t

e

4bit=0.5Byte

4bit=0.5Byte 因此:

7

B

×

0.5

B

y

t

e

3.5

G

B

7B\\times0.5Byte \\approx3.5GB

7B×0.5Byte3.5GB

所以:

7

B

 

I

N

T

4

3.5

G

B

\\boxed{ 7B\\ INT4\\approx3.5GB }

7B INT43.5GB

这就是 4-bit 最直观的价值。


4.1.5 FP16 → INT8 → INT4

现在把它们放在一起:

数据类型每参数占用7B 权重理论大小
FP32 4 Byte ≈28 GB
FP16 2 Byte ≈14 GB
INT8 1 Byte ≈7 GB
INT4 0.5 Byte ≈3.5 GB

因此:

F

P

16

I

N

T

8

FP16\\rightarrow INT8

FP16INT8 理论上:

M

e

m

o

r

y

÷

2

Memory\\div2

Memory÷2

而:

F

P

16

I

N

T

4

FP16\\rightarrow INT4

FP16INT4 理论上:

M

e

m

o

r

y

÷

4

Memory\\div4

Memory÷4

这就是为什么 4-bit 量化会如此受到关注。


4.1.6 但是 3.5GB ≠ 实际运行显存

如果:

7

B

 

I

N

T

4

3.5

G

B

7B\\ INT4\\approx3.5GB

7B INT43.5GB 并不意味着:

“运行这个模型只需要 3.5GB 显存。”

因为运行模型时还需要:

K

V

C

a

c

h

e

KVCache

KVCache 以及:

A

c

t

i

v

a

t

i

o

n

Activation

Activation

和:

R

u

n

t

i

m

e

 

B

u

f

f

e

r

Runtime\\ Buffer

Runtime Buffer 所以更准确地写:

$$ \\boxed{ M_{total}

M_{weight} + M_{KV} + M_{activation} + M_{buffer} } $$

因此:

M

o

d

e

l

S

i

z

e

R

u

n

t

i

m

e

M

e

m

o

r

y

ModelSize \\neq RuntimeMemory

ModelSize=RuntimeMemory

这一点在后面的实际部署章节中会越来越重要。


4.1.7 13B 模型呢?

同样计算。

FP16

13

B

×

2

B

y

t

e

26

G

B

13B\\times2Byte \\approx26GB

13B×2Byte26GB

INT8

13

B

×

1

B

y

t

e

13

G

B

13B\\times1Byte \\approx13GB

13B×1Byte13GB

INT4

13

B

×

0.5

B

y

t

e

6.5

G

B

13B\\times0.5Byte \\approx6.5GB

13B×0.5Byte6.5GB

所以:

模型FP16INT8INT4
7B ≈14 GB ≈7 GB ≈3.5 GB
13B ≈26 GB ≈13 GB ≈6.5 GB

注意:

这里仍然只是权重理论值。


4.1.8 70B 模型呢?

这时候差别会更加明显。

FP16

70

B

×

2

B

y

t

e

140

G

B

70B\\times2Byte \\approx140GB

70B×2Byte140GB

INT8

70

B

×

1

B

y

t

e

70

G

B

70B\\times1Byte \\approx70GB

70B×1Byte70GB

INT4

70

B

×

0.5

B

y

t

e

35

G

B

70B\\times0.5Byte \\approx35GB

70B×0.5Byte35GB

因此:

模型FP16INT8INT4
70B ≈140 GB ≈70 GB ≈35 GB

这就出现一个非常有意义的结果:

一个 FP16 需要大约 140GB 权重存储的模型,理论上使用 4-bit 后可以降到大约 35GB。

也就是说:

140

G

B

35

G

B

\\boxed{ 140GB\\rightarrow35GB }

140GB35GB

这就是低比特量化对大模型部署最大的吸引力之一。


4.1.9 4-bit 真的是“模型缩小 4 倍”吗?

如果比较:

F

P

16

FP16

FP16 和:

I

N

T

4

INT4

INT4

理论上:

16

4

=

4

\\frac{16}{4}=4

416=4 所以可以说:

权重存储理论上缩小约 4 倍。

但实际文件一般不会恰好:

4.00

×

4.00\\times

4.00× 因为还需要保存:

Scale
Zero Point
Group Metadata
Tensor Metadata
Alignment

等等辅助信息。

所以实际结果通常是:

接近

4

倍,而不是严格

4

\\boxed{ 接近4倍,而不是严格4倍 }

接近4倍,而不是严格4


4.2 INT4 的基本表示

现在进入本章第二个核心问题:

4-bit 到底能表示多少个数?

答案:

2

4

=

16

2^4=16

24=16 也就是说:

4

b

i

t

=

16

 

s

t

a

t

e

s

\\boxed{ 4bit=16\\ states }

4bit=16 states


4.2.1 只有 16 个状态的由来

一个 bit 只有:

0
1

所以:

2

1

=

2

2^1=2

21=2 两个 bit:

2

2

=

4

2^2=4

22=4

三个 bit:

2

3

=

8

2^3=8

23=8 四个 bit:

2

4

=

16

2^4=16

24=16

因此:

4

b

i

t

16

 

p

o

s

s

i

b

l

e

 

c

o

d

e

s

\\boxed{ 4bit\\rightarrow16\\ possible\\ codes }

4bit16 possible codes


4.2.2 如果是无符号 INT4

一种简单的理解是:

0

15

0\\sim15

015 一共:

16

16

16

个状态。

例如:

0
1
2
3

15


4.2.3 如果是有符号 INT4

如果采用有符号表示,可以理解成一个有限的正负整数范围。

例如常见的对称量化思路可以把量化结果映射到一个以零附近为中心的离散区间。

重点不是记住某一个具体端点,而是理解:

原始浮点数的连续空间,被压缩成了很少的离散等级。


4.2.4 连续空间 → 16 个量化点

假设原来的权重范围:

[

1

,

1

]

[-1,1]

[1,1] 现在只有 16 个等级。

那么我们可以想象:

-1.0 ───────────────────────── +1.0

↓ 量化

●──●──●──●──●──●──●──●──●──●──●──●──●──●──●──●

原来:

任意浮点值都可以出现。

现在:

只能选择附近的某一个离散点。


4.2.5 量化到底损失了什么?

假设:

x

=

0.73

x=0.73

x=0.73 经过 4-bit 量化以后,可能只能表示成:

0.67

0.67

0.67

或者:

0.73

0.75

0.73\\rightarrow0.75

0.730.75 于是产生:

E

r

r

o

r

=

x

x

^

Error=x-\\hat x

Error=xx^

例如:

E

r

r

o

r

=

0.73

0.75

=

0.02

Error=0.73-0.75=-0.02

Error=0.730.75=0.02 这就是:

Q

u

a

n

t

i

z

a

t

i

o

n

 

E

r

r

o

r

\\boxed{ Quantization\\ Error }

Quantization Error


4.2.6 Scale 的作用

我们在第2章已经学习过:

x

q

=

r

o

u

n

d

(

x

/

s

)

x_q=round(x/s)

xq=round(x/s) 其中:

s

s

s

就是 Scale。

再通过:

x

^

=

s

x

q

\\hat x=sx_q

x^=sxq 恢复到浮点空间。

所以:

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

FP Value

除以 Scale

Round

INT4

乘以 Scale

Approximate FP Value

完整写成:

x

x

q

x

^

x \\rightarrow x_q \\rightarrow \\hat x

xxqx^


4.2.7 不能直接把 0.73 变成一个 4-bit 数

因为:

0.73

0.73

0.73 本身是一个连续浮点数。

而 INT4 只能表示:

16

16

16 个离散状态。

所以必须建立一个映射:

C

o

n

t

i

n

u

o

u

s

 

R

a

n

g

e

16

 

d

i

s

c

r

e

t

e

 

l

e

v

e

l

s

\\boxed{ Continuous\\ Range \\rightarrow 16\\ discrete\\ levels }

Continuous Range16 discrete levels

Scale 就是这个映射中的关键参数。


4.2.8 一个简单的 4-bit 例子

假设:

x

[

1

,

1

]

x\\in[-1,1]

x[1,1] 把它划分成 16 个离散等级。

可以粗略想象:

-1.00
-0.87
-0.73
-0.60
-0.47

0.47
0.60
0.73
0.87
1.00

真实量化器的具体离散点会根据:

  • 对称/非对称
  • Scale
  • Zero Point
  • Quantization Scheme

而不同。

但基本思想不变:

把连续空间切成有限数量的格子。


4.2.9 这就产生了一个重要问题

如果所有参数都使用:

同一个 Scale

会怎么样?

假设整个 Layer 中:

99% 参数
范围:[-0.5, 0.5]

1% 参数
范围:[-20, 20]

如果 Scale 为了覆盖:

[

20

,

20

]

[-20,20]

[20,20] 那么大量普通参数就会被压缩得非常粗糙。

因此:

不是所有权重都应该简单地共用一个 Scale。


4.2.10 Group Size

这就是:

G

r

o

u

p

w

i

s

e

 

Q

u

a

n

t

i

z

a

t

i

o

n

\\boxed{ Group-wise\\ Quantization }

Groupwise Quantization

的思想。

例如:

1000 个权重

不要:

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

1000 个权重

1 个 Scale

而是:

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

1000 个权重

分成若干 Group

每个 Group 一个 Scale

例如:

Group 1 → Scale 1
Group 2 → Scale 2
Group 3 → Scale 3


4.2.11 Group Size 越小通常越灵活的原因

假设:

Group Size = 128

那么:

128

128

128 个权重共享一个 Scale。

如果:

Group Size = 32

那么:

32

32

32 个权重共享一个 Scale。

显然:

32

32

32 个数值的分布更容易接近。

所以:

S

m

a

l

l

e

r

 

G

r

o

u

p

M

o

r

e

 

L

o

c

a

l

 

S

c

a

l

e

s

U

s

u

a

l

l

y

 

L

e

s

s

 

Q

u

a

n

t

i

z

a

t

i

o

n

 

E

r

r

o

r

\\boxed{ Smaller\\ Group \\rightarrow More\\ Local\\ Scales \\rightarrow Usually\\ Less\\ Quantization\\ Error }

Smaller GroupMore Local ScalesUsually Less Quantization Error

但是代价是:

M

o

r

e

 

M

e

t

a

d

a

t

a

More\\ Metadata

More Metadata 也就是:

更好的精度可能换来更多额外存储和实现复杂度。


4.2.12 Group Size 是一个重要的工程权衡

可以理解成:

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

Group Size 小

Scale 多

适应能力强

但额外开销大

Group Size 大

Scale 少

额外开销小

但适应能力弱

所以:

$$ \\boxed{ Group\\ Size

Accuracy \\ vs Overhead } $$

这也是后面 GPTQ/AWQ 等 4-bit 方法中经常出现:

group_size = 128

等参数的原因。


4.3 低到 4-bit 仍然可以工作的原因

现在来到本章最核心的问题。

已经知道:

4

b

i

t

=

16

 

s

t

a

t

e

s

4bit=16\\ states

4bit=16 states 这看起来非常粗糙。

那么:

一个拥有几十亿参数的大模型,把权重压到 4-bit 后仍然可以正常工作。

这背后的原因不是:

“因为 4-bit 足够精确。”

实际上:

4-bit 一点也不精确。

它之所以仍然有效,是因为神经网络本身具有很强的:

R

e

d

u

n

d

a

n

c

y

\\boxed{Redundancy}

Redundancy

也就是:

一定程度的冗余性和容错能力。


4.3.1 第一:LLM 权重并不是完全随机的

神经网络中的权重通常不是:

[-1000000, 1000000]

这样毫无规律地分布。

很多参数会集中在一个相对有限的范围内。

可以想象:

*
* * *
** ******* **
*******************
**************************
——————————————→ Weight

即:

大部分权重集中在中心区域,少量值可能更大。

因此,如果合理设计量化区间:

可以使用有限的离散等级描述大部分权重。


4.3.2 第二:神经网络具有冗余性

假设一个模型有:

7

B

7B

7B 个参数。

并不是说:

每一个参数都承担完全独立、不可替代的功能。

神经网络通常具有一定冗余性。

可以简单理解成:

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

很多参数

共同完成某种表示

所以其中某些参数发生:

Δ

w

\\Delta w

Δw 并不一定导致输出发生灾难性变化。

这给量化留下了空间。


4.3.3 量化误差不一定会被无限放大

设:

W

W

W 量化为:

W

^

=

W

+

Δ

W

\\hat W=W+\\Delta W

W^=W+ΔW

原始计算:

Y

=

W

X

Y=WX

Y=WX 量化后:

Y

^

=

W

^

X

\\hat Y=\\hat WX

Y^=W^X

所以:

$$ \\hat Y

(W+\\Delta W)X $$

于是:

$$ \\hat Y-Y

\\Delta W X $$

因此输出误差由:

Δ

W

\\Delta W

ΔW 和输入:

X

X

X

共同决定。

只要量化误差在合理范围内:

最终输出不一定发生巨大变化。


4.3.4 第三:Transformer 有大量参数协同工作

一个 Transformer Block 并不是:

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

一个参数

直接决定最终答案

而是:

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

Embedding

Attention

Residual

Normalization

FFN

Residual

… 很多层

最终输出

所以一个单独的权重出现微小变化:

w

w

+

ϵ

w\\rightarrow w+\\epsilon

ww+ϵ 通常不会直接等价于:

“模型突然不会回答问题了。”

这也是低比特量化能够工作的一个重要原因。


4.3.5 但是不是所有权重都同样重要?

不是。

这点非常重要。

有些参数变化:

Δ

w

\\Delta w

Δw 可能对模型输出影响很小。

但另一些参数可能:

Δ

w

\\Delta w

Δw 就会导致比较明显的变化。

所以实际的 4-bit 量化研究中才会出现:

“如何保护重要权重?”

这也是后面 AWQ 等算法需要重点解决的问题。

注意:

本章只理解这个问题,不提前进入 AWQ。


4.3.6 4-bit 的真正逻辑不是“4-bit 很精确”

而是:

4

b

i

t

虽然粗糙

但模型允许一定程度的误差

\\boxed{ 4-bit虽然粗糙 \\rightarrow 但模型允许一定程度的误差 }

4bit虽然粗糙但模型允许一定程度的误差

进一步:

量化方案足够好

误差可以控制

\\boxed{ 量化方案足够好 \\rightarrow 误差可以控制 }

量化方案足够好误差可以控制

最终:

模型仍然可以工作

\\boxed{ 模型仍然可以工作 }

模型仍然可以工作


4.3.7 Quantization ≠ Pruning

这里要避免一个常见误解:“权重有冗余"不等于"可以随便删权重”。

4-bit 量化并不是删掉 75% 的参数,而是仍然保存全部参数,只是用更低精度的数值表示它们。例如

0.7321

0.7321

0.7321 变成

0.7

0.7

0.7——参数还在原来的位置,只是记录得更"粗"。这和 Pruning(剪枝)是完全不同的两件事:

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

Pruning

0.7321 → 删除(参数被移除 / 变稀疏)

Quantization

0.7321 → 0.7(参数还在,精度降低)

Q

u

a

n

t

i

z

a

t

i

o

n

P

r

u

n

i

n

g

\\boxed{ Quantization \\neq Pruning }

Quantization=Pruning

可以这样理解量化的直觉:原始模型说"这个参数是 0.731482",4-bit 模型说"这个参数大约在 0.7 这一档"。单个参数的信息确实明显减少,但几十亿参数放在一起时,整体仍能保留大量有价值的结构信息——这就是 4-bit 能工作的根本原因。


4.4 W4A16 到底是什么?

现在我们经常看到:

W

4

A

16

\\boxed{W4A16}

W4A16

这个名字一眼看起来很专业,实际上非常简单。

把它拆开:

W

4

A

16

W4A16

W4A16 其中:

W

=

W

e

i

g

h

t

W=Weight

W=Weight

A

=

A

c

t

i

v

a

t

i

o

n

A=Activation

A=Activation 所以:

$$ \\boxed{ W4A16

Weight\\ 4-bit + Activation\\ 16-bit } $$


4.4.1 W4A16

也就是:

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

Activation

16-bit

Weight

4-bit

因此:

W

e

i

g

h

t

=

I

N

T

4

\\boxed{ Weight=INT4 }

Weight=INT4

而:

A

c

t

i

v

a

t

i

o

n

=

F

P

16

/

B

F

16

\\boxed{ Activation=FP16/BF16 }

Activation=FP16/BF16

这里的“16”通常表示 16-bit 浮点计算,如 FP16 或 BF16,而不是必须存在一种名为“A16”的数据类型。


4.4.2 Weight 可以 4-bit 的原因

因为 Weight:

W

W

W 在推理过程中是固定的。

模型加载以后:

W

不会随着每个输入发生变化。

因此:

可以提前把 Weight 量化。


4.4.3 Activation 保留 16-bit 的原因

因为 Activation:

A

A

A 取决于输入:

X

X

X

也就是说:

A

=

f

(

X

,

W

)

A=f(X,W)

A=f(X,W) 换一个 Prompt:

X

1

X_1

X1

可能得到:

A

1

A_1

A1 另一个 Prompt:

X

2

X_2

X2

则:

A

2

A_2

A2 所以:

A

c

t

i

v

a

t

i

o

n

 

赞(0)
未经允许不得转载:171主机测评 » 第4章 LLM 4-bit 量化到底意味着什么?
分享到: 更多 (0)

评论 抢沙发

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