Skip to content

Commit 8a17cfb

Browse files
justinchubyCopilot
andcommitted
Make paged-attention typed refusals honest: direct declared-field access, real qk-norm gate
No-BC cleanup of the eligibility validator (no compat shims). Every field the rejection probes is a declared ArchitectureConfig field, so replace the defensive getattr(...) with direct attribute access (use_dsa, indexer_types, compress_ratios, o_lora_rank, o_groups, hc_mult, num_nextn_predict_layers, sliding_window) and the export_paged_attention reads in deepseek.py/glm_moe_dsa.py. Behavior is identical: each getattr default matched the declared default. Correctness fix (mandatory typed refusal): per-head QK-norm is consumed into the canonical attn_qk_norm / attn_qk_norm_full fields at config-extraction time (the HF use_qk_norm / qk_layernorm passthrough is not an ArchitectureConfig field, so the old check was always False and unreachable). Reject on the real fields instead — a qk-norm MLA would need the operator's q_norm/k_norm inputs, which this slice does not claim. DeepSeek-V2/V3 and GLM full-attention leave both False, so the targets are unaffected. The refusal is now reachable and tested at both component and model level. Remove the provably-dead head_sink / attention_sink branch: neither is an ArchitectureConfig field, so the guard was always False. Coverage is subsumed — the only sink-bearing MLA family (DeepSeek-V4) is already rejected by its CSA/HCA fields, a non-MLA sink model (gpt-oss) is rejected by the MLA-geometry check, and PagedLatentMLA structurally never emits a head_sink input. Documented in place; no silent-miscompute path is introduced. Tests: add component test_qk_norm_rejected (attn_qk_norm / attn_qk_norm_full) and test_window_rejected; add a model-level qk-norm typed-reject case. Full paged/GLM/DeepSeek/V4/tasks/CLI suites pass; ruff 0.16.2 clean. Co-authored-by: Copilot <[email protected]>
1 parent ee9dbdd commit 8a17cfb

5 files changed

Lines changed: 43 additions & 17 deletions

File tree

‎src/mobius/components/_paged_mla.py‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,41 +187,51 @@ def _has(name: str) -> bool:
187187
_has("index_n_heads")
188188
or _has("index_head_dim")
189189
or _has("index_topk")
190-
or bool(getattr(config, "indexer_types", None))
190+
or bool(config.indexer_types)
191191
)
192-
if getattr(config, "use_dsa", False) and _indexer_configured:
192+
if config.use_dsa and _indexer_configured:
193193
return (
194194
"PagedAttention LATENT cannot express GLM DeepSeek Sparse Attention "
195195
"(DSA/IndexShare): query-dependent sparse indices have no operator "
196196
"input. Export dense MLA (--glm-full-attention) to opt in."
197197
)
198-
if getattr(config, "compress_ratios", None):
198+
if config.compress_ratios:
199199
return (
200200
"PagedAttention LATENT cannot express DeepSeek-V4 compressed sparse "
201201
"attention (CSA/HCA): query-dependent compression is not an operator "
202202
"input."
203203
)
204-
if getattr(config, "o_lora_rank", None) or getattr(config, "o_groups", 1) not in (0, 1):
204+
if config.o_lora_rank or config.o_groups not in (0, 1):
205205
return (
206206
"PagedAttention LATENT cannot express DeepSeek-V4 grouped/low-rank "
207207
"output projection (o_groups/o_lora_rank)."
208208
)
209-
if getattr(config, "hc_mult", 1) not in (0, 1):
209+
if config.hc_mult not in (0, 1):
210210
return "PagedAttention LATENT cannot express Hyper-Connections (hc_mult > 1)."
211211

212212
# --- Multi-token prediction is out of scope. ---
213-
if getattr(config, "num_nextn_predict_layers", 0) > 0:
213+
if config.num_nextn_predict_layers > 0:
214214
return (
215215
"PagedAttention LATENT export does not cover Multi-Token Prediction "
216216
"(num_nextn_predict_layers > 0)."
217217
)
218218

219219
# --- Optional operator modes not implemented in this slice. ---
220-
if getattr(config, "attention_sink", False) or getattr(config, "head_sink", False):
221-
return "PagedAttention LATENT slice does not implement the head_sink input."
222-
if getattr(config, "use_qk_norm", False) or getattr(config, "qk_layernorm", False):
220+
# Per-head QK-norm resolves into the canonical ``attn_qk_norm`` /
221+
# ``attn_qk_norm_full`` fields (the HF ``use_qk_norm`` / ``qk_layernorm``
222+
# passthrough is consumed there at extraction time), so reject on those.
223+
# The dense-MLA targets (DeepSeek-V2/V3, GLM full-attention) leave both
224+
# False; a qk-norm MLA would need the operator's q_norm/k_norm inputs, which
225+
# this slice does not claim.
226+
if config.attn_qk_norm or config.attn_qk_norm_full:
223227
return "PagedAttention LATENT slice does not implement q_norm/k_norm inputs."
224-
window = getattr(config, "sliding_window", None)
228+
# Learned attention sinks (the operator's head_sink input) have no
229+
# ArchitectureConfig representation for dense MLA: the only sink-bearing MLA
230+
# family is DeepSeek-V4, already rejected above by its CSA/HCA fields, and a
231+
# non-MLA sink model (e.g. gpt-oss) is rejected by the MLA-geometry check.
232+
# PagedLatentMLA structurally never emits a head_sink input, so there is no
233+
# config knob to probe here.
234+
window = config.sliding_window
225235
if window is not None and window > 0:
226236
return (
227237
"PagedAttention LATENT slice does not implement windowed attention "

‎src/mobius/components/_paged_mla_test.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,18 @@ def test_deepseek_v4_csa_hca_rejected(self):
277277
def test_mtp_rejected(self):
278278
assert paged_attention_rejection(_cfg(num_nextn_predict_layers=1)) is not None
279279

280+
def test_qk_norm_rejected(self):
281+
# Per-head QK-norm resolves into attn_qk_norm / attn_qk_norm_full; a
282+
# qk-norm MLA would need the operator's q_norm/k_norm inputs, which this
283+
# slice does not claim. The dense-MLA targets leave both False.
284+
assert paged_attention_rejection(_cfg(attn_qk_norm=True)) is not None
285+
assert paged_attention_rejection(_cfg(attn_qk_norm_full=True)) is not None
286+
assert paged_attention_rejection(_cfg()) is None
287+
288+
def test_window_rejected(self):
289+
assert paged_attention_rejection(_cfg(sliding_window=64)) is not None
290+
assert paged_attention_rejection(_cfg(sliding_window=None)) is None
291+
280292
def test_quant_and_dtype_rejected(self):
281293
assert paged_attention_rejection(_cfg(dtype=ir.DataType.FLOAT)) is not None
282294
assert paged_attention_rejection(_cfg(dtype=ir.DataType.INT8)) is not None

‎src/mobius/models/deepseek.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class DeepSeekMLADecoderLayer(nn.Module):
221221
def __init__(self, config: ArchitectureConfig, is_moe: bool = False):
222222
super().__init__()
223223
linear_class = _linear_factory(config)
224-
if getattr(config, "export_paged_attention", False):
224+
if config.export_paged_attention:
225225
# Feature-on: emit LATENT PagedAttention. An ineligible geometry
226226
# raises here (typed reason) rather than silently falling back.
227227
self.self_attn = PagedLatentMLA(config, linear_class=linear_class)
@@ -408,7 +408,7 @@ def __init__(self, config: ArchitectureConfig):
408408
]
409409
)
410410
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
411-
self._export_paged_attention = getattr(config, "export_paged_attention", False)
411+
self._export_paged_attention = config.export_paged_attention
412412

413413
# For MLA, RoPE applies only to the qk_rope_head_dim portion of Q and K,
414414
# not the full head_dim. Create a modified config so the cos/sin cache
@@ -561,7 +561,7 @@ def preprocess_weights(
561561

562562
# Absorb kv_b_proj into the query/output projections for the opt-in
563563
# LATENT PagedAttention export (feature-off leaves weights untouched).
564-
if getattr(self.config, "export_paged_attention", False):
564+
if self.config.export_paged_attention:
565565
renamed = self._absorb_paged_mla_weights(renamed)
566566

567567
# Handle weight tying and GPTQ/AWQ conversion before flattening the

‎src/mobius/models/glm_moe_dsa.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def __init__(self, config: ArchitectureConfig):
651651
config = dataclasses.replace(config, indexer_types=_indexer_types(config))
652652
nn.Module.__init__(self)
653653
self.config = config
654-
if getattr(config, "export_paged_attention", False) and config.use_dsa:
654+
if config.export_paged_attention and config.use_dsa:
655655
# Feature-on must error rather than silently exporting the DSA graph.
656656
from mobius.components._paged_mla import paged_attention_rejection
657657

‎src/mobius/models/paged_mla_export_test.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
in-place-aliased latent cache IO;
1414
* feature-on with an inexpressible geometry or an *active* query-dependent
1515
sparse selection (GLM DSA, DeepSeek-V4 CSA/HCA, MTP, quantized cache,
16-
head_sink, qk-norm, sliding window) is a typed error at model construction /
16+
qk-norm, sliding window) is a typed error at model construction /
1717
build, never a silent dense fallback.
1818
1919
There is no CPU ``PagedAttention`` kernel and the base onnx checker does not
@@ -185,7 +185,7 @@ def test_deepseek_v3_emits_paged_not_name_gated(self):
185185
# DeepSeek-V3 has no indexer configured, so DSA is not active and the
186186
# paged export must still qualify (eligibility is property-based, not
187187
# gated on the vestigial use_dsa flag or the model name).
188-
assert getattr(config, "use_dsa", True) is True
188+
assert config.use_dsa is True
189189
graph = self._graph(config, model_cls=DeepSeekV3CausalLMModel)
190190
assert len(_paged_nodes(graph)) == config.num_hidden_layers
191191

@@ -284,11 +284,15 @@ def test_csa_hca_rejected_at_construction(self):
284284
DeepSeekV3CausalLMModel(config)
285285

286286
def test_optional_modes_rejected_at_construction(self):
287-
"""MTP / sliding-window are typed-rejected, not silently miscomputed."""
287+
"""MTP / qk-norm / sliding-window are typed-rejected, not miscomputed."""
288288
with pytest.raises(ValueError, match=r"Multi-Token|num_nextn"):
289289
DeepSeekV3CausalLMModel(
290290
_deepseek_config(export_paged_attention=True, num_nextn_predict_layers=1)
291291
)
292+
with pytest.raises(ValueError, match=r"q_norm|k_norm"):
293+
DeepSeekV3CausalLMModel(
294+
_deepseek_config(export_paged_attention=True, attn_qk_norm=True)
295+
)
292296
with pytest.raises(ValueError, match="window"):
293297
DeepSeekV3CausalLMModel(
294298
_deepseek_config(export_paged_attention=True, sliding_window=64)

0 commit comments

Comments
 (0)