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

Counting FLOPS for a custom op with set_op_handle: a toy example that doesn't work. #147

Open
guynich opened this issue Apr 9, 2024 · 0 comments

Comments

@guynich
Copy link

guynich commented Apr 9, 2024

I'm extending the given example for fvcore.nn.FlopCountAnalysis to add flops count of a custom op within my model class.

import torch

from collections import Counter

from fvcore.nn import FlopCountAnalysis
from torch import nn

class TestModel(nn.Module):
    """Toy model."""
    def __init__(self):
        super().__init__()
        self.act = nn.ReLU()
        self.conv = nn.Conv2d(in_channels=3, out_channels=10, kernel_size=1)
        self.fc = nn.Linear(in_features=1000, out_features=10)

    def forward(self, x):
        _ = self.custom_op_flop_counter(inputs=x, outputs=None)
        return self.fc(self.act(self.conv(x)).flatten(1))

    @staticmethod
    # Has no access to anything else in the class.
    def custom_op_flop_counter(inputs, outputs) -> Counter:
        """Returns counter value to include in flops."""
        # The function should return a counter object with per-operator statistics.
        return Counter({'custom_op': 500})


model = TestModel()
inputs = (torch.randn((1, 3, 10, 10)),)

flops = FlopCountAnalysis(
    model,
    inputs).set_op_handle(
        "custom_op", model.custom_op_flop_counter)

print(flops.by_module_and_operator())

The "custom_op" and its returned value of 500 are not seen in the print statement. It does print the expected values for the linear and conv operators, e.g.: {'': Counter({'linear': 10000, 'conv': 3000}), 'act': Counter(), 'conv': Counter({'conv': 3000}), 'fc': Counter({'linear': 10000})}. What am I doing wrong here that prevents my custom op count being included?

@guynich guynich changed the title Counting FLOPS for a custom ops with set_op_handle: a toy example that doesn't work. Counting FLOPS for a custom op with set_op_handle: a toy example that doesn't work. Apr 9, 2024
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

1 participant