Skip to content

Conversation

zhiminzhang0830
Copy link
Contributor

@zhiminzhang0830 zhiminzhang0830 commented Aug 8, 2025

PR Category

User Experience

PR Types

New features

Description

add init functions:

  • paddle.nn.init.kaiming_uniform_
  • paddle.nn.init.xavier_uniform_
  • paddle.nn.init.uniform_
  • paddle.nn.init.kaiming_normal_
  • paddle.nn.init.xavier_normal_
  • paddle.nn.init.normal_
  • paddle.nn.init.calculate_gain
  • paddle.nn.init.constant_
  • paddle.nn.init.dirac_
  • paddle.nn.init.eye_
  • paddle.nn.init.ones_
  • paddle.nn.init.orthogonal_
  • paddle.nn.init.trunc_normal_
  • paddle.nn.init.zeros_

Pcard-75624

Copy link

paddle-bot bot commented Aug 8, 2025

你的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.

@zhiminzhang0830 zhiminzhang0830 changed the title add nn.init.kaiming_uniform_ [API compatibility] add nn.init.kaiming_uniform_ Aug 8, 2025
@codecov-commenter
Copy link

codecov-commenter commented Aug 8, 2025

Codecov Report

❌ Patch coverage is 98.38710% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@6e31ae0). Learn more about missing BASE report.

Files with missing lines Patch % Lines
python/paddle/nn/init.py 98.27% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #74478   +/-   ##
==========================================
  Coverage           ?   98.38%           
==========================================
  Files              ?        5           
  Lines              ?       62           
  Branches           ?        0           
==========================================
  Hits               ?       61           
  Misses             ?        1           
  Partials           ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -319,4 +321,5 @@
'LPPool2D',
'ZeroPad1D',
'ZeroPad3D',
'init',
Copy link
Contributor

Choose a reason for hiding this comment

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

这个导出来是作为1个api吗,好像不用导出来

Copy link
Contributor Author

Choose a reason for hiding this comment

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

应该是由于__all__的原因,不导出的话这样使用会报错:

import paddle
tensor = paddle.zeros([32, 64])
paddle.nn.init.kaiming_uniform_(tensor)
AttributeError: module 'paddle.nn' has no attribute 'init'


def _no_grad_uniform_(tensor, a, b):
with paddle.no_grad():
tensor.set_value(
Copy link
Contributor

Choose a reason for hiding this comment

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

静态图下这个能跑不,如果不能跑 可以考虑复用之前的initializer,先实例化然后call:

init = paddle.nn.initializer()
init(param)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

return (b - a) * random.random() + a

def calculate_gain(self, nonlinearity, param):
recommended_gain = {
Copy link
Contributor

Choose a reason for hiding this comment

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

这些是从torch的calculate_gain里查到的吗,也补充到paddle的calculate_gain里吧

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个函数paddle也有一个在nn.initializer.initializer下,为了能够与torch的torch.nn.init.calculate_gain的调用接口对应,也将这个函数引用到了paddle.nn.init文件中,可以paddle.nn.init.calculate_gain调用使用

a: float = 0,
mode: str = "fan_in",
nonlinearity: str = "leaky_relu",
block: paddle.pir.Block | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

这个block建议先不加,这个是老IR用的,以后如果确实需要再加。这个对用户会有一些理解成本。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的,已经去掉

@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle.nn import init as init
Copy link
Contributor

Choose a reason for hiding this comment

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

这里不需要导入init,直接paddle.nn.init会自动访问到 init.py 下面的函数吧,看torch是这样的

Copy link
Contributor Author

@zhiminzhang0830 zhiminzhang0830 Aug 11, 2025

Choose a reason for hiding this comment

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

@zhiminzhang0830 zhiminzhang0830 changed the title [API compatibility] add nn.init.kaiming_uniform_ [API compatibility] add nn.init.* functions Aug 11, 2025
from ..base.framework import in_dygraph_mode, in_pir_mode
from .initializer.constant import Constant
from .initializer.dirac import Dirac
from .initializer.initializer import calculate_gain as calculate_gain_
Copy link
Contributor

@zhwesky2010 zhwesky2010 Aug 12, 2025

Choose a reason for hiding this comment

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

这个为何需要先别名成其他的,然后又别名回来

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

)
return new_tensor
else:
new_tensor = paddle.eye(
Copy link
Contributor

Choose a reason for hiding this comment

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

这里应该要像这样把输入tensor作为out来inplace。

infoflow 2025-08-12 19-21-03

这一系列API目前都不支持老IR,没有block,可以先不管 helper.append这个分支。如果后续要支持这个分支,就需要block这个参数。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

@zhiminzhang0830
Copy link
Contributor Author

/re-run all-failed

zhwesky2010
zhwesky2010 previously approved these changes Aug 13, 2025
Copy link
Contributor

@zhwesky2010 zhwesky2010 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhwesky2010 zhwesky2010 requested a review from SigureMo August 13, 2025 12:14
SigureMo
SigureMo previously approved these changes Aug 13, 2025
@zhiminzhang0830
Copy link
Contributor Author

/re-run coverage build

@zhiminzhang0830 zhiminzhang0830 dismissed stale reviews from SigureMo and zhwesky2010 via d330635 August 14, 2025 06:06
zhwesky2010
zhwesky2010 previously approved these changes Aug 14, 2025
SigureMo
SigureMo previously approved these changes Aug 14, 2025
Copy link
Contributor

@XiaoguangHu01 XiaoguangHu01 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhwesky2010 zhwesky2010 merged commit cc7cae4 into PaddlePaddle:develop Aug 18, 2025
52 of 54 checks passed
Luckycheng222 pushed a commit to Luckycheng222/Paddle that referenced this pull request Aug 25, 2025
* add nn.init.kaiming_uniform_

* update kaiming_uniform_

* update unit test for kaiming_uniform_

* add nn.init.kaiming_uniform_

* update kaiming_uniform_

* update unit test for kaiming_uniform_

* add xavier_uniform_, kaiming_normal_, uniform_

* add unit test for xavier_uniform_, kaiming_normal_, uniform_

* add xavier_normal_ and its unit test

* add normal_ and its unit test

* fix: remove 'block' parameter from init.*() function

* fix

* add nn.init.constant_, nn.init.ones_, nn.init.zeros_

* support paddle.pir.Value type

* add dirac_, eye_, orthogonal_

* update unit test for nn.init.*

* update init

* add paddle.pir.Value

* update unit test for nn.init.orthogonal_

* fix unit test for nn.init.eye_

* fix: skip unit test on dcu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants