From f4227032b2a1c68d1b86a759243fcaf71bd92066 Mon Sep 17 00:00:00 2001 From: enkilee Date: Thu, 8 Aug 2024 11:42:54 +0800 Subject: [PATCH 1/4] fix --- python/paddle/hapi/dynamic_flops.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/python/paddle/hapi/dynamic_flops.py b/python/paddle/hapi/dynamic_flops.py index 817d19c1bbf95..ae3de38da304d 100644 --- a/python/paddle/hapi/dynamic_flops.py +++ b/python/paddle/hapi/dynamic_flops.py @@ -11,8 +11,15 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations import warnings +from typing import ( + TYPE_CHECKING, + Any, + Callable, + TypeAlias, +) import numpy as np @@ -22,10 +29,21 @@ from .static_flops import Table, static_flops +if TYPE_CHECKING: + from paddle import Tensor + from paddle.nn import Layer + from paddle.static import Program +_CustomOpsAlias: TypeAlias = Callable[[Callable[..., Any]], Callable[..., Any]] + __all__ = [] -def flops(net, input_size, custom_ops=None, print_detail=False): +def flops( + net: Layer | Program, + input_size: list[int], + custom_ops: _CustomOpsAlias | None = None, + print_detail: bool = False, +) -> int: """Print a table about the FLOPs of network. Args: @@ -215,7 +233,12 @@ def count_io_info(m, x, y): } -def dynamic_flops(model, inputs, custom_ops=None, print_detail=False): +def dynamic_flops( + model: Layer | Program, + inputs: Tensor, + custom_ops: _CustomOpsAlias | None = None, + print_detail: bool = False, +) -> int: handler_collection = [] types_collection = set() if custom_ops is None: From bb24dd867dc3927a2cca3729b54e144057dfaa65 Mon Sep 17 00:00:00 2001 From: enkilee Date: Thu, 8 Aug 2024 14:47:06 +0800 Subject: [PATCH 2/4] fix --- python/paddle/hapi/dynamic_flops.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/python/paddle/hapi/dynamic_flops.py b/python/paddle/hapi/dynamic_flops.py index ae3de38da304d..adc4ad77f0b3f 100644 --- a/python/paddle/hapi/dynamic_flops.py +++ b/python/paddle/hapi/dynamic_flops.py @@ -16,8 +16,6 @@ import warnings from typing import ( TYPE_CHECKING, - Any, - Callable, TypeAlias, ) @@ -30,16 +28,19 @@ from .static_flops import Table, static_flops if TYPE_CHECKING: + from collections.abc import Callable + from paddle import Tensor from paddle.nn import Layer from paddle.static import Program -_CustomOpsAlias: TypeAlias = Callable[[Callable[..., Any]], Callable[..., Any]] + + _CustomOpsAlias: TypeAlias = dict[Layer, Callable[..., None]] __all__ = [] def flops( - net: Layer | Program, + net: Layer, input_size: list[int], custom_ops: _CustomOpsAlias | None = None, print_detail: bool = False, From 8fa7be93f79bd60e771237543a2964e91608d726 Mon Sep 17 00:00:00 2001 From: enkilee Date: Fri, 9 Aug 2024 15:12:50 +0800 Subject: [PATCH 3/4] fix --- python/paddle/hapi/dynamic_flops.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/paddle/hapi/dynamic_flops.py b/python/paddle/hapi/dynamic_flops.py index adc4ad77f0b3f..eaace1614b0a3 100644 --- a/python/paddle/hapi/dynamic_flops.py +++ b/python/paddle/hapi/dynamic_flops.py @@ -14,12 +14,10 @@ from __future__ import annotations import warnings -from typing import ( - TYPE_CHECKING, - TypeAlias, -) +from typing import TYPE_CHECKING import numpy as np +from typing_extensions import TypeAlias import paddle from paddle import nn From d51e19569dbdeaf4d1387e8ed5b4b2c9c5d941a2 Mon Sep 17 00:00:00 2001 From: enkilee Date: Mon, 12 Aug 2024 09:06:01 +0800 Subject: [PATCH 4/4] fix --- python/paddle/hapi/dynamic_flops.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/paddle/hapi/dynamic_flops.py b/python/paddle/hapi/dynamic_flops.py index eaace1614b0a3..94c50a43c7f58 100644 --- a/python/paddle/hapi/dynamic_flops.py +++ b/python/paddle/hapi/dynamic_flops.py @@ -32,13 +32,13 @@ from paddle.nn import Layer from paddle.static import Program - _CustomOpsAlias: TypeAlias = dict[Layer, Callable[..., None]] + _CustomOpsAlias: TypeAlias = dict[type[Layer], Callable[..., None]] __all__ = [] def flops( - net: Layer, + net: Layer | Program, input_size: list[int], custom_ops: _CustomOpsAlias | None = None, print_detail: bool = False, @@ -102,7 +102,7 @@ def flops( ... [1, 1, 28, 28], ... custom_ops= {nn.LeakyReLU: count_leaky_relu}, ... print_detail=True) - >>> print(FLOPs) + >>> # doctest: +SKIP('numpy print with different version') 's flops has been counted 's flops has been counted Cannot find suitable count function for . Treat it as zero FLOPs. @@ -121,6 +121,8 @@ def flops( | linear_2 | [1, 84] | [1, 10] | 850 | 840 | +--------------+-----------------+-----------------+--------+--------+ Total Flops: 347560 Total Params: 61610 + >>> # doctest: -SKIP + >>> print(FLOPs) 347560 """ if isinstance(net, nn.Layer): @@ -233,7 +235,7 @@ def count_io_info(m, x, y): def dynamic_flops( - model: Layer | Program, + model: Layer, inputs: Tensor, custom_ops: _CustomOpsAlias | None = None, print_detail: bool = False,