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

卷积算子计算量计算公式 #69371

Open
peakgrin opened this issue Nov 13, 2024 · 0 comments
Open

卷积算子计算量计算公式 #69371

peakgrin opened this issue Nov 13, 2024 · 0 comments
Assignees

Comments

@peakgrin
Copy link

peakgrin commented Nov 13, 2024

bug描述 Describe the Bug

飞桨框架使用的卷积算子计算量的计算公式为:
FLOPs = M'*N'*P*D*U*V+M'*N'*P
而我推导出的卷积算子计算量(乘加运算次数)计算公式为:
FLOPs = M'*N'*P*D*U*V

依据如下:
1.理论推导
(1)每个卷积核与图像上区域作卷积计算,乘法次数为U*V,加法次数为U*V-1,总次数为2*U*V-1
(2)输入通道数为D,所以D个卷积核乘加运算次数为D*(2*U*V-1)
(3)D个卷积核卷积运算后结果相加,结果中每一个点的加法次数为(D-1)
(4)结果中每一个点,加偏置项,加法次数为1
(5)综合(1)-(4),乘法+加法, 总次数为
D*(2*U*V-1)+(D-1)+1 = 2*D*U*V-D+D-1+1 = 2*D*U*V
即:D*(卷积核计算的乘法和加法总次数)+(D个卷积后的结果值相加)+(偏置项)
(6)由(5)的,结果中每一个特征图中每一个点的获得,需要的乘加运算个数为D*U*V
(7)由(6)、结果中每一个特征图的获得,需要D*U*V*M'*N'个乘加运算
(8)由(7),结果中P个特征图的获得,需要P*D*U*V*M'*N'个乘加运算

2.实验验证
from thop import profile
dummy_input = torch.randn(1, 1, 32, 32)
flops, params = profile(model,(dummy_input,))
print('Total params: {}'.format(params))
print('Total FLOPs: {}'.format(flops))
LeNet-5总的计算量为:416920
这个结果和我上面的理论推导出的公式一致

3.我反推了下
FLOPs = paddle.flops(model, (1, 1, 32, 32), print_detail=True)
print(FLOPs) #423344
这个结果可以由飞桨框架的卷积算子计算量公式计算得到

4.问题:
请核实下飞桨卷积算子计算量公式的正确性

其他补充信息 Additional Supplementary Information

No response

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

No branches or pull requests

2 participants