Repository navigation
[torchlib] Support class probability targets in aten::cross_entropy_loss - #3071
Open
Raashish Aggarwal (raashish1601) wants to merge 1 commit into
Open
Raashish Aggarwal (raashish1601) wants to merge 1 commit into
Raashish Aggarwal (raashish1601) wants to merge 1 commit into
Conversation
SoftmaxCrossEntropyLoss only takes class indices, so exporting cross_entropy with float (probability) targets produced an invalid model. Compute it with LogSoftmax like PyTorch's cross_entropy_loss_prob_target, including weight, label_smoothing and all reductions.
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.
F.cross_entropyalso accepts class probabilities as the target (same shape as the input, float dtype), which is common for soft labels, mixup and distillation.aten_cross_entropy_lossalways lowered toSoftmaxCrossEntropyLoss, which only accepts integer class indices, so the export produced an invalid model:For a floating-point target this now follows PyTorch's
cross_entropy_loss_prob_target:-sum(log_softmax(x) * target * weight)over the class dim, withlabel_smoothingapplied to the target (target * (1 - eps) + eps / C).meandivides by the number of elements excluding the class dim, as PyTorch does for probability targets. Unbatched(C,)input is handled too. Integer targets keep the existingSoftmaxCrossEntropyLosspath, and thetargetannotation changes fromIntTypetoTensorType.Testing:
nn.functional.cross_entropy. Those 20 samples fail onmainand pass with the change.pytest tests/function_libs/torch_lib/ops_test.py -k cross_entropypasses locally (onnxruntime 1.23.0).torch.onnx.export(..., dynamo=True)+ onnxruntime against eager PyTorch for probability targets on(C,),(N, C),(N, C, d1)and(N, C, d1, d2)inputs with every reduction, with and withoutweight, and withlabel_smoothing0 and 0.2 (48 cases). All match within 1e-5.This touches the same function as #3069 (label_smoothing for index targets), but the two branches merge without conflicts and the combined tests pass.