Repository navigation
Description
Background
Every model in mlxcel (src/models/*.rs) is written against MLX through the mlxcel-core cxx bridge. MLX owns the device abstraction (Metal, CUDA, CPU) and a dynamic-graph executor with its own memory planner. The current device model is effectively RuntimeDevice { Cpu, Gpu }, where Gpu resolves to Metal on macOS or CUDA behind a build feature. In all of these cases MLX performs the actual compute.
A consequence of this shape: adding an accelerator that already has an MLX backend (CUDA today, ROCm if MLX gains it) is cheap. You consume the MLX fork plus a Cargo feature and the existing model code runs unchanged. What this shape does not provide is any way to host an execution engine that MLX does not cover, where forward computation, KV cache representation, and weight loading have to happen outside MLX entirely.
Motivation
The motivating first such target is FuriosaAI's Tensor Contraction Processor (TCP, product name RNGD). Its toolchain (furiosa-opt) is not an op library in the style of cuBLAS. It is a Rust eDSL that describes the hardware mapping directly and compiles to a virtual ISA executed by the Furiosa SDK runtime. Its execution model (static shapes, manual memory placement, explicit pipeline orchestration) is the opposite of MLX's dynamic graph with an automatic memory planner. A TCP path therefore cannot route through the MLX bridge at all. It is a second forward-execution engine, not a new MLX device.
mlxcel has no backend boundary today, so before any non-MLX accelerator can be hosted, the forward path needs a seam. This issue introduces that seam and nothing else.
Public references for the motivating target:
- furiosa-opt documentation: https://developer.furiosa.ai/furiosa-opt/book
- furiosa-opt repository (Apache-2.0): https://github.com/furiosa-ai/furiosa-opt
Proposed design
Introduce a ComputeBackend trait that abstracts who executes forward(), not individual ops. The existing LanguageModel trait (mlxcel-core/src/generate.rs) is already the forward entry point. A non-MLX backend implements the same forward contract with a different execution engine behind it.
Suggested layout (illustrative, not prescriptive):
src/backend/mod.rs: theComputeBackendtrait plus backend selection logic.src/backend/mlx.rs: the existing MLX path moved behind the trait as a behavior-preserving refactor.- A separate, feature-gated crate for any non-MLX backend's kernels and runtime glue, isolated so it does not affect Apple Silicon or CUDA builds.
Backend selection is feature-gated and default off. Shipping binaries (Apple Silicon, CUDA) do not compile the extra crate, and codegen stays identical to today: when the non-MLX feature is off, the backend choice folds to a single-variant enum or a compile-time constant so there is no runtime dispatch on the hot path.
Scope (this issue)
This issue lands the seam only, backend-neutral. It does not implement any Furiosa or TCP kernels. Concretely it delivers:
- The
ComputeBackendabstraction. - Relocation of the current MLX path behind that abstraction as a behavior-preserving refactor.
- The feature-gating scaffold for an optional non-MLX backend.
The control plane is reused unchanged and confirmed to sit above the seam: downloader, safetensors parsing, tokenizer, chat template, sampling policy, the OpenAI and llama-server compatible server, the batch scheduler, and the paged-KV abstraction (block table and pool concepts).
Non-goals and cautions
- The seam must sit at the
forward()entry boundary, called once per token in decode and once per chunk in prefill. It must NOT be an op-level abstraction. An op-level seam would lose MLX graph fusion andmx.compileand add real overhead in the inner loop. - Do not type-erase hot internal types (for example KV cache tensors) behind
Box<dyn>. That pushes indirection into the inner loop. The trait is an entry boundary, not a wall across the whole stack. The MLX path keeps its concrete types exposed. - Preserve mlxcel's MLX-specific KV cache optimizations (paged KV, prompt-cache detach and adopt). Draw the trait narrowly so these specialized paths are not forced through a generic interface.
Acceptance criteria
- The MLX path runs entirely behind
ComputeBackendwith no behavior change. A temp-0 token-parity test shows byte-identical output before and after the refactor, validated on at least one real checkpoint (not synthetic only). - With the non-MLX backend feature off, generated code is equivalent to the pre-seam build (the backend choice folds away, no added runtime dispatch on the hot path).
- No measurable throughput regression on the MLX path.
Follow-ups (not implemented here)
- A separate hardware feasibility gate (go / no-go) must precede any Furiosa or TCP kernel work. It confirms the Furiosa SDK higher-level compiler path and its model coverage, and runs one GEMM on real RNGD hardware in both simulation and npu modes. That gate decides how much is handled by the SDK compiler versus hand-written virtual-ISA kernels.
- Subsequent phases (small-model end-to-end, quantized decode as the real success bar, performance tuning, multi-chip scale-out) are out of scope for this seam issue and will be tracked separately.
Activity
docs: document the ComputeBackend seam and note the concrete-return-t…
feat: introduce a ComputeBackend seam to abstract the forward-executi…
feat: introduce a ComputeBackend seam to abstract the forward-executi…