mirror of
https://github.com/ollama/ollama.git
synced 2026-09-21 13:38:14 -05:00
The MLX runner is the only Go inference runner left and is no longer experimental, so its packages leave x/. The bindings become a top-level mlx package beside the carried patches in mlx/compat, mirroring how llama/ holds the llama.cpp integration, and the runner becomes mlxrunner with the architectures nested under the package they implement. Subpackages move with their parent unless listed. x/mlxrunner/mlx mlx x/internal/mlxthread mlx/mlxthread x/internal/mlxthreadtest mlx/mlxthread/mlxthreadtest x/internal/mlxtest mlx/mlxtest x/quant mlx/quant mlx/compat/*.patch mlx/compat/mlx-c (MLX patches go in mlx/compat/mlx) x/mlxrunner mlxrunner x/models/nn mlxrunner/nn x/models/<arch> mlxrunner/model/<arch> x/mlxrunner/imports.go mlxrunner/model/architectures (new package) x/create create x/safetensors fs/safetensors x/tokenizer mlxrunner/tokenizer Every package keeps its name, so the Go changes are the import path rewrites the moves force, and the CMake, Dockerfile, CI cache keys, drift check and Darwin payload script follow the new paths. Four edits are not paths: the runner's blank architecture imports become the package mlxrunner/model/architectures, so the list to extend for a new model sits beside the architecture directories; a depguard rule keeps the two test harnesses out of non-test code, as the x/internal placement used to; the CI change filter's two entries for the long-deleted x/imagegen/mlx now name the bindings' CMake project and the carried patches, so a change to either builds the payload; and the tokenizer parity test reads its fixtures from its own testdata instead of walking out of x/. x/server and x/imagegen/manifest stay for the next two commits.
156 lines
4.0 KiB
Go
156 lines
4.0 KiB
Go
package mlx
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/ollama/ollama/mlx/mlxthread/mlxthreadtest"
|
|
)
|
|
|
|
func TestCompileFusion(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
testCompileFusion(t)
|
|
})
|
|
}
|
|
|
|
func testCompileFusion(t *mlxthreadtest.T) {
|
|
// Compile fuses the ops inside a function body into a single kernel,
|
|
// eliminating intermediate buffers. Use a diamond-shaped graph where
|
|
// two branches must be materialized simultaneously without fusion,
|
|
// then compare peak memory against the compiled version which fuses
|
|
// everything into one kernel with no intermediates.
|
|
const n = 1024 * 1024 // 4MB per float32 array
|
|
data := make([]float32, n)
|
|
for i := range data {
|
|
data[i] = float32(i + 1)
|
|
}
|
|
|
|
// Diamond: both a*b and a+b must be live for the final multiply.
|
|
// Without fusion: peak includes both intermediates (~8MB extra).
|
|
// With fusion: single kernel, no intermediates.
|
|
body := func(a, b *Array) *Array {
|
|
return a.Multiply(b).Multiply(a.Add(b))
|
|
}
|
|
|
|
a := FromValues(data, n)
|
|
b := FromValues(data, n)
|
|
|
|
// Compiled: ops fused into a single kernel.
|
|
EnableCompile()
|
|
fn := Compile2("diamond", body, Shapeless())
|
|
Scoped(func() { Eval(fn(a, b)) })
|
|
ClearCache()
|
|
ResetPeakMemory()
|
|
var compiledPeak int
|
|
Scoped(func() {
|
|
Eval(fn(a, b))
|
|
compiledPeak = PeakMemory()
|
|
})
|
|
|
|
// Uncompiled: ops evaluated individually, intermediates materialized.
|
|
ClearCache()
|
|
ResetPeakMemory()
|
|
var uncompiledPeak int
|
|
Scoped(func() {
|
|
Eval(body(a, b))
|
|
uncompiledPeak = PeakMemory()
|
|
})
|
|
|
|
if compiledPeak == 0 && uncompiledPeak == 0 {
|
|
t.Skip("peak memory tracking not available")
|
|
}
|
|
|
|
t.Logf("peak memory: compiled=%d uncompiled=%d", compiledPeak, uncompiledPeak)
|
|
|
|
if compiledPeak >= uncompiledPeak {
|
|
t.Fatalf("compilation did not reduce peak memory: compiled=%d uncompiled=%d", compiledPeak, uncompiledPeak)
|
|
}
|
|
}
|
|
|
|
func TestCompileNested(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
testCompileNested(t)
|
|
})
|
|
}
|
|
|
|
func testCompileNested(t *mlxthreadtest.T) {
|
|
// A compiled function that calls another compiled function should
|
|
// produce correct results. The inner function inlines via isTracing()
|
|
// during the outer's trace.
|
|
inner := Compile1("silu", func(a *Array) *Array {
|
|
return a.Multiply(a.Sigmoid())
|
|
}, Shapeless())
|
|
|
|
outer := Compile2("swiglu", func(gate, up *Array) *Array {
|
|
return inner(gate).Multiply(up)
|
|
}, Shapeless())
|
|
|
|
gate := FromValues([]float32{0, 1, 2}, 3)
|
|
up := FromValues([]float32{1, 1, 1}, 3)
|
|
|
|
y := outer(gate, up)
|
|
Eval(y)
|
|
|
|
// silu(x) = x * sigmoid(x); for x=0 -> 0, x=1 -> ~0.7311, x=2 -> ~1.7616
|
|
got := y.Floats()
|
|
want := []float32{0, 0.7310586, 1.7615942}
|
|
for i, v := range got {
|
|
if v-want[i] > 1e-4 || want[i]-v > 1e-4 {
|
|
t.Fatalf("got[%d]=%v want %v", i, v, want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCompileCallbackPanicRecovers(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
testCompileCallbackPanicRecovers(t)
|
|
})
|
|
}
|
|
|
|
func testCompileCallbackPanicRecovers(t *mlxthreadtest.T) {
|
|
boom := Compile1("boom", func(a *Array) *Array {
|
|
panic("intentional test panic")
|
|
})
|
|
|
|
x := FromValues([]float32{1}, 1)
|
|
|
|
defer func() {
|
|
r := recover()
|
|
if r == nil {
|
|
t.Fatal("expected panic from Call, got none")
|
|
}
|
|
if _, ok := r.(error); !ok {
|
|
t.Fatalf("expected error panic, got %T: %v", r, r)
|
|
}
|
|
}()
|
|
boom(x)
|
|
}
|
|
|
|
func TestCompileNoTrackingGrowth(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
testCompileNoTrackingGrowth(t)
|
|
})
|
|
}
|
|
|
|
func testCompileNoTrackingGrowth(t *mlxthreadtest.T) {
|
|
// Repeated invocations of a compiled kernel should not grow the
|
|
// tracked-arrays list; the callback's scope collects intermediates
|
|
// during tracing and frees them when the callback returns.
|
|
fn := Compile2("mul_add", func(a, b *Array) *Array {
|
|
return a.Multiply(b).Add(b)
|
|
})
|
|
|
|
a := FromValues([]float32{1, 2}, 2)
|
|
b := FromValues([]float32{3, 4}, 2)
|
|
|
|
before := len(currentScope.arrays)
|
|
|
|
for range 100 {
|
|
Scoped(func() { _ = fn(a, b) })
|
|
}
|
|
|
|
after := len(currentScope.arrays)
|
|
if after > before+2 {
|
|
t.Fatalf("tracked arrays grew from %d to %d across 100 calls (includes initial trace)", before, after)
|
|
}
|
|
}
|