fix: set cross_entropy ignore_index to PAD token index (#305)#443
Draft
ljluestc wants to merge 1 commit into
Draft
fix: set cross_entropy ignore_index to PAD token index (#305)#443ljluestc wants to merge 1 commit into
ljluestc wants to merge 1 commit into
Conversation
…#305) The AutoregressiveWrapper defaults ignore_index to -100, but the [PAD] token has index 0. This caused padding tokens to incorrectly contribute to the cross-entropy loss and gradients during training. Pass ignore_index=args.pad_token alongside pad_value so that PAD positions are properly excluded from the loss computation.
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.
fix: align cross_entropy
ignore_indexwith PAD token id (0)Fixes #305
Problem
AutoregressiveWrapperfromx-transformerscomputes the training loss viaF.cross_entropy(..., ignore_index=self.ignore_index). Theignore_indexparameter tells PyTorch which target value to skip during loss and gradient computation.In
pix2tex/models/transformer.py, theget_decoder()function constructsCustomARWrapperwithpad_value=args.pad_token(which is0), but does not passignore_index. This causesignore_indexto default to-100(PyTorch's default), which means:[PAD]token (index0) is incorrectly included in the cross-entropy loss.Root Cause
In
pix2tex/models/transformer.py(lines 55–58), the original code was:pad_value=0correctly pads input sequences, but the loss function doesn't know to ignore index0— it only ignores-100, which never appears in the vocabulary.Fix
Pass
ignore_index=args.pad_tokenalongsidepad_valueso thatF.cross_entropyskips positions where the target is the PAD token:Files Changed
pix2tex/models/transformer.py— Addedignore_index=args.pad_tokento theCustomARWrapperconstructor call (1 line).tests/test_ignore_index.py— New test file with 3 tests covering the fix (83 lines).How to Test
Prerequisites
Run the unit tests
Expected output
What each test verifies
test_ignore_index_matches_pad_token— Constructs a decoder withpad_token=0and assertsdecoder.ignore_index == 0(not-100).test_ignore_index_with_custom_pad_token— Repeats the check for multiple pad token values (0,3,5) to ensureignore_indexalways followspad_token.test_pad_tokens_ignored_in_loss— Builds two sequences that are identical (same real tokens, same padding), runs them through the decoder, and asserts the losses are equal — confirming padding doesn't affect the loss.Manual verification (optional)
Risk Assessment
x_transformers==0.15.0(insetup.py) — theAutoregressiveWrapperAPI has not changed, so the fix is compatible.References
CrossEntropyLossdocs —ignore_indexparameter