Skip to content

Honor the dtype argument of aten::mean.dim before reducing - #3067

Open
Raashish Aggarwal (raashish1601) wants to merge 1 commit into
microsoft:mainfrom
raashish1601:fix/mean-dim-dtype
Open

Raashish Aggarwal (raashish1601) wants to merge 1 commit into
microsoft:mainfrom
raashish1601:fix/mean-dim-dtype

Conversation

@raashish1601

Copy link
Copy Markdown

aten_mean_dim applied the dtype argument after ReduceMean, so the mean was computed in the input type and only the result was cast. #3009 fixed this for aten::mean (no dim), but aten::mean.dim still has the old order. For an integer input this truncates:

torch.mean(torch.tensor([[1, 2, 2]]), dim=1, dtype=torch.float32)  # tensor([1.6667])
# exported model returns 1.0 (ReduceMean on int64, then Cast)

and for float inputs it keeps the input precision (e.g. [1e8, 1, -1e8] in float32 with dtype=float64 gives 0 instead of 1/3).

The cast now happens before the reduction, like in aten_mean. The rank check runs before the cast, because in full-graph mode the output of Cast has no static shape.

Testing:

  • Added an ops.aten.mean.dim_dtype OpInfo (several shapes and dims, plus the precision-sensitive sample from Honor the dtype argument of aten::mean when exporting without dim #3009) and registered it for aten_mean_dim. It fails on main and passes with the change.
  • pytest tests/function_libs/torch_lib/ops_test.py -k mean passes locally (9 passed, 68 subtests), and ruff check / format are clean on the changed files.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

1 participant