首頁  >  文章  >  科技週邊  >  淺析計算GMAC和GFLOPS

淺析計算GMAC和GFLOPS

王林
王林轉載
2023-05-26 08:59:361236瀏覽

GMAC 代表「Giga Multiply-Add Operations per Second」(每秒千兆乘法累積運算),是用來衡量深度學習模型計算效率的指標。此指標以每秒十億個乘加運算的形式表示模型的計算速度。

淺析計算GMAC和GFLOPS

乘法累加 (MAC) 運算是許多數學計算中的基本運算,包括矩陣乘法、卷積和深度學習中常用的其他張量運算。每個 MAC 操作都涉及將兩個數字相乘並將結果新增至累加器。

可以使用以下公式計算 GMAC 指標:

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

乘加運算的數量通常透過分析網路架構和模型參數的維度來確定,例如權重和偏差。

透過 GMAC 指標,研究人員和從業者可以就模型選擇、硬體需求和最佳化策略做出明智的決策,以實現高效且有效的深度學習運算。

淺析計算GMAC和GFLOPS

GFLOPS是電腦系統或特定運算運算效能的衡量指標,代表每秒可執行十億次浮點運算。它是每秒鐘浮點運算的數量,以十億 (giga) 表示。

浮點運算是指以 IEEE 754 浮點格式表示的實數進行算術計算。這些運算通常包括加法、減法、乘法、除法和其他數學運算。

GFLOPS 通常用於高效能運算 (HPC) 和基準測試,特別是在需要繁重運算任務的領域,例如科學模擬、資料分析和深度學習。

計算 GFLOPS公式如下:

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

GFLOPS是一項有效的測量不同電腦系統、處理器或特定操作的運算能力的指標。它有助於評估執行浮點計算的硬體或演算法的速度和效率。 GFLOPS 是衡量理論峰值效能的指標,可能無法反映實際場景中實現的實際效能,因為它沒有考慮記憶體存取、平行化和其他系統限制等因素。

GMAC 和GFLOPS 之間的關係

<code>1 GFLOP = 2 GMAC</code>

如果我們想計算這兩個指標,手動寫程式碼的話會比較麻煩,但是Python已經有現成的函式庫讓我們使用:

ptflops 函式庫就可以計算GMAC 和GFLOPs

<code>pip install ptflops</code>

使用也非常簡單:

<code>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: {:</code>

結果如下:

<code>Computational complexity: 7.82 GMac Computational complexity: 15.64 GFlops Number of parameters: 28.68 M</code>

我們可以自訂一個模型來看看結果是否正確:

<code>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: {:</code>

結果如下:

<code>Computational complexity: 670.73 KMac Computational complexity: 1341.46 KFlops Number of parameters: 669.71 k</code>

為了方便演示,我們只編寫全連接層程式碼來手動計算GMAC。遍歷模型權重參數並計算乘法和加法操作數量的形狀取決於權重參數,這是計算GMAC的關鍵。計算GMAC所需的全連接層權重的公式為2 x (輸入維度 x 輸出維度) 。總的GMAC值是透過將每個線性層的權重參數的形狀相乘並累積而得出的,這個過程是基於模型的結構。

<code>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</code>

根據上面給定的模型,計算GMAC的結果如下:

<code>0.66972288</code>

由於GMAC的結果以十億為單位,因此與我們上面使用類別庫計算的結果相差不大。最後再說一下,計算卷積的GMAC稍微有些複雜,公式為((輸入通道x 卷積核高度x 卷積核寬度) x 輸出通道) x 2,這裡給一個簡單的代碼,不一定完全正確,供參考

<code>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</code>

以上是淺析計算GMAC和GFLOPS的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51cto.com。如有侵權,請聯絡admin@php.cn刪除