Skip to content

[Typing][C-43, C-44][BUAA] Add type annotations for python/paddle/distributed/fleet/* - #67469

Merged
luotao1 merged 8 commits into
PaddlePaddle:developfrom
Whsjrczr:th004
Aug 21, 2024
Merged

luotao1 merged 8 commits into
PaddlePaddle:developfrom
Whsjrczr:th004

Conversation

@Whsjrczr

@Whsjrczr Whsjrczr commented Aug 15, 2024 •

Copy link
Copy Markdown
Contributor

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

@paddle-bot

paddle-bot Bot commented Aug 15, 2024

Copy link
Copy Markdown

你的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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

不用标注

# 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
Copy Markdown
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"]))


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

同上,看一下示例代码 ~

from typing import TYPE_CHECKING, Any, Literal

import numpy as np
import numpy.typing as npt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

放到 TYPE_CHECKING 里面

self.dist_strategy: DistributedStrategy | None = None

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

Copy link
Copy Markdown
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
Copy Markdown
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],

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
Copy Markdown
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]]]

用 Sequence ,Sequence[tuple[str, list[str]]]

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
Copy Markdown
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

from __future__ import annotations

import sys
from typing import Sequence

Copy link
Copy Markdown
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
Copy Markdown
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]]]

了解一下 list 与 Sequence 的关系

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

已修改

@megemini megemini left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

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