본문 바로가기
기술관련 기록

pytorch MAC 측정 thop vs ptflops

by 저녁추천좀 2024. 2. 8.

결론 : ptflops가 더 좋음

 

+ thop는 모듈의 옵션을 반영하지 않음

+ thop는 Attention 모듈 지원하지 않음 

 

 

1.

m = nn.Conv2d(1,2,(3,3),(1,1),bias=False)
m.eval()

input = torch.rand(1,1,10,10)
macs_thop, params_thop = profile(m, inputs=(input,))
print("thop : MACS : {} | Param : {}".format(macs_thop,params_thop))

# https://github.com/sovrasov/flops-counter.pytorch

macs_ptflos, params_ptflops = get_model_complexity_info(m, (1,10,10), as_strings=False,print_per_layer_stat=True, verbose=True)   
print("ptflops : MACS {} |  PARAM {}".format(macs_ptflos,params_ptflops))
[INFO] Register count_convNd() for <class 'torch.nn.modules.conv.Conv2d'>.
thop : MACS : 1152.0 | Param : 0
Conv2d(18, 100.000% Params, 1.15 KMac, 100.000% MACs, 1, 2, kernel_size=(3, 3), stride=(1, 1), bias=False)
ptflops : MACS 1152 |  PARAM 18

 

 

 

2.

m = nn.Conv2d(1,2,(3,3),(1,1),bias=True)
m.eval()

input = torch.rand(1,1,10,10)
macs_thop, params_thop = profile(m, inputs=(input,))
print("thop : MACS : {} | Param : {}".format(macs_thop,params_thop))

# https://github.com/sovrasov/flops-counter.pytorch

macs_ptflos, params_ptflops = get_model_complexity_info(m, (1,10,10), as_strings=False,print_per_layer_stat=True, verbose=True)   
print("ptflops : MACS {} |  PARAM {}".format(macs_ptflos,params_ptflops))

 

[INFO] Register count_convNd() for <class 'torch.nn.modules.conv.Conv2d'>.
thop : MACS : 1152.0 | Param : 0
Conv2d(20, 100.000% Params, 1.28 KMac, 100.000% MACs, 1, 2, kernel_size=(3, 3), stride=(1, 1))
ptflops : MACS 1280 |  PARAM 20

 

 

3.

 

m = nn.ConvTranspose2d(1,2,(1,1),(1,1))
m.eval()

input = torch.rand(1,1,10,10)
macs_thop, params_thop = profile(m, inputs=(input,))
print("thop : MACS : {} | Param : {}".format(macs_thop,params_thop))

# https://github.com/sovrasov/flops-counter.pytorch

macs_ptflos, params_ptflops = get_model_complexity_info(m, (1,10,10), as_strings=False,print_per_layer_stat=True, verbose=True)   
print("ptflops : MACS {} |  PARAM {}".format(macs_ptflos,params_ptflops))

 

[INFO] Register count_convNd() for <class 'torch.nn.modules.conv.ConvTranspose2d'>.
thop : MACS : 200.0 | Param : 0
ConvTranspose2d(4, 100.000% Params, 400.0 Mac, 100.000% MACs, 1, 2, kernel_size=(1, 1), stride=(1, 1))
ptflops : MACS 400 |  PARAM 4

 

4.

 

class SA(nn.Module) :
    def __init__(self):
        super(SA,self).__init__()
        self.aa = nn.MultiheadAttention(128,8)
        
    def forward(self,x):
        y = self.aa(x,x,x)
        return y
m = SA()
m.eval()

input = torch.rand(1,250,128)
macs_thop, params_thop = profile(m, inputs=(input,))
print("thop : MACS : {} | Param : {}".format(macs_thop,params_thop))

# https://github.com/sovrasov/flops-counter.pytorch

macs_ptflos, params_ptflops = get_model_complexity_info(m, (250,128), as_strings=False,print_per_layer_stat=True, verbose=True)   
print("ptflops : MACS {} |  PARAM {}".format(macs_ptflos,params_ptflops))
thop : MACS : 0.0 | Param : 0
Warning: module NonDynamicallyQuantizableLinear is treated as a zero-op.
Warning: module SA is treated as a zero-op.
SA(
  66.05 k, 100.000% Params, 16.61 MMac, 99.604% MACs, 
  (aa): MultiheadAttention(
    66.05 k, 100.000% Params, 16.61 MMac, 99.604% MACs, 
    (out_proj): NonDynamicallyQuantizableLinear(0, 0.000% Params, 0.0 Mac, 0.000% MACs, in_features=128, out_features=128, bias=True)
  )
)
ptflops : MACS 16676000 |  PARAM 66048