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-90] Add type annotations for python/paddle/inference/wrapper.py #67366

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Changes from all 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
20 changes: 16 additions & 4 deletions python/paddle/inference/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import logging
import os
from typing import TYPE_CHECKING, Any, TypedDict

import numpy as np

Expand All @@ -31,6 +32,16 @@
)
from paddle.base.log_helper import get_logger

if TYPE_CHECKING:
import numpy.typing as npt
from typing_extensions import Unpack

from paddle import Tensor

class _WhiteList(TypedDict):
white_list: set[str]


_logger = get_logger(
__name__, logging.INFO, fmt='%(asctime)s-%(levelname)s: %(message)s'
)
Expand All @@ -43,7 +54,7 @@
Predictor = PaddleInferPredictor


def tensor_copy_from_cpu(self, data):
def tensor_copy_from_cpu(self, data: npt.NDArray[Any] | list[str]) -> None:
'''
Support input type check based on tensor.copy_from_cpu.
'''
Expand All @@ -57,7 +68,7 @@ def tensor_copy_from_cpu(self, data):
)


def tensor_share_external_data(self, data):
def tensor_share_external_data(self, data: Tensor) -> None:
'''
Support input type check based on tensor.share_external_data.
'''
Expand Down Expand Up @@ -86,8 +97,8 @@ def convert_to_mixed_precision(
backend: PlaceType,
keep_io_types: bool = True,
black_list: set[str] = set(),
**kwargs,
):
**kwargs: Unpack[_WhiteList],
) -> None:
'''
Convert a fp32 model to mixed precision model.

Expand All @@ -99,6 +110,7 @@ def convert_to_mixed_precision(
mixed_precision: The precision, e.g. PrecisionType.Half.
backend: The backend, e.g. PlaceType.GPU.
keep_io_types: Whether the model input and output dtype remains unchanged.
Default is True.
black_list: Operators that do not convert precision.
kwargs: Supported keys including 'white_list'.
- white_list: Operators that do convert precision.
Expand Down