浅析计算GMAC和GFLOPS

王林
王林 转载
2023-05-26 08:59:36 734浏览

GMAC 代表“Giga Multiply-Add Operations per Second”(每秒千兆乘法累加运算),是用于衡量深度学习模型计算效率的指标。该指标用每秒十亿个乘加运算的形式表示模型的计算速度。

浅析计算GMAC和GFLOPS

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

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

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

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

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

浅析计算GMAC和GFLOPS

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: {:<8}'.format(macs)) print('Computational complexity: {} {}Flops'.format(flops, flops_unit)) print('Number of parameters: {:<8}'.format(params))

结果如下:

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: {:<8}'.format(macs)) print('Computational complexity: {} {}Flops'.format(flops, flops_unit)) print('Number of parameters: {:<8}'.format(params))

结果如下:

Computational complexity: 670.73 KMac Computational complexity: 1341.46 KFlops Number of parameters: 669.71 k

为了方便演示,我们只编写全连接层代码来手动计算GMAC。遍历模型权重参数并计算乘法和加法操作数量的形状取决于权重参数,这是计算GMAC的关键。计算GMAC所需的全连接层权重的公式为2 x (输入维度 x 输出维度) 。总的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的结果如下:

0.66972288

由于GMAC的结果以十亿为单位,因此与我们上面使用类库计算的结果相差不大。最后再说一下,计算卷积的GMAC稍微有些复杂,公式为 ((输入通道 x 卷积核高度 x 卷积核宽度) x 输出通道) x 2,这里给一个简单的代码,不一定完全正确,供参考

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

以上就是浅析计算GMAC和GFLOPS的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:51CTO.COM,如有侵犯,请联系admin@php.cn删除