0


计算GMAC和GFLOPS

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

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

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

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

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

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

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

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

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

计算 GFLOPS公式如下:

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

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

GMAC 和 GFLOPS 之间的关系

  1. 1 GFLOP = 2 GMAC

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

ptflops 库就可以计算 GMAC 和 GFLOPs

  1. pip install ptflops

使用也非常简单:

  1. importtorchvision.modelsasmodels
  2. importtorch
  3. fromptflopsimportget_model_complexity_info
  4. importre
  5. #Model thats already available
  6. net=models.densenet161()
  7. macs, params=get_model_complexity_info(net, (3, 224, 224), as_strings=True,
  8. print_per_layer_stat=True, verbose=True)
  9. # Extract the numerical value
  10. flops=eval(re.findall(r'([\d.]+)', macs)[0])*2
  11. # Extract the unit
  12. flops_unit=re.findall(r'([A-Za-z]+)', macs)[0][0]
  13. print('Computational complexity: {:<8}'.format(macs))
  14. print('Computational complexity: {} {}Flops'.format(flops, flops_unit))
  15. print('Number of parameters: {:<8}'.format(params))

结果如下:

  1. Computational complexity: 7.82 GMac
  2. Computational complexity: 15.64 GFlops
  3. Number of parameters: 28.68 M

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

  1. importos
  2. importtorch
  3. fromtorchimportnn
  4. classNeuralNetwork(nn.Module):
  5. def__init__(self):
  6. super().__init__()
  7. self.flatten=nn.Flatten()
  8. self.linear_relu_stack=nn.Sequential(
  9. nn.Linear(28*28, 512),
  10. nn.ReLU(),
  11. nn.Linear(512, 512),
  12. nn.ReLU(),
  13. nn.Linear(512, 10),
  14. )
  15. defforward(self, x):
  16. x=self.flatten(x)
  17. logits=self.linear_relu_stack(x)
  18. returnlogits
  19. custom_net=NeuralNetwork()
  20. macs, params=get_model_complexity_info(custom_net, (28, 28), as_strings=True,
  21. print_per_layer_stat=True, verbose=True)
  22. # Extract the numerical value
  23. flops=eval(re.findall(r'([\d.]+)', macs)[0])*2
  24. # Extract the unit
  25. flops_unit=re.findall(r'([A-Za-z]+)', macs)[0][0]
  26. print('Computational complexity: {:<8}'.format(macs))
  27. print('Computational complexity: {} {}Flops'.format(flops, flops_unit))
  28. print('Number of parameters: {:<8}'.format(params))

结果如下:

  1. Computational complexity: 670.73 KMac
  2. Computational complexity: 1341.46 KFlops
  3. Number of parameters: 669.71 k

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

  1. (输入维度 x 输出维度) x 2

。根据模型的结构,将每个线性层的权重参数形状相乘并累加得到总的GMAC值。

  1. importtorch
  2. importtorch.nnasnn
  3. defcompute_gmac(model):
  4. gmac_count=0
  5. forparaminmodel.parameters():
  6. shape=param.shape
  7. iflen(shape) ==2: # 全连接层的权重
  8. gmac_count+=shape[0] *shape[1] *2
  9. gmac_count=gmac_count/1e9 # 转换为十亿为单位
  10. returngmac_count

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

  1. 0.66972288

GMAC的结果是以十亿为单位,所以跟我们上面用类库计算的结果相差不大。最后再说一下,计算卷积的GMAC稍微有些复杂,公式为

  1. ((输入通道 x 卷积核高度 x 卷积核宽度) x 输出通道) x 2

,这里给一个简单的代码,不一定完全正确,供参考

  1. defcompute_gmac(model):
  2. gmac_count=0
  3. forparaminmodel.parameters():
  4. shape=param.shape
  5. iflen(shape) ==2: # 全连接层的权重
  6. gmac_count+=shape[0] *shape[1] *2
  7. eliflen(shape) ==4: # 卷积层的权重
  8. gmac_count+=shape[0] *shape[1] *shape[2] *shape[3] *2
  9. gmac_count=gmac_count/1e9 # 转换为十亿为单位
  10. returngmac_count

“计算GMAC和GFLOPS”的评论:

还没有评论