Repository navigation
Description
Parent and design context
- Parent epic: epic: OpenXLA multimodal / VLM architecture support (Qwen2-VL, Gemma3n, Phi4MM, Molmo, and more) #566
- Original design spike: design: scope the multimodal / VLM track (Qwen2-VL/2.5-VL/3-VL, Gemma3n, Phi4MM, Molmo, and more) #503
- Existing token prefill implementation:
src/lib/mlxcel-xla/src/emitter/model.rs
Problem
emit_prefill_with currently accepts token IDs, gathers embed[tokens], applies the architecture's embedding scale, constructs the attention mask internally, and emits logits plus KV cache. A multimodal preprocessor instead produces an already merged hidden-state sequence and, for some families, an additive attention bias. Reusing the token entry point would either re-embed the sequence or lose the multimodal mask semantics.
The new entry must coexist with prefill.main: a session needs token prefill, embeddings prefill, and decode modules at the same time. It must also preserve the embedding weight when the LM head is tied to it.
Goal
Emit a distinct prefill_embeddings.main StableHLO module that begins at the shared transformer stack while preserving the exact output/KV contract of token prefill.
Input and output contract
- Accept merged embeddings with shape
[Lp, hidden_size]in the model hidden-state dtype. - Accept position IDs,
real_len, and an explicit additive attention-bias tensor with one documented canonical shape and broadcasting rule. Padding and causal/image-attention semantics must be represented unambiguously; do not infer boolean-mask polarity. - Treat the embeddings as post-token-embedding-scale values. Skip both token lookup and
scale_embedding; otherwise text and image positions are scaled twice relative to the existing MLXforward_with_embeddingspath. - Produce the same prefill logits/argmax variant and per-layer K/V tensors, with the same dtype, ordering, and padding semantics as
prefill.main. - Keep the token embedding weight in the schema when required by a tied LM head, even though the input lookup is bypassed.
- Keep 1D text position semantics in this issue. The schema must be extendable without conflating it with the later 3D M-RoPE work.
Required implementation
- Refactor the emitter so token prefill and embeddings prefill share the layer stack, normalization, LM head, output selection, and KV output construction. Only input preparation and attention-bias construction should differ.
- Give the new module/function a stable name distinct from
prefill.main; do not replace or overload the existing entry. - Define one additive-mask convention, including the allowed dtype, finite masked value or negative infinity policy, padding rows, and shape validation.
- Reject wrong hidden size, sequence length, dtype, position shape, mask shape, or
real_lenbefore compiling/invoking the module. - Preserve deterministic argument ordering and document it next to the emitted schema.
- Keep the emitted text-only
prefill.mainbyte-identical unless a reviewed shared-core refactor makes that impossible; in that case prove semantic and token-exact equivalence and explain the textual diff.
Non-goals
- C ABI or IREE session wiring.
- Host image preprocessing or placeholder expansion.
- Vision/audio encoder StableHLO emission.
- M-RoPE or family-specific 3D positions.
Validation
- Add schema and golden-IR assertions for both prefill modules.
- Build a parity fixture that gathers token embeddings using the same checkpoint weights, supplies the corresponding causal bias to
prefill_embeddings.main, and compares final logits and every layer's K/V output againstprefill.mainwithin a documented tolerance. - Cover tied and untied LM heads, nonzero padding,
real_len = 1, and a near-capacity sequence. - Compile the new module with IREE and execute the parity fixture on an available target; emitter-string tests alone are insufficient.
- Run
cargo test -p mlxcel-xla --liband relevantscripts/xla/validate_arch.shchecks.
Acceptance criteria
-
prefill.mainandprefill_embeddings.maincoexist with stable, documented schemas. - The embeddings path performs neither token gather nor embedding scaling.
- Explicit attention bias reaches the shared attention core without polarity or padding ambiguity.
- Tied embedding/LM-head weights remain available.
- Token-derived embeddings produce equivalent logits and all-layer KV caches in a real IREE run.
- Existing text-only XLA validation remains token-exact.
Activity
feat(xla): add prefill embeddings entry