-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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][A-39,A-47,A-51] Add type annotations for paddle/optimizer/optimizer.py
#65076
[Typing][A-39,A-47,A-51] Add type annotations for paddle/optimizer/optimizer.py
#65076
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
python/paddle/optimizer/optimizer.py
Outdated
name=None, | ||
): | ||
learning_rate: float | LRScheduler, | ||
parameters: list[Tensor] | tuple[Tensor] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议用 Sequence[Tensor]
~ 另外,如果用 tuple
的话,应该是 tuple[Tensor, ...]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改~
python/paddle/optimizer/optimizer.py
Outdated
startup_program: Program | None = None, | ||
parameters: list[Tensor] | list[str] | None = None, | ||
no_grad_set: set[Tensor] | set[str] | None = None, | ||
) -> tuple[list[tuple[Tensor, Tensor]], list[Any]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是不是写反了?optimize_ops 在前面, params_grads 在后面?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改~,后面的Any也改成了Operator
@@ -15,7 +15,7 @@ | |||
import logging |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没用 PEP 563 么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP 563 是对于类中的注解如果需要使用未定义的类型不用转换成字符串嘛?这样好像没有使用到 PEP 563
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不止,|
也必须要 PEP 563 才能在 3.8 用
python/paddle/optimizer/optimizer.py
Outdated
Args: | ||
None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里需要确认下预览
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
python/paddle/optimizer/optimizer.py
Outdated
@@ -111,24 +122,24 @@ class Optimizer: | |||
Args: | |||
learning_rate (float|LRScheduler): The learning rate used to update ``Parameter``. | |||
It can be a float value or any subclass of ``LRScheduler`` . | |||
parameters (list|tuple, optional): List/Tuple of ``Tensor`` names to update to minimize ``loss``. \ | |||
parameters (list[Tensor]|tuple[Tensor,...]|None, optional): List/Tuple of ``Tensor`` names to update to minimize ``loss``. \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里只写 list|tuple|None
就可以了 ~ 因为 parameters
的输入其实还可以是类似 list[dict]
的形式,这里保持简洁,就不展开写了吧 ~
可以参考 https://github.com/cattidea/paddlepaddle-stubs/tree/main/paddle-stubs/optimizer
,看一下 parameters
在签名里面的写法 ~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
python/paddle/optimizer/optimizer.py
Outdated
regularization: Any | ||
helper: Any | ||
clear_gradients: Any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这几个有更细致的 type 么?
python/paddle/optimizer/optimizer.py
Outdated
name=None, | ||
): | ||
learning_rate: float | LRScheduler, | ||
parameters: Sequence[Tensor] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里支持分组的 parameter 吧?
SGD、Adam 等示例代码中用到的优化器在这个 PR 一起做了吧,不然检测不到问题
@@ -408,7 +426,7 @@ def _append_optimize_op(self, block, param_and_grad): | |||
|
|||
@imperative_base.no_grad |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个装饰器没加类型提示吧,应该会丢失掉类型信息,可以参考 #64954 加一下
这个实现和其他的不太一样,需要自己看一下
python/paddle/optimizer/adam.py
Outdated
@@ -30,6 +33,21 @@ | |||
) | |||
from .optimizer import Optimizer | |||
|
|||
if TYPE_CHECKING: | |||
from typing_extensions import NotRequired |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不用放这下面
python/paddle/optimizer/adam.py
Outdated
from .lr import LRScheduler | ||
from .optimizer import ParameterConfig | ||
|
||
class AdamParameterConfig(ParameterConfig): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class AdamParameterConfig(ParameterConfig): | |
class _AdamParameterConfig(_ParameterConfig): |
非暴露,加个下划线吧,可以不用放到 TYPE_CHECKING
下面
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我把 ParameterConfig 定义在 optimizer.py 的 TYPE_CHECKING
下面(里面用到很多type_checking里导入的类),这里不放在 TYPE_CHECKING
下面是不是会找不到 ParameterConfig 啊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂 肯定是要改一起改啊
Static Check 报错也需要看下 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ci 中的错误
2024-06-14 13:19:52 ----------------Check results--------------------
2024-06-14 13:19:52 paddle.optimizer.Adam:code-example2
2024-06-14 13:19:52 <string>:24:16: error: Argument "parameters" to "Adam" has incompatible type "List[Dict[str, Any]]"; expected "Union[Sequence[Tensor], Sequence[AdamParameterConfig], None]" [arg-type]
2024-06-14 13:19:52 Found 1 error in 1 file (checked 1 source file)
2024-06-14 13:19:52
2024-06-14 13:19:52
2024-06-14 13:19:52 paddle.optimizer.Optimizer:1
2024-06-14 13:19:52 <string>:19:16: error: Argument "parameters" to "SGD" has incompatible type "List[Dict[str, Any]]"; expected "Union[Sequence[Tensor], Sequence[ParameterConfig], None]" [arg-type]
2024-06-14 13:19:52 Found 1 error in 1 file (checked 1 source file)
应该是依赖了 python/paddle/nn/layer/layers.py
,如:
>>> import paddle
>>> linear = paddle.nn.Linear(10, 10)
>>> inp = paddle.uniform(shape=[10, 10], min=-0.1, max=0.1)
>>> out = linear(inp)
>>> loss = paddle.mean(out)
>>> adam = paddle.optimizer.Adam(learning_rate=0.1,
... parameters=linear.parameters()) # 此处需要优先标注 Layer 和 Linear
@liyongchao911 是否可以先做这个任务?
python/paddle/optimizer/optimizer.py
Outdated
startup_program: Program | None = None, | ||
parameters: list[Tensor] | list[str] | None = None, | ||
no_grad_set: set[Tensor] | set[str] | None = None, | ||
callbacks: list[Callback] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是不是可以展开:
callbacks: list[Callback[[Block, dict[str, Tensor | paddle.core.OpDesc], None]] | None = None
def append_backward
里
callbacks(list[callable object]|tuple[callable object], optional): List/Tuple of callback functions.
The callbacks are used for
doing some custom jobs during
backward part building. All
callable objects in it will
be invoked once each time a
new gradient operator is added
into the program. The callable
object must have two input
parameters: ``block`` and ``context`` .
The ``block`` is the :ref:`api_guide_Block_en` which
the new gradient operator will
be added to. The ``context`` is a
map, whose keys are gradient
Tensor names and values are
corresponding original :ref:`api_guide_tensor_en` .
In addition to this, the ``context``
has another special key-value pair:
the key is string ``__current_op_desc__``
and the value is the op_desc of the
gradient operator who has just
triggered the callable object.
Default: None.
参考
def error_clip_callback(block, context):
另外,这里只能用 list
,下面有一句判断 assert isinstance(callbacks, list)
@SigureMo 看看是否需要?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
callbacks: list[Callback[[Block, dict[str, Tensor | paddle.core.OpDesc], None]] | None = None
Callback 不是泛型,需要改成泛型才能这样写
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
另外,这里只能用 list,下面有一句判断
assert isinstance(callbacks, list)
这个位置,必须有默认值吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
callbacks: list[Callback[[Block, dict[str, Tensor | paddle.core.OpDesc], None]] | None = None
Callback 不是泛型,需要改成泛型才能这样写
改成 Callable 就可以了叭,一开始标错了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里应该是 Callable 还是 Callback 呢?
另外这里有 Block 和 OpDesc,大多是老 IR 的概念了,相关逻辑在 PIR 下是否有适配过呢?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是 Callable !
当时在跟踪这个标注的时候,还说要改为 Callable,结果还是忘了 ~ 抱歉 ~~~
另外,这里只能用 list,下面有一句判断
assert isinstance(callbacks, list)
这个位置,必须有默认值吧
if callbacks is None:
callbacks = [paddle.nn.clip.error_clip_callback]
else:
assert isinstance(callbacks, list)
代码里面这样处理的,默认是 None
,会手动创建 ~
@ooooo-create 那啥,这个 PR 里面做了好几个任务,能不能在 ISSUE #65008 里面报一下名,然后改一下这个 PR 的标题 |
paddle/optimizer/optimizer.py
paddle/optimizer/optimizer.py
还是我来吧…… |
#65190 Layer 已合,可以继续推进下了~ |
… pr/ooooo-create/65076
python/paddle/base/dygraph/base.py
Outdated
def no_grad( | ||
func: Callable[_InputT, _RetT] | None = None | ||
) -> Callable[_InputT, _RetT] | ContextManager: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注意示例代码里的两种用法,应该对应两种签名,可以使用 overload~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Paddle 代码库搜 @overload
就可以找到很多参考
python/paddle/optimizer/optimizer.py
Outdated
startup_program: Program | None = None, | ||
parameters: list[Tensor] | list[str] | None = None, | ||
no_grad_set: set[Tensor] | set[str] | None = None, | ||
callbacks: list[Callback] | None = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该是 Callable,Callback 是模型训练等调用的
所以这里是 Callable 还是 Callback 呢?是 Callable 的话,这里好像还没改?
python/paddle/optimizer/optimizer.py
Outdated
to be updated. The default value is None. | ||
callbacks (list, optional): list of callable objects to run when appending backward | ||
callbacks (list[Callback]|None, optional): list of callable objects to run when appending backward |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果是 Callable,这里是有问题的
我们不强求修改 docstring,因为按理说这里的内容是应该退场的,而且修改后会导致同时有多种奇怪的写法,用户会更加困惑
python/paddle/optimizer/optimizer.py
Outdated
paddle.framework.Block, | ||
dict[str, Tensor | paddle.core.OpDesc], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
话说 PIR 是不是根本就没用这个 callbacks?这里直接 Callable[[...], Any]
吧,尚不清楚 PIR 下形态如何,没必要在这里暴露老 IR 的概念
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
返回值确定是 None 么?那就 Callable[[...], None]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是 None ~
刚刚看 ci 里面显示 parameters 是 has incompatible type |
|
python/paddle/optimizer/adam.py
Outdated
>>> adam = paddle.optimizer.Adam( | ||
... learning_rate=0.1, | ||
... parameters=[{ | ||
... 'params': linear_1.parameters() | ||
... }, { | ||
... 'params': linear_2.parameters(), | ||
... 'weight_decay': 0.001, | ||
... 'learning_rate': 0.1, | ||
... 'beta1': 0.8 | ||
... }], | ||
... parameters=[{ # type: ignore | ||
... 'params': linear_1.parameters() # type: ignore | ||
... }, { # type: ignore | ||
... 'params': linear_2.parameters(), # type: ignore | ||
... 'weight_decay': 0.001, # type: ignore | ||
... 'learning_rate': 0.1, # type: ignore | ||
... 'beta1': 0.8 # type: ignore | ||
... }], # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个……离谱了吧……看看加在哪可以解决,这每行都加不太能接受……
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doge(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
本地跑 tools/type_checking.py
可以快速验证,如:
python tools/type_checking.py paddle.optimizer.lr.CosineAnnealingWarmRestarts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Category
User Experience
PR Types
Improvements
Description
类型标注:
-
paddle/optimizer/optimizer.py
Related links
@SigureMo @megemini