【计算FLOPs】GFLOPs、FLOPS和FLOPs的区别和联系(含代码示例)

文章目录

这三个概念其实都差不多,都涉及浮点运算,但是还是有一些小的不同之处,下面简单总结一下:

一、GFLOPs、FLOPs、FLOPS

GFLOPS 就是 Giga Floating-point Operations Per Second,即每秒10亿次的浮点运算数,常作为GPU性能参数但不一定代表GPU的实际表现,因为还要考虑具体如何拆分多边形和像素、以及纹理填充,理论上该数值越高越好。

FLOPs 是floating point of operations的缩写,是浮点运算次数,可以用来衡量算法/模型复杂度。常用当然还有GFLOPs和TFLOPs

FLOPS (全部大写)是floating-point operations per second的缩写,意指每秒浮点运算次数。用来衡量硬件的性能。

二、单位换算

MFLOPS(megaFLOPS):等于每秒一百万(

$10^6$

)次的浮点运算 GFLOPS(gigaFLOPS):等于每秒十亿(= 1 0 9 10^9 109)次的浮点运算 TFLOPS(teraFLOPS):等于每秒一万亿(= 1 0 12 10^{12} 1012)次的浮点运算,(1太拉) PFLOPS(petaFLOPS):等于每秒一千万亿(= 1 0 15 10^{15} 1015)次的浮点运算 EFLOPS(exaFLOPS):等于每秒一百京(= 1 0 18 10^{18} 1018)次的浮点运算 ZFLOPS(zettaFLOPS):等于每秒十万京(= 1 0 21 10^{21} 1021)次的浮点运算

三、如何计算FLOPs

其中就是指计算量的大小,表示FLOPs。对于卷积层而言,FLOPs计算公式如下:

$$10^6$$

而后把常数项去掉,简化操作:

经过测试,基本上两个可以对齐的,任意选择一个就好。

四、计算示例

我们使用thop库来计算resnet50模型的计算量:

import torch
from thop import profile
from torchvision.models import resnet50
model = resnet50()
input1 = torch.randn(4, 3, 224, 224) 
flops, params = profile(model, inputs=(input1, ))
print('FLOPs = ' + str(flops / 1000 ** 3) + 'G')

输出结果为:

[INFO] Register count_convNd() for <class 'torch.nn.modules.conv.Conv2d'>.
[INFO] Register count_normalization() for <class 'torch.nn.modules.batchnorm.BatchNorm2d'>.
[INFO] Register zero_ops() for <class 'torch.nn.modules.activation.ReLU'>.
[INFO] Register zero_ops() for <class 'torch.nn.modules.pooling.MaxPool2d'>.
[INFO] Register zero_ops() for <class 'torch.nn.modules.container.Sequential'>.
[INFO] Register count_adap_avgpool() for <class 'torch.nn.modules.pooling.AdaptiveAvgPool2d'>.
[INFO] Register count_linear() for <class 'torch.nn.modules.linear.Linear'>.
FLOPs = 16.534970368G
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值