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-34][BUAA] Add type annotations for python/paddle/distributed/fleet/fleet.py #67624

Merged
merged 11 commits into from
Aug 25, 2024

Conversation

Luohongzhige
Copy link
Contributor

PR Category

User Experience

PR Types

Improvements

Description

#65008
C-34

Copy link

paddle-bot bot commented Aug 21, 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.

@paddle-bot paddle-bot bot added the contributor External developers label Aug 21, 2024
@luotao1 luotao1 added the HappyOpenSource Pro 进阶版快乐开源活动,更具挑战性的任务 label Aug 22, 2024
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.

没有逐个 review, 几个问题:

  • 类型的标注尽量简洁,如 paddle.Tensor 改为 Tensor ,相应的导入语句放到 TYPE_CHECKING 里面
  • 泛型的参数化,如 list 需要写为 list[xxx],以及 set dict tuple
  • 不是 callable ,是 Callable ,从 collections.abc 导入
  • 注意 paddle._typing 中通用类型的使用,如 place: paddle.CUDAPlace, 是否可以使用 paddle._typing 中的类型
  • Literal 的使用,如 def all_reduce(self, input, mode="sum") 中的 mode
  • 类中实例参数的标注,如 Fleetstrategy_compiler, user_defined_optimizer
  • 返回的 Self 的使用
  • 私有函数/方法不需要标注

def all_reduce(
self,
input: Tensor,
mode: str = mode_Literal,
Copy link
Contributor

Choose a reason for hiding this comment

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

Literal 的用法看一下其他 PR 是如何实现的 ~

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.

为什么要删掉默认值???

def apply_ir_passes(
main_program: Program,
startup_program: Program,
config: BuildStrategy,
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
config: BuildStrategy,
config: Fleet,

self._role_maker = None
self.strategy_compiler = None
self.strategy_compiler: StrategyCompiler = None
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.strategy_compiler: StrategyCompiler = None
self.strategy_compiler: StrategyCompiler | None = None

is_collective: bool = False,
strategy: DistributedStrategy | None = None,
log_level: int | str = "INFO",
) -> Fleet:
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
) -> Fleet:
) -> Self:

):
iteration: int,
x: Tensor,
group: HybridCommunicateGroup,
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
group: HybridCommunicateGroup,
group: Group,

self,
iteration: int,
x: Tensor,
group: HybridCommunicateGroup,
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
group: HybridCommunicateGroup,
group: Group,

@@ -500,7 +546,7 @@ def reduce_scatter_perf(
f"[Perf Warning] ReduceScatter Test Timeout! {ret} > {perf_threshold_time}"
)

def _collective_perf_impl(self, round=50, context={}, hcg=None):
def _collective_perf_impl(self, round, context, hcg=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

默认值呢?

self,
optimizer: Optimizer,
strategy: DistributedStrategy | None = None,
) -> Fleet:
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
) -> Fleet:
) -> Self:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Self在python3.11才引入,此处使用TypeVar实现

Copy link
Contributor

Choose a reason for hiding this comment

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

嗯 好问题 ~ 但是,为什么项目中都在用 Self 呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

抱歉,资料没有找全,python3.11之前Selftyping_extensions引入

@@ -1319,10 +1394,14 @@ def set_date(self, table_id, day_id):

@is_non_distributed_check
@inited_runtime_handler
def shrink(self, threshold=None):
def shrink(self, threshold: int) -> None:
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
def shrink(self, threshold: int) -> None:
def shrink(self, threshold: int | None = None) -> None:

Comment on lines 1679 to 1683
self,
loss,
startup_program,
parameter_list,
no_grad_set,
Copy link
Contributor

Choose a reason for hiding this comment

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

默认值呢?

Comment on lines 1983 to 1985
startup_programs,
parameter_list,
no_grad_set,
Copy link
Contributor

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.

Any,
Iterable,
Literal,
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 导入 Sequence Iterable

Comment on lines 32 to 34
from paddle import (
Tensor,
)
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
from paddle import (
Tensor,
)
from paddle import Tensor

def all_reduce(self, input, mode="sum"):
def all_reduce(
self,
input: int,
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: int,
input: Any,

应该不只是 int

Operator,
Parameter,
Program,
Scope,
Copy link
Contributor

Choose a reason for hiding this comment

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

不是这个 Scope ,是 from paddle.base.core import _Scope

TypedDict,
)

if TYPE_CHECKING:
Copy link
Contributor

Choose a reason for hiding this comment

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

if TYPE_CHECKING 这部分放到 80 行 __all__ 上面吧(注意前后留一个空行~),其他应该没啥问题了 ~ 辛苦 🤟

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 ~

@SigureMo SigureMo merged commit cdf544d into PaddlePaddle:develop Aug 25, 2024
28 of 29 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