Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix validate_dataset returning an infinite min_length for an empty dataset (#7872)
Vineeth Sai Varikuntla3c5e5ca71dd5
diff --git a/tests/test_raw_text.py b/tests/test_raw_text.pyindex 77fa1cb5389..5c0155a0a08 100644--- a/tests/test_raw_text.py+++ b/tests/test_raw_text.py@@ -584,6 +584,37 @@ def test_validate_dataset_streams_instead_of_materialising_columns():return True+def test_validate_dataset_reports_zero_min_length_when_nothing_has_content():+ """`min_length` must not come back as infinity.++ It is seeded with float("inf") and only ever lowered 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 returned min_length = inf to the caller.++ The warning guard has to move with it. With the normalisation hoisted, min_length+ becomes 0 for an empty dataset, and `0 < 10` would newly claim "some samples are+ very short" about zero measured samples.+ """++ preprocessor = TextPreprocessor()++ for label, texts in (("all blank", ["", " ", "\n"]), ("no rows", [])):+ stats = preprocessor.validate_dataset({"text": texts})+ assert stats["min_length"] == 0, (label, stats)+ assert stats["max_length"] == 0, (label, stats)+ assert not any("very short" in w for w in stats["warnings"]), (label, stats)++ # a genuinely short sample must still be reported+ stats = preprocessor.validate_dataset({"text": ["hi", "a much longer sample of text"]})+ assert stats["min_length"] == 2, stats+ assert any("very short" in w for w in stats["warnings"]), stats++ print("test_validate_dataset_reports_zero_min_length_when_nothing_has_content passed")+ return True++if __name__ == "__main__":success = test_raw_text_loader()success = test_smart_chunk_text_single_chunk_no_eos_returns_plain_list() and success@@ -593,4 +624,5 @@ if __name__ == "__main__":success = test_validate_dataset_handles_tokenized_and_text_columns() and successsuccess = test_validate_dataset_accepts_objects_without_column_names() and successsuccess = test_validate_dataset_streams_instead_of_materialising_columns() and success+ success = test_validate_dataset_reports_zero_min_length_when_nothing_has_content() and successsys.exit(0 if success else 1)diff --git a/unsloth/dataprep/raw_text.py b/unsloth/dataprep/raw_text.pyindex fa2c8b56712..ccd4eb856a4 100644--- a/unsloth/dataprep/raw_text.py+++ b/unsloth/dataprep/raw_text.py@@ -417,7 +417,11 @@ class TextPreprocessor:# Calculate average lengthif 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++ # No sample had content, so min_length is still its float("inf") seed. Report 0,+ # like max_length and avg_length beside it, rather than handing back an infinity.+ if stats["min_length"] == float("inf"):+ stats["min_length"] = 0# Generate warningsif stats["empty_samples"] > 0:@@ -429,7 +433,9 @@ class TextPreprocessor:if stats["encoding_issues"] > 0:stats["warnings"].append(f"Found {stats['encoding_issues']} encoding issues")- if stats["min_length"] < 10:+ # Guard on text_lengths, not on min_length: with nothing measured it is now 0,+ # and zero measured samples is not "some samples are very short".+ if text_lengths and stats["min_length"] < 10:stats["warnings"].append("Some samples are very short (< 10 characters)")return stats