From 8678012725b3a7a40981be0c7444f0432ba90d18 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Wed, 20 Aug 2025 18:19:30 +0200 Subject: [PATCH 1/4] Update workflow --- .github/workflows/main.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 1e2b042..ab205bb 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -45,5 +45,5 @@ jobs: python -m pytest . -s - name: Linting with flake8 run: | - python -m flake8 . - python -m isort -rc --check-only --diff ./ptflops ./tests \ No newline at end of file + python -m flake8 ./ptflops ./tests ./samples + python -m isort -rc --check-only --diff ./ptflops ./tests ./samples \ No newline at end of file From 1e43df9ae9da93ff0f81edeb26e788550dbd80f6 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Wed, 20 Aug 2025 18:19:41 +0200 Subject: [PATCH 2/4] Fix linters --- .isort.cfg | 2 +- ptflops/aten_engine.py | 4 ++-- ptflops/pytorch_engine.py | 6 +++--- ptflops/pytorch_ops.py | 3 ++- pyproject.toml | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/.isort.cfg b/.isort.cfg index fdeb838..9949965 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -1,5 +1,5 @@ [isort] -line_length = 79 +line_length = 89 multi_line_output = 0 known_standard_library = setuptools known_first_party = ptflops diff --git a/ptflops/aten_engine.py b/ptflops/aten_engine.py index c149ca8..1202832 100644 --- a/ptflops/aten_engine.py +++ b/ptflops/aten_engine.py @@ -54,7 +54,7 @@ def f(*args): def exit_module(self, name): def f(*args): - assert(self.parents[-1] == name) + assert (self.parents[-1] == name) self.parents.pop() return f @@ -138,7 +138,7 @@ def get_flops_aten(model, input_res, except Exception as e: print("Flops estimation was not finished successfully because of" - f" the following exception:\n{type(e)} : {e}") + f" the following exception: \n{type(e)}: {e}") traceback.print_exc() return None, None diff --git a/ptflops/pytorch_engine.py b/ptflops/pytorch_engine.py index 8585ef7..4b14c16 100644 --- a/ptflops/pytorch_engine.py +++ b/ptflops/pytorch_engine.py @@ -15,8 +15,8 @@ import torch.nn as nn import torch.nn.functional as F -from .pytorch_ops import (CUSTOM_MODULES_MAPPING, FUNCTIONAL_MAPPING, - MODULES_MAPPING, TENSOR_OPS_MAPPING) +from .pytorch_ops import (CUSTOM_MODULES_MAPPING, FUNCTIONAL_MAPPING, MODULES_MAPPING, + TENSOR_OPS_MAPPING) from .utils import flops_to_string, params_to_string @@ -72,7 +72,7 @@ def reset_environment(): except Exception as e: print("Flops estimation was not finished successfully because of" - f" the following exception:\n{type(e)} : {e}") + f" the following exception: \n{type(e)}: {e}") traceback.print_exc() reset_environment() diff --git a/ptflops/pytorch_ops.py b/ptflops/pytorch_ops.py index 7fe061a..2ae14aa 100644 --- a/ptflops/pytorch_ops.py +++ b/ptflops/pytorch_ops.py @@ -57,7 +57,8 @@ def bn_flops_counter_hook(module, input, output): module.__flops__ += int(batch_flops) -def conv_flops_counter_hook(conv_module, input, output, extra_per_position_flops=0, transpose=False): +def conv_flops_counter_hook(conv_module, input, output, extra_per_position_flops=0, + transpose=False): # Can have multiple inputs, getting the first one input = input[0] diff --git a/pyproject.toml b/pyproject.toml index 0467520..7da9eef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,7 +26,7 @@ classifiers = [ [project.optional-dependencies] dev = [ - "flake8==3.8.1", + "flake8==5.0.1", "flake8-import-order==0.18.1", "isort==4.3.21", "torchvision>=0.5.0", From 93311b2851d91ea0c1cddcda9be6767159a1097a Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Wed, 20 Aug 2025 18:46:25 +0200 Subject: [PATCH 3/4] Update pytest run command --- .github/workflows/main.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index ab205bb..851b9b4 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -42,7 +42,7 @@ jobs: pip install .[dev] - name: Testing with pytest run: | - python -m pytest . -s + python -m pytest ./tests -s -v - name: Linting with flake8 run: | python -m flake8 ./ptflops ./tests ./samples From 994faeb5b929f97e596728ee4e01909442df24ab Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Wed, 20 Aug 2025 18:46:47 +0200 Subject: [PATCH 4/4] Finally fix transposed conv --- ptflops/pytorch_ops.py | 3 +-- tests/common_test.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ptflops/pytorch_ops.py b/ptflops/pytorch_ops.py index 2ae14aa..2dd2149 100644 --- a/ptflops/pytorch_ops.py +++ b/ptflops/pytorch_ops.py @@ -85,8 +85,7 @@ def conv_flops_counter_hook(conv_module, input, output, extra_per_position_flops bias_flops = 0 if conv_module.bias is not None: - - bias_flops = out_channels * active_elements_count + bias_flops = batch_size * int(np.prod(list(output.shape[1:]), dtype=np.int64)) overall_flops = overall_conv_flops + bias_flops diff --git a/tests/common_test.py b/tests/common_test.py index 566d3de..f3b319c 100644 --- a/tests/common_test.py +++ b/tests/common_test.py @@ -33,6 +33,17 @@ def test_conv(self, default_input_image_size, backend: FLOPS_BACKEND): assert params == 3 * 3 * 2 * 3 + 2 assert macs == 2759904 + @pytest.mark.parametrize("backend", [FLOPS_BACKEND.PYTORCH, FLOPS_BACKEND.ATEN]) + def test_conv_t(self, default_input_image_size, backend: FLOPS_BACKEND): + net = nn.ConvTranspose2d(3, 2, 3, stride=(2, 2), bias=True) + macs, params = get_model_complexity_info(net, default_input_image_size, + as_strings=False, + print_per_layer_stat=False, + backend=backend) + + assert params == 3 * 3 * 2 * 3 + 2 + assert macs == 3112706 + @pytest.mark.parametrize("backend", [FLOPS_BACKEND.PYTORCH, FLOPS_BACKEND.ATEN]) def test_fc(self, backend: FLOPS_BACKEND): net = nn.Sequential(nn.Linear(3, 2, bias=True))