You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f51ad25
Browse filesBrowse the repository at this point in the historyBrowse files
## What
Adds a flag-gated, bias-aware variant of the shared static-cache
`Attention` path: it can now carry an optional float additive bias
(causal + sliding-window + Gemma-4 block-overlay + padding) over the
opset-24 external KV cache (`TensorScatter` + `nonpad_kv_seqlen`), with
`is_causal=0` strictly paired to bias-present. The maskless
`is_causal=1` path (#345) stays the default and is byte-identical when
the flag is off.
This is the contract-independent, ORT-testable CORE of #349 (float-bias
decoders → near-GQA decode). It does NOT re-emit Gemma-4 itself or wire
genai — those are deliberately out of scope (see below).
## Changes
- `components/_common.py`: NEW `create_static_cache_attention_bias` —
emits a `(B,1,S_q,max_seq)` additive bias keyed on absolute query
positions (`write_indices + arange(S_q)`) over dense cache slots with
validity `slot < nonpad_kv_seqlen`, reusing the proven
`create_attention_bias` causal/sliding/block-overlay/padding rules
(re-keyed to static-cache geometry). Exported from
`components/__init__.py`.
- `components/_attention.py`: static branch now derives `(mask_arg,
is_causal)` from `attn_mask` presence — bias-present ⇒ `is_causal=0`
(strictly paired; incoming `is_causal` ignored so the two can never
disagree), maskless ⇒ `is_causal=1`. `nonpad_kv_seqlen` kept as
Attention input #6 in both modes.
- `models/base.py`: `TextModel` threads the bias when
`flags.static_cache_bias` is on AND the model has a `sliding_window` AND
static cache is active; otherwise exact prior maskless behavior.
- `_flags.py`: NEW `MOBIUS_STATIC_CACHE_BIAS` env flag (default OFF) →
`flags.static_cache_bias`.
- `tests/static_cache_bias_parity_test.py`: NEW — ORT CPU (MEA)
manual-feed parity vs an independent numpy dense-attention reference for
a prefill chunk, a decode step, a per-batch B=2 decode (write/nonpad
broadcasting), a sliding-window sweep, and a nonpad padding-clamp; plus
graph-wiring tests asserting flag-off=maskless,
flag-on+sliding=bias-threaded (is_causal=0, bias input#3, nonpad
input#6), and flag-on+non-sliding (llama) stays maskless. 10/10 on CPU;
no genai, no Flash, no onnxruntime#28958 required.
## Why flag-gated / what's deferred (Phase 2, under #349)
The bias geometry is a pure function of model semantics + cache layout,
independent of how the runtime drives the graph — so it is safe to land
before the genai consumer exists. Explicitly OUT OF SCOPE here:
re-emitting Gemma-4's own task/model on the static path,
`genai_config.json` wiring, single-graph vs prefill/decode-split
packaging, e2e generation/decode profiling, the 4D KV layout migration,
and block-overlay at non-zero write offsets (decode-time block overlay
is a no-op under the current scatter-current-chunk geometry; TODO noted
in the test file).
## Verified
- 10/10 new parity tests on CPU (ORT MEA) vs an independent numpy dense
reference; broad non-integration sanity 2927 passed / 43 skipped;
lintrunner clean (real pyproject config).
- Triple-reviewed (code + critical + readability) on the impl + a
code/critical delta pass on the review polish. Critical empirically
verified on CPU that the bias-MEA contract keeps every query row's
diagonal slot valid (no fully-masked row → no NaN edge).
## Relationships
- Closes#366
- Part of #349 (Gemma-4 end-to-end near-GQA decode consumer)
- Sibling to #345 (maskless `is_causal=1` static-cache graph) — shares
the `TensorScatter` + `nonpad_kv_seqlen` primitive; this PR adds the
bias variant.
- genai-side driving tracked separately at
microsoft/onnxruntime-genai#2204.
---------
Signed-off-by: titaiwang <[email protected]>
Co-authored-by: Copilot <[email protected]>
0 commit comments