Jermmy / pytorch-quantization-demo

A simple network quantization demo using pytorch from scratch.
Apache License 2.0
497 stars 96 forks source link

为什么训练过程中fold BN #23

Closed CheungBH closed 1 year ago

CheungBH commented 1 year ago

如题。我看到你在训练的forward里面进行了BN folding。 https://github.com/Jermmy/pytorch-quantization-demo/blob/8dc1f8da75ad0ff3110eec57b96c846cd41d3462/module.py#L399 我想知道这么做的意图是什么。BN folding不是训完得到checkpoint才进行的么

Jermmy commented 1 year ago

您好,您的邮件已收到,我会尽快查看。

Jermmy commented 1 year ago

参考google的论文,qat的时候最好fold bn,这样和推理才是一致的 image

CheungBH commented 1 year ago

感谢,我去看一下。 还有个问题就是,training的时候BN为什么要这样处理? https://github.com/Jermmy/pytorch-quantization-demo/blob/8dc1f8da75ad0ff3110eec57b96c846cd41d3462/module.py#L345 从345行到356行这

Jermmy commented 1 year ago

因为BN在Conv中是对输出feature的每个channel都算一个均值和方差,这个permute只是把channel换到第一维,方便对每个channel算均值和方差

CheungBH commented 1 year ago
Screenshot 2023-04-03 at 8 18 06 PM

那下面的这几句呢,这几句加上动量的目的是什么啊

Jermmy commented 1 year ago

这是BN更新均值方差的算法,可以参考下pytorch BN源码

CheungBH commented 1 year ago

这是BN更新均值方差的算法,可以参考下pytorch BN源码

谢谢,还有个问题哈。 https://github.com/Jermmy/pytorch-quantization-demo/blob/8dc1f8da75ad0ff3110eec57b96c846cd41d3462/module.py#L340 https://github.com/Jermmy/pytorch-quantization-demo/blob/8dc1f8da75ad0ff3110eec57b96c846cd41d3462/module.py#L367 这一个forward里包含两个F.conv的目的是什么

Jermmy commented 1 year ago

看一下我上面po的google论文图,有两个conv,第二个对应conv fold

CheungBH commented 1 year ago

看一下我上面po的google论文图,有两个conv,第二个对应conv fold

感谢。我基本明白了