浮点运算量FLOPs与算力单位FLOPS

概念


FLOPS:注意全大写,是floating point operations per second的缩写,意指每秒浮点运算次数,理解为计算速度。是一个衡量硬件性能的指标。

FLOPs:注意s小写,是floating point operations的缩写(s表复数),意指浮点运算数,理解为计算量。可以用来衡量算法/模型的复杂度。

单位换算


1 MFLOPs(mega) = 10^6 FLOPs,即:100万次浮点运算

1 GFLOPs(giga) = 10^9 FLOPs,即:10亿次浮点运算

1 TFLOPs(tera) = 10^12 FLOPs,即:1万亿次浮点运算
 

常见算力单位

TOPS是Tera Operations Per Second的缩写,1TOPS代表处理器每秒钟可进行一万亿次(10^12)操作

与此对应的还有GOPS(Giga Operations Per Second),MOPS(Million Operation Per Second)算力单位。

1GOPS代表处理器每秒钟可进行十亿次(10^9)操作,1MOPS代表处理器每秒钟可进行一百万次(10^6)操作。

TOPS同GOPS与MOPS可以换算,都代表每秒钟能处理的次数,单位不同而已。

在某些情况下,还使用 TOPS/W 来作为评价处理器运算能力的一个性能指标,TOPS/W 用于度量在1W功耗的情况下,处理器能进行多少万亿次操作。

Swin Transformer模型的运算可以通过计算每个操作的浮点操作数(FLOPs)来估计。FLOPs函数可以通过统计每个操作的计算来实现。 Swin Transformer模型中的关键操作是多头自注意力(multi-head self-attention)和MLP (多层感知机)。对于每个操作,我们可以计算其FLOPs并进行累加。 以下是一个示例代码,用于估计Swin Transformer模型的FLOPs: ```python import torch def count_flops(module, input, output): flops = 0 if hasattr(module, 'weight'): flops += module.weight.numel() if hasattr(module, 'bias') and module.bias is not None: flops += module.bias.numel() if isinstance(module, torch.nn.Linear): flops *= 2 # Linear operations involve both multiplication and addition # Accumulate flops for each operation module.__flops__ += flops def flops(model, input_size): model.eval() model.apply(lambda module: setattr(module, '__flops__', 0)) model.apply(lambda module: module.register_forward_hook(count_flops)) with torch.no_grad(): model(torch.randn(1, *input_size)) total_flops = sum([module.__flops__ for module in model.modules()]) return total_flops ``` 使用该函数,您可以计算Swin Transformer模型的总FLOPs。请确保将正确的输入大小传递给`flops`函数。 ```python import torchvision.models as models model = models.swin_transformer.SwinTransformer() input_size = (3, 224, 224) # Assuming input images of size 224x224 and 3 channels total_flops = flops(model, input_size) print('Total FLOPs:', total_flops) ``` 请注意,这只是一个简单的估计方法,实际的FLOPs可能会有所差异。此外,不同的库和工具可能会提供不同的FLOPs估计结果。这个代码示例可以作为一个起点,您可以根据具体情况进行修改和调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Joejwu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值