Skip to content

Pair the linearity measurements, so a busy runner stops reading as quadratic - #10868

Merged
danielhanchen merged 1 commit into
mainfrom
robustify-linearity-ratio
Sep 13, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
robustify-linearity-ratio

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Why

test_deepseek_r1_huge_fenceless_body_is_linear failed on #10864 at 6.56 against a bar of 6.0, on a branch that touches no parser code. It failed the same way on #10825. It is a repeat offender that says nothing about the PR carrying it.

The parser is fine. Measured locally across all four shapes, the median 4x ratio is ~4.0, which is exactly what linear looks like:

shape min median max
R1 3.52 4.11 4.53
R1-distant 2.75 3.99 4.39
GLM 3.12 3.97 4.29
V3 3.75 4.04 4.30

What is actually wrong

_growth computed:

min(big over repeats) / min(small over repeats)

and its comment claimed that "dividing one minimum by the other cancels the machine out". It does not. The two minima come from two batches run at different times, so a quiet window during the small batch and a busy window during the big batch multiply rather than cancel. The quotient of two separately-taken minima is not a minimum of anything, and it has no bounded relationship to the true ratio.

Reproduced on 2 pinned CPUs under load, 15 trials per shape:

strategy R1 R1-distant GLM V3 >= 6.0
min/min (current) max 8.69 max 8.23 max 8.27 max 8.59 7 of 60
paired median (this PR) max 4.26 max 4.83 max 4.83 max 5.42 0 of 60

End to end, 10 contended runs of the four linearity tests: the old form fails 3 of 10 runs, the new form 0 of 10.

The change

Each pair times the small input and then the big one back to back, and forms the ratio inside the pair. Contention then hits both halves and divides out, which is what the old comment claimed for the unpaired form. The median across pairs discards one that got unlucky, in either direction.

The bar stays at 6.0. Nothing here loosens the threshold. It only stops the number fed to it from being dominated by whoever else is on the runner.

Detection is kept

This is the half worth checking before touching a flaky-looking assertion, because the easy "fix" is to quietly stop catching the bug.

The 60 second backstop still reads the best big time rather than the worst, so it fires on a path that is genuinely too slow rather than on a runner that stalled once.

Checks

  • studio/backend/tests/test_pr5624_regressions.py: 67 passed.
  • 10 contended runs, old vs new: 3/10 failing runs to 0/10.
  • Sabotage check above, then parser restored and re-verified green.
  • ruff check --select E9,F63,F7,F82 clean; the pinned ruff-format-with-kwargs hook is a fixed point.

…adratic

test_deepseek_r1_huge_fenceless_body_is_linear failed on #10864 at 6.56 against
a bar of 6.0, on a branch that touches no parser code. It failed the same way on
#10825. The parser is fine: measured locally across all four shapes the median
4x ratio is ~4.0, which is what linear looks like.

The measurement is what is wrong. _growth computed

    min(big over repeats) / min(small over repeats)

and its comment claimed that dividing one minimum by the other cancels the
machine out. It does not. The two minima come from two batches run at different
times, so a quiet window during the small batch and a busy window during the big
batch multiply rather than cancel, and the quotient of two separately-taken
minima is not a minimum of anything.

Measured on 2 pinned CPUs under load, 15 trials per shape:

    strategy        R1        R1-distant  GLM       V3        >= 6.0
    min/min         max 8.69  max 8.23    max 8.27  max 8.59  7 of 60
    paired median   max 4.26  max 4.83    max 4.83  max 5.42  0 of 60

End to end, 10 contended runs of the four linearity tests: the old form fails
3 of 10 runs, the new form 0 of 10.

Each pair now times the small input and then the big one back to back and forms
the ratio inside the pair, so contention hits both halves and divides out, which
is what the old comment claimed for the unpaired form. The median across pairs
then discards one that got unlucky in either direction.

Detection is kept, which is the half worth checking before loosening a bar.
Sabotaging the R1 sweep back to quadratic (re-adding a per-marker tail rescan)
still fails, at 14.2x and 15.3x. On a synthetic 16x path the new form reports
8 of 8 over the bar, the same as the old one. On the marginal 6.7x shape
(#10832's partially fixed sweep) it reports 9 of 12 against the old 10 of 12,
a difference well inside the noise at that sample size, and that shape's sibling
test measures 12.2x when broken, so the suite still catches it.

The bar stays at 6.0. Nothing here loosens the threshold; it only stops the
measurement fed to it from being dominated by whoever else is on the runner.

The 60 second backstop still reads the BEST big time rather than the worst, so
it fires on a path that is genuinely too slow and not on a runner that stalled
once.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 13, 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-13T14:07:17.895612Z ab94362 PR opened
🔒 Security Review ✅ Completed 2026-09-13T14:06:49.932119Z ab94362 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.

@danielhanchen
danielhanchen merged commit b3dfd08 into main Sep 13, 2026
32 checks passed
@danielhanchen
danielhanchen deleted the robustify-linearity-ratio branch September 13, 2026 14:33
danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Sep 14, 2026
unslothai#10868 replaced the linearity check in test_pr5624_regressions.py after it
failed on unslothai#10825 and again on unslothai#10864 at 6.56 against a 6.0 bound, both times on
branches that touch none of that code. The same shape is elsewhere in the suite:
sub-100ms budgets, two separately-taken durations compared against each other,
and concurrency proved by how long four threads took. All of them run under
pytest -n 4 on a shared runner, where descheduling is indistinguishable from the
regression they guard.

Each one is rewritten to assert the thing it is named for.

test_llama_cpp_wait_for_vram_settle.py has five, all of them claims about the
helper's sleeping: did it skip the wait, nap once per poll, clip the last nap to
the deadline. Record the durations it asks for and assert on those. The tightest
was elapsed < 0.05 around a short-circuit, which one GC pause exceeds.

test_account_model_access.py proved eight probes ran together by taking under
0.4s. Its own sibling two functions up already uses a threading.Barrier for the
same claim, so use one here too, sized off _PROBE_FANOUT so lowering the fan-out
fails this rather than satisfying a Barrier(1).

test_mcp_http_sessions.py had three. Two chats running concurrently needed a
counter that spans clients, since no single client's max_live can see an overlap
between two of them; six parallel closes get a Barrier(6), so a serial close
deadlocks and says so instead of merely running slowly; and the eviction test
asserts the caller returned while the victim's teardown was still running, which
is the property the 0.5s bound was standing in for.

test_stream_cancel_registration_timing.py compared two separately-taken
durations, exactly what unslothai#10868 had to remove from _growth. The two patterns
differ in chunks seen, deterministically: the blocking loop never lets the cancel
through and drains all eight, the awaiting loop stops at the first.

Four guards against a quadratic blow-up carried absolute budgets
(test_tool_xml_strip.py, test_update_release_notes.py, test_rag_store.py).
Backtracking is superlinear by definition, so growth is the property, and
unslothai#10868's paired-ratio statistics already exist for it. Lift _growth and
_assert_linear out of test_pr5624_regressions.py into tests/_shared/growth.py,
generalised over the function under measurement, next to real_accelerator.py and
for the same reason.

test_profile_stats.py and the two liveness files keep a wall-clock bound, because
"it did not block" is what they mean, but a generous one: the sharp claim in each
is already asserted next to it (the row count is zero, stop() returned False, the
stub raises if detection starts).

test_backend_ci_parallel_isolation.py scans for this shape and required three
named files to keep exhibiting it, which made it a second, invisible reason those
files could not be fixed: rewriting the vram-settle bounds failed there, in a file
about CI topology, with a message about a sample. It builds all three shapes
itself now.

Not touched: test_health_answers_within_probe_budget.py. Its bounds are the
subject, derived from the real desktop probe timeout rather than from a guess,
and it already asserts on state where state was the honest answer.

Each rewritten assertion was checked against a deliberately broken
implementation: no cold-start short-circuit, an unclipped sleep, a one-shot wait
loop, _PROBE_FANOUT of 1, _MAX_CLOSE_THREADS of 1, and two synthetic quadratic
sweeps. Full backend suite run the way CI runs it, three times: identical failure
set to main.
danielhanchen added a commit that referenced this pull request Sep 14, 2026
#10897)

* Stop ten test assertions from measuring the runner instead of the code

#10868 replaced the linearity check in test_pr5624_regressions.py after it
failed on #10825 and again on #10864 at 6.56 against a 6.0 bound, both times on
branches that touch none of that code. The same shape is elsewhere in the suite:
sub-100ms budgets, two separately-taken durations compared against each other,
and concurrency proved by how long four threads took. All of them run under
pytest -n 4 on a shared runner, where descheduling is indistinguishable from the
regression they guard.

Each one is rewritten to assert the thing it is named for.

test_llama_cpp_wait_for_vram_settle.py has five, all of them claims about the
helper's sleeping: did it skip the wait, nap once per poll, clip the last nap to
the deadline. Record the durations it asks for and assert on those. The tightest
was elapsed < 0.05 around a short-circuit, which one GC pause exceeds.

test_account_model_access.py proved eight probes ran together by taking under
0.4s. Its own sibling two functions up already uses a threading.Barrier for the
same claim, so use one here too, sized off _PROBE_FANOUT so lowering the fan-out
fails this rather than satisfying a Barrier(1).

test_mcp_http_sessions.py had three. Two chats running concurrently needed a
counter that spans clients, since no single client's max_live can see an overlap
between two of them; six parallel closes get a Barrier(6), so a serial close
deadlocks and says so instead of merely running slowly; and the eviction test
asserts the caller returned while the victim's teardown was still running, which
is the property the 0.5s bound was standing in for.

test_stream_cancel_registration_timing.py compared two separately-taken
durations, exactly what #10868 had to remove from _growth. The two patterns
differ in chunks seen, deterministically: the blocking loop never lets the cancel
through and drains all eight, the awaiting loop stops at the first.

Four guards against a quadratic blow-up carried absolute budgets
(test_tool_xml_strip.py, test_update_release_notes.py, test_rag_store.py).
Backtracking is superlinear by definition, so growth is the property, and
#10868's paired-ratio statistics already exist for it. Lift _growth and
_assert_linear out of test_pr5624_regressions.py into tests/_shared/growth.py,
generalised over the function under measurement, next to real_accelerator.py and
for the same reason.

test_profile_stats.py and the two liveness files keep a wall-clock bound, because
"it did not block" is what they mean, but a generous one: the sharp claim in each
is already asserted next to it (the row count is zero, stop() returned False, the
stub raises if detection starts).

test_backend_ci_parallel_isolation.py scans for this shape and required three
named files to keep exhibiting it, which made it a second, invisible reason those
files could not be fixed: rewriting the vram-settle bounds failed there, in a file
about CI topology, with a message about a sample. It builds all three shapes
itself now.

Not touched: test_health_answers_within_probe_budget.py. Its bounds are the
subject, derived from the real desktop probe timeout rather than from a guess,
and it already asserts on state where state was the honest answer.

Each rewritten assertion was checked against a deliberately broken
implementation: no cold-start short-circuit, an unclipped sleep, a one-shot wait
loop, _PROBE_FANOUT of 1, _MAX_CLOSE_THREADS of 1, and two synthetic quadratic
sweeps. Full backend suite run the way CI runs it, three times: identical failure
set to main.

* Size the growth fixtures off the old bound, and derive the liveness ceilings

Two review findings, both right.

assert_linear measures units AND units * factor, so replacing an absolute budget
means passing the old size divided by factor. Three of the five conversions did
that; the backtick-run and orphan-opens ones did not, and their big leg ran at
4x anything previously measured. That matters only on the regression each guards:
the backtick parser took over a minute at 20k when broken, so at 80k it would
take about sixteen, and the job timeout would kill the run before the ratio could
say why. The big < 60.0 backstop cannot help, because it is checked after the
measurement rather than during it. Both call sites now put the previously
measured size on the big leg, and assert_linear says so where a caller reads it.

The liveness ceilings were widened to a number with no relation to the product.
commands.rs gives each watchdog probe HEALTH_PROBE_TIMEOUT, ten seconds, so a
route that blocked for 10 to 29 seconds passed the test while timing out every
real probe. Read that constant, as test_health_answers_within_probe_budget.py
already does for its own budget, and use half of it: ten times the headroom of
the 0.5s it replaces, and provably inside what the launcher allows.

* Tighten the comments this branch added

* Pin two timing assertions to what they are about, not to slack

The eviction test's victim slept 1.5s, which a caller only had to outwait part of
to read as prompt; it now holds its teardown on an event the test releases, so a
caller that waits on it at all cannot return, and the remaining ceiling comes
from _SESSION_CLOSE_TIMEOUT rather than being picked.

The writer shutdown test now records the timeout stop() hands its join instead of
allowing 5s of wall clock, so a stop() that waits whole seconds fails even though
the elapsed budget would have passed it.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Assert both ends of the settle window, and stop buying slack this leg does not need

The never-settles case only checked that the helper napped more than once, so a
loop capped at two iterations recorded [0.1, 0.1] and passed while the VRAM was
still moving. Both ends are now read off the recorded naps.

The terminal-callback bound goes back to 1.0s: studio-backend-ci.yml ignores
test_profile_stats.py in the -n 4 leg and reruns it serially, so a looser bound
only lets a stalling callback through.

* Bound liveness against a route that does nothing, not against the watchdog

Half the watchdog's probe budget is 5s, which passes a route that grew seconds of
synchronous work on the event loop. The probe snippets now serve a route that only
returns a dict through the same app and client, so both pay the same interpreter and
scheduler and what is left is the route's own cost.

The watchdog bound stays as the second half of the pair: a relative bound cannot
see an absolute timeout, since a control that took seconds would scale with it.

* Bound the eviction call against the same call with nothing to evict

Half of _SESSION_CLOSE_TIMEOUT is five seconds, which passes a caller that blocks
for a second before handing the victim to the cleanup worker. The cap is now
applied after a control call on a second URL, so the two differ only in whether an
eviction happens and the claim is measured rather than budgeted.

* Take the runner out of the clipping test, abort a known-slow growth run, drop a live dependency

The slow-probe test asked for a nap that a correct helper need not make: if the
scheduler ate the 0.1s left of the budget it returned at the deadline check instead,
and the assertion failed on a good run. It now runs on a clock the test moves, so
the probe charges its cost instead of sleeping and every sample is exact.

growth() takes abort_over_s, so a big leg already over the backstop stops the
repeats instead of measuring the same known-bad path three times.

The parallel-isolation meta-test no longer requires the live backend suite to still
contain a fragile assertion; the synthetic files cover every shape, and that
requirement would have turned cleaning the last one into a failure.

* Pair the liveness controls, clock the drift test, ask the eviction by thread

A control taken in its own batch shares no scheduler delay with the request it is
compared against, so a pause landing on the measurement alone was not divided out
and the 50ms floor made that stricter than the bound it replaced. Both liveness
snippets now alternate control and measurement and take the best of three on each
side.

The never-settles test moves to the same fake clock as the slow-probe one: its lower
bound says the helper did not stop early, and real time spent outside sleep()
retires the deadline without appearing in the naps.

The eviction test asks which thread calls close() instead of how long the caller
took. __aexit__ cannot answer it, since that always runs on the session's own loop
thread, but the caller of close() is exactly the claim in the test's name.

* Size the close barrier off the fan-out, and watch the join as well as the close

A fixed Barrier(6) deadlocks a correctly batched close configured any narrower than
six and then reports it as serial. It now waits for one batch and opens exactly that
many sessions, so a partial last batch cannot leave stragglers waiting for a party
that never arrives.

The eviction test also records joins on the cleanup worker. Handing the teardown off
and then waiting on the worker anyway spends the same deadline, and nothing else here
can see it: a bounded join leaves the close off the calling thread and the session
still open.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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