Repository navigation
Honor the dtype argument of torch sum, mean and prod - #2927
Open
devteamaegis wants to merge 1 commit into
Open
devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
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
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.
Summary
The PyTorch
sum/mean/prodconverter parses thedtypeargument but never applies it, so the reduction runs in the input dtype. torch casts the input todtypefirst, so the converted model silently returns different values: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 theneuralnetworkbackend the int mean case already worked, because inputs are fp32 there; the float-to-int cases were still wrong.The fix reads
dtype(positionalinputs[3]for the.dimoverloads under TorchScript and torch.export,inputs[1]/ thedtypekwarg for torch.export's no-dim overloads) and castsxbefore reducing, the same way thecumsumconverter already does.all/any/logsumexptake nodtypeand are unchanged.Not covered here: under TorchScript,
torch.sum(x, dtype=...)/torch.mean(x, dtype=...)with nodimtrace to a 2-input node whose position 1 is read asdim. 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:
TestSum::test_reduction_dtype(sum/mean/prodx int32/fp32 input x int32/fp32dtype; it callsct.convertandpredictthroughrun_compare_torch). Default backends: 40 passed, 8 skipped (torch.meanwith an int dtype is not valid torch). WithINCLUDE_MIL_FP32_UNIT_TESTS=1: 60 passed, 12 skipped.mainwithout theops.pychange, 10 of those default-backend cases fail (the int-inputmeancases and the float-inputsum/prodwithdtype=torch.int32cases).TestSum: 161 passed, 8 skipped. A widertest_torch_ops.pysubset (TestSum, TestReduction, TestNorms, TestNormalize, TestLinAlg*, TestVarStd, TestLayerNorm, TestGroupNorm, TestInstanceNorm, TestBatchNorm, TestCumSum, TestMaskedFill, TestScaledDotProductAttention): about 4700 passed, 757 skipped, 1024 xfailed. The only failures were 2TestScaledDotProductAttention::test_enable_gqa_dynamic_shapescases where the worker segfaulted; that test segfaults the same way on unmodifiedmainin 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.