Repository navigation
[torchlib] Support label_smoothing in aten::cross_entropy_loss - #3069
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
label_smoothing was silently ignored, so exported models gave a different loss than PyTorch. Compute the smoothing term from the log_prob output of SoftmaxCrossEntropyLoss, following PyTorch's cross_entropy_loss_label_smoothing (weight, ignore_index 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.
aten_cross_entropy_lossacceptedlabel_smoothingbut ignored it, so models that useF.cross_entropy(..., label_smoothing=...)(ornn.CrossEntropyLoss(label_smoothing=...)) export without errors but compute a different loss:This PR follows PyTorch's
cross_entropy_loss_label_smoothing. It takes thelog_proboutput ofSoftmaxCrossEntropyLoss, sums-log_prob(weighted whenweightis given) over the class dim, zeros it wheretarget == ignore_index, and reduces it the same way as the nll term (formean, divided by the summed weights or the count of non-ignored targets). The result is(1 - eps) * nll + eps / C * smooth. Whenlabel_smoothing == 0the graph is unchanged.Testing:
nn.functional.cross_entropyOpInfo samples in PyTorch don't uselabel_smoothing, so I added anops.aten.cross_entropy_loss.label_smoothingOpInfo with index targets, all three reductions, with and withoutweight, and with anignore_indexthat hits some targets (2D and 4D inputs). Its 24 subtests fail onmainand pass with the change.pytest tests/function_libs/torch_lib/ops_test.py -k cross_entropypasses locally.torch.onnx.export(..., dynamo=True)+ onnxruntime against eager PyTorch for 72 combinations (2D/3D/4D inputs, reductions, weight, ignore_index, label_smoothing 0 and 0.2). All match within 1e-5.