Skip to content

test: size the slot-cap request from what the ledger accepts - #175

Merged
Gajesh2007 merged 7 commits into
mainfrom
test/historical-window-slot-cap
Oct 2, 2026
Merged

Gajesh2007 merged 7 commits into
mainfrom
test/historical-window-slot-cap

Conversation

@davidtai

@davidtai davidtai commented Sep 29, 2026 •

Copy link
Copy Markdown

Proposed changes

HistoricalWindowCheckpointTests.slotCapLeavesRoomForServing() failed once on the hosted runner. It failed at !uncapped.reserved (#174 run 36513144872). The request size that the test printed was different on each machine:

Run requestTokens Uncapped reservation
CI, four passing runs 1,044,128 refused (pass)
CI, #174 run 1,043,808 accepted (fail)
This laptop, alone 1,044,512 refused (pass)

Cause

The test sized its request from admissibleBytesCapacity - bytesReserved, with half a window (38,098 bytes) to spare. Then it gave the same request to an uncapped fixture with six more staged windows. It expected a refusal.

  • bytesReserved is the ledger plus the physical-floor overhead: physical - nominal (AdmissionPhysicalFloor.swift:13-17, AdmissionV2.swift:401-403 and 1193-1197).
  • A request's target KV replaces that overhead when it reserves ("Nominal target KV from its own requests can replace this charge", AdmissionPhysicalFloor.swift:3-4). So the real slack is half a window plus the overhead.
  • The physical bytes are the sizes of the Metal buffers that MLX returns (PagedKVSegments.swift:186-191, PagedKVPool+Segments.swift:178-190). Each segment needs 36,864 bytes. A new buffer has 49,152 bytes. A buffer reused from MLX's process-wide cache can have 65,536 bytes. So the overhead depends on what earlier tests left in the cache.
  • The uncapped reservation fits when the overhead is more than 5.5 windows (419,084 bytes). On this laptop the overhead is 245,760 bytes. The failed CI run had 81,920 bytes more overhead than the passing CI runs. That is 320 fewer tokens at 256 bytes per token.

The earlier guess was that a charge from earlier tests stayed in bytesReserved. That guess was wrong. The charge belongs to the test's own pool. Only its size depends on the process-wide allocator cache. The product charges the real buffer size and refunds it correctly. The rule that a request's target KV replaces the overhead is by design. So no product code changes.

Fix

The test now finds the largest request that the ledger accepts beside the capped set (the closure parameter is candidate). It uses trial reserve and releaseAll calls in a binary search. Then it takes half a window off that size. No assertion changes. The capped request must still fit, and the uncapped arm must still refuse it.

The first commit only formats the file with .swift-format. The Lint job formats each changed file as a whole.

Test plan

Local probe (not in this PR). Before the test, it fills the MLX buffer cache with 64 × 65,536-byte buffers (seg64):

Code Cache requestTokens Result
main clean 1,044,512 pass
main seg64 1,043,552 fail at !uncapped.reserved (3 of 3). Overhead 491,520 bytes
this PR clean 1,045,472 pass (2 of 2)
this PR seg64 1,045,472 pass (2 of 2)

The request size no longer depends on the cache. The difference from main on a clean cache, 960 tokens (245,760 bytes), is the overhead that the old sizing counted. HistoricalWindowCheckpointTests passes all 20 tests locally. Local runs are only evidence for the ledger arithmetic here, not for numerics.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes: the changed file is formatted with swift format format --configuration .swift-format
  • I have added tests that prove my fix is effective or that my feature works: not applicable, this PR fixes a test; the probe above shows the effect
  • I have updated the necessary documentation (if needed): not applicable, no document describes this test

Dependencies

Stacked on #174 (base test/gate-bit-exact-reference-tests). Merge #174 first, then this PR. This PR's Fork Tests run contains both changes.

Authorship

The commits were written by Claude Opus 5.5.

🤖 Generated with Claude Code

davidtai and others added 4 commits September 28, 2026 21:22
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Nine DiffusionGemma tests compare float bits with frozen references.
On the hosted macos-26 runner (Apple M1 VM) they differ in the low bits,
with no change in shape, dtype or sign. Add a `.referenceHardware`
Swift Testing trait that runs a test only when MLX_REFERENCE_HARDWARE=1,
and mark the nine tests with it. Elsewhere they report as skipped with
the reason. No assertion, tolerance or reference changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Three gated tests also held checks that pass on any hardware. Split them
so only the bit-for-bit comparison is gated:

- prefixIdentityTokensOwnerAndEmptyStateFailClosed() runs its
  fail-closed probes on all hardware. The donor-logits bit comparison
  moves to failClosedProbesLeaveDonorLogitsBitExact().
- nativeFactoryLoadsExactStateAndReleasesItsModel() keeps the load and
  model-release checks on all hardware. The logits bit comparison moves
  to nativeFactoryDenoiseMatchesReferenceBits().
- The two parameterized vision tests run their bf16 case on all
  hardware. The fp32 case moves to a separate gated test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@davidtai
davidtai force-pushed the test/historical-window-slot-cap branch from a763e8b to aca2ba9 Compare September 29, 2026 04:44
@davidtai
davidtai changed the base branch from main to test/gate-bit-exact-reference-tests September 29, 2026 04:44
davidtai and others added 3 commits September 29, 2026 00:06
…a tests

Split the three tests that were still gated whole, so their checks that
pass on any hardware run everywhere:

- chunkedSessionBoundsPrefillAndCanCancelAfterPrefill() keeps the
  prefill bounds, the complete flag, the counters, cancel, the emitted
  checkpoint and the released cache for widths 1, 3 and 7. The KV and
  logits bit comparisons move to chunkedSessionMatchesIndependentNativeReference().
- sharedWeightsConditioningKeepsCommittedCacheState() keeps the fixture
  checks, both cache positions and the unchanged encoder KV after the
  provisional canvas. The reference comparisons stay in
  sharedWeightsConditioningAndCommittedCacheMatchReference().
- committedPrefixRestoreKeepsPositionsAfterDonorRetirement() keeps the
  token count, retained bytes, donor release and both positions. The
  reference comparisons stay in
  committedPrefixRestoreIsExactAfterDonorAppendAndRetirement().

Each pair shares one helper. The fixture reference, case-count and
parameter-count checks of the block and fp32 vision fixtures also run
everywhere now, in blockOracleFixtureIsPinned() and fp32FixtureIsPinned().
CONTRIBUTING.md says which comparisons the gate covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
slotCapLeavesRoomForServing() sized its request from bytesReserved.
bytesReserved includes the paged pool's physical-floor overhead, which a
request's target KV replaces when it reserves. The overhead depends on
the buffer sizes the MLX allocator returns: a 36,864-byte segment gets a
49,152-byte buffer, or a 65,536-byte buffer reused from the process-wide
cache. When earlier work left 64 KiB buffers in the cache, the capped
arm left more than 5.5 windows of real slack instead of half a window,
and the uncapped arm's reservation fit.

Find the largest request the ledger accepts beside the capped set with
trial reservations, then take half a window off it. The assertions do
not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.

2 participants