Skip to content

Conversation

Qin-sx
Copy link
Contributor

@Qin-sx Qin-sx commented Jun 15, 2025

PR Category

User Experience

PR Types

Improvements

Description

当前问题
paddle.nn.initializer.KaimingNormal目前只fan_in模式,不支持fan_out模式,fan_out需要自己计算out_feature值后填入fan_in参数

解决方案
paddle.nn.initializer.KaimingNormal中增加一个mode参数:

  • mode参数可以设置为'fan_in'或者'fan_out'
  • 在mode='fan_in'时,fan_in参数才会生效
  • 在mode='fan_out'时,会自动计算out feature值用于计算

	modified:   python/paddle/nn/initializer/kaiming.py
	modified:   test/legacy_test/test_initializer.py
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (develop@917ecbd). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #73347   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?         9           
  Branches           ?         0           
===========================================
  Hits               ?         9           
  Misses             ?         0           
  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.

@Qin-sx
Copy link
Contributor Author

Qin-sx commented Jun 16, 2025

docs: PaddlePaddle/docs#7324
PaConver: PaddlePaddle/PaConvert#594

)
if self._mode == 'fan_out' and self._fan_in is not None:
raise ValueError(
"The mode of MSRAInitializer is 'fan_out', "
Copy link
Contributor

Choose a reason for hiding this comment

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

KaimingNormal/KaimingUniform

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯,收到,已修改

Qin-sx added 2 commits June 16, 2025 22:31
	modified:   python/paddle/nn/initializer/kaiming.py
	modified:   python/paddle/nn/initializer/kaiming.py
	modified:   test/legacy_test/test_initializer.py
@Qin-sx Qin-sx changed the title 在KaimingNormal中加入fan_out参数 在KaimingNormal/Uniform中加入fan_out参数 Jun 17, 2025
@zhwesky2010 zhwesky2010 changed the title 在KaimingNormal/Uniform中加入fan_out参数 在KaimingNormal/Uniform中增加mode参数 Jun 17, 2025
@zhwesky2010
Copy link
Contributor

@Qin-sx CI可能随机挂了,注意rerun下

@Qin-sx
Copy link
Contributor Author

Qin-sx commented Jun 17, 2025

@Qin-sx CI可能随机挂了,注意rerun下

嗯,收到,已重跑

@@ -62,6 +62,7 @@ class MSRAInitializer(Initializer):
seed (int32, optional): random seed. Default is 0.
negative_slope (float, optional): negative_slope (only used with leaky_relu). Default is 0.0.
nonlinearity(str, optional): the non-linear function. Default is relu.
mode(str, optional): the mode of initialization, can be 'fan_in' or 'fan_out'. When set to 'fan_in', the fan_in parameter is used for initialization. When set to 'fan_out', the out_features of trainable Tensor will be used. Default is 'fan_in'.
Copy link
Contributor

Choose a reason for hiding this comment

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

这个逻辑是不是得这么改一下,如果设置了 fan_in,则fan_in生效,mode不生效。如果没设置 fan_in,则mode生效。

fan_in与fan_out不可同时设置,给出明确的报错提示。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

嗯,我想的逻辑是以mode参数为判断标准,mode='fan_in'时,fan_in参数生效。mode='fan_out'时,fan_in参数不生效。感觉以fan_in参数为判断标准,逻辑不太好解释。

        if self._mode == 'fan_in':
            fan_in = f_in if self._fan_in is None else self._fan_in
        if self._mode == 'fan_out':
            fan_in = f_out

以mode参数为判断标准的报错逻辑在这里

        if self._mode not in ['fan_in', 'fan_out']:
            raise ValueError(
                "The mode of KaimingNormal/KaimingUniform should be 'fan_in' or 'fan_out', "
                f"but received {self._mode}."
            )
        if self._mode == 'fan_out' and self._fan_in is not None:
            raise ValueError(
                "The mode of KaimingNormal/KaimingUniform is 'fan_out', "
                "but fan_in is set. Please set fan_in to None."
            )

Copy link
Contributor

@zhwesky2010 zhwesky2010 Jun 20, 2025

Choose a reason for hiding this comment

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

OK,这样更合理

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 merged commit 4b11dfd into PaddlePaddle:develop Jun 20, 2025
51 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants