Lyken17 / pytorch-OpCounter

Count the MACs / FLOPs of your PyTorch model.
MIT License
4.9k stars 528 forks source link

AttributeError: 'LayerNorm' object has no attribute 'affine' #182

Open Xinchengzelin opened 2 years ago

Xinchengzelin commented 2 years ago

When I use profile, the error: AttributeError: 'LayerNorm' object has no attribute 'affine', is it a bug?

environment: OS: Ubuntu 2004 Python: 3.8.5 Pytorch : 1.10.2 thop: thop-0.1.1.post2207130030

Fusica commented 2 years ago

Same problem, I can only downgrade to 0.0.31 to solve, hope to know why

yuchen2199 commented 2 years ago

Same problem...

Raychen0617 commented 2 years ago

Same problem here

MilchstraB commented 2 years ago

Same problem here

changesthishard commented 2 years ago

Me too

TangYuan96 commented 2 years ago

Same problem, I can only downgrade to 0.0.31 to solve, hope to know why

me, too.

as you say

pip install thop==0.0.31-2005241907

work!!!

ae86208 commented 2 years ago

change count_normalization in thop/vision/basic_hooks.py works for me

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 hasattr(m, 'affine') and m.affine or hasattr(m, 'elementwise_affine') and  m.elementwise_affine:
        flops *= 2
    m.total_ops += flops
Sinp17 commented 2 years ago

Same problem here......the above methods do not work for me....

ziippy commented 2 years ago

change the basic_hooks.py works for me

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())
    try:
        if m.affine:
            flops *= 2
    except:
        logging.warning('no attribute affine')
    m.total_ops += flops

If you change the code, you must restart the kernel.

seraphzl commented 2 years ago

change count_normalization in thop/vision/basic_hooks.py works for me

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 hasattr(m, 'affine') and m.affine or hasattr(m, 'elementwise_affine') and  m.elementwise_affine:
        flops *= 2
    m.total_ops += flops

Works for me, too.

Lyken17 commented 2 years ago

Got it. Let me fix the issue.

Lyken17 commented 2 years ago

@JaredFern has pushed a PR that should fix issue. The pypi package is also updated. Please have a check.

https://github.com/Lyken17/pytorch-OpCounter/pull/189