mirror of
https://github.com/ollama/ollama.git
synced 2026-09-21 05:28:00 -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.
130 lines
2.7 KiB
Go
130 lines
2.7 KiB
Go
package mlx
|
|
|
|
// #include "generated.h"
|
|
// #include <stdlib.h>
|
|
import "C"
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"strconv"
|
|
"unsafe"
|
|
)
|
|
|
|
func (b Byte) String() string {
|
|
return strconv.FormatInt(int64(b), 10) + " B"
|
|
}
|
|
|
|
func (b KibiByte) String() string {
|
|
return strconv.FormatFloat(float64(b)/(1<<10), 'f', 2, 64) + " KiB"
|
|
}
|
|
|
|
func (b MebiByte) String() string {
|
|
return strconv.FormatFloat(float64(b)/(1<<(2*10)), 'f', 2, 64) + " MiB"
|
|
}
|
|
|
|
func (b GibiByte) String() string {
|
|
return strconv.FormatFloat(float64(b)/(1<<(3*10)), 'f', 2, 64) + " GiB"
|
|
}
|
|
|
|
func (b TebiByte) String() string {
|
|
return strconv.FormatFloat(float64(b)/(1<<(4*10)), 'f', 2, 64) + " TiB"
|
|
}
|
|
|
|
func PrettyBytes(n int) fmt.Stringer {
|
|
switch {
|
|
case n < 1<<10:
|
|
return Byte(n)
|
|
case n < 1<<(2*10):
|
|
return KibiByte(n)
|
|
case n < 1<<(3*10):
|
|
return MebiByte(n)
|
|
case n < 1<<(4*10):
|
|
return GibiByte(n)
|
|
default:
|
|
return TebiByte(n)
|
|
}
|
|
}
|
|
|
|
func ActiveMemory() int {
|
|
var active C.size_t
|
|
mlxCheck(C.mlx_get_active_memory(&active))
|
|
return int(active)
|
|
}
|
|
|
|
func CacheMemory() int {
|
|
var cache C.size_t
|
|
mlxCheck(C.mlx_get_cache_memory(&cache))
|
|
return int(cache)
|
|
}
|
|
|
|
func PeakMemory() int {
|
|
var peak C.size_t
|
|
mlxCheck(C.mlx_get_peak_memory(&peak))
|
|
return int(peak)
|
|
}
|
|
|
|
func ResetPeakMemory() {
|
|
mlxCheck(C.mlx_reset_peak_memory())
|
|
}
|
|
|
|
// MaxRecommendedWorkingSetSize returns the device's recommended upper bound
|
|
// for resident Metal allocations.
|
|
func MaxRecommendedWorkingSetSize() (int, error) {
|
|
info := mlxCheck(C.mlx_device_info_new())
|
|
if err := mlxError(C.mlx_device_info_get(&info, DefaultDevice().ctx)); err != nil {
|
|
return 0, err
|
|
}
|
|
defer freeDeviceInfo(info)
|
|
|
|
key := C.CString("max_recommended_working_set_size")
|
|
defer C.free(unsafe.Pointer(key))
|
|
|
|
var size C.size_t
|
|
rc := C.mlx_device_info_get_size(&size, info, key)
|
|
if err := lastError(); err != nil {
|
|
return 0, err
|
|
}
|
|
if rc != 0 {
|
|
// mlx-c reports a missing key with a non-zero return and no message.
|
|
return 0, fmt.Errorf("mlx: no max_recommended_working_set_size in device info")
|
|
}
|
|
return int(size), nil
|
|
}
|
|
|
|
// SetWiredLimit sets the maximum amount of Metal memory MLX keeps resident and
|
|
// returns the previous limit.
|
|
func SetWiredLimit(limit int) (int, error) {
|
|
if limit < 0 {
|
|
return 0, fmt.Errorf("mlx: wired limit must be non-negative")
|
|
}
|
|
|
|
var previous C.size_t
|
|
if err := mlxError(C.mlx_set_wired_limit(&previous, C.size_t(limit))); err != nil {
|
|
return 0, err
|
|
}
|
|
return int(previous), nil
|
|
}
|
|
|
|
type Memory struct{}
|
|
|
|
func (Memory) LogValue() slog.Value {
|
|
return slog.GroupValue(
|
|
slog.Any("active", PrettyBytes(ActiveMemory())),
|
|
slog.Any("cache", PrettyBytes(CacheMemory())),
|
|
slog.Any("peak", PrettyBytes(PeakMemory())),
|
|
)
|
|
}
|
|
|
|
type (
|
|
Byte int
|
|
KibiByte int
|
|
MebiByte int
|
|
GibiByte int
|
|
TebiByte int
|
|
)
|
|
|
|
func ClearCache() {
|
|
mlxCheck(C.mlx_clear_cache())
|
|
}
|