Skip to content

Commit 59a595e

Browse files
justinchubyCopilot
andauthored
Add opt-in --paged-attention export for dense MLA (LATENT PagedAttention) [Slice 3B] (#599)
## Slice 3B — Mobius opt-in `--paged-attention` export (dense MLA, LATENT) Emits `com.microsoft::PagedAttention` v1 in **LATENT / absorbed-MLA** mode for property-compatible **dense MLA** (DeepSeek-V2/V3, GLM-5.2 `--glm-full-attention`). Builds on the merged onnx-genai native runtime contract: audit/typed-validator/oracle (#1940), KV index emission (#1955), CUDA LATENT kernel (#1978). ### Guarantees - **Default off.** Feature-off exports are **byte-identical** to the current dense-MLA graph (all new code is gated behind the flag/task). A byte-identical test locks this. - **Property-based eligibility, never model names.** Decided from semantic geometry: latent width, `v_head_size`, partial-RoPE suffix (`rotary_offset = kv_lora_rank`), cache dtype (fp16/bf16), and the onnx-genai-kv page/block constraints (head_size %8, kv_lora %8, rotary_dim %16, rotary_offset %8, block pow2 ≥16). - **Typed reject, never silent fallback.** Active query-dependent sparse selection (GLM **DSA/IndexShare**, DeepSeek-V4 **CSA/HCA**), **MTP**, **quantized cache**, **head_sink**, **qk-norm**, **sliding window** all raise a typed reason at model construction / build. Feature-on with an incompatible geometry errors. GLM's *vestigial* indexer config (dropped under `--glm-full-attention`) does **not** reject. - **One cache authority.** The op consumes/mutates caller-owned page buffers in place; Mobius **allocates no pages** and creates no second cache manager. `onnx-genai-kv` stays the sole authority. The graph binds caller-owned `block_table` / `slot_mapping` / cumulative + past lengths / per-layer LATENT `key_cache`, aliases the cache in place, and derives token positions from the length tensors (no `position_ids` input). ### Emitted contract - One `PagedAttention` node per layer, `_domain="com.microsoft"`, LATENT attrs (`kv_cache_layout=LATENT`, `kv_num_heads=1`, explicit `scale`, `v_head_size=kv_lora_rank`, `rotary_offset=kv_lora_rank`, `do_rotary=1`); `rotary_dim` is **derived** from `cos_cache`, never emitted. - Model inputs: `input_ids`, `block_table` (i32), `slot_mapping` (i32), `cumulative_sequence_length` (i32), `past_seqlens` (i32), per-layer `key_cache.{i}` `[num_blocks, block_size, 1, head_size]`. Outputs: `logits`, `updated_key_cache.{i}` (aliases input). - `kv_b_proj` is absorbed into the query/output projections at weight-apply time; numeric contract mirrors the onnx-genai equivalence oracle. ### Tests (same commit) - **component** (`components/_paged_mla_test.py`): eligibility rejects, absorption, numpy **decomposed-vs-absorbed LATENT parity** (rel < 1e-6), structural emission. - **model-level** (`models/paged_mla_export_test.py`): feature-off byte-identical; feature-on structural + exact op attrs/inputs/outputs/model IO; typed rejects (DSA/CSA/HCA/MTP/window); torch-tensor weight absorption; DeepSeek-V3 path proves eligibility is not name-gated. - **CLI** (`tests/cli_test.py`): `--features paged-attention` plumbing + task resolution + mutual-exclusion errors. - Existing GLM / DeepSeek / task / build-graph tests pass unchanged. ### Scope / gates - **No full-size performance claim.** Tiny correctness only. Native runtime full-size verification is separate. - Executing the emitted graph needs **ORT ≥ 1.29 CUDA** (contrib op is unknown to the base onnx checker, so model-level assertions are structural). - Avoids #578 (GGUF preflight) and #591 (BQMoE) files. **Draft — do not merge.** Independent review required (reviewer excluding Leon/Sapper; Gaff or Roy final approval). Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent d16cd4a commit 59a595e

11 files changed

Lines changed: 1741 additions & 8 deletions

File tree

‎src/mobius/__main__.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"prune-prefill-prefix": "prune_prefill_prefix",
4545
"text-only": "text_only",
4646
"glm-full-attention": "glm_full_attention",
47+
"paged-attention": "export_paged_attention",
4748
}
4849

4950

@@ -269,6 +270,23 @@ def _resolve_static_cache_task(model_type: str) -> ModelTask:
269270
}
270271
else:
271272
static_cache_params = None
273+
274+
# PagedAttention (LATENT dense-MLA) export uses the paged-cache task with
275+
# caller-owned page buffers. It is a distinct cache authority, so it cannot
276+
# be combined with the static-cache task or an explicit --task.
277+
export_paged_attention = getattr(args, "export_paged_attention", False)
278+
if export_paged_attention:
279+
if static_cache_params is not None:
280+
raise SystemExit(
281+
"Error: --features paged-attention cannot be combined with "
282+
"--features static-cache."
283+
)
284+
if task is not None:
285+
raise SystemExit(
286+
"Error: --features paged-attention cannot be combined with --task. "
287+
"Remove --task to use --features paged-attention."
288+
)
289+
task = CausalLMTask(paged_cache=True)
272290
trust_remote_code = args.trust_remote_code
273291
revision = args.revision
274292
output_dir = args.output_dir
@@ -358,6 +376,16 @@ def _resolve_static_cache_task(model_type: str) -> ModelTask:
358376
f"model_type 'glm_moe_dsa' (got '{model_type}')."
359377
)
360378
config = dataclasses.replace(config, use_dsa=False)
379+
if export_paged_attention:
380+
from mobius.components._paged_mla import paged_attention_rejection
381+
382+
config = dataclasses.replace(config, export_paged_attention=True)
383+
reason = paged_attention_rejection(config)
384+
if reason is not None:
385+
raise SystemExit(
386+
f"Error: --features paged-attention is not supported for this "
387+
f"model: {reason}"
388+
)
361389
if static_cache_params is not None:
362390
task = _resolve_static_cache_task(model_type)
363391
elif task is None:
@@ -406,6 +434,7 @@ def _resolve_static_cache_task(model_type: str) -> ModelTask:
406434
kv_cache_scales=kv_cache_scales,
407435
prune_prefill_prefix=prune_prefill_prefix,
408436
glm_full_attention=args.glm_full_attention,
437+
export_paged_attention=export_paged_attention,
409438
)
410439

411440
_save_package(pkg, output_dir, args, optimize, component_filter)

‎src/mobius/_configs/_base.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,12 @@ class ArchitectureConfig(BaseModelConfig):
503503
# (reusing ``DeepSeekV3TextModel`` unchanged) on runtimes that cannot yet
504504
# execute ``pkg.nxrt::IndexShare``.
505505
use_dsa: bool = True
506+
# Opt-in export of ``com.microsoft::PagedAttention`` (LATENT / absorbed-MLA
507+
# mode) for property-compatible dense MLA. Default off. When off, exports are
508+
# byte-identical to the current dense-MLA graph. Eligibility is decided from
509+
# semantic geometry (see ``mobius.components._paged_mla``), never model names;
510+
# an incompatible geometry raises rather than silently falling back.
511+
export_paged_attention: bool = False
506512
# Per-layer indexer schedule ("full" runs the indexer; "shared" reuses the
507513
# top-k selection from the closest preceding "full" layer). When the
508514
# checkpoint config omits this list, it is derived from

‎src/mobius/components/__init__.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@
207207
MLPMultiModalProjector,
208208
)
209209
from mobius.components._muse_glimmer_vision import MuseGlimmerVisionModel
210+
from mobius.components._paged_mla import (
211+
PagedCacheState as PagedCacheState,
212+
)
213+
from mobius.components._paged_mla import (
214+
PagedLatentMLA as PagedLatentMLA,
215+
)
216+
from mobius.components._paged_mla import (
217+
absorb_mla_weights as absorb_mla_weights,
218+
)
219+
from mobius.components._paged_mla import (
220+
mla_paged_geometry as mla_paged_geometry,
221+
)
222+
from mobius.components._paged_mla import (
223+
paged_attention_eligible as paged_attention_eligible,
224+
)
225+
from mobius.components._paged_mla import (
226+
paged_attention_rejection as paged_attention_rejection,
227+
)
210228
from mobius.components._parakeet_audio import ParakeetFastConformerEncoder
211229
from mobius.components._pixtral_vision import (
212230
Mistral3MultiModalProjector as Mistral3MultiModalProjector,

0 commit comments

Comments
 (0)