Skip to content

[PyMNN:Bugfix] Keep dimensions when reducing mean - #5011

Closed
hulkbig wants to merge 1 commit into
alibaba:masterfrom
hulkbig:feature/numpy-mean-keepdims
Closed

hulkbig wants to merge 1 commit into
alibaba:masterfrom
hulkbig:feature/numpy-mean-keepdims

Conversation

@hulkbig

@hulkbig hulkbig commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Preserve reduced dimensions in MNN.numpy.mean(..., axis=None, keepdims=True).

The documented keepdims argument currently has no effect when axis is omitted: the wrapper always ravels the input and returns a scalar. For example, a float32 vector [-1.5, -1.0, -0.5] returns -1.0 rather than a variable with shape [1] and value [-1.0].

Keep the existing scalar path for axis=None, keepdims=False. For the retained-dimension path, pass the original input and an empty axis list to the existing reduction. No changes to var, std, or dtype handling. No numerical kernel changes.

Tests

Extend the existing test_statistics method with 28 mean cases covering ranks 1–4, singleton dimensions, omitted and explicit nonnegative axes, both keepdims settings, values, and input preservation. The original statistics assertions remain unchanged.

  • The exact extended method fails in four omitted-axis/keepdims cases before the fix and passes afterward.
  • All 28 focused mean cases pass after the fix; all False controls preserve their return types, shapes, and values.
  • Independent review with an index-grouping/math.fsum reference passes 168 mean cases and 504 combined mean/var/std cases. All 252 False controls and 336 var/std results retain exact return types, shapes, and values when this fix is added.
  • Applying this patch and [PyMNN:Bugfix] Preserve dimensions in variance reductions #5008 in either order produces identical files. The combined statistics method passes, and the 56 var/std cases retain identical results.

Local validation uses the source-built native Expr binding and pristine CPU libraries from 45a002ab19f9021e982ed153826586bb79e41837, with a namespace bootstrap that loads Expr/NumPy without optional package imports. The two affected source files are byte-identical at the current base f8585f650ad92c67bfb65ac8bd8e32481e266ecb. This is focused native CPU validation, not a full Python-package suite, a fresh build of the latest master, or GPU/device validation.

AI assistance was used for implementation and review.

@hulkbig
hulkbig marked this pull request as ready for review October 20, 2026 07:14
@hulkbig hulkbig closed this Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant