Repository navigation
[Typing][C-95, C-96, C-99]Add annotations for python/paddle/incubate/optimizer/{lookahead, modelaverage, lbfgs}.py - #67448
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
megemini
left a comment
There was a problem hiding this comment.
这几个类中实例的参数都没写,如:
class A:
a: int
def __init__(self)->None:
self.a = 1
self._b = 'b'上面 A 中的 a ~ 只需要添加可公开属性即可,如 self._b 不需要标注 ~
另外,PR 标题中的标号写错了,应该是 C-95
|
|
||
| from collections import defaultdict | ||
| from functools import reduce | ||
| from typing import TYPE_CHECKING, Callable |
There was a problem hiding this comment.
从 collections.abc 导入 Callable
| tolerance_change: float = 1e-9, | ||
| history_size: int = 100, | ||
| line_search_fn: str | None = None, | ||
| parameters: list[Tensor] | tuple[Tensor] | None = None, |
There was a problem hiding this comment.
| parameters: list[Tensor] | tuple[Tensor] | None = None, | |
| parameters: Sequence[Tensor] | Sequence[_ParameterConfig] | None = None, |
参考 Optimizer 的初始化参数
| return loss, flat_grad | ||
|
|
||
| def step(self, closure): | ||
| def step(self, closure: Callable) -> Tensor: |
There was a problem hiding this comment.
| def step(self, closure: Callable) -> Tensor: | |
| def step(self, closure: Callable[[], _T]) -> _T: |
_T = TypeVar('_T', covariant=True)
| name=None, | ||
| ): | ||
| average_window_rate: float, | ||
| parameters: list[Tensor] | None = None, |
| startup_program: Program | None = None, | ||
| parameters: list[Tensor] | list[str] | None = None, | ||
| no_grad_set: set[Tensor] | set[str] | None = None, | ||
| ) -> tuple[list[Operator], list[tuple[Tensor, Tensor]]]: |
There was a problem hiding this comment.
| ) -> tuple[list[Operator], list[tuple[Tensor, Tensor]]]: | |
| ) -> None: |
应该是方法没写完整
| def apply(self, executor=None, need_restore=True): | ||
| def apply( | ||
| self, executor: Executor | None = None, need_restore: bool = True | ||
| ): |
There was a problem hiding this comment.
| ): | |
| ) -> Generator[None, None, None]: |
python/paddle/incubate/optimizer/{lookahead, modelaverage, lbfgs].pypython/paddle/incubate/optimizer/{lookahead, modelaverage, lbfgs}.py
| tolerance_change: float | ||
| history_size: int | ||
| line_search_fn: str | None | ||
| state: dict[_KT, _VT] |
There was a problem hiding this comment.
这个 _KT、_VT 的意义是什么,单独使用一个泛型是没有任何约束的,和 dict[Any, Any] 没有区别
| tolerance_grad: float | ||
| tolerance_change: float | ||
| history_size: int | ||
| line_search_fn: str | None |
There was a problem hiding this comment.
| line_search_fn: str | None | |
| line_search_fn: Literal['strong_wolfe'] | None |
下同
| weight_decay: NotRequired[float | WeightDecayRegularizer | None] | ||
| learning_rate: NotRequired[float | Tensor | LRScheduler | None] | ||
|
|
||
| _T = TypeVar('_T', covariant=True) |
There was a problem hiding this comment.
| _T = TypeVar('_T', covariant=True) | |
| _T_co = TypeVar('_T_co', covariant=True) |
协变、逆变应该从名字上直接体现
| from paddle.base.dygraph import base as imperative_base | ||
| from paddle.base.framework import Variable | ||
| from paddle.base.layer_helper import LayerHelper | ||
| from paddle.base.layer_helper import LayerHelper, LayerHelperBase |
There was a problem hiding this comment.
非运行时相关 import
放到 if TYPE_CHECKING: 下
|
|
||
| if TYPE_CHECKING: | ||
| from paddle import Tensor | ||
| from paddle.base.framework import Operator, Program |
There was a problem hiding this comment.
Program 从 paddle.static 下 import
| from paddle.regularizer import WeightDecayRegularizer | ||
| from paddle.static import Executor | ||
|
|
||
| class _ParameterConfig(TypedDict): |
There was a problem hiding this comment.
这几个 _ParameterConfig,有什么理由不复用 python/paddle/optimizer/optimizer.py 中的 _ParameterConfig?
| tolerance_change: float | ||
| history_size: int | ||
| line_search_fn: Literal['strong_wolfe'] | None | ||
| state: defaultdict |
There was a problem hiding this comment.
| state: defaultdict | |
| state: dict[str, dict[str, Any]] |
| self._numel_cache = None | ||
|
|
||
| def state_dict(self): | ||
| def state_dict(self) -> dict[str, Tensor]: |
There was a problem hiding this comment.
| def state_dict(self) -> dict[str, Tensor]: | |
| def state_dict(self) -> dict[str, dict[str, Any]]: |
| alpha: float | ||
| k: int | ||
| type: str | ||
| helper: LayerHelperBase | None |
There was a problem hiding this comment.
| helper: LayerHelperBase | None | |
| helper: LayerHelper |
没必要用 LayerHelperBase 吧?
| from paddle.base import framework | ||
| from paddle.base.dygraph import base as imperative_base | ||
| from paddle.base.layer_helper import LayerHelper | ||
| from paddle.base.layer_helper import LayerHelper, LayerHelperBase |
There was a problem hiding this comment.
| from paddle.base.layer_helper import LayerHelper, LayerHelperBase | |
| from paddle.base.layer_helper import LayerHelper |
貌似不需要 LayerHelperBase 吧
| from collections.abc import Generator, Sequence | ||
|
|
||
| from paddle import Tensor | ||
| from paddle.base.framework import Program |
There was a problem hiding this comment.
| from paddle.base.framework import Program | |
| from paddle.static import Program |
|
|
||
| """ | ||
|
|
||
| helper: LayerHelperBase | None |
There was a problem hiding this comment.
| helper: LayerHelperBase | None | |
| helper: LayerHelper |
没有必要用 LayerHelperBase 吧?
PR Category
User Experience
PR Types
Improvements
Description
类型标注:
python/paddle/incubate/optimizer/{lookahead, modelaverage, bfgs}.pyRelated links
@SigureMo @megemini