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-88] Add type annotations for python/paddle/nn/utils/transform_parameters.py #65811

Merged
merged 4 commits into from
Jul 13, 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
21 changes: 16 additions & 5 deletions python/paddle/nn/utils/transform_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from functools import reduce
from typing import TYPE_CHECKING, Iterable

import paddle
from paddle import _C_ops
Expand All @@ -23,9 +26,13 @@
in_dygraph_mode,
)

if TYPE_CHECKING:
from paddle import Tensor
from paddle._typing import ShapeLike


# input==output, inplace strategy of reshape has no cost almostly
def _inplace_reshape_dygraph(x, shape):
def _inplace_reshape_dygraph(x: Tensor, shape: ShapeLike) -> None:
x_shape = _create_tensor(dtype='int64')
if in_dygraph_mode():
with paddle.base.dygraph.no_grad():
Expand All @@ -42,12 +49,12 @@ def _inplace_reshape_dygraph(x, shape):


@dygraph_only
def _stride_column(param):
def _stride_column(param: Tensor) -> None:
"""
A tool function. Permute date of parameter as a 'columns' stride. Now, it only support 2-D parameter.

Args:
param(Tensor]): The param that will be strided according to 'columns'.
param(Tensor): The param that will be strided according to 'columns'.

Examples:
.. code-block:: python
Expand Down Expand Up @@ -75,7 +82,9 @@ def _stride_column(param):


@dygraph_only
def parameters_to_vector(parameters, name=None):
def parameters_to_vector(
parameters: Iterable[Tensor], name: str | None = None
) -> Tensor:
"""
Flatten parameters to a 1-D Tensor.

Expand Down Expand Up @@ -126,7 +135,9 @@ def parameters_to_vector(parameters, name=None):


@dygraph_only
def vector_to_parameters(vec, parameters, name=None):
def vector_to_parameters(
vec: Tensor, parameters: Iterable[Tensor], name: str | None = None
) -> None:
"""
Transform a 1-D Tensor to the input ``parameters`` .

Expand Down