Files
Jesse Gross 2e036e7cdf mlx, mlxrunner: move the MLX engine out of x/
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.
2026-09-16 14:06:08 -07:00

169 lines
4.6 KiB
Go

// Package mlx wraps the MLX C API.
//
// MLX keeps stream and backend state in thread-locals, so all calls into this
// package must come from a single goroutine locked to its OS thread (see
// mlx/mlxthread).
package mlx
//go:generate go run generator/main.go -output=. ./include/mlx/c/*.h
// #cgo CXXFLAGS: -std=c++17
// #cgo CPPFLAGS: -I${SRCDIR}/include
// #cgo LDFLAGS: -lstdc++
// #cgo darwin LDFLAGS: -framework Foundation -framework Metal -framework Accelerate
// #include "generated.h"
// #include <string.h>
//
// static char _mlx_last_error[1024];
//
// static void _mlx_capture_error(const char* msg, void* data) {
// (void)data;
// strncpy(_mlx_last_error, msg, sizeof(_mlx_last_error) - 1);
// }
//
// static void mlx_install_capture_handler(void) {
// if (mlx_set_error_handler_) {
// mlx_set_error_handler_(_mlx_capture_error, NULL, NULL);
// }
// }
//
// static char* mlx_last_error(void) {
// return _mlx_last_error;
// }
import "C"
import (
"errors"
"fmt"
)
func init() {
// Replace the default exit(-1) error handler with one that captures
// the error message so we can surface it in Go.
C.mlx_install_capture_handler()
}
var errBuf = C.mlx_last_error()
// lastError consumes the captured MLX error, or returns nil when none is
// pending.
func lastError() error {
if *errBuf == 0 {
return nil
}
err := fmt.Errorf("mlx: %s", C.GoString(errBuf))
*errBuf = 0
return err
}
// mlxError returns the MLX error captured by the call that produced v. mlx-c
// signals failure with a non-zero int status; a message next to a zero
// status came from an earlier unchecked call.
func mlxError[T comparable](v T) error {
var zero T
var failed, signaled bool
switch any(zero).(type) {
case C.int:
failed, signaled = v != zero, true
default:
// Only an int status signals failure. Handles, pointers, sizes, and
// dtypes are all valid at zero: a null handle is what the out-param
// constructors return, and an empty array has no data.
}
if *errBuf != 0 {
err := lastError()
if signaled && !failed {
return fmt.Errorf("mlx: unchecked error from an earlier call: %w", err)
}
return err
}
if failed {
return errors.New("mlx: call failed without an error message")
}
return nil
}
// mlxCheck panics on a failed call and otherwise passes its result through.
// Most array operations cannot recover from a failed graph construction or
// evaluation.
func mlxCheck[T comparable](v T) T {
if err := mlxError(v); err != nil {
panic(err)
}
return v
}
// Deferred frees go through these helpers: defer evaluates a call's
// arguments immediately, so defer mlxCheck(C.mlx_..._free(v)) would
// free v on the spot and defer only the check.
func freeArray(a C.mlx_array) { mlxCheck(C.mlx_array_free(a)) }
func freeString(s C.mlx_string) { mlxCheck(C.mlx_string_free(s)) }
func freeVectorArray(v C.mlx_vector_array) { mlxCheck(C.mlx_vector_array_free(v)) }
func freeClosure(c C.mlx_closure) { mlxCheck(C.mlx_closure_free(c)) }
func freeStream(s C.mlx_stream) { mlxCheck(C.mlx_stream_free(s)) }
func freeDevice(d C.mlx_device) { mlxCheck(C.mlx_device_free(d)) }
func freeDeviceInfo(i C.mlx_device_info) { mlxCheck(C.mlx_device_info_free(i)) }
func freeArrayMap(m C.mlx_map_string_to_array) {
mlxCheck(C.mlx_map_string_to_array_free(m))
}
func freeStringMap(m C.mlx_map_string_to_string) {
mlxCheck(C.mlx_map_string_to_string_free(m))
}
func freeArrayMapIterator(it C.mlx_map_string_to_array_iterator) {
mlxCheck(C.mlx_map_string_to_array_iterator_free(it))
}
// Version returns the MLX core library version string.
func Version() string {
str := mlxCheck(C.mlx_string_new())
mlxCheck(C.mlx_version(&str))
defer freeString(str)
return C.GoString(mlxCheck(C.mlx_string_data(str)))
}
func doEval(outputs []*Array, async bool) {
if len(outputs) == 0 {
return
}
vector := mlxCheck(C.mlx_vector_array_new())
defer freeVectorArray(vector)
for _, output := range outputs {
if output != nil {
mlxCheck(C.mlx_vector_array_append_value(vector, output.ctx))
}
}
if async {
mlxCheck(C.mlx_async_eval(vector))
} else {
mlxCheck(C.mlx_eval(vector))
}
}
func AsyncEval(outputs ...*Array) {
doEval(outputs, true)
}
func Eval(outputs ...*Array) {
doEval(outputs, false)
}
// MetalIsAvailable returns true if a Metal GPU is available.
func MetalIsAvailable() bool {
var available C._Bool
mlxCheck(C.mlx_metal_is_available(&available))
return bool(available)
}
// CUDAIsAvailable returns true if a CUDA GPU is available.
func CUDAIsAvailable() bool {
var available C._Bool
mlxCheck(C.mlx_cuda_is_available(&available))
return bool(available)
}