[API Compatiblity] add mean, Tensor.mean by LLSGYN · Pull Request #74955 · PaddlePaddle/Paddle

@LLSGYN

PR Category

User Experience

PR Types

New features

Description

paddle.mean, paddle.Tensor.mean支持dtype和out参数。未下沉至c++实现。

@paddle-bot

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

zhwesky2010

@@ -108,9 +116,18 @@ def mean(
>>> print(out4.numpy())
[ 8.5 12.5 16.5]
"""

Choose a reason for hiding this comment

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

只用处理 dtype != None的情况,减少非必要的判断

@codecov-commenter

Codecov Report

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

Files with missing lines Patch % Lines
python/paddle/tensor/stat.py 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #74955   +/-   ##
==========================================
  Coverage           ?   92.85%           
==========================================
  Files              ?        2           
  Lines              ?       14           
  Branches           ?        0           
==========================================
  Hits               ?       13           
  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.

zhwesky2010

if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)):
dtype = convert_np_dtype_to_dtype_(dtype)
if x.dtype != dtype:
x = cast_(x, dtype)

Choose a reason for hiding this comment

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

这里非inplace的API就用cast,inplace的API用cast_,之前修改的同学有些也改错了。一起更正下吧。

# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Choose a reason for hiding this comment

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

这些单测case直接加到原来的test_mean_op里去吧

np.testing.assert_allclose(result.numpy(), expected, rtol=1e-05)

@skip_if_xpu_or_onednn_and_not_float32('float64')
def test_all_parameters_combination(self):

Choose a reason for hiding this comment

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

反向也跑下吧

zhwesky2010

Choose a reason for hiding this comment

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

LGTM

SigureMo

XiaoguangHu01

Choose a reason for hiding this comment

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

LGTM

zhwesky2010

Choose a reason for hiding this comment

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

LGTM

@LLSGYN LLSGYN deleted the feat/op_mean_naive branch

August 30, 2025 04:14

This was referenced

Sep 1, 2025