文献阅读(35)2022 Transformer加速器

  • 题目:I-BERT: Integer-only BERT Quantization
  • 时间:2021
  • 会议:Proceedings of the 38th International Conference on Machine Learning, PMLR
  • 研究机构:UCB
  • Github:https://github.com/kssteven418/I-BERT

1 I-BERT

本篇论文的主要贡献:
针对Transformer计算量大的问题,提出了轻量化的量化方法,针对非线性函数GELU、softmax、Layer Normalization进行量化,从而在GPU上通过int8完成推理
在这里插入图片描述

1.1 softmax

Softmax ⁡ ( x ) i = exp ⁡ x i ∑ j = 1 k exp ⁡ x j \operatorname{Softmax}(\mathbf{x})_{i}=\frac{\exp x_{i}}{\sum_{j=1}^{k} \exp x_{j}} Softmax(x)i=j=1kexpxjexpxi
首先为了避免指数范围过大,分子分母同时减掉最大值
Softmax ⁡ ( x ) i = exp ⁡ ( x i − x max ⁡ ) ∑ j = 1 k exp ⁡ ( x j − x max ⁡ ) \operatorname{Softmax}(\mathbf{x})_{i}=\frac{\exp \left(x_{i}-x_{\max }\right)}{\sum_{j=1}^{k} \exp \left(x_{j}-x_{\max }\right)} Softmax(x)i=j=1kexp(xjxmax)exp(xixmax)
此时指数一定为负数,数值范围小于1,再令 x i − x max ⁡ = ( − l n 2 ) z + p x_i - x_{\max} = (-ln2)z + p xixmax=(ln2)z+p,其中z一定非负
exp ⁡ ( x ~ ) = 2 − z exp ⁡ ( p ) = exp ⁡ ( p ) > > z \exp (\tilde{x})=2^{-z} \exp (p)=\exp (p)>>z exp(x~)=2zexp(p)=exp(p)>>z
此时p的范围为-ln2到0,就可以用二阶近似来
L ( p ) = 0.3585 ( p + 1.353 ) 2 + 0.344 ≈ exp ⁡ ( p ) L(p)=0.3585(p+1.353)^{2}+0.344 \approx \exp (p) L(p)=0.3585(p+1.353)2+0.344exp(p)

1.2 LayerNorm

unlike BatchNorm whose parameters/statistics can be fused into the previous convolutional layer in inference, LayerNorm requires the dynamic computation of the square root of the variance for each input

主要的问题是算标准差需要开平方
在这里插入图片描述
问题:需要除法??

1.3 GELU

GELU ⁡ ( x ) : = x ⋅ 1 2 [ 1 + erf ⁡ ( x 2 ) ] \operatorname{GELU}(x):=x \cdot \frac{1}{2}\left[1+\operatorname{erf}\left(\frac{x}{\sqrt{2}}\right)\right] GELU(x):=x21[1+erf(2 x)]
其中 erf ⁡ ( x ) \operatorname{erf}(x) erf(x)为误差函数,
erf ⁡ ( x ) : = 2 π ∫ 0 x exp ⁡ ( − t 2 ) d t \operatorname{erf}(x):=\frac{2}{\sqrt{\pi}} \int_{0}^{x} \exp \left(-t^{2}\right) d t erf(x):=π 20xexp(t2)dt
一种方法是用sigmoid函数近似GELU函数,
GELU ⁡ ( x ) ≈ x σ ( 1.702 x ) \operatorname{GELU}(x) \approx x \sigma(1.702 x) GELU(x)xσ(1.702x)
但是sigmoid函数本身也是非线性操作,如果再用h-sigmoid函数近似sigmoid函数的话,就会变成
 h-GELU  ( x ) : = x ReLU ⁡ 6 ( 1.702 x + 3 ) 6 ≈ GELU ⁡ ( x ) \text { h-GELU }(x):=x \frac{\operatorname{ReLU} 6(1.702 x+3)}{6} \approx \operatorname{GELU}(x)  h-GELU (x):=x6ReLU6(1.702x+3)GELU(x)
但是精度下降的很多,不如还是用多项式L(x)逼近erf函数
L ( x ) = sgn ⁡ ( x ) [ a ( clip ⁡ ( ∣ x ∣ , max ⁡ = − b ) + b ) 2 + 1 ] \mathrm{L}(x)=\operatorname{sgn}(x)\left[a(\operatorname{clip}(|x|, \max =-b)+b)^{2}+1\right] L(x)=sgn(x)[a(clip(x,max=b)+b)2+1]
根据clip函数可以知道,这里实际上是逼近x在[0, -b]之间的数值,因为erf首先是奇函数,其次当x很大的时候,由于 − t 2 -t^2 t2很负, e x p ( − t 2 ) exp(-t^2) exp(t2)接近于零,结果会接近于定值(1),所以可以clip到一定的定义域内,此时就可以用线性函数拟合GELU
在这里插入图片描述


  • 题目:EdgeBERT: Sentence-Level Energy Optimizations for Latency-Aware Multi-Task NLP Inference
  • 时间:2021
  • 会议:MICRO
  • 研究机构:哈佛大学

2 EdgeBERT

**边缘端的NLP运算需求: **智能虚拟助手更适合在边缘端运行,已实现个人数据的保密与低延时
本篇论文的主要贡献:

  1. entropy-based early exit predication降低计算延时,实现对multi-task NLP的软硬件协同优化
  2. 利用low-dropout voltage regulator
    (LDO)和all-digital phase-locked loop (ADPLL)实现DVFS
  3. 利用embedded non-volatile memories(eNVMs)与bitmask编码存储权重,实现稀疏计算,降低功耗与延时开销

BERT的变体:

  • DistilBERT、MobileBERT:知识蒸馏
  • SqueezeBERT:利用一维组卷积
  • Q8BERT:对BERT的量化操作
  • ALBERT:压缩后的参数量只有12M

在这里插入图片描述

Entropy-based Early Exit: 逻辑在于让长的复杂的句子用更复杂的网络模型,让短的句子用更简单的网络模型。通过entropy metric来评判分类的可靠性,当熵H(x)小于一定阈值的时候就可以输出了
H ( x ) = − ∑ p ( x ) log ⁡ p ( x ) H(x) = - \sum{p(x)\log p(x)} H(x)=p(x)logp(x)
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值