Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] ViT 量化 #3913

Open
tpoisonooo opened this issue Jun 14, 2022 · 0 comments
Open

[RFC] ViT 量化 #3913

tpoisonooo opened this issue Jun 14, 2022 · 0 comments

Comments

@tpoisonooo
Copy link
Contributor

tpoisonooo commented Jun 14, 2022

为了避免类似 #3911 一天白干的事再发生,写一下 FQ-ViT 方案。

背景

目前 mmdeploy merge 这个 PR 之后,已经可以生成很漂亮的 ViT.onnx
5 月份的 FQ-ViT 论文很好, 源码 repo 俺也熟悉了
ppq 搞量化表很方便,已经根据 FQ-ViT 的思路改得差不多了,WIP openppl-public/ppq#154

现在就差动 ncnn 了。

量化部分

原始 fp32 的结构这样:

  flowchart  TD;
      add0-->layernorm;
      add0-->nop;
      nop-->add1;
      layernorm-->mha;
      mha-->add1;
Loading

论文是这么计算 scale 的:

  flowchart  TD;
      add0-->perchannel0;
      perchannel0-->layernorm;
      perchannel0-->add1;
      layernorm-->perlayer0;
      perlayer0-->mha;
      mha-->perlayer1;
      perlayer1-->add1;
      add1-->perchannel1;
Loading

实际推理的时候,计算过程是这样:

  flowchart  TD;
      add0.fp32-->quant;
      quant-->int_tensor0;
      int_tensor0-->layernorm.int;
      int_tensor0-->requant0;
      requant0-->add1;
      layernorm.int-->int8_tensor;
      int8_tensor-->mha.int;
      mha.int-->requant1;
      requant1-->add1;
      add1-->int_tensor1;
Loading

修改内容

  1. ppq 的量化结果,ncnn 怎么吃掉?
    使用 .ini 格式,写了个 reader/writer。已完成 refactor quant table #3911

  2. add/mha/layernorm 要支持 int8
    打算把 naive 版本的写了,把量化模型精度验了就提 PR。
    论文用的是 [-128, 127],有的地方还是 [0, 255] 。和推理并不完全一致。

注意事项

  1. <Batch, TokenNum, Channel> 语义下的 per-channel 和 <n,c,h,w> 并不一样,多了一步 transpose。
  2. add1 前面是两个 requant,公式来源在这里 : ViT-B add ptf reshape_tensor 问题 megvii-research/FQ-ViT#19

cc @nihui @BUG1989

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant