Skip to content

[Draft] Token-weighted datasets: Control up/down-sampling of multiple datasets #2794

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

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

casper-hansen
Copy link
Collaborator

Description

Preprocessing is often a hurdle that requires a lot of care especially when preprocessing before axolotl touches the datasets. Weighting datasets is a feature I consider essential to finetuning a single model for multiple tasks. To achieve this, you would like to control the token distribution which this PR allows.

This implementation is co-authored by me and Devin. I made the initial prototype of the weighting and asked Devin to do the integration work and testing.

Closes #1508

How has this been tested?

A good number of tests have been implemented to verify this implementation, but further manual testing is required to ensure the behavior is correct and as expected. Especially around when to upsample and when to downsample. One assumption that I made during the implementation is that we want the weights to sum to 1 as its otherwise hard to control exactly what happens.

Outputs from one of the tests:

Running merge_datasets with token weighting (0.7, 0.3)...

INFO:axolotl.utils.data.shared:Merging datasets with token-based weighting...
INFO:axolotl.utils.data.shared:Dataset 'dataset1.jsonl': 13 tokens (2 samples)
INFO:axolotl.utils.data.shared:Dataset 'dataset2.jsonl': 11 tokens (2 samples)
INFO:axolotl.utils.data.shared:Total original tokens across all datasets: 24
INFO:axolotl.utils.data.shared:Dataset 'dataset1.jsonl': 13 → 16 tokens (weight=0.700, upsampling)
INFO:axolotl.utils.data.shared:Dataset 'dataset2.jsonl': 11 → 7 tokens (weight=0.300, downsampling)
INFO:axolotl.utils.data.shared:Weighted dataset parts before concatenation:
INFO:axolotl.utils.data.shared:  Part 1: 13 tokens (2 samples)
INFO:axolotl.utils.data.shared:  Part 2: 5 tokens (1 samples)
INFO:axolotl.utils.data.shared:  Part 3: 7 tokens (1 samples)
INFO:axolotl.utils.data.shared:Total tokens in weighted parts: 25
INFO:axolotl.utils.data.shared:Final merged dataset: 25 tokens (4 samples)
INFO:axolotl.utils.data.shared:Token count change: 24 → 25 (1.04x)
INFO:axolotl.utils.data.shared:Final weight verification:
INFO:axolotl.utils.data.shared:  dataset1.jsonl: requested=0.700, achieved≈0.640 (16/25 tokens)
INFO:axolotl.utils.data.shared:  dataset2.jsonl: requested=0.300, achieved≈0.280 (7/25 tokens)

=== Summary ===
Final merged dataset: 4 samples
Total tokens in result: 25

My Nits

  • Validation of weighting could probably be implemented somewhere else.
  • Logging is verbose which is nice for testing and validation, but should consider how to slim this down.
  • A working example config should be provided so users can see how this works in a real example.
  • Add documentation of the new parameters.

devin-ai-integration bot and others added 14 commits June 16, 2025 06:09
- Add weight and weight_strategy fields to all dataset schemas
- Implement token-based dataset merging with validation
- Support upsample and downsample strategies
- Validate weights are 0.0-1.0 and sum to 1.0
- Maintain backward compatibility when no weights specified
- Add comprehensive tests for token weighting functionality

Co-Authored-By: Casper Hansen <[email protected]>
- Apply multi-line dictionary formatting for long DictDefault calls
- Add trailing commas to list items
- Remove trailing whitespace
- Wrap long pytest.raises calls properly
- Follow project's code style guidelines to fix pre-commit failures

Co-Authored-By: Casper Hansen <[email protected]>
- Add missing trailing commas to DictDefault list items
- Fix blank line spacing between test sections
- Wrap long pytest.raises calls properly
- Address all remaining pre-commit formatting violations

Co-Authored-By: Casper Hansen <[email protected]>
- Remove extra blank lines in shared.py
- Combine multi-line ValueError message
- Put function parameters on single line
- Reformat imports in test file to multi-line format
- Reformat Dataset.from_list calls to multi-line format
- Simplify DictDefault calls to single line format

Co-Authored-By: Casper Hansen <[email protected]>
Remove unnecessary parentheses around comparison in _validate_weights function
to resolve pre-commit pylint error C0325

Co-Authored-By: Casper Hansen <[email protected]>
- Log original token counts for each dataset
- Show before/after token counts with weight and strategy
- Display final merged dataset statistics
- Include total token count change ratio

Co-Authored-By: Casper Hansen <[email protected]>
- Show token counts for each weighted part before merging
- Display total tokens across all weighted parts
- Helps users understand the exact breakdown of weighted datasets

Co-Authored-By: Casper Hansen <[email protected]>
…ting

Implement token-weighting parameter for datasets
- Change target_tok calculation from tok_cnt * weight to weight * total_original_tokens
- Weights now correctly represent relative proportions of final merged dataset
- Fix upsampling strategy to handle both increase and decrease scenarios
- Resolves issue where weights 0.7/0.3 were incorrectly decreasing token counts

Co-Authored-By: Casper Hansen <[email protected]>
…stead of configured strategy

- Replace misleading 'strategy=upsample' with effective operation
- Now correctly shows 'downsampling' when dataset tokens are reduced
- Addresses user feedback about contradictory logging

Co-Authored-By: Casper Hansen <[email protected]>
- Shows requested vs achieved weights for each dataset
- Provides transparency into token weighting results
- Helps users verify weighting was applied correctly

Co-Authored-By: Casper Hansen <[email protected]>
Copy link

coderabbitai bot commented Jun 16, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Adding Dataset sampling weights
1 participant