Repository navigation
sc-8529: expose mx.fast.metal_kernel JIT as a safe Rust API - #12
Merged
michaeltrefry merged 1 commit intoJun 29, 2026
Merged
Conversation
Add a safe wrapper over MLX's custom-kernel JIT (mx.fast.metal_kernel) so hand-written MSL kernels can be reused from Rust as source strings. The underlying mlx_fast_metal_kernel* C entry points were already surfaced by the mlx-sys bindgen layer (fast.h is reachable from mlx.h, no allowlist); this is a fresh safe wrapper, not a new C binding. The API mirrors the existing fast:: wrapping pattern: - MetalKernel::new / with_options builds (and JIT-compiles on first dispatch) a kernel from name, input/output names, and MSL source. - MetalKernel::apply() returns a MetalKernelDispatch builder for inputs, output shapes/dtypes, grid/threadgroup dims, template params (dtype/int/ bool), init value, and verbose; run()/run_device() return Vec<Array>. - All C config/handles are freed; errors surface via the global mlx error handler. Tests (gated on the `metal` feature, Apple Silicon): a trivial elementwise scale (out = a*2) and a two-input add with a dtype template param, each compared against a pure-MLX-ops reference within 1e-5. Unblocks Phase 2 (epic sc-8528). RVQ (Phase 1) does not depend on this. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
michaeltrefry
deleted the
michaeltrefry1818/sc-8529/a-mlx-rs-expose-mx-fast-
branch
June 29, 2026 16:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Story
sc-8529 — A. mlx-rs — expose
mx.fast.metal_kernelJIT binding (Phase 0 enabler). Epic sc-8528.Validation finding: fresh wrapper, not a new C binding, not a cherry-pick
mlx-rs/mlx-sysformetal_kernel/MetalKernel/fast_metal— none.mlx-csubmodule exposes the fullmlx_fast_metal_kernel*family inmlx/c/fast.h(config new/free,add_output_arg,set_grid,set_thread_group,set_init_value,set_verbose,add_template_arg_{dtype,int,bool}, kernelnew/free/apply).fast.his included bymlx.h, andmlx-sysruns bindgen with no allowlist, so allmlx_fast_metal_kernel*symbols are already generated. So this is a fresh safe wrapper over existing C symbols — no new bindgen/C work, and no upstream cherry-pick needed (upstream mlx-rs has no safe wrapper either).What changed
Added a safe
MetalKernelAPI tomlx-rs/src/fast.rs, mirroring the existingfast::wrapping conventions (Array in/out,try_from_op/VectorArrayguards, errors via the global mlx error handler):MetalKernel::new(name, input_names, output_names, source)andwith_options(.., header, ensure_row_contiguous, atomic_outputs)— builds the kernel; MLX JIT-compiles + caches it on first dispatch.MetalKernel::apply()→MetalKernelDispatchbuilder:.input()/.inputs(),.output()/.output_shape(shape, dtype),.grid(x,y,z),.thread_group(x,y,z),.template_arg(name, TemplateArg::{Dtype,Int,Bool}),.init_value(),.verbose(), then.run()(default stream) /.run_device(stream)→Result<Vec<Array>>.mlx_vector_string, config, kernel) are freed; null kernel ctx is turned into anErr.This is ergonomic enough to later port
_scalar_quant.py,_bit_packing.py,_rvq_attend.py,_vecinfer.py,_rabitq.py,_comm_vq.py,_qjl.py,fused_sdpa.py.Tests
Gated on the
metalfeature (Apple Silicon), both green:test_metal_kernel_elementwise_scale— trivial MSLout = a * 2.0fround-trip vs pure-opsa * 2.0within 1e-5.test_metal_kernel_two_inputs_with_template— 2 inputs + a dtype template arg, MSLout = a + bvsa + bwithin 1e-5.cargo build,cargo test fast::tests::test_metal_kernel,cargo clippy --all-features, andcargo fmt --checkall pass. Single-mlx-rs pin invariant intact (no crate-graph changes).Follow-ups
🤖 Generated with Claude Code