Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Typing][C-17] Add type annotations for python/paddle/distributed/communication/gather.py #66276

Merged
merged 5 commits into from
Jul 23, 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
17 changes: 16 additions & 1 deletion python/paddle/distributed/communication/gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,26 @@
# limitations under the License.


from __future__ import annotations

from typing import TYPE_CHECKING

from paddle import framework
from paddle.distributed.communication import stream

if TYPE_CHECKING:
from paddle import Tensor
from paddle.base.core import task
from paddle.distributed.communication.group import Group


def gather(tensor, gather_list=None, dst=0, group=None, sync_op=True):
def gather(
tensor: Tensor,
gather_list: list = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
gather_list: list = None,
gather_list: list[Tensor] = None,

泛型必须写明参数类型

dst: int = 0,
group: Group | None = None,
sync_op: bool = True,
) -> task | None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返回值是 Union 的情况优先考虑使用 overload 进行区分,这里可以使用 sync_op 来进行区分

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可参考 python/paddle/tensor/linalg.pylu 函数

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@overload
def lu(
x: Tensor,
pivot: bool = ...,
get_infos: Literal[True] = ...,
name: str | None = ...,
) -> tuple[Tensor, Tensor, Tensor]:
...

@overload
def lu(
x: Tensor, pivot: bool = ..., get_infos: bool = ..., name: str | None = ...
) -> tuple[Tensor, Tensor] | tuple[Tensor, Tensor, Tensor]:
...

def lu(
x, pivot=True, get_infos=False, name=None
) -> tuple[Tensor, Tensor] | tuple[Tensor, Tensor, Tensor]:

是也要像这样写多了def lu()吗?还是把返回值改成tuple就行?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overload 就是要写多个的

"""

Gather tensors from all participators.
Expand Down