-
Notifications
You must be signed in to change notification settings - Fork 6k
【Type Hints】Paddle 中引入 Tensor stub 文件 #63953
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
Merged
Merged
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
828abaf
add tensor.pyi
megemini 3877923
.pre-commit-config.yaml
megemini 90646a9
run gen_tensor_stub.py
megemini ac2eed7
edit cmakelists.txt
megemini f6970e1
edit cmakelists.txt
megemini 61b822b
gen pyi encoding
megemini ad945d5
not use removeprefix
megemini 28b44cc
tensor.pyi with doc
megemini b847648
add tensor.pyi.in
megemini cd97ca8
update tensor.pyi.in
megemini d88a089
update tensor.py.in
megemini e0eb289
gen_tensor_stub.py with template
megemini 770c25b
update pre-commit-config
megemini e6055d4
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
megemini 4bc21ba
[Update] tensor.pyi.in to tensor.prototype.pyi
megemini 4aa925b
[Update] .pre-commit-config.yaml
megemini 8f54b55
[Update] not pack tensor.prototype.pyi
megemini f744bdf
[Update] tensor.prototype.pyi type annotations
megemini 6df6ba0
[Update] tensor.prototype.pyi
megemini 0ba2cc1
use type string for `TYPE_CHECKING` only vars
SigureMo aae012e
[Update] tensor.prototype.pyi
megemini 0bc457b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
megemini 547b75d
[Update] tensor.prototype.pyi
megemini 0496a7f
Merge branch 'type_hint_tensor' of github.com:megemini/Paddle into ty…
megemini e3a10f6
[Update] _typing.basic TensorLike
megemini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| # 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. | ||
|
|
||
| # The `Tensor` template for `tools/gen_tensor_stub.py` generates the stub file `tensor.pyi`. | ||
| # Add docstring, attributes, methods and alias with type annotaions for `Tensor` | ||
| # if not conveniently coding in original place (like c++ source file). | ||
|
|
||
| from typing import Any, overload | ||
|
|
||
| import numpy.typing as npt | ||
| from typing_extensions import TypeAlias | ||
|
|
||
| import paddle | ||
| from paddle import _typing | ||
|
|
||
| # avoid same name: Tensor.slice | ||
| _Slice: TypeAlias = slice | ||
|
|
||
| class Tensor: | ||
| # annotation: ${tensor_docstring} | ||
|
|
||
| # annotation: ${tensor_attributes} | ||
|
|
||
| # If method defined below, we should make the method's signature complete, | ||
| # and ignore the signature extracted from `paddle.Tensor`. | ||
| # `gen_tensor.stub.py` will NOT overwrite the signature below. | ||
| # If method has docstring (ignoring the spaces), `gen_tensor.stub.py` also will NOT overwrite it. | ||
|
|
||
| # annotation: ${tensor_methods} | ||
| @overload | ||
| def __init__(self) -> None: ... | ||
| @overload | ||
| def __init__( | ||
| self, dtype, dims, name: str, type, persistable: bool | ||
| ) -> None: ... | ||
| @overload | ||
| def __init__( | ||
| self, | ||
| value: npt.NDArray[Any], | ||
| place, | ||
| persistable: bool, | ||
| zero_copy: bool, | ||
| name: str, | ||
| stop_gradient: bool, | ||
| ) -> None: ... | ||
| @overload | ||
| def __init__(self, value: npt.NDArray[Any]) -> None: ... | ||
| @overload | ||
| def __init__(self, value: Tensor) -> None: ... | ||
| @overload | ||
| def __init__( | ||
| self, value: Tensor, place, name: str, process_mesh, placements | ||
| ) -> None: ... | ||
| @overload | ||
| def __init__( | ||
| self, value: Tensor, dims, name: str, process_mesh, placements | ||
| ) -> None: ... | ||
| @overload | ||
| def __init__(self, value: Tensor, place, name: str) -> None: ... | ||
| @overload | ||
| def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
| """ | ||
| ref: paddle/fluid/pybind/eager.cc | ||
|
|
||
| We should have init function with signature: | ||
| 1. | ||
| def __init__ () | ||
| 2. | ||
| def __init__ ( | ||
| dtype: paddle::framework::proto::VarType::Type, | ||
| dims: vector<int>, | ||
| name: std::string, | ||
| type: paddle::framework::proto::VarType::LodTensor, | ||
| persistable: bool) | ||
| 3. (multi-place) | ||
| (should have at least one parameter, one parameter equals to case 4, zero | ||
| parameter equals to case 1) | ||
| def __init__ ( | ||
| value: ndarray, | ||
| place: paddle::platform::Place, | ||
| persistable: bool, | ||
| zero_copy: bool, | ||
| name: std::string, | ||
| stop_gradient: bool) | ||
| 4. | ||
| def __init__ ( | ||
| value: ndarray) | ||
| 5. | ||
| def __init__ ( | ||
| tensor: Tensor) | ||
| 6. (multi-place) | ||
| (should have at least one parameter, one parameter equals to case 5, zero | ||
| parameter equals to case 1.) | ||
| def __init__ ( | ||
| global_tensor: Tensor, | ||
| place: paddle::platform::Place, | ||
| name: std::string, | ||
| process_mesh: phi::distributed::ProcessMesh | ||
| placements: std::vector<Placement>) | ||
| 7. (multi-place) | ||
| (should have at least one parameter, one parameter equals to case 5, zero | ||
| parameter equals to case 1.) | ||
| def __init__ ( | ||
| local_tensor: Tensor, | ||
| global_dims: vector<int>, | ||
| name: std::string, | ||
| process_mesh: phi::distributed::ProcessMesh | ||
| placements: std::vector<Placement>) | ||
| 8. (multi-place) (should have at least one parameter, one parameter similar | ||
| to case 5, zero parameter equals to case 1.) | ||
| def __init__ ( | ||
| tensor: FrameworkTensor, | ||
| place: paddle::platform::Place, | ||
| name: std::string) | ||
| """ | ||
| ... | ||
| # rich comparison | ||
| def __eq__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore[override] | ||
| def __ge__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __gt__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __lt__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __le__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __ne__(self, y: _typing.TensorLike) -> Tensor: ... # type: ignore[override] | ||
|
|
||
| # binary arithmetic operations | ||
| def __add__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __sub__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __mul__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __matmul__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __truediv__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __floordiv__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __mod__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __pow__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __and__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __div__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __radd__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __rsub__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __rmul__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __rtruediv__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __rpow__(self, y: _typing.TensorLike) -> Tensor: ... | ||
| def __rdiv__(self, y: _typing.TensorLike) -> Tensor: ... | ||
|
|
||
| # type cast | ||
| def __bool__(self) -> bool: ... | ||
| def __float__(self) -> float: ... | ||
| def __int__(self) -> int: ... | ||
| def __long__(self) -> float: ... | ||
| def __nonzero__(self) -> bool: ... | ||
|
|
||
| # emulating container types | ||
| def __getitem__( | ||
| self, | ||
| item: ( | ||
| None | ||
| | bool | ||
| | int | ||
| | _Slice | ||
| | tuple[None | bool | int | _Slice, ...] | ||
| | list[Tensor | bool | int] | ||
| ), | ||
| ) -> Tensor: ... | ||
| def __setitem__( | ||
| self, | ||
| item: ( | ||
| None | ||
| | bool | ||
| | int | ||
| | _Slice | ||
| | tuple[None | bool | int | _Slice, ...] | ||
| | list[Tensor | bool | int] | ||
| ), | ||
| value: Tensor | npt.NDArray[Any] | int | float | complex | bool, | ||
| ) -> None: ... | ||
| def __len__(self) -> int: ... | ||
|
|
||
| # emulating numeric types | ||
| def __index__(self) -> int: ... | ||
|
|
||
| # unary arithmetic operations | ||
| def __invert__(self) -> Tensor: ... | ||
| def __neg__(self) -> Tensor: ... | ||
|
|
||
| # basic | ||
| def __hash__(self) -> int: ... | ||
| def clear_gradient(self, set_to_zero: bool = True) -> None: ... | ||
| def clone(self) -> Tensor: ... | ||
| def cols(self) -> Tensor: ... | ||
| def contiguous(self) -> Tensor: ... | ||
| def copy_(self) -> Tensor: ... | ||
| def crows(self) -> Tensor: ... | ||
| @property | ||
| def data(self) -> Tensor: ... | ||
| def data_ptr(self) -> int: ... | ||
| def detach(self) -> Tensor: ... | ||
| def detach_(self) -> Tensor: ... | ||
| @property | ||
| def dtype(self) -> paddle.dtype: ... | ||
| def element_size(self) -> int: ... | ||
| def get_map_tensor(self) -> Tensor: ... | ||
| def get_selected_rows(self) -> None: ... | ||
| def get_strides(self) -> list[int]: ... | ||
| def get_tensor(self) -> Tensor: ... | ||
| @property | ||
| def grad(self) -> Tensor | None: ... | ||
| @property | ||
| def grad_(self) -> Tensor | None: ... | ||
| @property | ||
| def grad_fn(self) -> Any: ... | ||
| def is_contiguous(self) -> bool: ... | ||
| def is_dense(self) -> bool: ... | ||
| def is_dist(self) -> bool: ... | ||
| @property | ||
| def is_leaf(self) -> bool: ... | ||
| def is_same_shape(self, y: Tensor) -> bool: ... | ||
| def is_selected_rows(self) -> bool: ... | ||
| def is_sparse(self) -> bool: ... | ||
| def is_sparse_coo(self) -> bool: ... | ||
| def is_sparse_csr(self) -> bool: ... | ||
| @property | ||
| def layout(self) -> _typing.DataLayoutND: ... | ||
| @property | ||
| def name(self) -> str: ... | ||
| @property | ||
| def ndim(self) -> int: ... | ||
| def nnz(self) -> int: ... | ||
| @property | ||
| def num_shard(self) -> int: ... | ||
| def numpy(self) -> npt.NDArray[Any]: ... | ||
| @property | ||
| def offset(self) -> int: ... | ||
| @property | ||
| def persistable(self) -> bool: ... | ||
| @property | ||
| def place(self) -> paddle.core.Place: ... | ||
| @property | ||
| def placements(self) -> list[paddle.distributed.Placement] | None: ... | ||
| @property | ||
| def process_mesh(self) -> paddle.distributed.ProcessMesh | None: ... | ||
| def rows(self) -> list[int]: ... | ||
| def set_string_list(self, value: str) -> None: ... | ||
| def set_vocab(self, value: dict) -> None: ... | ||
| @property | ||
| def shape(self) -> list[int]: ... | ||
| @property | ||
| def size(self) -> int: ... | ||
| @property | ||
| def strides(self) -> list[int]: ... | ||
| @property | ||
| def type(self) -> Tensor: ... | ||
|
|
||
| # annotation: ${tensor_alias} | ||
| __qualname__ = "Tensor" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.