mirror of
https://github.com/ollama/ollama.git
synced 2026-10-21 19:11:17 +00: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.
151 lines
4.2 KiB
Go
151 lines
4.2 KiB
Go
package mlx
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/ollama/ollama/mlx/mlxthread/mlxthreadtest"
|
|
)
|
|
|
|
// A function scope frees what was created in it. What fn returns moves to the
|
|
// caller's scope instead, and a returned array the scope does not own stays
|
|
// where it is.
|
|
func TestScopeFreesWhatIsNotReturned(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
held := NewScope()
|
|
defer held.Close()
|
|
var kept, returned, dropped *Array
|
|
Scoped(func() {
|
|
kept = FromValue(1)
|
|
held.Attach(kept)
|
|
out := ScopedArrays(func() []*Array {
|
|
dropped = FromValue(2)
|
|
return []*Array{FromValue(3), nil, kept}
|
|
})
|
|
returned = out[0]
|
|
if !returned.valid() {
|
|
t.Fatal("returned array was freed with the scope that created it")
|
|
}
|
|
if dropped.valid() {
|
|
t.Fatal("array not returned survived its scope")
|
|
}
|
|
})
|
|
if returned.valid() {
|
|
t.Fatal("returned array survived the scope it was returned into")
|
|
}
|
|
if !kept.valid() {
|
|
t.Fatal("returning a held array moved it out of its scope")
|
|
}
|
|
})
|
|
}
|
|
|
|
// ScopedEval ends the build scope before it evaluates, so the intermediates
|
|
// are gone by then and the returned arrays come back evaluated.
|
|
func TestScopedEvalEvaluatesAfterBuild(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
Scoped(func() {
|
|
var tmp *Array
|
|
out := ScopedEval(func() []*Array {
|
|
tmp = FromValue(2)
|
|
return []*Array{FromValue(1).Add(tmp)}
|
|
})
|
|
if tmp.valid() {
|
|
t.Fatal("intermediate survived the build scope")
|
|
}
|
|
if !out[0].valid() || out[0].Int() != 3 {
|
|
t.Fatal("returned array was not evaluated after the build scope")
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
// A held scope keeps arrays past the function scope that created them.
|
|
// Discard frees one now, Detach hands one to the current scope, and Close
|
|
// frees what remains. A scope refuses to discard or detach an array it does
|
|
// not hold, and to attach one that is already held, by itself or another
|
|
// scope.
|
|
func TestHeldScope(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
held, other := NewScope(), NewScope()
|
|
defer other.Close()
|
|
var kept, discarded, detached *Array
|
|
Scoped(func() {
|
|
kept, discarded, detached = FromValue(1), FromValue(2), FromValue(3)
|
|
held.Attach(kept, discarded, detached)
|
|
held.Discard(discarded)
|
|
if discarded.valid() {
|
|
t.Fatal("discarded array survived")
|
|
}
|
|
Scoped(func() { held.Detach(detached) })
|
|
if detached.valid() {
|
|
t.Fatal("detached array survived the scope it was detached into")
|
|
}
|
|
if !panics(func() { other.Discard(kept) }) {
|
|
t.Fatal("no panic discarding an array the scope does not hold")
|
|
}
|
|
if !panics(func() { other.Detach(kept) }) {
|
|
t.Fatal("no panic detaching an array the scope does not hold")
|
|
}
|
|
if !panics(func() { other.Attach(kept) }) {
|
|
t.Fatal("no panic holding an array another scope holds")
|
|
}
|
|
if !panics(func() { held.Attach(kept) }) {
|
|
t.Fatal("no panic holding an array twice")
|
|
}
|
|
})
|
|
if !kept.valid() {
|
|
t.Fatal("held array was freed with the scope that created it")
|
|
}
|
|
held.Close()
|
|
if kept.valid() {
|
|
t.Fatal("held array survived its scope's close")
|
|
}
|
|
})
|
|
}
|
|
|
|
// A scope ends when its function panics, so whatever recovers is back in
|
|
// the scope it started from.
|
|
func TestScopeEndsOnPanic(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
start := currentScope
|
|
var a *Array
|
|
func() {
|
|
defer func() { _ = recover() }()
|
|
Scoped(func() {
|
|
a = FromValue(1)
|
|
panic("build failed")
|
|
})
|
|
}()
|
|
if a.valid() {
|
|
t.Fatal("array survived the scope that panicked")
|
|
}
|
|
if currentScope != start {
|
|
t.Fatal("registry not back in the caller's scope after a panic")
|
|
}
|
|
})
|
|
}
|
|
|
|
// Nothing built in a compile trace may outlive it: holding a trace array
|
|
// fails the compiled call.
|
|
func TestCompileTraceRefusesEscapes(t *testing.T) {
|
|
withMLXThread(t, func(t *mlxthreadtest.T) {
|
|
held := NewScope()
|
|
defer held.Close()
|
|
double := Compile("scope_test_escape", func(in ...*Array) []*Array {
|
|
out := in[0].Add(in[0])
|
|
held.Attach(out)
|
|
return []*Array{out}
|
|
})
|
|
defer func() {
|
|
if recover() == nil {
|
|
t.Fatal("no panic for an array held out of a compile trace")
|
|
}
|
|
}()
|
|
Scoped(func() { double(FromValue(1)) })
|
|
})
|
|
}
|
|
|
|
func panics(fn func()) (panicked bool) {
|
|
defer func() { panicked = recover() != nil }()
|
|
fn()
|
|
return false
|
|
}
|