mirror of
https://github.com/ollama/ollama.git
synced 2026-04-17 21:54:08 +02:00
This change adds a new MLX based runner which includes: * Method-based MLX bindings * Subprocess-based MLX runner (x/mlxrunner) * KV cache with tree management * A basic sampler The GLM4-MoE-Lite model has been ported to use the new bindings. --------- Co-authored-by: Michael Yang <git@mxy.ng>
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
//go:build mlx
|
|
|
|
package mlx
|
|
|
|
// #include "dynamic.h"
|
|
// #include "generated.h"
|
|
// #include <stdlib.h>
|
|
import "C"
|
|
|
|
import (
|
|
"io/fs"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"unsafe"
|
|
)
|
|
|
|
func init() {
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
|
|
case "windows":
|
|
default:
|
|
return
|
|
}
|
|
|
|
paths, ok := os.LookupEnv("OLLAMA_LIBRARY_PATH")
|
|
if !ok {
|
|
slog.Debug("OLLAMA_LIBRARY_PATH not set, skipping mlx dynamic loading")
|
|
return
|
|
}
|
|
|
|
for _, path := range filepath.SplitList(paths) {
|
|
matches, err := fs.Glob(os.DirFS(path), "libmlxc.*")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for _, match := range matches {
|
|
path := filepath.Join(paths, match)
|
|
slog.Info("Loading MLX dynamic library", "path", path)
|
|
|
|
cPath := C.CString(path)
|
|
defer C.free(unsafe.Pointer(cPath))
|
|
|
|
var handle C.mlx_dynamic_handle
|
|
if C.mlx_dynamic_load(&handle, cPath) != 0 {
|
|
slog.Error("Failed to load MLX dynamic library", "path", path)
|
|
continue
|
|
}
|
|
|
|
if C.mlx_dynamic_load_symbols(handle) != 0 {
|
|
slog.Error("Failed to load MLX dynamic library symbols", "path", path)
|
|
C.mlx_dynamic_unload(&handle)
|
|
continue
|
|
}
|
|
|
|
slog.Info("Loaded MLX dynamic library", "path", path)
|
|
return
|
|
}
|
|
}
|
|
|
|
panic("Failed to load any MLX dynamic library")
|
|
}
|