【发布时间】:2021-10-23 07:28:57
【问题描述】:
受到“Transformer 神经机器语言的高效 8 位量化”的启发 翻译模型”,我决定遵循论文的警告。但是,我对在量化期间设置偏移变量感到困惑。
INPUT : A (tensor of FP32, [1, 4, 1024, 256])
# Quantization
offset = torch.empty(A.shape)
offset = torch.zeros_like(offset)
scale = 255 / (torch.max(A) - torch.min(A))
A_int8 = (A - offset) * scale
# Probability Distribution
P = norm.pdf(A, torch.mean(A, dim=[2, 3]), torch.std(A, dim = [2,3]))
Q = norm.pdf(A_int8, torch.mean(A_int8, dim=[2, 3]), torch.std(A_int8, dim = [2,3]))
P = torch.from_numpy(P)
Q = torch.from_numpy(Q)
# KLD
kld = (P * (P / Q).log()).sum()
print(kld)
# After this, I'm going to apply self-attention operation.
# B_int8 = A_int8.clone()
# AB = A_int8.matmul(B_int8.transpose(-1, -2))
我现在得到了积极的 kld 值,但我不确定我是否通过了正确的方法来做到这一点。感谢您提供任何帮助或建议。
【问题讨论】:
标签: python nlp pytorch quantization