Repository navigation
fix(dataprep): guard smart_chunk_text against stride >= chunk_size - #7126
Conversation
RawTextDataLoader.smart_chunk_text takes chunk_size and stride as its own arguments, so a direct call with stride >= chunk_size bypasses the constructor validation. In that case `start_idx += chunk_size - stride` is non-positive, so start_idx never advances past the first window and the chunking loop never terminates (hangs). Re-add the chunk_size/stride guard at the top of smart_chunk_text so direct callers fail fast with a clear ValueError. The constructor keeps its own guard for the internal callers (defense in depth). Add a regression test that calls smart_chunk_text directly with stride == chunk_size and stride > chunk_size and asserts it raises instead of hanging. Signed-off-by: Anas Khan <[email protected]>
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
There was a problem hiding this comment.
Code Review
This pull request introduces validation checks to smart_chunk_text in unsloth/dataprep/raw_text.py to ensure chunk_size is positive and stride is smaller than chunk_size, preventing potential infinite loops during chunking. It also adds corresponding unit tests in tests/test_raw_text.py. The review feedback recommends extending this validation to explicitly reject negative stride values and adding a test case to verify this behavior.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if stride >= chunk_size: | ||
| raise ValueError( | ||
| f"stride ({stride}) must be smaller than chunk_size ({chunk_size}) to progress the chunking loop" | ||
| ) |
There was a problem hiding this comment.
A negative stride is logically invalid for chunking with overlap, as it would cause the loader to skip portions of the text. We should explicitly guard against negative stride values in addition to checking if stride >= chunk_size.
| if stride >= chunk_size: | |
| raise ValueError( | |
| f"stride ({stride}) must be smaller than chunk_size ({chunk_size}) to progress the chunking loop" | |
| ) | |
| if stride < 0 or stride >= chunk_size: | |
| raise ValueError( | |
| f"stride ({stride}) must be non-negative and smaller than chunk_size ({chunk_size})" | |
| ) |
| try: | ||
| loader.smart_chunk_text(long_text, chunk_size = 5, stride = 10) | ||
| assert False, "Should raise ValueError for stride > chunk_size" | ||
| except ValueError as e: | ||
| assert "stride" in str(e) and "chunk_size" in str(e) |
There was a problem hiding this comment.
Add a test case to verify that a negative stride (stride < 0) is also correctly rejected with a ValueError.
| try: | |
| loader.smart_chunk_text(long_text, chunk_size = 5, stride = 10) | |
| assert False, "Should raise ValueError for stride > chunk_size" | |
| except ValueError as e: | |
| assert "stride" in str(e) and "chunk_size" in str(e) | |
| try: | |
| loader.smart_chunk_text(long_text, chunk_size = 5, stride = 10) | |
| assert False, "Should raise ValueError for stride > chunk_size" | |
| except ValueError as e: | |
| assert "stride" in str(e) and "chunk_size" in str(e) | |
| try: | |
| loader.smart_chunk_text(long_text, chunk_size = 5, stride = -1) | |
| assert False, "Should raise ValueError for stride < 0" | |
| except ValueError as e: | |
| assert "stride" in str(e) |
ErenAta16
left a comment
There was a problem hiding this comment.
Confirmed the hang: the chunking loop advances via start_idx += chunk_size - stride, so stride >= chunk_size makes that increment zero or negative, start_idx never moves forward (or moves backward), and the loop never reaches the text's end condition, an infinite loop rather than a crash, which is worse since it doesn't fail loudly. Adding the guard directly inside smart_chunk_text rather than relying only on the constructor-level check is the right call given the comment that this method can be called directly with its own chunk_size/stride, bypassing whatever validation lives on the class constructor.
Tests cover the three relevant cases: a valid stride still produces chunks (no regression on the working path), stride == chunk_size raises, and stride > chunk_size raises, both with the expected message content. Small, correct fix for a real hang.
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Thanks for the PR! |
What
RawTextDataLoader.smart_chunk_text(a public method on the exportedRawTextDataLoader) can hang forever when called directly withstride >= chunk_size.The method advances its window with
start_idx += chunk_size - strideand onlyexits the loop on
if end_idx == len(tokens): break:When
stride == chunk_sizethe increment is0; whenstride > chunk_sizeitis negative. For any text longer than one chunk,
start_idxnever advances pastthe first window,
end_idxnever reacheslen(tokens), and thewhileloopnever terminates.
The constructor already validates this (
__init__raises whenstride >= chunk_size), butsmart_chunk_texttakeschunk_sizeandstrideas its own explicit arguments, so a direct call bypasses the constructor guard:
This guard used to live in
smart_chunk_textitself and was later moved to theconstructor when parameter validation was centralized, which left the public
method unguarded for direct callers.
Fix
Re-add the
chunk_size/strideguard at the top ofsmart_chunk_textso directcallers fail fast with a clear
ValueErrorinstead of hanging. The constructorkeeps its guard as well (defense in depth for the internal callers, which pass
the validated
self.chunk_size/self.stride). No behavior changes for validinputs or for any existing internal call path.
Test
Adds a regression test to
tests/test_raw_text.pythat callssmart_chunk_textdirectly and asserts:
stride < chunk_size) still produces chunks, andstride == chunk_sizeandstride > chunk_sizeraiseValueError(instead ofhanging).
Notes
ruff checkon both files passes.dataprep.