mean-dtype-kwarg-crash: cast operand to output dtype before reduce_mean#42
Open
arames wants to merge 1 commit into
Open
mean-dtype-kwarg-crash: cast operand to output dtype before reduce_mean#42arames wants to merge 1 commit into
arames wants to merge 1 commit into
Conversation
torch.mean(x, dtype=...) on an integer tensor crashed during conversion (ValueError: mean[0]: dtype si32 vs f32): replace_mean_default and replace_mean_dim never honored the dtype kwarg, so reduce_mean ran on the int operand and produced a result whose type mismatched the node's declared float output. Cast the operand to the node's output element type before reducing, mirroring replace_sum_dim_intlist. Adds integer-operand coverage for aten.mean.default and aten.mean.dim across float32/float16 targets.
arames
marked this pull request as ready for review
July 16, 2026 23:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
torch.mean(x, dtype=...)on an integer tensor crashes during conversion:ValueError: mean[0]: dtype si32 vs f32. torch's contract is to promote theoperand to the requested dtype before averaging.
Root cause
replace_mean_default/replace_mean_dimincoreai_torch/_aten_to_core.pyignored the
dtypekwarg and calledcoreai.reduce_meanon the originalinteger operand. The result came back integer while the node's declared output
type is float, so the converter's own result-type check raised.
Fix
Cast the operand to the node's output element type before reducing (guarded by
x.type.element_type != target_type), mirroring the existingreplace_sum_dim_intlistpattern in the same file. No change for the commonfloat path (
!=guard makes it a no-op).Tests
Adds integer-operand coverage for
aten.mean.defaultandaten.mean.dimacross float32/float16 targets and static/dynamic shapes. Reverting the fix
fails all new cases.