Skip to content

Count the stripper's whole-buffer work instead of timing it - #11735

Merged
danielhanchen merged 2 commits into
mainfrom
fix/stripper-early-markup-count
Sep 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/stripper-early-markup-count

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

(Python 3.13, a-k) failed on #11724, which does not touch the stripper:

FAILED tests/test_streaming_stripper.py::test_early_markup_is_not_slower_than_the_code_it_replaces
AssertionError: early markup cost 0.444s against the reference's 0.404s

The test compared the CPU time of StreamingMarkupStripper.strip against the reference full strip with a 10% margin. With markup at the front and nothing settled, the two arms do the same strip, so the ratio sits at 1.0 by design.

Measured on an idle machine, best of 3 per trial:

code incremental / reference
main 0.89, 1.09, 0.97, 1.04, 1.04, 1.01
whole-buffer guards removed (the regression) 1.33, 1.11, 1.22, 1.19

The healthy runs reach 1.09 and the regressed runs start at 1.11, so no timing margin reliably tells them apart. The product is fine and the test cannot make the distinction it was written for.

Change

The work is countable, so the test now counts it instead of timing it:

  • _needs_whole_buffer is called on no token.
  • _full_strip reads no more characters than the reference (on main it is exactly equal).
  • Every output still equals _reference_strip, and a check that _full_strip ran on every token keeps the counters from being vacuous.

A second case puts the markup at offset 0. That reaches the degenerate branch and its self._floor and guard, which the timed version never exercised.

Testing

  • studio/backend/tests/test_streaming_stripper.py: 67 passed.
  • Sabotage, removing both (cut or self._floor) and and self._floor and: both cases fail, with 1500 of 1500 tokens paying the checks.
  • Sabotage, removing only the degenerate-branch self._floor and: [at-offset-0] fails (1486 tokens) and [near-front] passes. The old timed test measured 0.94 to 1.08 there and could not see it.

test_early_markup_is_not_slower_than_the_code_it_replaces compared CPU time
against the reference strip with a 10% margin. With markup at the front and
nothing settled the two arms do the same strip, so the ratio sits at 1.0 and
runner noise alone spans 0.89 to 1.09; it failed on #11724, which does not touch
the stripper, at 0.444s against 0.404s. The regression it guards, paying the
whole-buffer checks on every token, measured 1.11 to 1.33, so no margin
separates it from the noise.

Count the work instead: no token pays _needs_whole_buffer, and _full_strip
reads no more text than the reference. A second case puts the markup at offset
0, which reaches the degenerate branch the timed version never exercised.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-23T21:18:48.541973Z 50088b8 Manual request
🔒 Security Review ⚠️ Failed 2026-09-23T21:10:59.391723Z e94839c PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e94839cc55

ℹ️ 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".

Comment on lines +480 to +482
def counting_full_strip(self, text):
stripped_chars.append(len(text))
return full_strip(self, text)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Count the scans performed inside _full_strip

When a regression adds or reintroduces a whole-buffer pass inside _full_strip, this wrapper still records only one len(text) and the assertion below remains equal to reference_chars, even if the implementation scans that text repeatedly or becomes quadratic. Because this commit removes the runtime comparison, it no longer protects the test's stated performance guarantee; instrument the underlying passes or otherwise measure the actual work rather than only the size of each _full_strip argument.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed. Counting only the size of _full_strip's argument could not see a second pass inside it. Fixed in 50088b8: the test now counts the characters each arm hands to strip_outside_think and strip_segment (both arms reach them through the same module objects), plus the blocked-body masking only the incremental arm runs, and holds the incremental arm to the reference's totals. Duplicating the strip_outside_think call or the masking call inside _full_strip now fails both cases with the doubled count (11446500 against 5723250). A slowdown inside strip_segment itself costs both arms the same, so the timed version could not catch that either.

Counting only _full_strip's argument could not see a second strip or scan
inside it. Count the characters each arm hands to strip_outside_think and
strip_segment, and to the blocked-body masking, and hold the incremental arm
to the reference's totals. A doubled strip_outside_think or masking pass now
fails with the counts that grew.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 50088b8a75

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 609253d into main Sep 23, 2026
34 of 35 checks passed
@danielhanchen
danielhanchen deleted the fix/stripper-early-markup-count branch September 23, 2026 21:51
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…i#11735)

* Count the stripper's whole-buffer work instead of timing it

test_early_markup_is_not_slower_than_the_code_it_replaces compared CPU time
against the reference strip with a 10% margin. With markup at the front and
nothing settled the two arms do the same strip, so the ratio sits at 1.0 and
runner noise alone spans 0.89 to 1.09; it failed on unslothai#11724, which does not touch
the stripper, at 0.444s against 0.404s. The regression it guards, paying the
whole-buffer checks on every token, measured 1.11 to 1.33, so no margin
separates it from the noise.

Count the work instead: no token pays _needs_whole_buffer, and _full_strip
reads no more text than the reference. A second case puts the markup at offset
0, which reaches the degenerate branch the timed version never exercised.

* Count the passes inside the strip, in both arms

Counting only _full_strip's argument could not see a second strip or scan
inside it. Count the characters each arm hands to strip_outside_think and
strip_segment, and to the blocked-body masking, and hold the incremental arm
to the reference's totals. A doubled strip_outside_think or masking pass now
fails with the counts that grew.
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.

1 participant