Skip to content

Fix prod dtype promotion when reducing a size-1 axis#3898

Merged
angeloskath merged 1 commit into
ml-explore:mainfrom
eyupcanakman:prod-noop-dtype
Jul 23, 2026
Merged

Fix prod dtype promotion when reducing a size-1 axis#3898
angeloskath merged 1 commit into
ml-explore:mainfrom
eyupcanakman:prod-noop-dtype

Conversation

@eyupcanakman

Copy link
Copy Markdown
Contributor

Proposed changes

prod computes its promoted output type but discards it on the no-op branch, so the result dtype depends on the length of the reduced axis. Reducing a size-1 axis returns the input dtype while a size-2 axis promotes:

mx.prod(mx.array([2], dtype=mx.int8), axis=0).dtype     # int8   (before)
mx.prod(mx.array([2, 2], dtype=mx.int8), axis=0).dtype  # int32

sum casts on the same branch (is_noop ? astype(a, out_type, s) : ...), but prod returned bare a. The fix applies the same astype, so prod matches sum and the other promoting reductions. The promotion is there to keep the product from overflowing the narrow input type, so the size-1 case was also silently keeping a narrow accumulator.

Added a test that the output dtype does not depend on the reduced axis length.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@angeloskath
angeloskath merged commit 9b40c9d into ml-explore:main Jul 23, 2026
28 checks passed
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.

2 participants