登录
首页 >  科技周边 >  人工智能

浅析计算GMAC和GFLOPS

来源:51CTO.COM

时间:2023-05-26 06:00:18 328浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《浅析计算GMAC和GFLOPS》,以下内容主要包含等知识点,如果你正在学习或准备学习科技周边,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

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

浅析计算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的计算公式为 (输入维度 x 输出维度) x 2,其中输入维度和输出维度均为全连接层的权重。总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》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于深度学习的内容请关注golang学习网公众号!

声明:本文转载于:51CTO.COM 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>