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][C-43, C-44][BUAA] Add type annotations for python/paddle/distributed/fleet/* #67469

Merged
merged 8 commits into from
Aug 21, 2024

Conversation

Whsjrczr
Copy link
Contributor

@Whsjrczr Whsjrczr commented Aug 15, 2024

PR Category

User Experience

PR Types

Improvements

Description

类型标注:

  • python/paddle/distributed/fleet/base/util_factory.py
  • python/paddle/distributed/fleet/data_generator/data_generator.py

Related links

@SigureMo @megemini

Copy link

paddle-bot bot commented Aug 15, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@luotao1 luotao1 added contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 labels Aug 15, 2024
@megemini
Copy link
Contributor

关联 #65008

@Whsjrczr Whsjrczr changed the title [Typing][C-43, C-44][BUAA] Add type annotations for 2 files in python/paddle/distributed/fleet/ [Typing][C-43, C-44][BUAA] Add type annotations for python/paddle/distributed/fleet/* Aug 16, 2024
from collections.abc import MutableSet

from paddle.base.framework import Block
from paddle.distributed.auto_parallel import Strategy
Copy link
Contributor

Choose a reason for hiding this comment

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

DistributedStrategy

Comment on lines 63 to 64
self.role_maker: None | PaddleCloudRoleMaker = None
self.dist_strategy: None | Strategy = None
Copy link
Contributor

Choose a reason for hiding this comment

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

None 放到后面

def all_reduce(self, input, mode="sum", comm_world="worker"):
def all_reduce(
self,
input: list | tuple | np.array,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
input: list | tuple | np.array,
input: NestedNumbericSequence | npt.NDArray[Any],

def all_reduce(
self,
input: list | tuple | np.array,
mode: str = "sum",
Copy link
Contributor

Choose a reason for hiding this comment

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

Literal

self,
input: list | tuple | np.array,
mode: str = "sum",
comm_world: str = "worker",
Copy link
Contributor

Choose a reason for hiding this comment

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

Literal

Comment on lines 452 to 461
def feed_gen(
batch_size: int,
feeded_vars_dims: list[int],
feeded_vars_filelist: list[str],
) -> list[list[np.array[list[float]]]]:
def reader(
batch_size: int,
fn: str,
dim: list[float] | tuple[float] | float,
) -> list[np.array[list[float]]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

内部函数不用标注

def reader(batch_size, fn, dim):
def _params_check(
self, config: Any
) -> list | list[np.array[Any]] | Literal[False]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
) -> list | list[np.array[Any]] | Literal[False]:
) -> list[Tensor] | list[npt.NDArray[Any]] | Literal[False]:

Comment on lines 507 to 509
def check_not_expected_ops(
prog: Program, not_expected_op_types: list
) -> MutableSet:
Copy link
Contributor

Choose a reason for hiding this comment

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

不用标注

@@ -230,7 +230,7 @@ def local_iter():
# add more generalized DataGenerator that can adapt user-defined slot
# for example, [(name, float_list), (name, str_list), (name, int_list)]
class MultiSlotStringDataGenerator(DataGenerator):
def _gen_str(self, line):
def _gen_str(self, line: str) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

参考示例格式

            >>> [("words", ["1926", "08", "17"]), ("label", ["1"])]
            >>> or (("words", ["1926", "08", "17"]), ("label", ["1"]))

@@ -275,7 +275,7 @@ def _gen_str(self, line):


class MultiSlotDataGenerator(DataGenerator):
def _gen_str(self, line):
def _gen_str(self, line: str) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

同上,看一下示例代码 ~


import numpy as np
import numpy.typing as npt
Copy link
Contributor

Choose a reason for hiding this comment

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

放到 TYPE_CHECKING 里面


def _set_strategy(self, dist_strategy):
def _set_strategy(self, dist_strategy: None | DistributedStrategy) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

None 放到后面 ... ... 别说哪儿改哪儿啊 ~ 相关的问题都要修改 ~

def all_reduce(self, input, mode="sum", comm_world="worker"):
def all_reduce(
self,
input: NestedNumbericSequence | np.NDArray[Any],
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
input: NestedNumbericSequence | np.NDArray[Any],
input: NestedNumbericSequence | npt.NDArray[Any],

@@ -230,7 +231,9 @@ def local_iter():
# add more generalized DataGenerator that can adapt user-defined slot
# for example, [(name, float_list), (name, str_list), (name, int_list)]
class MultiSlotStringDataGenerator(DataGenerator):
def _gen_str(self, line):
def _gen_str(
self, line: tuple[tuple[str, list[str]]] | list[tuple[str, list[str]]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self, line: tuple[tuple[str, list[str]]] | list[tuple[str, list[str]]]
self, line: tuple[tuple[str, list[str]], ...] | list[tuple[str, list[str]]]

SequenceSequence[tuple[str, list[str]]]

@@ -275,7 +278,9 @@ def _gen_str(self, line):


class MultiSlotDataGenerator(DataGenerator):
def _gen_str(self, line):
def _gen_str(
self, line: tuple[tuple[str, list[int]]] | list[tuple[str, list[int]]]
Copy link
Contributor

Choose a reason for hiding this comment

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

同上。另外,应该是 list[float]

the type of element{type(elem)} must be in int or float

@@ -14,6 +14,7 @@
from __future__ import annotations

import sys
from typing import Sequence
Copy link
Contributor

Choose a reason for hiding this comment

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

collections.abc 导入

def _gen_str(
self,
line: (
Sequence[tuple[str, list[str]], ...] | list[tuple[str, list[str]]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Sequence[tuple[str, list[str]], ...] | list[tuple[str, list[str]]]
Sequence[tuple[str, list[str]]]

了解一下 listSequence 的关系

def _gen_str(self, line):
def _gen_str(
self,
line: Sequence[tuple[str, list[float]]] | list[tuple[str, list[float]]],
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
line: Sequence[tuple[str, list[float]]] | list[tuple[str, list[float]]],
line: Sequence[tuple[str, list[float]]],

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

Copy link
Contributor

@megemini megemini left a comment

Choose a reason for hiding this comment

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

LGTM

@luotao1 luotao1 merged commit a6282ef into PaddlePaddle:develop Aug 21, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants