Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bias not taken into account for FLOPs computation. #53

Closed
pzobel opened this issue Oct 24, 2019 · 2 comments
Closed

Bias not taken into account for FLOPs computation. #53

pzobel opened this issue Oct 24, 2019 · 2 comments

Comments

@pzobel
Copy link

pzobel commented Oct 24, 2019

The bias of a Linear layer is not taken into account for the FLOPs computation, as shown below:

Example:

import torch
import torch.nn as nn
from thop import profile

class Simple_FC_Net(nn.Module):
    def __init__(self, bias=True):
        super(Simple_FC_Net, self).__init__()
        self.fc = nn.Linear(in_features=17, out_features=49, bias=bias)

    def forward(self, x):
        x = self.fc(x)
        return x

input_simple_fc = torch.randn(1, 17)

# Model without bias
print("Model without bias:")
model = Simple_FC_Net(bias=False)
flops, params = profile(model, inputs=(input_simple_fc,))
print("Flops: {}, Params: {}".format(flops, params))

# Model with bias
print("Model with bias:")
model = Simple_FC_Net(bias=True)
flops, params = profile(model, inputs=(input_simple_fc,))
print("Flops: {}, Params: {}".format(flops, params))

Output:

Model without bias:
Register FLOP counter for module Linear(in_features=17, out_features=49, bias=False)
Flops: 1617.0, Params: 833.0

Model with bias:
Register FLOP counter for module Linear(in_features=17, out_features=49, bias=True)
Flops: 1617.0, Params: 882.0
@Lyken17
Copy link
Owner

Lyken17 commented Oct 25, 2019

Thanks for pointing out. Can you submit a PR?

@Lyken17
Copy link
Owner

Lyken17 commented Nov 14, 2019

Close because the PR is merged. Thanks for @pzobel 's contribution!

@Lyken17 Lyken17 closed this as completed Nov 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants