Skip to content

fix(dataprep): guard smart_chunk_text against stride >= chunk_size - #7126

Merged
Etherll merged 1 commit into
unslothai:mainfrom
anxkhn:patch-7
Jul 15, 2026
Merged

Etherll merged 1 commit into
unslothai:mainfrom
anxkhn:patch-7

Conversation

@anxkhn

@anxkhn anxkhn commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

What

RawTextDataLoader.smart_chunk_text (a public method on the exported
RawTextDataLoader) can hang forever when called directly with
stride >= chunk_size.

The method advances its window with start_idx += chunk_size - stride and only
exits the loop on if end_idx == len(tokens): break:

# unsloth/dataprep/raw_text.py
while start_idx < len(tokens):
    end_idx = min(start_idx + chunk_size, len(tokens))
    ...
    if end_idx == len(tokens):
        break
    start_idx += chunk_size - stride   # <= 0 when stride >= chunk_size

When stride == chunk_size the increment is 0; when stride > chunk_size it
is negative. For any text longer than one chunk, start_idx never advances past
the first window, end_idx never reaches len(tokens), and the while loop
never terminates.

The constructor already validates this (__init__ raises when
stride >= chunk_size), but smart_chunk_text takes chunk_size and stride
as its own explicit arguments, so a direct call bypasses the constructor guard:

loader = RawTextDataLoader(tokenizer, chunk_size = 5, stride = 2)  # ok
loader.smart_chunk_text(long_text, chunk_size = 5, stride = 5)     # hangs

This guard used to live in smart_chunk_text itself and was later moved to the
constructor when parameter validation was centralized, which left the public
method unguarded for direct callers.

Fix

Re-add the chunk_size/stride guard at the top of smart_chunk_text so direct
callers fail fast with a clear ValueError instead of hanging. The constructor
keeps its guard as well (defense in depth for the internal callers, which pass
the validated self.chunk_size / self.stride). No behavior changes for valid
inputs or for any existing internal call path.

Test

Adds a regression test to tests/test_raw_text.py that calls smart_chunk_text
directly and asserts:

  • a valid stride (stride < chunk_size) still produces chunks, and
  • stride == chunk_size and stride > chunk_size raise ValueError (instead of
    hanging).

Notes

  • Single focused change.
  • Lint: ruff check on both files passes.
  • No linked issue exists; found while reading dataprep.

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]>
@anxkhn
anxkhn requested a review from danielhanchen as a code owner July 14, 2026 16:31
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +137 to +140
if stride >= chunk_size:
raise ValueError(
f"stride ({stride}) must be smaller than chunk_size ({chunk_size}) to progress the chunking loop"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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})"
)

Comment thread tests/test_raw_text.py
Comment on lines +158 to +162
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Add a test case to verify that a negative stride (stride < 0) is also correctly rejected with a ValueError.

Suggested change
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 ErenAta16 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Etherll

Etherll commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@Etherll Etherll self-assigned this Jul 15, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: e6e42804b4

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Etherll Etherll left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Etherll

Etherll commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR!

@Etherll
Etherll merged commit 387b2f2 into unslothai:main Jul 15, 2026
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.

3 participants