Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

MLX: version bump (#18576)

Daniel Hiltgen421d74d20e3f
diff --git a/MLX_VERSION b/MLX_VERSION
index e96224e66..4d756b2cc 100644
--- a/MLX_VERSION
+++ b/MLX_VERSION
@@ -1 +1 @@
-d9add9d11f3154111a4c85f267ec2fd307ecd18e
\ No newline at end of file
+59d600b5e64c238427d0f8d897ab7c682ef4d3d2
\ No newline at end of file
diff --git a/mlx/compat/mlx-c/0002-fast-gated-delta-update.patch b/mlx/compat/mlx-c/0002-fast-gated-delta-update.patch
new file mode 100644
index 000000000..b2a0d196c
--- /dev/null
+++ b/mlx/compat/mlx-c/0002-fast-gated-delta-update.patch
@@ -0,0 +1,63 @@
+diff --git a/mlx/c/fast.cpp b/mlx/c/fast.cpp
+index 11a6008..2054aeb 100644
+--- a/mlx/c/fast.cpp
++++ b/mlx/c/fast.cpp
+@@ -270,6 +270,37 @@ extern "C" int mlx_fast_cuda_kernel_apply(
+ return 0;
+ }
+
++extern "C" int mlx_fast_gated_delta_update(
++ mlx_vector_array* res,
++ const mlx_array queries,
++ const mlx_array keys,
++ const mlx_array values,
++ const mlx_array gates,
++ const mlx_array beta_,
++ const mlx_array initial_state /* may be null */,
++ const mlx_array mask /* may be null */,
++ const mlx_stream s) {
++ try {
++ mlx_vector_array_set_(
++ *res,
++ mlx::core::fast::gated_delta_update(
++ mlx_array_get_(queries),
++ mlx_array_get_(keys),
++ mlx_array_get_(values),
++ mlx_array_get_(gates),
++ mlx_array_get_(beta_),
++ (initial_state.ctx
++ ? std::make_optional(mlx_array_get_(initial_state))
++ : std::nullopt),
++ (mask.ctx ? std::make_optional(mlx_array_get_(mask))
++ : std::nullopt),
++ mlx_stream_get_(s)));
++ } catch (std::exception& e) {
++ mlx_error(e.what());
++ return 1;
++ }
++ return 0;
++}
+ extern "C" int mlx_fast_layer_norm(
+ mlx_array* res,
+ const mlx_array x,
+diff --git a/mlx/c/fast.h b/mlx/c/fast.h
+index 914fd2a..e4445f7 100644
+--- a/mlx/c/fast.h
++++ b/mlx/c/fast.h
+@@ -96,6 +96,16 @@ int mlx_fast_cuda_kernel_apply(
+ const mlx_fast_cuda_kernel_config config,
+ const mlx_stream stream);
+
++int mlx_fast_gated_delta_update(
++ mlx_vector_array* res,
++ const mlx_array queries,
++ const mlx_array keys,
++ const mlx_array values,
++ const mlx_array gates,
++ const mlx_array beta_,
++ const mlx_array initial_state /* may be null */,
++ const mlx_array mask /* may be null */,
++ const mlx_stream s);
+ int mlx_fast_layer_norm(
+ mlx_array* res,
+ const mlx_array x,
diff --git a/mlx/fast.go b/mlx/fast.go
index e9301514e..7d5506b37 100644
--- a/mlx/fast.go
+++ b/mlx/fast.go
@@ -30,6 +30,38 @@ type LayerNorm struct {
Bias *Array `weight:"bias"`
}
+// fastGatedDeltaUpdate applies MLX's gated-delta recurrence and returns its
+// per-token outputs and final float32 state. state and mask may be nil.
+func fastGatedDeltaUpdate(q, k, v, gates, beta, state, mask *Array) (y, nextState *Array) {
+ outVec := mlxCheck(C.mlx_vector_array_new())
+ defer freeVectorArray(outVec)
+
+ var stateCtx, maskCtx C.mlx_array
+ if state != nil {
+ stateCtx = state.ctx
+ }
+ if mask != nil {
+ maskCtx = mask.ctx
+ }
+
+ mlxCheck(C.mlx_fast_gated_delta_update(
+ &outVec,
+ q.ctx,
+ k.ctx,
+ v.ctx,
+ gates.ctx,
+ beta.ctx,
+ stateCtx,
+ maskCtx,
+ DefaultStream().ctx))
+
+ y = New("FAST_GATED_DELTA_Y")
+ nextState = New("FAST_GATED_DELTA_STATE")
+ mlxCheck(C.mlx_vector_array_get(&y.ctx, outVec, C.size_t(0)))
+ mlxCheck(C.mlx_vector_array_get(&nextState.ctx, outVec, C.size_t(1)))
+ return y, nextState
+}
+
func (r *LayerNorm) Forward(x *Array, eps float32) *Array {
out := New("FAST_LAYERNORM")
mlxCheck(C.mlx_fast_layer_norm(&out.ctx, x.ctx, r.Weight.ctx, r.Bias.ctx, C.float(eps), DefaultStream().ctx))
diff --git a/mlx/fast_gated_delta_test.go b/mlx/fast_gated_delta_test.go
new file mode 100644
index 000000000..9f849211d
--- /dev/null
+++ b/mlx/fast_gated_delta_test.go
@@ -0,0 +1,129 @@
+package mlx
+
+import (
+ "fmt"
+ "slices"
+ "testing"
+
+ "github.com/ollama/ollama/mlx/mlxthread/mlxthreadtest"
+)
+
+// gates stay in (0,1]: both paths treat them as decay factors, and negative
+// values diverge at the kernel's log clamp.
+type fastGatedDeltaInputs struct {
+ q, k, v, gates, beta, state *Array
+}
+
+func fastGatedDeltaPatternInputs(B, T, Hk, Dk, Hv, Dv int, dtype DType) fastGatedDeltaInputs {
+ return fastGatedDeltaInputs{
+ q: patternArray(dtype, []int{B, T, Hk, Dk}, 0.01, 0.003, 37, 257),
+ k: patternArray(dtype, []int{B, T, Hk, Dk}, -0.02, 0.004, 11, 101),
+ v: patternArray(dtype, []int{B, T, Hv, Dv}, 0.05, 0.002, 23, 61),
+ gates: patternArray(dtype, []int{B, T, Hv}, 0.7, 0.04, 1, 5),
+ beta: patternArray(dtype, []int{B, T, Hv}, 0.8, 0.05, 3, 7),
+ state: Zeros(DTypeFloat32, B, Hv, Dv, Dk),
+ }
+}
+
+// The supported geometries cover MLX's sequential (T<16) and chunked
+// (T>=16) kernels; the final geometry takes the graph fallback. Float32 uses
+// upstream's 1e-4 kernel tolerance, while BF16 allows one coarse output step.
+func TestFastGatedDeltaUpdateMatchesGraph(t *testing.T) {
+ cases := []struct {
+ Hk, Dk, Hv, Dv int
+ Ts []int
+ dtype DType
+ tolerance float64
+ }{
+ {Hk: 16, Dk: 128, Hv: 32, Dv: 128, Ts: []int{1, 3, 20}, dtype: DTypeFloat32, tolerance: 1e-4},
+ // Qwen3.8-27B's BF16 geometry, including batch-1 decode/MTP and
+ // prompt lengths on both sides of MLX's chunking threshold.
+ {Hk: 16, Dk: 128, Hv: 48, Dv: 128, Ts: []int{1, 11, 16, 33, 127}, dtype: DTypeBFloat16, tolerance: 0.1},
+ {Hk: 4, Dk: 64, Hv: 8, Dv: 32, Ts: []int{1, 20}, dtype: DTypeFloat32, tolerance: 1e-4},
+ }
+ for _, c := range cases {
+ for _, T := range c.Ts {
+ for _, B := range []int{1, 2} {
+ name := fmt.Sprintf("hk%d_dk%d_hv%d_dv%d_t%d_b%d", c.Hk, c.Dk, c.Hv, c.Dv, T, B)
+ t.Run(name, func(t *testing.T) {
+ withMLXThread(t, func(t *mlxthreadtest.T) {
+ in := fastGatedDeltaPatternInputs(B, T, c.Hk, c.Dk, c.Hv, c.Dv, c.dtype)
+
+ refY, refState := gatedDeltaRecurrenceGraph(in.q, in.k, in.v, in.gates, in.beta, in.state)
+ y, state := fastGatedDeltaUpdate(in.q, in.k, in.v, in.gates, in.beta, in.state, nil)
+
+ if got, want := y.Dims(), []int{B, T, c.Hv, c.Dv}; !slices.Equal(got, want) {
+ t.Fatalf("%s: y dims %v, want %v", name, got, want)
+ }
+ if got, want := state.Dims(), []int{B, c.Hv, c.Dv, c.Dk}; !slices.Equal(got, want) {
+ t.Fatalf("%s: state dims %v, want %v", name, got, want)
+ }
+ if y.DType() != c.dtype || state.DType() != DTypeFloat32 {
+ t.Fatalf("%s: dtypes y=%v state=%v, want %v/float32", name, y.DType(), state.DType(), c.dtype)
+ }
+
+ var failures []error
+ failures = appendArrayCloseError(failures, name+"/y", y.AsType(DTypeFloat32), refY.AsType(DTypeFloat32), c.tolerance)
+ failures = appendArrayCloseError(failures, name+"/state", state, refState, c.tolerance)
+ if len(failures) > 0 {
+ t.Fatal(failures[0])
+ }
+ })
+ })
+ }
+ }
+ }
+}
+
+func TestFastGatedDeltaUpdateNilState(t *testing.T) {
+ withMLXThread(t, func(t *mlxthreadtest.T) {
+ in := fastGatedDeltaPatternInputs(1, 3, 16, 128, 32, 128, DTypeFloat32)
+ yExplicit, stateExplicit := fastGatedDeltaUpdate(in.q, in.k, in.v, in.gates, in.beta, in.state, nil)
+ yNil, stateNil := fastGatedDeltaUpdate(in.q, in.k, in.v, in.gates, in.beta, nil, nil)
+ if err := requireExact("y", yNil, yExplicit); err != nil {
+ t.Fatal(err)
+ }
+ if err := requireExact("state", stateNil, stateExplicit); err != nil {
+ t.Fatal(err)
+ }
+ })
+}
+
+// A mask forces the fallback path: a padded position leaves the state
+// unchanged and emits zero output, so the final state must match a scan
+// over just the real tokens.
+func TestFastGatedDeltaUpdateMask(t *testing.T) {
+ withMLXThread(t, func(t *mlxthreadtest.T) {
+ B, T, Hk, Dk, Hv, Dv := 1, 3, 16, 128, 32, 128
+ in := fastGatedDeltaPatternInputs(B, T, Hk, Dk, Hv, Dv, DTypeFloat32)
+ mask := FromValues([]bool{true, false, true}, B, T)
+
+ y, state := fastGatedDeltaUpdate(in.q, in.k, in.v, in.gates, in.beta, in.state, mask)
+
+ yPad := SliceStartStop(y, []int32{0, 1, 0, 0}, []int32{int32(B), 2, int32(Hv), int32(Dv)})
+ if err := requireExact("masked y", yPad, Zeros(DTypeFloat32, B, 1, Hv, Dv)); err != nil {
+ t.Fatal(err)
+ }
+
+ pick := func(a *Array, ndim int, t int) *Array {
+ start := make([]int32, ndim)
+ stop := make([]int32, ndim)
+ for d := range stop {
+ stop[d] = int32(a.Dim(d))
+ }
+ start[1], stop[1] = int32(t), int32(t+1)
+ return SliceStartStop(a, start, stop)
+ }
+ cat := func(a *Array, ndim int) *Array {
+ return Concatenate([]*Array{pick(a, ndim, 0), pick(a, ndim, 2)}, 1)
+ }
+ skipState := Zeros(DTypeFloat32, B, Hv, Dv, Dk)
+ _, skipFinal := fastGatedDeltaUpdate(
+ cat(in.q, 4), cat(in.k, 4), cat(in.v, 4), cat(in.gates, 3), cat(in.beta, 3), skipState, nil)
+ // The masked run takes the fallback while the skip scan runs the kernel,
+ // so equivalence holds to tolerance, not bit-for-bit.
+ if failures := appendArrayCloseError(nil, "state", state, skipFinal, 1e-4); len(failures) > 0 {
+ t.Fatal(failures[0])
+ }
+ })
+}
diff --git a/mlx/generated.c b/mlx/generated.c
index dc96581f4..05af86e6f 100644
--- a/mlx/generated.c
+++ b/mlx/generated.c
@@ -434,6 +434,16 @@ int (*mlx_fast_cuda_kernel_apply_)(
const mlx_vector_array inputs,
const mlx_fast_cuda_kernel_config config,
const mlx_stream stream) = NULL;
+int (*mlx_fast_gated_delta_update_)(
+ mlx_vector_array* res,
+ const mlx_array queries,
+ const mlx_array keys,
+ const mlx_array values,
+ const mlx_array gates,
+ const mlx_array beta_,
+ const mlx_array initial_state /* may be null */,
+ const mlx_array mask /* may be null */,
+ const mlx_stream s) = NULL;
int (*mlx_fast_layer_norm_)(
mlx_array* res,
const mlx_array x,
@@ -2754,6 +2764,7 @@ int mlx_dynamic_load_symbols(mlx_dynamic_handle handle) {
CHECK_LOAD(handle, mlx_fast_cuda_kernel_new);
CHECK_LOAD(handle, mlx_fast_cuda_kernel_free);
CHECK_LOAD(handle, mlx_fast_cuda_kernel_apply);
+ CHECK_LOAD(handle, mlx_fast_gated_delta_update);
CHECK_LOAD(handle, mlx_fast_layer_norm);
CHECK_LOAD(handle, mlx_fast_metal_kernel_config_new);
CHECK_LOAD(handle, mlx_fast_metal_kernel_config_free);
diff --git a/mlx/generated.h b/mlx/generated.h
index 470a71411..4bc9cf58c 100644
--- a/mlx/generated.h
+++ b/mlx/generated.h
@@ -179,6 +179,7 @@
#define mlx_fast_cuda_kernel_new mlx_fast_cuda_kernel_new_mlx_gen_orig_
#define mlx_fast_cuda_kernel_free mlx_fast_cuda_kernel_free_mlx_gen_orig_
#define mlx_fast_cuda_kernel_apply mlx_fast_cuda_kernel_apply_mlx_gen_orig_
+#define mlx_fast_gated_delta_update mlx_fast_gated_delta_update_mlx_gen_orig_
#define mlx_fast_layer_norm mlx_fast_layer_norm_mlx_gen_orig_
#define mlx_fast_metal_kernel_config_new mlx_fast_metal_kernel_config_new_mlx_gen_orig_
#define mlx_fast_metal_kernel_config_free mlx_fast_metal_kernel_config_free_mlx_gen_orig_
@@ -856,6 +857,7 @@
#undef mlx_fast_cuda_kernel_new
#undef mlx_fast_cuda_kernel_free
#undef mlx_fast_cuda_kernel_apply
+#undef mlx_fast_gated_delta_update
#undef mlx_fast_layer_norm
#undef mlx_fast_metal_kernel_config_new
#undef mlx_fast_metal_kernel_config_free
@@ -1785,6 +1787,16 @@ extern int (*mlx_fast_cuda_kernel_apply_)(
const mlx_vector_array inputs,
const mlx_fast_cuda_kernel_config config,
const mlx_stream stream);
+extern int (*mlx_fast_gated_delta_update_)(
+ mlx_vector_array* res,
+ const mlx_array queries,
+ const mlx_array keys,
+ const mlx_array values,
+ const mlx_array gates,
+ const mlx_array beta_,
+ const mlx_array initial_state /* may be null */,
+ const mlx_array mask /* may be null */,
+ const mlx_stream s);
extern int (*mlx_fast_layer_norm_)(
mlx_array* res,
const mlx_array x,
@@ -4708,6 +4720,18 @@ static inline int mlx_fast_cuda_kernel_apply(
const mlx_stream stream) {
return mlx_fast_cuda_kernel_apply_(outputs, cls, inputs, config, stream);
}
+static inline int mlx_fast_gated_delta_update(
+ mlx_vector_array* res,
+ const mlx_array queries,
+ const mlx_array keys,
+ const mlx_array values,
+ const mlx_array gates,
+ const mlx_array beta_,
+ const mlx_array initial_state /* may be null */,
+ const mlx_array mask /* may be null */,
+ const mlx_stream s) {
+ return mlx_fast_gated_delta_update_(res, queries, keys, values, gates, beta_, initial_state, mask, s);
+}
static inline int mlx_fast_layer_norm(
mlx_array* res,
const mlx_array x,
diff --git a/mlx/include/mlx/c/fast.h b/mlx/include/mlx/c/fast.h
index 914fd2a28..e4445f72f 100644
--- a/mlx/include/mlx/c/fast.h
+++ b/mlx/include/mlx/c/fast.h
@@ -96,6 +96,16 @@ int mlx_fast_cuda_kernel_apply(
const mlx_fast_cuda_kernel_config config,
const mlx_stream stream);
+int mlx_fast_gated_delta_update(
+ mlx_vector_array* res,
+ const mlx_array queries,
+ const mlx_array keys,
+ const mlx_array values,
+ const mlx_array gates,
+ const mlx_array beta_,
+ const mlx_array initial_state /* may be null */,
+ const mlx_array mask /* may be null */,
+ const mlx_stream s);
int mlx_fast_layer_norm(
mlx_array* res,
const mlx_array x,