【问题标题】:INT8 quantization for matmulmatmul 的 INT8 量化
【发布时间】: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


    【解决方案1】:

    KL 散度可以计算为 P 中每个事件的概率的负和乘以 Q 中事件的概率与 P 中事件的概率的对数。

    KL(P || Q) = – sum x in X P(x) * log(Q(x) / P(x))
    

    这等于 P 中每个事件的概率的正和乘以 P 中事件的概率与 Q 中事件的概率的对数。

    KL(P || Q) = sum x in X P(x) * log(P(x) / Q(x))
    

    “仅当 P 和 Q 都为 1 并且对于任何 i 使得 P(i) > 0 时 Q(i) > 0 时才定义 K-L 散度。”

    【讨论】:

      猜你喜欢
      • 2020-04-25
      • 2021-12-05
      • 2022-01-12
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多