Lyken17 / pytorch-OpCounter

Count the MACs / FLOPs of your PyTorch model.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

question about BN

dyhBUPT opened this issue · comments

Hi, I have a question about BN here:

def count_normalization(m: nn.modules.batchnorm._BatchNorm, x, y):
# TODO: add test cases
# https://github.com/Lyken17/pytorch-OpCounter/issues/124
# y = (x - mean) / sqrt(eps + var) * weight + bias
x = x[0]
# bn is by default fused in inference
flops = calculate_norm(x.numel())
if (getattr(m, 'affine', False) or getattr(m, 'elementwise_affine', False)):
flops *= 2
m.total_ops += flops

def calculate_norm(input_size):
"""input is a number not a array or tensor"""
return torch.DoubleTensor([2 * input_size])

Maybe the MACs of BN (eval, no affine) should be x.numel(), not 2 * x.numel()?