Skip to content

feat(xla): add a prefill-from-embeddings StableHLO entry #858

Closed

Description

@inureyes

Parent and design context

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 MLX forward_with_embeddings path.
  • 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_len before compiling/invoking the module.
  • Preserve deterministic argument ordering and document it next to the emitted schema.
  • Keep the emitted text-only prefill.main byte-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 against prefill.main within 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 --lib and relevant scripts/xla/validate_arch.sh checks.

Acceptance criteria

  • prefill.main and prefill_embeddings.main coexist 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

  1. added
    status:ready
    type:enhancement
    priority:high
    area:architecture
    area:core
    on Jul 22, 2026
  2. added
    status:in-progress
    status:review
    status:done
    and removed
    status:ready
    status:in-progress
    status:review
    on Jul 22, 2026
  3. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:architecturearea:corepriority:highstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions