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

[Typing][B-94] Add type annotations for python/paddle/hapi/dynamic_flops.py #67204

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions python/paddle/hapi/dynamic_flops.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
# 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,
TypeAlias,
enkilee marked this conversation as resolved.
Show resolved Hide resolved
)

import numpy as np

Expand All @@ -22,10 +27,24 @@

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 = dict[Layer, Callable[..., None]]
enkilee marked this conversation as resolved.
Show resolved Hide resolved

__all__ = []


def flops(net, input_size, custom_ops=None, print_detail=False):
def flops(
net: Layer,
enkilee marked this conversation as resolved.
Show resolved Hide resolved
input_size: list[int],
custom_ops: _CustomOpsAlias | None = None,
print_detail: bool = False,
) -> int:
"""Print a table about the FLOPs of network.

Args:
Expand Down Expand Up @@ -215,7 +234,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,
enkilee marked this conversation as resolved.
Show resolved Hide resolved
inputs: Tensor,
custom_ops: _CustomOpsAlias | None = None,
print_detail: bool = False,
) -> int:
handler_collection = []
types_collection = set()
if custom_ops is None:
Expand Down