Skip to content

Report min_length as 0 when no sample has content - #7872

Merged
oobabooga merged 2 commits into
unslothai:mainfrom
vineethsaivs:fix/validate-dataset-min-length-inf
Aug 7, 2026
Merged

oobabooga merged 2 commits into
unslothai:mainfrom
vineethsaivs:fix/validate-dataset-min-length-inf

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

The bug

TextPreprocessor.validate_dataset hands back min_length: inf whenever no sample has content:

all rows blank: min_length=inf max_length=0 avg=0 warnings=['Found 3 empty samples']
zero rows:      min_length=inf max_length=0 avg=0 warnings=[]
one real row:   min_length=21  max_length=21 avg=21.0 warnings=[]

The dict is internally inconsistent as well as wrong: max_length and avg_length are correctly 0 for exactly the same input, and only min_length comes back as a float infinity. Anything that formats, compares or serialises the stats then has to cope with an inf that the other two fields never produce.

Root cause

min_length is seeded with float("inf") and lowered only inside the loop:

length = len(text)
text_lengths.append(length)
stats["min_length"] = min(stats["min_length"], length)

so it is lowered on exactly the iterations that also append to text_lengths. The normalisation that exists to undo the seed sits inside if text_lengths::

if text_lengths:
    stats["avg_length"] = sum(text_lengths) / len(text_lengths)
    stats["min_length"] = stats["min_length"] if stats["min_length"] != float("inf") else 0

Inside that guard min_length can never be inf, because a non-empty text_lengths means the min() ran at least once. So the branch is dead, and the one case it was written for, no sample with content, skips the line entirely.

The fix

Two lines. Hoist the normalisation out of the guard so it runs whether or not anything was measured, and move the short-sample warning onto text_lengths at the same time:

if stats["min_length"] < 10:
    stats["warnings"].append("Some samples are very short (< 10 characters)")

That guard reads min_length, which is inf today, and inf < 10 is false. Once the normalisation is hoisted it becomes 0, and 0 < 10 would newly claim "Some samples are very short" about zero measured samples. So the warning has to key off text_lengths instead, which is the thing that actually says whether anything was measured. Hoisting alone would trade one wrong output for another.

After:

all rows blank: min_length=0 max_length=0 avg=0 warnings=['Found 3 empty samples']
zero rows:      min_length=0 max_length=0 avg=0 warnings=[]
one real row:   min_length=21 max_length=21 avg=21.0 warnings=[]

and a genuinely short sample still warns:

['hi', 'a much longer sample of text'] -> min_length=2, warnings=['Some samples are very short (< 10 characters)']

Scope, stated plainly

This is a correctness fix on a diagnostics dict rather than on training data. validate_dataset is public (unsloth/dataprep/__init__.py does from .raw_text import *) so a caller can see the inf, but nothing inside the repo consumes these stats, so the blast radius is the returned value and the warning list. I would rather say that up front than dress it up.

Tests

Added test_validate_dataset_reports_zero_min_length_when_nothing_has_content to the existing tests/test_raw_text.py, following that file's convention of a plain function that asserts, prints and returns True, and wired into the __main__ block with the others. It covers both empty shapes (all-blank rows and zero rows), asserts no spurious "very short" warning, and separately asserts that a genuinely short sample still warns, so a fix that hoisted the normalisation without moving the guard would fail it.

Three runs, including a clean-tree control:

control   upstream raw_text.py + upstream tests   8 passed
before    upstream raw_text.py + this test        1 failed
after     this branch                             9 passed

ruff check with the repo's own pyproject.toml reports All checks passed! on both changed files, and the same on the unmodified ones, so that is a real comparison rather than a config that checks nothing.

The file is picked up by Repo tests (CPU) in studio-backend-ci.yml, which auto-discovers tests/ and does not ignore this path, so the new test runs in CI without a workflow change.

Prior art

None. gh issue list --search "validate_dataset min_length" --state all returns nothing; gh pr list --search "validate_dataset" --state all returns nothing; --search "raw_text" --state all returns only #3612, the PR that introduced the file. No open PR touches unsloth/dataprep/raw_text.py.

validate_dataset seeds min_length with float("inf") and only ever lowers
it inside the loop, on exactly the iterations that also append to
text_lengths. The inf->0 normalisation sat inside `if text_lengths:`, so
within that guard it could never see inf: the branch was dead, and the
case it existed for, a dataset where no sample has content, skipped the
line entirely and handed the caller min_length = inf.

  all rows blank -> min_length=inf max_length=0 avg=0
  zero rows      -> min_length=inf max_length=0 avg=0

max_length and avg_length beside it are correctly 0 for the same input,
so the dict is internally inconsistent as well as wrong.

Hoist the normalisation out of the guard, and move the short-sample
warning onto text_lengths at the same time: with min_length now 0 for an
empty dataset, `0 < 10` would newly claim "Some samples are very short"
about zero measured samples. A genuinely short sample still warns.
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 46c53f30de

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@oobabooga
oobabooga merged commit 3c5e5ca into unslothai:main Aug 7, 2026
45 of 47 checks passed
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