Lyken17 / pytorch-OpCounter

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

Incorrect macs without specifying batch size for conv layers #212

Open pcolange opened 1 year ago

pcolange commented 1 year ago

Below is a simple and reproducible example. I have only seen this with Conv1D and Conv2D layers where L != inp_channels.

torch 2.0.1 thop 0.1.1

import torch
from thop import profile

inp_channels = 32
L = 128
model = torch.nn.Conv1d(
    in_channels=inp_channels,
    out_channels=32,
    kernel_size=3,
    padding=4,
    stride=2,
    bias=True,
)
batch_size = 1
inp = torch.ones(batch_size, inp_channels, L)
macs, params = profile(model, inputs=(inp,))
print("macs: ", macs) # 205824.0 correct

inp = torch.ones(inp_channels, L)
macs, params = profile(model, inputs=(inp,))
print("macs: ", macs) # 823296.0 incorrect