mirror of
https://github.com/ollama/ollama.git
synced 2026-09-23 07:05:22 -04: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.
212 lines
4.1 KiB
Go
212 lines
4.1 KiB
Go
// Package mlxthreadtest runs tests on a persistent MLX worker thread.
|
|
package mlxthreadtest
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/ollama/ollama/mlx/mlxthread"
|
|
)
|
|
|
|
// Thread is a pinned worker used by MLX tests.
|
|
type Thread struct {
|
|
worker *mlxthread.Thread
|
|
id uint64
|
|
}
|
|
|
|
// Start creates a pinned test worker.
|
|
func Start(name string, init func() error) (*Thread, error) {
|
|
t := &Thread{}
|
|
thread, err := mlxthread.Start(name, func() error {
|
|
t.id = currentThreadID()
|
|
if init != nil {
|
|
return init()
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
t.worker = thread
|
|
return t, nil
|
|
}
|
|
|
|
// Do runs fn on the pinned worker.
|
|
func (t *Thread) Do(ctx context.Context, fn func() error) error {
|
|
if t.onWorker() {
|
|
panic("mlxthreadtest.Thread.Do called from its pinned worker")
|
|
}
|
|
return t.worker.Do(ctx, fn)
|
|
}
|
|
|
|
// Stop shuts down the pinned worker after running cleanup on it.
|
|
func (t *Thread) Stop(ctx context.Context, cleanup func()) error {
|
|
if t.onWorker() {
|
|
panic("mlxthreadtest.Thread.Stop called from its pinned worker")
|
|
}
|
|
return t.worker.Stop(ctx, cleanup)
|
|
}
|
|
|
|
func (t *Thread) onWorker() bool {
|
|
id := currentThreadID()
|
|
return id != 0 && id == t.id
|
|
}
|
|
|
|
// T is the subset of testing.T supported by MLX test bodies. Operations that
|
|
// end a test are replayed by Run on the test goroutine so the MLX worker remains
|
|
// alive.
|
|
type T struct {
|
|
testReporter
|
|
cleanups []func()
|
|
skipped bool
|
|
aborted bool
|
|
}
|
|
|
|
// abortPanic unwinds a test body without terminating the worker goroutine.
|
|
var abortPanic = new(struct{ marker byte })
|
|
|
|
type testReporter interface {
|
|
Error(...any)
|
|
Errorf(string, ...any)
|
|
Fail()
|
|
Failed() bool
|
|
Helper()
|
|
Log(...any)
|
|
Logf(string, ...any)
|
|
}
|
|
|
|
// Run executes fn on thread. The callback must use its T argument; calling
|
|
// FailNow or SkipNow on a captured *testing.T terminates the pinned worker.
|
|
func Run(t *testing.T, thread *Thread, fn func(*T)) {
|
|
t.Helper()
|
|
if thread.onWorker() {
|
|
panic("mlxthreadtest.Run called recursively from its pinned worker")
|
|
}
|
|
|
|
mt := &T{testReporter: t}
|
|
result := make(chan runResult, 1)
|
|
go func() {
|
|
defer func() {
|
|
if v := recover(); v != nil {
|
|
result <- runResult{panicValue: v}
|
|
}
|
|
}()
|
|
err := thread.Do(context.Background(), func() error {
|
|
returned := false
|
|
defer func() {
|
|
if v := recover(); v != nil {
|
|
panic(v)
|
|
}
|
|
if !returned {
|
|
result <- runResult{goexit: true}
|
|
}
|
|
}()
|
|
|
|
mt.run(fn)
|
|
returned = true
|
|
return nil
|
|
})
|
|
result <- runResult{err: err}
|
|
}()
|
|
|
|
res := <-result
|
|
if res.goexit {
|
|
panic("pinned test body called runtime.Goexit; use the test value passed to the callback")
|
|
}
|
|
if res.panicValue != nil {
|
|
panic(res.panicValue)
|
|
}
|
|
if res.err != nil {
|
|
t.Fatal(res.err)
|
|
}
|
|
if mt.skipped {
|
|
t.SkipNow()
|
|
}
|
|
if mt.aborted {
|
|
t.FailNow()
|
|
}
|
|
}
|
|
|
|
type runResult struct {
|
|
err error
|
|
panicValue any
|
|
goexit bool
|
|
}
|
|
|
|
// Cleanup registers fn to run on the MLX thread after the current body.
|
|
func (t *T) Cleanup(fn func()) {
|
|
t.cleanups = append(t.cleanups, fn)
|
|
}
|
|
|
|
func (t *T) FailNow() {
|
|
t.Fail()
|
|
t.aborted = true
|
|
panic(abortPanic)
|
|
}
|
|
|
|
func (t *T) Fatal(args ...any) {
|
|
t.Helper()
|
|
t.Error(args...)
|
|
t.aborted = true
|
|
panic(abortPanic)
|
|
}
|
|
|
|
func (t *T) Fatalf(format string, args ...any) {
|
|
t.Helper()
|
|
t.Errorf(format, args...)
|
|
t.aborted = true
|
|
panic(abortPanic)
|
|
}
|
|
|
|
func (t *T) Skip(args ...any) {
|
|
t.Helper()
|
|
t.Log(args...)
|
|
t.SkipNow()
|
|
}
|
|
|
|
func (t *T) Skipf(format string, args ...any) {
|
|
t.Helper()
|
|
t.Logf(format, args...)
|
|
t.SkipNow()
|
|
}
|
|
|
|
func (t *T) SkipNow() {
|
|
t.skipped = true
|
|
t.aborted = true
|
|
panic(abortPanic)
|
|
}
|
|
|
|
func (t *T) Skipped() bool {
|
|
return t.skipped
|
|
}
|
|
|
|
func (t *T) run(fn func(*T)) {
|
|
defer func() {
|
|
if v := recover(); v != nil && v != abortPanic {
|
|
panic(v)
|
|
}
|
|
}()
|
|
defer t.runCleanups()
|
|
fn(t)
|
|
}
|
|
|
|
func (t *T) runCleanups() {
|
|
var panicValue any
|
|
for len(t.cleanups) > 0 {
|
|
last := len(t.cleanups) - 1
|
|
cleanup := t.cleanups[last]
|
|
t.cleanups = t.cleanups[:last]
|
|
func() {
|
|
defer func() {
|
|
if v := recover(); v != nil && v != abortPanic && panicValue == nil {
|
|
panicValue = v
|
|
}
|
|
}()
|
|
cleanup()
|
|
}()
|
|
}
|
|
if panicValue != nil {
|
|
panic(panicValue)
|
|
}
|
|
}
|