- model compression
- quantization
- PTQ and QAT
- paper(OneBit: Towards Extremely Low-bit Large Language Models)
- 参考资料
model compression
模型压缩(轻量化)的四种常见方法
-
pruning
把权重矩阵W中的某些元素置0,希望对输出结果影响较小。
如果置0的时候按行或按列或按块,就称为结构化剪枝,否则为非结构化剪枝。
剪枝的时候可以调整权重来补偿剪枝带来的误差 -
knowledge distillation
训练一个较小的模型,用大模型(教师模型)来指导小模型(学生模型),学生模型不仅要去拟合数据,也要去逼近教师模型的分布。例如可以通过损失函数的定义来实现。例如 -
matrix decomposition
将原来(矩阵的形状)大的权重矩阵分解成多个小的(低秩的)矩阵,用低秩矩阵近似原有权重矩阵。这样可以大大降低模型分解之后的计算量 -
把实数上的运算转化到整数域上,加速运算和减少存储需求。
quantization
把浮点数上的存储和运算转化成整数的存储和运算。
上的运算转化到, 其中是连续的,是离散的整数。
被量化后的指为, 被反量化后的值为
把记作s
把带入中
如果不在内,那也极有可能不在[c,d]内。
如果超出了[c,d],那就取较近的边界值。
观察上述(1),(2),(3),(4)可以不依赖与区间和的存在。可以看作对整个数轴的线性变换。s为缩放比例。a和c决定的平移。
我们假设值都在正常范围内,没有超出,不考虑clip(即使用(3)中的)
所以
pytorch的quantization
1. 根据权重和数据的分布(或最值)确定量化参数scale和zero_point
例如,8比特的对称量化和非对称量化
其中的最大值和最小值不一定是“真实”的最大值和最小值
确定最值有多种方式
- 真实的最值
- 最值的移动平均: 还有一种是根据数据的分布确定最值。(具体的怎么算得我还没看。)
2. 量化
这样量化使得整数z对应实数0
pytorch的量化误差
量化误差和之前得到的一样。
所以
PTQ and QAT
PTQ和QAT都是训练完后再执行量化操作。
PTQ(Post Training Qunatization)
直接对模型进行量化。
根据是否确定actvation的量化参数,可以分为PTDQ和PTSQ。
-
只对权重量化,因为activation的范围是未知的(scale和zero_point未知)。等到推理的时候,计算出了activation,确定了量化参数,再对activation进行量化,称为PTDQ(D表示Dynamic),(这个可能理解有点不准确)
-
对权重和activation都量化,虽然不知道actvation的范围但可以根据已有的数据进行估计。称为PTSQ(S表示static)
在对模型量化前,在模型需要量化的结点插入Observer,用来统计和计算最值,从而确定量化参数。
QAT(Quantization Aware Training)
- 获取一个训练好的模型。
- 插入伪量化(fake quantization)结点,在某个数据集上进行微调。(模型会在微调期间确定量化的参数)
- 使用第2步中得到参数对模型进行量化。
QAT一般只静态量化.
关于QAT中的伪量化
伪量化=量化+反量化。伪量化是模型的参数仍然都是浮点数。
模型在微调时模拟了量化。伪量化结点也充当了Observer的角色。
伪量化结点的反向传播使用了(STE)
Quantization is discrete-valued, and thus the derivative is 0 almost everywhere.
The neural network will learn nothing since gradients become 0 and the weights won’t get updated.
Straight-Through Estimator (STE) simply passes the gradients through the quantization as if it had been the identity function.
也就是说,“将被量化”的结点后会插入一个“伪量化”结点。前向传播时,伪量化结点模拟了量化误差,使得模型在微调阶段适应量化误差;反向传播时,伪量化结点的梯度首先会被计算出来,这里STE的意思就是把对【伪量化结点的梯度】作为【将被量化的结点的梯度】
paper(OneBit: Towards Extremely Low-bit Large Language Models)
rank-1 approximation
奇异值分解
取最大的奇异值(),和最大的奇异值对应的左奇异向量和右奇异向量
(《Foundations of Data Science》的3.5节证明了这样分解是Frobenius范数下最好的秩1分解,证明我还没看)
(符号和绝对值的Hadamard积)
(也就是说把矩阵的绝对值秩1分解)
proposition 1
这个命题的是为了得到想到的网络结构。这个里面的b和a就是模型架构里的g和h,其中操作表示对进行广播(是一个行向量,广播的意思就是把一行一行重复地排列下去,直到和X的形状一样。),然后和X作hadamard积。
proof
, where is the element of . Hence we have
lemma 1
Let denote the -th biggest singular value of matrix . The following inequality holds:
Proof
According to the definition of induced norm, there are
Note that for and we have
因为我们总是可以让y取x的绝对值,所以:
This lemma is proved.
proposition 2
Given matrices and , . We decompose these matrices in the way and , where denotes the error matrices. In terms of the Frobenius-norm, the SVID is closer to the original matrix :
这个命题主要想说,在Frobenius范数意义下,先取绝对值再分解比直接分解的误差小。
proof
Here we consider SVD to prove it. For SVD, the norm of the error matrix in the rank1 approximation is the sum of the squares of all singular values except for the largest one. We have
u和v是U和V里的奇异向量,, 且u和v都是单位向量。,所以
然后使用迹技巧
所以
因为矩阵的Frobenius范数的平方等于矩阵的所有奇异值的平方和,即
所以误差矩阵的Frobenius范数的平方等于除了最大奇异值的其他所有奇异值的平方和。
Based on , we have
According to Lemma 1, we can conclude
From the equation in this proposition, we can formulate
Hence we have
Therefore
where is the element of . Hence the inequation in this proposition is proved.
知识蒸馏
这里就是用命题1得到的Y的表达式。
where denotes the number of classes and denotes the number of training samples in the current batch. and are the teacher model and student model, respectively. The error of hidden states is defined as
where denotes the number of layers and denotes the hidden state. Hence the final objective function can be formulated as
使得学生模型在输出上接近教师模型。
使得学生模型在内部参数上接近教师模型。
参考资料
-
https://leimao.github.io/article/Neural-Networks-Quantization/#Quantization
-
https://www.dropbox.com/scl/fi/1mo0umu0qtq7uxap2l5m3/lec06.pdf?rlkey=bdl2mgusgajddjuvjxb0fot36&dl=0
-
论文:OneBit: Towards Extremely Low-bit Large Language Models
-
论文:Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
-
论文:A Survey on Model Compression for Large Language Models