Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

5.2 KB
Raw Blame
package mlx

// #include "generated.h"
import "C"

import (
	"encoding/binary"
	"fmt"
	"log/slog"
	"reflect"
	"strings"
	"unsafe"
)

// An Array's lifetime is governed by the scope it belongs to; see scope.go.
type Array struct {
	ctx   C.mlx_array
	name  string
	scope *Scope
}

// constructor utilities

func New(name string) *Array {
	t := &Array{name: name}
	currentScope.take(t)
	return t
}

type scalarTypes interface {
	~bool | ~int | ~float32 | ~float64 | ~complex64
}

func FromValue[T scalarTypes](t T) *Array {
	tt := New("")
	switch v := any(t).(type) {
	case bool:
		tt.ctx = mlxCheck(C.mlx_array_new_bool(C.bool(v)))
	case int:
		tt.ctx = mlxCheck(C.mlx_array_new_int(C.int(v)))
	case float32:
		tt.ctx = mlxCheck(C.mlx_array_new_float32(C.float(v)))
	case float64:
		tt.ctx = mlxCheck(C.mlx_array_new_float64(C.double(v)))
	case complex64:
		tt.ctx = mlxCheck(C.mlx_array_new_complex(C.float(real(v)), C.float(imag(v))))
	default:
		panic("unsupported type")
	}
	return tt
}

type arrayTypes interface {
	~bool | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~int8 | ~int16 | ~int32 | ~int64 |
		~float32 | ~float64 |
		~complex64
}

func FromValues[S ~[]E, E arrayTypes](s S, shape ...int) *Array {
	if len(shape) == 0 {
		panic("shape must be provided for non-scalar tensors")
	}

	cShape := make([]C.int, len(shape))
	for i := range shape {
		cShape[i] = C.int(shape[i])
	}

	var dtype DType
	switch reflect.TypeOf(s).Elem().Kind() {
	case reflect.Bool:
		dtype = DTypeBool
	case reflect.Uint8:
		dtype = DTypeUint8
	case reflect.Uint16:
		dtype = DTypeUint16
	case reflect.Uint32:
		dtype = DTypeUint32
	case reflect.Uint64:
		dtype = DTypeUint64
	case reflect.Int8:
		dtype = DTypeInt8
	case reflect.Int16:
		dtype = DTypeInt16
	case reflect.Int32:
		dtype = DTypeInt32
	case reflect.Int64:
		dtype = DTypeInt64
	case reflect.Float32:
		dtype = DTypeFloat32
	case reflect.Float64:
		dtype = DTypeFloat64
	case reflect.Complex64:
		dtype = DTypeComplex64
	default:
		panic("unsupported type")
	}

	bts := make([]byte, binary.Size(s))
	if _, err := binary.Encode(bts, binary.LittleEndian, s); err != nil {
		panic(err)
	}

	tt := New("")
	tt.ctx = mlxCheck(C.mlx_array_new_data(unsafe.Pointer(&bts[0]), unsafe.SliceData(cShape), C.int(len(cShape)), C.mlx_dtype(dtype)))
	return tt
}

func (t *Array) Set(other *Array) {
	mlxCheck(C.mlx_array_set(&t.ctx, other.ctx))
}

func (t *Array) Clone() *Array {
	tt := New(t.name)
	mlxCheck(C.mlx_array_set(&tt.ctx, t.ctx))
	return tt
}

// misc. utilities

// valid reports whether t still refers to an array: false once its scope
// freed it.
func (t *Array) valid() bool {
	return t.ctx.ctx != nil
}

func (t *Array) free() {
	mlxCheck(C.mlx_array_free(t.ctx))
	t.ctx.ctx = nil
}

func (t *Array) String() string {
	str := mlxCheck(C.mlx_string_new())
	mlxCheck(C.mlx_array_tostring(&str, t.ctx))
	defer freeString(str)
	return strings.TrimSpace(C.GoString(mlxCheck(C.mlx_string_data(str))))
}

func (t *Array) LogValue() slog.Value {
	attrs := []slog.Attr{
		slog.String("name", t.name),
	}
	if t.valid() {
		attrs = append(attrs,
			slog.Any("dtype", t.DType()),
			slog.Any("shape", t.Dims()),
			slog.Int("num_bytes", t.NumBytes()),
		)
	}
	return slog.GroupValue(attrs...)
}

// shape utilities

func (t *Array) Size() int {
	return int(mlxCheck(C.mlx_array_size(t.ctx)))
}

func (t *Array) NumBytes() int {
	return int(mlxCheck(C.mlx_array_nbytes(t.ctx)))
}

func (t *Array) NumDims() int {
	return int(mlxCheck(C.mlx_array_ndim(t.ctx)))
}

func (t *Array) Dims() []int {
	dims := make([]int, t.NumDims())
	for i := range dims {
		dims[i] = t.Dim(i)
	}

	return dims
}

func (t *Array) Dim(dim int) int {
	n := C.mlx_array_dim(t.ctx, C.int(dim))
	if err := lastError(); err != nil {
		panic(err)
	}
	return int(n)
}

func (t *Array) DType() DType {
	return DType(mlxCheck(C.mlx_array_dtype(t.ctx)))
}

// data utilities

func (t *Array) Int() int32 {
	if dt := t.DType(); dt != DTypeInt32 {
		panic(fmt.Sprintf("mlx: Int requires a DTypeInt32 array, got %v", dt))
	}
	var item C.int32_t
	mlxCheck(C.mlx_array_item_int32(&item, t.ctx))
	return int32(item)
}

func (t *Array) Float() float32 {
	if dt := t.DType(); dt != DTypeFloat32 {
		panic(fmt.Sprintf("mlx: Float requires a DTypeFloat32 array, got %v", dt))
	}
	var item C.float
	mlxCheck(C.mlx_array_item_float32(&item, t.ctx))
	return float32(item)
}

func (t *Array) Ints() []int32 {
	if dt := t.DType(); dt != DTypeInt32 {
		panic(fmt.Sprintf("mlx: Ints requires DTypeInt32, got %v", dt))
	}
	Eval(t)
	data := mlxCheck(C.mlx_array_data_int32(t.ctx))
	ints := make([]int32, t.Size())
	copy(ints, unsafe.Slice((*int32)(unsafe.Pointer(data)), len(ints)))
	return ints
}

func (t *Array) Floats() []float32 {
	if dt := t.DType(); dt != DTypeFloat32 {
		panic(fmt.Sprintf("mlx: Floats requires DTypeFloat32, got %v", dt))
	}
	Eval(t)
	data := mlxCheck(C.mlx_array_data_float32(t.ctx))
	floats := make([]float32, t.Size())
	copy(floats, unsafe.Slice((*float32)(unsafe.Pointer(data)), len(floats)))
	return floats
}

func (t *Array) Save(name string) error {
	cName := C.CString(name)
	defer C.free(unsafe.Pointer(cName))
	if err := mlxError(C.mlx_save(cName, t.ctx)); err != nil {
		return fmt.Errorf("failed to save array to %s: %w", name, err)
	}
	return nil
}