Repository navigation
[torchlib] Cast to dtype before Softmax/LogSoftmax in softmax and log_softmax - #3196
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Qiming Bao (14H034160212) wants to merge 1 commit into
Conversation
…_softmax PyTorch casts the input to dtype before the operation. The graph cast the result instead, so an integer input gave an invalid LogSoftmax (or wrong Softmax output) and a float16 input was computed in float16.
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.
torch.softmax(x, dim, dtype=...)andtorch.log_softmax(x, dim, dtype=...)cast the input todtypebefore the operation.aten_softmaxandaten_special_log_softmax(whichaten::log_softmax.intmaps to) cast the result instead, so the ONNX graph wasSoftmax(x) -> Cast(dtype):torch.softmax(torch.tensor([[1, 2, 3]]), -1, dtype=torch.float32)): ONNXSoftmax/LogSoftmaxtake floats only.log_softmaxexports an invalid model (Type 'tensor(int64)' of input parameter ... of operator (LogSoftmax) ... is invalid), andsoftmaxreturned[0, 0, 0]instead of[0.090, 0.245, 0.665].dtype=torch.float32, as Hugging Face models use for attention probabilities (softmax(attn_weights, dim=-1, dtype=torch.float32)): the softmax is computed in float16 and only the result is cast. ONNX Runtime's CPU provider hides this because it has no float16Softmaxand upcasts on its own, but providers with a float16 kernel compute it in float16. Withoptimize=Falsethe exported graph isSoftmax(float16) -> Cast.This PR casts
selftodtypebeforeSoftmax/LogSoftmax, and widens the input type constraint fromTFloattoTReal, so integer inputs are accepted.Tests
test_softmax_dtype_casts_the_input_first(softmax and log_softmax) ine2e_ops_tests.py, exported withoptimize=False. It checks that an int64 input withdtype=torch.float32matches PyTorch, and that for a float16 input theSoftmax/LogSoftmaxnode is fed by aCast(to=FLOAT). Both cases fail onmainand pass with this change.main.e2e_ops_tests.pyhas the same 5 failures as onmainin my environment (onnx_ir/onnx versions), plus the 2 new passing tests.ruff checkandruff formatare clean.