Skip to content

feat: add number token loss implementation #38960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

happybear-21
Copy link

feat: Add Number Token Loss (NTL) implementation

Issue: #38950

Add Number Token Loss (NTL) to improve language model performance on numerical tasks.
NTL addresses the fundamental limitation of cross-entropy loss on numerical tokens
by incorporating ordinal information into the training objective.

Key features:

  • NTL-WAS: Uses Wasserstein-1 distance between numerical values
  • NTL-MSE: Uses Mean Squared Error between numerical values
  • Automatic numerical token detection (integers, floats, number words, ordinals)
  • Efficient implementation with minimal computational overhead
  • Caching of token-to-number mappings for performance
  • Backward compatibility with fallback to standard cross-entropy

Implementation includes:

  • Core NTL functions in src/transformers/loss/number_token_loss.py
  • Integration with LOSS_MAPPING in loss_utils.py
  • Comprehensive test suite in tests/test_number_token_loss.py
  • Working example in examples/number_token_loss_example.py

The loss is designed to augment cross-entropy for tasks involving numerical
reasoning, mathematical operations, and any scenario where token ordering
matters for numerical values.

Resolves the issue where predicting "6" vs "9" for target "5" yields
the same cross-entropy loss, despite "6" being numerically closer.

@Rocketknight1
Copy link
Member

Closing as we probably don't want this feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants