Skip to content

Commit a65a78d

Browse files
justinchubyCopilot
andcommitted
Address code-review findings: dead code and tied embeddings
* `preprocess_weights` now honours `tie_word_embeddings`. Only an explicit `language_model.lm_head.weight` populated `decoder.lm_head` before, so a tied `neo_chat` variant would leave that initializer unfilled. The released 8B checkpoint is untied, so this was latent, but the registry declares a family/variant pair that anticipates siblings. Mirrors the existing HunYuan VL-MoT handling. Covered by two new tests. * Drops the `update_cache` parameter from `_MoTAttention`, `_MoTDecoderLayer` and `_SenseNovaU1DecoderBody`. It was declared, threaded through three modules, and never read: ONNX `Attention` always emits `present = concat(past, new)`. The understanding prefix stays frozen because the *caller* discards the denoiser's present outputs, which is documented at that contract boundary in the task. Keeping a flag that implied graph-level behaviour it did not have was a trap. * Removes the unused expanded `mask` in the embedding model. `op.*` calls emit IR nodes, so `del mask` dropped only the Python name — the Unsqueeze/Expand survived until DCE. Also drops the vestigial `num_heads` argument from `_rotate_half_axis`. * Documents why GQA fusion cannot fire for either decoder graph (the block-causal bias contains an `Or`, the denoiser has no attention_mask input, and the three-axis RoPE is not `op.RotaryEmbedding`), so the CUDA/fp16 export warning is not misdiagnosed later. Verified graph-identical to the exported artifacts: all five components match the on-disk node counts exactly, and the end-to-end image is bit-identical to the pre-refactor run. Co-authored-by: Copilot <[email protected]> Signed-off-by: justinchuby <[email protected]>
1 parent 973b689 commit a65a78d

3 files changed

Lines changed: 58 additions & 14 deletions

File tree

‎docs/design/sensenova-u1-neo-unify.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,27 @@ differently under conversion:
221221
Mixing the two is supported end to end: the sampler casts the fp16 KV
222222
cache to the generation branch's dtype once, after prefill.
223223

224+
### GQA fusion does not apply
225+
226+
Both decoder graphs keep plain opset-24 `Attention` nodes after
227+
CUDA/fp16 optimization, and Mobius emits its "GQA fusion expected"
228+
warning for each. That is expected here, not a defect — three
229+
independent guards each decline for a correct reason:
230+
231+
* `decoder`'s attention bias is block-causal, so it contains an `Or`.
232+
`local_window_from_attention_bias` treats any `Or` in the bias walk as
233+
unrecognized and `AttentionToGQA` bails.
234+
* `image_gen_denoiser` has no `attention_mask` graph input at all (the
235+
generation pass is unmasked), and `AttentionToGQA` needs one to
236+
synthesize `seqlens_k`.
237+
* RoPE here is three independent axes with different bases, hand-built
238+
from Slice/Mul/Add rather than `op.RotaryEmbedding`, so
239+
`RotaryAttentionToGQA` cannot match either.
240+
241+
Closing this would need `local_window_from_attention_bias` to learn the
242+
block-causal `Or` pattern and the denoiser to gain an explicit mask
243+
input; neither is required for correctness.
244+
224245
## Runtime / metadata status
225246

226247
Export and execution are complete — all five graphs build, load, and run

‎src/mobius/models/sensenova_u1.py‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def _split_norm_rope(
422422
part_hw = norm_hw(op, part_hw)
423423

424424
cos_t, sin_t, cos_h, sin_h, cos_w, sin_w = position_embeddings
425-
part_t = _rotate_half_axis(op, part_t, cos_t, sin_t, num_heads, half)
425+
part_t = _rotate_half_axis(op, part_t, cos_t, sin_t, half)
426426
part_h = op.Slice(
427427
part_hw,
428428
op.Constant(value_ints=[0]),
@@ -435,8 +435,8 @@ def _split_norm_rope(
435435
op.Constant(value_ints=[half]),
436436
op.Constant(value_ints=[3]),
437437
)
438-
part_h = _rotate_half_axis(op, part_h, cos_h, sin_h, num_heads, quarter)
439-
part_w = _rotate_half_axis(op, part_w, cos_w, sin_w, num_heads, quarter)
438+
part_h = _rotate_half_axis(op, part_h, cos_h, sin_h, quarter)
439+
part_w = _rotate_half_axis(op, part_w, cos_w, sin_w, quarter)
440440

441441
merged = op.Concat(part_t, part_h, part_w, axis=-1)
442442
return op.Reshape(
@@ -452,7 +452,6 @@ def forward(
452452
attention_mask: ir.Value | None = None,
453453
past_key: ir.Value | None = None,
454454
past_value: ir.Value | None = None,
455-
update_cache: bool = True,
456455
):
457456
query = self._p("q_proj")(op, hidden_states)
458457
key = self._p("k_proj")(op, hidden_states)
@@ -500,7 +499,6 @@ def _rotate_half_axis(
500499
x: ir.Value,
501500
cos: ir.Value,
502501
sin: ir.Value,
503-
num_heads: int,
504502
axis_dim: int,
505503
):
506504
"""Half-split (``rotate_half``) RoPE over one axis of every head.
@@ -523,7 +521,6 @@ def _rotate_half_axis(
523521
)
524522
cos_b = op.Unsqueeze(cos, op.Constant(value_ints=[2]))
525523
sin_b = op.Unsqueeze(sin, op.Constant(value_ints=[2]))
526-
del num_heads
527524
rot_first = op.Sub(op.Mul(first, cos_b), op.Mul(second, sin_b))
528525
rot_second = op.Add(op.Mul(second, cos_b), op.Mul(first, sin_b))
529526
return op.Concat(rot_first, rot_second, axis=-1)
@@ -560,7 +557,6 @@ def forward(
560557
attention_mask: ir.Value | None = None,
561558
past_key: ir.Value | None = None,
562559
past_value: ir.Value | None = None,
563-
update_cache: bool = True,
564560
):
565561
residual = hidden_states
566562
hidden_states = getattr(self, f"input_layernorm{self.branch}")(op, hidden_states)
@@ -571,7 +567,6 @@ def forward(
571567
attention_mask=attention_mask,
572568
past_key=past_key,
573569
past_value=past_value,
574-
update_cache=update_cache,
575570
)
576571
hidden_states = op.Add(residual, hidden_states)
577572

@@ -722,7 +717,6 @@ def forward(
722717
attention_mask: ir.Value | None = None,
723718
position_ids: ir.Value | None = None,
724719
past_key_values: list | None = None,
725-
update_cache: bool = True,
726720
):
727721
position_embeddings = self.rotary_emb(op, position_ids)
728722
# Block-causal masking is intrinsic to NEO-unify: image patches that
@@ -751,7 +745,6 @@ def forward(
751745
attention_mask=attention_bias,
752746
past_key=past_key,
753747
past_value=past_value,
754-
update_cache=update_cache,
755748
)
756749
present.append((present_key, present_value))
757750
hidden_states = getattr(self, f"norm{self.branch}")(op, hidden_states)
@@ -780,8 +773,6 @@ def forward(
780773
if image_features is None:
781774
return inputs_embeds
782775
# Scatter image features at ``<IMG_CONTEXT>`` placeholder positions.
783-
mask = op.Unsqueeze(image_mask, op.Constant(value_ints=[-1]))
784-
mask = op.Expand(mask, op.Shape(inputs_embeds))
785776
flat_embeds = op.Reshape(
786777
inputs_embeds, op.Constant(value_ints=[-1, self.config.hidden_size])
787778
)
@@ -793,7 +784,6 @@ def forward(
793784
op.Constant(value_ints=[-1, 1]),
794785
)
795786
scattered = op.ScatterND(flat_embeds, indices, flat_features)
796-
del mask
797787
return op.Reshape(scattered, op.Shape(inputs_embeds))
798788

799789

@@ -921,7 +911,6 @@ def forward(
921911
attention_mask=None,
922912
position_ids=position_ids,
923913
past_key_values=past_key_values,
924-
update_cache=False,
925914
)
926915
# (B, h*w, hidden) -> (B, hidden, h, w)
927916
grid_h = op.Slice(
@@ -1022,6 +1011,14 @@ def preprocess_weights(
10221011
for key, value in state_dict.items():
10231012
for target in self._targets_for(key):
10241013
renamed[target] = value
1014+
# Tied-embedding variants ship no ``lm_head`` tensor. The released
1015+
# 8B checkpoint is untied, but the family is declared with a
1016+
# ``mot_unified`` variant so smaller tied backbones are plausible.
1017+
if self.config.tie_word_embeddings:
1018+
head = "decoder.lm_head.weight"
1019+
embed = "embedding.embed_tokens.weight"
1020+
if head not in renamed and embed in renamed:
1021+
renamed[head] = renamed[embed]
10251022
return renamed
10261023

10271024
def _targets_for(self, key: str) -> list[str]:

‎src/mobius/models/sensenova_u1_test.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,29 @@ def test_native_vlm_emission_blocks_on_the_image_processor_contract(self):
523523
# ships no preprocessor_config.json, so no processor program binds.
524524
assert "preprocessing metadata" in message
525525
assert "pixel_values" in message
526+
527+
def test_tied_embeddings_populate_the_lm_head(self):
528+
"""Tied variants ship no ``lm_head`` tensor of their own."""
529+
import torch
530+
531+
config = _tiny_config()
532+
config.tie_word_embeddings = True
533+
module = SenseNovaU1Model(config)
534+
weights = module.preprocess_weights(
535+
{"language_model.model.embed_tokens.weight": torch.zeros(VOCAB, HIDDEN)}
536+
)
537+
assert "decoder.lm_head.weight" in weights
538+
assert weights["decoder.lm_head.weight"] is weights["embedding.embed_tokens.weight"]
539+
540+
def test_untied_checkpoint_keeps_its_own_lm_head(self):
541+
import torch
542+
543+
_, module, _ = _build()
544+
head = torch.ones(VOCAB, HIDDEN)
545+
weights = module.preprocess_weights(
546+
{
547+
"language_model.model.embed_tokens.weight": torch.zeros(VOCAB, HIDDEN),
548+
"language_model.lm_head.weight": head,
549+
}
550+
)
551+
assert weights["decoder.lm_head.weight"] is head

0 commit comments

Comments
 (0)