Lyken17 / pytorch-OpCounter

Count the MACs / FLOPs of your PyTorch model.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Xinchengzelin opened this issue · comments

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

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

Same problem...

Same problem here

Same problem here

Me too

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!!!

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

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

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.

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.

Got it. Let me fix the issue.

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

#189