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

应用也非常简单:

 importtorchvision.modelsasmodels importtorch fromptflopsimportget_model_complexity_info importre  #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

咱们能够自定义一个模型来看看后果是否正确:

 importos importtorch fromtorchimportnn  classNeuralNetwork(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),         )      defforward(self, x):         x=self.flatten(x)         logits=self.linear_relu_stack(x)         returnlogits      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值。

 importtorch importtorch.nnasnn  defcompute_gmac(model):     gmac_count=0     forparaminmodel.parameters():         shape=param.shape         iflen(shape) ==2:  # 全连贯层的权重             gmac_count+=shape[0] *shape[1] *2     gmac_count=gmac_count/1e9  # 转换为十亿为单位     returngmac_count

依据下面给定的模型,计算GMAC的后果如下:

 0.66972288

GMAC的后果是以十亿为单位,所以跟咱们下面用类库计算的后果相差不大。最初再说一下,计算卷积的GMAC略微有些简单,公式为

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

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

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

https://avoid.overfit.cn/post/338fd20f0d014afabe273aaca4b05408