Report cached token retention statistics when filtering datasets by max_length

Open
#10,704 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

This issue has not been assessed yet.

Description

enhancement
Checklist / 检查清单
  • I have searched existing issues, and this is a new feature request. / 我已经搜索过现有的 issues,确认这是一个新的 Feature Request。
Feature Request Description / Feature Request 描述

Background

When using cached datasets for large-scale SFT, ms-swift filters samples exceeding max_length under the delete truncation strategy.

Currently, _select_dataset() in swift/pipelines/utils.py reports the number of samples before and after filtering, but does not report how many cached tokens are retained.

For long-context SFT and multi-turn Agent training, a relatively small proportion of removed samples may account for a substantial proportion of the total tokens.

Example

Consider a synthetic cached dataset with the following sequence lengths:

Sample | Tokens | Retained -- | -- | -- Sample 1 | 100 | Yes Sample 2 | 100 | Yes Sample 3 | 1000 | No

With max_length=100:

  • Sample retention rate: 66.67%

  • Cached token retention rate: 16.67%

Reporting only sample counts may therefore underestimate the amount of discarded training data.

Proposed Solution

Add cached token retention statistics to the existing dataset filtering logs, for example:

Dataset filtered, origin length: 3, filtered dataset length: 2
Cached tokens retained: 200/1200 (16.67%)

The statistics can be calculated using the existing cached sequence lengths and filtering mask, without loading a model or performing additional tokenization.

For list-valued sequence lengths, sum the lengths within each row for token statistics while preserving the existing filtering criterion.

This feature does not change dataset filtering behavior or training semantics. The reported counts represent cached sequence lengths rather than loss-bearing training tokens.

Expected Benefits

  • Improve transparency in cached dataset filtering.

  • Help users select appropriate context lengths.

  • Identify excessive token loss in long-context and multi-turn Agent training.

Pull Request / Pull Request 信息

I have prepared an initial implementation and regression tests in the following branch:

https://github.com/hamster-oom/ms-swift/tree/feat/cached-token-retention

I plan to submit a pull request after reviewing the implementation, completing the relevant tests, and receiving feedback on the proposed feature.

The implementation preserves the existing filtering behavior and introduces no additional dependencies.

Dominant language
Python
Stars
16.2k
Forks
1.8k
Avg merge
1d 14h
Merged PRs (30d)
68

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelscope/ms-swift

All issues in modelscope/ms-swift

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.