Skip to content

refactor: redraw ComputeBackend as an inference-session engine contract and move the MLX path behind it (byte-identical) #448

Closed

Description

@inureyes

Problem / Background

PR #446 landed a provisional ComputeBackend seam at the model-load boundary, returning the concrete MLX LoadedModel from select_backend. That seam is at the wrong level for the real targets. ADR 0004 (docs/adr/0004-compute-backend-session-seam-and-stablehlo-family.md, Proposed in PR #447) reframes the seam to the inference-session / engine level, because the actual backends we want (FuriosaAI, Tenstorrent, OpenXLA) are graph-compiler backends and the current LanguageModel::forward is itself MLX-coupled: it takes &mut [KVCache] and returns UniquePtr<MlxArray>. A load-boundary seam that hands back an MLX object cannot host a compiler backend that never produces an MlxArray.

This issue redraws the contract at the token level and moves the existing MLX path behind it as a behavior-preserving refactor. Because MLX is the only backend we have today, the redraw is fully validatable on the MLX path: it must be byte-identical, with no behavior or throughput change. This supersedes the provisional load-boundary contract introduced in #446. Follow-on from issue #338 and PR #446.

Proposed Solution

Define an inference-session / engine trait at the token level rather than at the load boundary. The session, not the caller, owns per-sequence KV state. The hot path exchanges token ids plus optional logprobs, with sampling done on-device, so a compiler backend can keep KV and sampling inside its compiled graph. The contract exposes prefill plus decode-step primitives, and the session advertises its capabilities (batching, paged KV, speculative decode, multimodal) so callers can gate on what a given backend supports.

Move the MLX path behind this contract. CxxGenerator becomes the MLX session implementation and must cover both the CLI single-sequence generate path and the server batch-scheduler path. Keep the select_backend selection skeleton and the default-off experimental-backend feature gate from PR #446, and keep the non-MLX scaffold compiling; only the contract shape changes, replacing the provisional load-boundary contract with the session/engine contract.

Design deliverables to resolve in this issue

These are open and to be decided as part of the work, not pre-decided here.

  • Whether the session owns the decode loop, or exposes step primitives that the existing server batch scheduler drives. The current leaning is step primitives, so cross-sequence batching survives and the backend keeps ownership of KV; confirm or revise during the work.
  • Synchronous vs asynchronous session API.
  • Where the trait lives: mlxcel-core vs a root src/backend module.
  • How capabilities are advertised and gated (batching, paged KV, speculative decode, multimodal).

Acceptance Criteria

  • The MLX path runs entirely behind the new session contract with no behavior change.
  • Byte-identical temp-0 greedy parity verified on a real checkpoint on the CLI generate path.
  • Byte-identical temp-0 greedy parity verified on a real checkpoint on the server path.
  • No measurable throughput regression on MLX decode.
  • With only MLX compiled (default features), the generated code adds no runtime dispatch on the hot path.
  • The server batch scheduler still drives batched decode correctly through the new contract.
  • The experimental-backend scaffold still compiles (default-off).

Technical Considerations

This supersedes the provisional contract from #446. The experimental-backend non-MLX scaffold stays default-off, so default Apple-Silicon and CUDA builds are unaffected. The session/engine seam is the contract that Track B (OpenXLA reference backend) will exercise and validate; expect reconciliation between the two tracks as the second backend reveals MLX-shaped assumptions in the trait.

Activity

  1. added
    type:refactor
    area:architecture
    priority:medium
    status:ready
    status:in-progress
    status:review
    status:done
    and removed
    status:ready
    status:in-progress
    status:review
    on Jun 26, 2026
  2. 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:architecturepriority:mediumstatus:donetype:refactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions