2024-05-13 11:33:38 +08:00
|
|
|
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# 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
|
|
|
|
|
|
2025-05-26 23:34:40 +08:00
|
|
|
from collections.abc import Sequence
|
2024-06-19 22:00:57 +08:00
|
|
|
from typing import (
|
|
|
|
|
TYPE_CHECKING,
|
|
|
|
|
Any,
|
|
|
|
|
TypeVar,
|
|
|
|
|
Union,
|
|
|
|
|
)
|
2024-05-13 11:33:38 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2024-05-23 21:32:26 +08:00
|
|
|
import numpy.typing as npt
|
2024-06-30 11:14:58 +08:00
|
|
|
from typing_extensions import Never, TypeAlias
|
2024-05-13 11:33:38 +08:00
|
|
|
|
2024-06-17 00:32:44 +08:00
|
|
|
from .backport import EllipsisType
|
|
|
|
|
|
2024-05-13 11:33:38 +08:00
|
|
|
if TYPE_CHECKING:
|
2024-06-28 20:26:51 +08:00
|
|
|
from paddle import ParamAttr, Tensor
|
|
|
|
|
from paddle.nn.initializer import Initializer
|
|
|
|
|
from paddle.regularizer import WeightDecayRegularizer
|
2024-05-13 11:33:38 +08:00
|
|
|
|
2024-06-17 00:32:44 +08:00
|
|
|
|
2025-07-22 08:06:46 +08:00
|
|
|
Numeric: TypeAlias = Union[int, float, bool, complex, np.number, "Tensor"]
|
2024-11-05 10:55:10 +08:00
|
|
|
TensorLike: TypeAlias = Union[npt.NDArray[Any], "Tensor", Numeric]
|
2024-06-17 00:32:44 +08:00
|
|
|
_TensorIndexItem: TypeAlias = Union[
|
|
|
|
|
None, bool, int, slice, "Tensor", EllipsisType
|
|
|
|
|
]
|
|
|
|
|
TensorIndex: TypeAlias = Union[
|
|
|
|
|
_TensorIndexItem,
|
2025-05-26 23:34:40 +08:00
|
|
|
tuple[_TensorIndexItem, ...],
|
|
|
|
|
list[_TensorIndexItem],
|
2024-06-17 00:32:44 +08:00
|
|
|
]
|
|
|
|
|
|
2024-05-13 11:33:38 +08:00
|
|
|
|
2024-06-05 17:15:45 +08:00
|
|
|
_T = TypeVar("_T")
|
2024-05-13 11:33:38 +08:00
|
|
|
|
2024-06-05 17:15:45 +08:00
|
|
|
NestedSequence = Union[_T, Sequence["NestedSequence[_T]"]]
|
2025-05-26 23:34:40 +08:00
|
|
|
NestedList = Union[_T, list["NestedList[_T]"]]
|
2024-06-21 00:40:47 +08:00
|
|
|
NestedStructure = Union[
|
2025-05-26 23:34:40 +08:00
|
|
|
_T, dict[str, "NestedStructure[_T]"], Sequence["NestedStructure[_T]"]
|
2024-06-21 00:40:47 +08:00
|
|
|
]
|
2024-11-05 10:55:10 +08:00
|
|
|
NumericSequence = Sequence[Numeric]
|
|
|
|
|
NestedNumericSequence: TypeAlias = NestedSequence[Numeric]
|
2024-05-13 11:33:38 +08:00
|
|
|
TensorOrTensors: TypeAlias = Union["Tensor", Sequence["Tensor"]]
|
2024-06-28 20:26:51 +08:00
|
|
|
|
|
|
|
|
ParamAttrLike: TypeAlias = Union[
|
|
|
|
|
"ParamAttr", "Initializer", "WeightDecayRegularizer", str, bool
|
|
|
|
|
]
|
2024-06-30 11:14:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def unreached() -> Never:
|
|
|
|
|
"""Mark a code path as unreachable.
|
|
|
|
|
Refer to https://typing.readthedocs.io/en/latest/source/unreachable.html#marking-code-as-unreachable
|
|
|
|
|
"""
|
|
|
|
|
raise RuntimeError("Unreachable code path")
|