Skip to content

Honor the dtype argument of torch sum, mean and prod - #2927

Open
devteamaegis wants to merge 1 commit into
apple:mainfrom
devteamaegis:fix/torch-reduction-dtype
Open

devteamaegis wants to merge 1 commit into
apple:mainfrom
devteamaegis:fix/torch-reduction-dtype

Conversation

@devteamaegis

Copy link
Copy Markdown

Summary

The PyTorch sum / mean / prod converter parses the dtype argument but never applies it, so the reduction runs in the input dtype. torch casts the input to dtype first, so the converted model silently returns different values:

class M(torch.nn.Module):
    def forward(self, x):
        return torch.mean(x, dim=1, dtype=torch.float32)

x = torch.tensor([[-7, -3, 5, 7, 0, 3]], dtype=torch.int32)
# torch:   [0.8333]
# Core ML: [0]   (int32 reduce_mean, truncated)

The same happens for torch.sum(x_float, dim=1, dtype=torch.int32) / torch.prod(...) (torch truncates each element to int before reducing, Core ML returns the float sum), with both TorchScript and torch.export. On the neuralnetwork backend the int mean case already worked, because inputs are fp32 there; the float-to-int cases were still wrong.

The fix reads dtype (positional inputs[3] for the .dim overloads under TorchScript and torch.export, inputs[1] / the dtype kwarg for torch.export's no-dim overloads) and casts x before reducing, the same way the cumsum converter already does. all / any / logsumexp take no dtype and are unchanged.

Not covered here: under TorchScript, torch.sum(x, dtype=...) / torch.mean(x, dtype=...) with no dim trace to a 2-input node whose position 1 is read as dim. That arity issue is what #2809 fixes; this PR doesn't touch that branch.

Testing

On macOS (Apple Silicon), Python 3.12, torch 2.8.0, with Core ML predictions:

  • New TestSum::test_reduction_dtype (sum / mean / prod x int32/fp32 input x int32/fp32 dtype; it calls ct.convert and predict through run_compare_torch). Default backends: 40 passed, 8 skipped (torch.mean with an int dtype is not valid torch). With INCLUDE_MIL_FP32_UNIT_TESTS=1: 60 passed, 12 skipped.
  • On main without the ops.py change, 10 of those default-backend cases fail (the int-input mean cases and the float-input sum / prod with dtype=torch.int32 cases).
  • TestSum: 161 passed, 8 skipped. A wider test_torch_ops.py subset (TestSum, TestReduction, TestNorms, TestNormalize, TestLinAlg*, TestVarStd, TestLayerNorm, TestGroupNorm, TestInstanceNorm, TestBatchNorm, TestCumSum, TestMaskedFill, TestScaledDotProductAttention): about 4700 passed, 757 skipped, 1024 xfailed. The only failures were 2 TestScaledDotProductAttention::test_enable_gqa_dynamic_shapes cases where the worker segfaulted; that test segfaults the same way on unmodified main in my environment (I ran the source tree against the native libraries from the 9.0 wheel), so it looks unrelated to this change.

This was investigated and written with an AI coding agent (Claude) working on my behalf.

torch casts the input to dtype before reducing, but the converter parsed
dtype and dropped it, so e.g. torch.mean(int_x, dim, dtype=torch.float32)
was computed as an int32 reduce_mean and truncated.

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

None yet

Development

Successfully merging this pull request may close these issues.

1 participant