[API Compatibility] Improve compat.nn.Linear init and param reset
#76196
+23
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Category
Operator Mechanism
PR Types
Improvements
Description
优化了
paddle.compat.nn.Linear#76169中引入的
compat.nn.Linear的初始化和 reset 函数。reset 函数中,nn.Initializer.Uniform与nn.Initializer.KaimingUniform的行为都被修改:nn.initializer.XXX被直接实例化然后用 forward 形式初始化参数时,原始的行为如下代码块所示:可以看到,place 从
cpu变成了gpu:0。这是因为 initializer 在动态图执行时,传入的 place 是_current_expected_place(),而不是输入 tensor 的 place。而如果强行将所有 initializer 对应位置修改为var.place也会引起问题:如果 initializer 实例传入 ParamAttr 中,ParamAttr 辅助创建 parameters 时,tensor 的 place 是 undefined:0,此时必须使用_current_expected_place()。本 PR 只修改了与
compat.nn.Linear有关的 initializer(kaiming_uniform 以及 uniform),其他 initializer 行为未变(个人觉得原始行为是不合理的,很显然,如果确实不合理,对应的initializer都需要修改,则需要一个新的PR完成对所有的 initializer 行为的‘修正’)。基于此,修正了compat.nn.Linear的初始化行为:不再手动进行 place 一致性检查以及 to 操作。Pcard-89620