Skip to content

mean-dtype-kwarg-crash: cast operand to output dtype before reduce_mean#42

Open
arames wants to merge 1 commit into
apple:mainfrom
arames:user/arames/fuzz/mean-dtype-kwarg-crash
Open

mean-dtype-kwarg-crash: cast operand to output dtype before reduce_mean#42
arames wants to merge 1 commit into
apple:mainfrom
arames:user/arames/fuzz/mean-dtype-kwarg-crash

Conversation

@arames

@arames arames commented Jul 16, 2026

Copy link
Copy Markdown

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 the
operand to the requested dtype before averaging.

Root cause

replace_mean_default / replace_mean_dim in coreai_torch/_aten_to_core.py
ignored the dtype kwarg and called coreai.reduce_mean on the original
integer 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 existing
replace_sum_dim_intlist pattern in the same file. No change for the common
float path (!= guard makes it a no-op).

Tests

Adds integer-operand coverage for aten.mean.default and aten.mean.dim
across float32/float16 targets and static/dynamic shapes. Reverting the fix
fails all new cases.

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
arames marked this pull request as ready for review July 16, 2026 23:55
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