首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

计算GMAC和GFLOPS

GMAC 代表“Giga Multiply-Add Operations per Second”(每秒千兆乘法累加运算),是用于衡量深度学习模型计算效率的指标。它表示每秒在模型中执行的乘法累加运算的数量,以每秒十亿 (giga) 表示。

乘法累加 (MAC) 运算是许多数学计算中的基本运算,包括矩阵乘法、卷积和深度学习中常用的其他张量运算。每个 MAC 操作都涉及将两个数字相乘并将结果添加到累加器。

可以使用以下公式计算 GMAC 指标:

GMAC =(乘法累加运算次数)/(10⁹)

乘加运算的数量通常通过分析网络架构和模型参数的维度来确定,例如权重和偏差。

通过 GMAC 指标,研究人员和从业者可以就模型选择、硬件要求和优化策略做出明智的决策,以实现高效且有效的深度学习计算。

GFLOPS 代表“每秒千兆浮点运算”,是用于衡量计算机系统或特定运算的计算性能的指标。它表示每秒执行的浮点运算次数,也是以每秒十亿 (giga) 表示。

浮点运算包括涉及以 IEEE 754 浮点格式表示的实数的算术计算。这些运算通常包括加法、减法、乘法、除法和其他数学运算。

GFLOPS 通常用于高性能计算 (HPC) 和基准测试,特别是在需要繁重计算任务的领域,例如科学模拟、数据分析和深度学习。

计算 GFLOPS公式如下:

GFLOPS =(浮点运算次数)/(以秒为单位的运行时间)/ (10⁹)

GFLOPS 是比较不同计算机系统、处理器或特定操作的计算性能的有用指标。它有助于评估执行浮点计算的硬件或算法的速度和效率。GFLOPS 是衡量理论峰值性能的指标,可能无法反映实际场景中实现的实际性能,因为它没有考虑内存访问、并行化和其他系统限制等因素。

GMAC 和 GFLOPS 之间的关系

1 GFLOP = 2 GMAC

如果我们想计算这两个指标,手动写代码的话会比较麻烦,但是Python已经有现成的库让我们使用:

ptflops 库就可以计算 GMAC 和 GFLOPs

pip install ptflops

使用也非常简单:

import torchvision.models as models

import torch

from ptflops import get_model_complexity_info

import re

#Model thats already available

net = models.densenet161()

macs, params = get_model_complexity_info(net, (3, 224, 224), as_strings=True,

print_per_layer_stat=True, verbose=True)

# Extract the numerical value

flops = eval(re.findall(r'([\d.]+)', macs)[0])*2

# Extract the unit

flops_unit = re.findall(r'([A-Za-z]+)', macs)[0][0]

print('Computational complexity: {:

print('Computational complexity: {} {}Flops'.format(flops, flops_unit))

print('Number of parameters: {:

结果如下:

Computational complexity: 7.82 GMac

Computational complexity: 15.64 GFlops

Number of parameters: 28.68 M

我们可以自定义一个模型来看看结果是否正确:

import os

import torch

from torch import nn

class NeuralNetwork(nn.Module):

   def __init__(self):

       super().__init__()

       self.flatten = nn.Flatten()

       self.linear_relu_stack = nn.Sequential(

           nn.Linear(28*28, 512),

           nn.ReLU(),

           nn.Linear(512, 512),

           nn.ReLU(),

           nn.Linear(512, 10),

      )

   def forward(self, x):

       x = self.flatten(x)

       logits = self.linear_relu_stack(x)

       return logits

custom_net = NeuralNetwork()

macs, params = get_model_complexity_info(custom_net, (28, 28), as_strings=True,

                                       print_per_layer_stat=True, verbose=True)

# Extract the numerical value

flops = eval(re.findall(r'([\d.]+)', macs)[0])*2

# Extract the unit

flops_unit = re.findall(r'([A-Za-z]+)', macs)[0][0]

print('Computational complexity: {:

print('Computational complexity: {} {}Flops'.format(flops, flops_unit))

print('Number of parameters: {:

结果如下:

Computational complexity: 670.73 KMac

Computational complexity: 1341.46 KFlops

Number of parameters: 669.71 k

我们来尝试手动计算下GMAC,为了演示方便我们只写全连接层的代码,因为比较简单。计算GMAC的关键是遍历模型的权重参数,并根据权重参数的形状计算乘法和加法操作的数量。对于全连接层的权重,GMAC的计算公式为 。根据模型的结构,将每个线性层的权重参数形状相乘并累加得到总的GMAC值。

import torch

import torch.nn as nn

def compute_gmac(model):

   gmac_count = 0

   for param in model.parameters():

       shape = param.shape

       if len(shape) == 2:  # 全连接层的权重

           gmac_count += shape[0] * shape[1] * 2

   gmac_count = gmac_count / 1e9  # 转换为十亿为单位

   return gmac_count

根据上面给定的模型,计算GMAC的结果如下:

GMAC的结果是以十亿为单位,所以跟我们上面用类库计算的结果相差不大。最后再说一下,计算卷积的GMAC稍微有些复杂,公式为 ,这里给一个简单的代码,不一定完全正确,供参考

def compute_gmac(model):

   gmac_count = 0

   for param in model.parameters():

       shape = param.shape

       if len(shape) == 2:  # 全连接层的权重

           gmac_count += shape[0] * shape[1] * 2

       elif len(shape) == 4:  # 卷积层的权重

           gmac_count += shape[0] * shape[1] * shape[2] * shape[3] * 2

   gmac_count = gmac_count / 1e9  # 转换为十亿为单位

   return gmac_count

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20230522A01OL900?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券