Skip to content

feat(gemma4): cross the sliding window — front-release with retained global - #887

Merged
FeathBow merged 1 commit into
pegainfer-project:mainfrom
FeathBow:feat/gemma4-window-eviction
Aug 15, 2026
Merged

feat(gemma4): cross the sliding window — front-release with retained global#887
FeathBow merged 1 commit into
pegainfer-project:mainfrom
FeathBow:feat/gemma4-window-eviction

Conversation

@FeathBow

Copy link
Copy Markdown
Collaborator

Description

Closes #886

The local family becomes a sliding state: resident pages held as single-page reservations, released from the front once (page + 1) * 16 + window <= frontier — after the step's attention, since append-then-attend keeps the old window and the new tokens co-resident through the layers. What stops growing is the logical page count a request holds once a step completes; the step itself still peaks at everything it wrote, and the pool's device buffer is allocated up front either way, so a release returns pages to the pool rather than to the driver. The paged preps take the resident row plus a page_origin: the origin is page-aligned, so in-page offsets stay position-invariant and only the row index shifts. RoPE keeps absolute positions; the attention plan lives in cache-relative coordinates, where window_left masks whatever sub-window prefix the first page still carries — a non-aligned resident start loses nothing.

Test Env

Single GPU (sm_89, x86_64), CUDA 12.9, pinned 12B checkpoint, --test-threads=1

Verification

  • clippy -D warnings across gemma4/core/kernels targets — clean.
  • Window crossing, distribution-level: greedy chains are not reference material at this depth — measured per-step top1-top2 margins collapse to 0.0-1.3 past ~1000 tokens for every corpus tried, and HF's own sdpa/eager continue the same prompt in different directions (their teacher-forced top-1 self-agreement is 6/9 per case). The fixture records teacher-forced top-64 logprobs under BOTH backends at 1023/1024/1025/4096 prompt lengths (+8 forced steps each); the gate asserts our path within 2x the measured cross-backend gap. Results: max |dlogprob| 0.97 / 2.62 / 1.35 / 4.27 against tolerances 2.92 / 4.61 / 4.99 / 7.65 — the first and third sit BELOW the backends' own gap — with top-1 agreement 7/8/8/6 of 9, at or above HF's self-agreement.
  • Chunked crossing: the 4096 case re-run in window-sized chunks, which is the only shape that reaches a multi-token step with the resident row already shifted — max |dlogprob| 2.55 against the same 7.65 tolerance, top-1 8/9, and the test asserts such a step actually occurred rather than trusting the arithmetic.
  • Release preserves the generated tokens while reducing the logical footprint: the same crossing request with front-release disabled generates identical tokens while holding strictly more pages (65 vs 66 at the test length). The A/B compares tokens, not logits.
  • Footprint is asserted exactly, not loosely: each case asserts ceil(kv_len / 16) - floor((kv_len - window) / 16) resident pages, so releasing one page late fails the gate.
  • Footprint curves: the local family's logical resident pages, counted once a step completes, stop growing while global retains everything. For the 4096 case: the prefill step peaks at 256 local pages and ends at local 64 against global 256; after the eight teacher-forced tokens (kv_len 4104) it is local 65 against global 257. Two boundaries on that claim: the pool's device buffer is allocated up front, so releasing pages returns them to the pool's free list rather than lowering process HBM; and a single 4096-token prefill still holds all 256 local pages at once until that step's attention is done, since the front is only released afterwards.
  • KV12 gates re-run green over the sliding refactor (regression): 25 library tests plus the six checkpoint gates from feat(gemma4): paged KV serving across both attention families #885, unchanged.
  • What each case actually exercises: front-release only triggers once (origin + 1) * 16 + window <= frontier, i.e. at frontier 1040, so w1023/w1024/w1025 (1031/1032/1033 tokens after their forced steps) gate the mask boundary — window_left starting to hide the oldest keys — while w4096 is the case that releases, and it is where the footprint claim comes from (local 65 pages against global 257). The eviction A/B runs at the same shorter length, so its margin is one page (65 vs 66); the wide margin lives in w4096.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

@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: f60224f5d5

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

pub(crate) fn advance_and_release(&mut self, count: usize, window: usize) -> Result<()> {
let page = self.pool.layout().page_size;
let frontier = self.frontier + count;
let target = frontier.saturating_sub(window) / page;

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 Release pages as soon as their last token expires

At a post-step frontier of 1039 with window = 1024 and page = 16, the next query is position 1039 and its mask starts at position 16, so page 0 (tokens 0–15) is already unreachable. frontier.saturating_sub(window) / page nevertheless retains it until frontier 1040; this occurs for one step at every page boundary and can consume an unnecessary page per request or refuse otherwise admissible work under pool pressure. Derive the target from frontier - (window - 1) or an equivalent inclusive-window calculation.

Useful? React with 👍 / 👎.

Comment thread tools/accuracy/dump_gemma4_window_golden.py
@FeathBow
FeathBow force-pushed the feat/gemma4-window-eviction branch from f60224f to 90c5e99 Compare August 15, 2026 21:05
@FeathBow

Copy link
Copy Markdown
Collaborator Author

@codex review please

@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: 90c5e99291

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +265 to +267
if max_abs > tolerance {
over.push(format!("{label} ({max_abs} > {tolerance})"));
}

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 Enforce the measured top-1 agreement floor

When a window-crossing regression changes token rankings but keeps the reference tokens' log-probability deltas below the deliberately broad backend-derived tolerance, this gate still passes: score_rows computes top1, but the value is only printed and never asserted. In particular, the 4096-token case permits several log-probability units of error, so even zero top-1 matches could remain green; compare top1 against a calibrated floor such as the sdpa/eager self-agreement already available in the fixture.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Signed-off-by: Feathbow <feathbow@gmail.com>
@FeathBow
FeathBow force-pushed the feat/gemma4-window-eviction branch from 90c5e99 to 80a6148 Compare August 15, 2026 21:14
@FeathBow

Copy link
Copy Markdown
Collaborator Author

@codex review please

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 80a6148c78

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@FeathBow
FeathBow merged commit 162b503 into pegainfer-project:main Aug 15, 2026
12 of 13 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.

gemma4: serving is fail-closed at the sliding window

1 participant