Implementation:Ollama Ollama MLXRunner Array
| Knowledge Sources | |
|---|---|
| Domains | MLX Runtime, Tensor Operations |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Defines the core Array type that wraps MLX C arrays, providing Go-idiomatic constructors and utilities for creating, inspecting, and managing MLX tensors.
Description
Array holds a C-level mlx_array handle and a tensorDesc for debugging (name, inputs, reference count). Provides generic constructors: FromValue[T] for scalars (bool, int, float32, float64, complex64) and FromValues[S] for slices of any numeric type with explicit shape. Uses CGo to call MLX C API functions for array creation, setting, cloning, and property access (dtype, dims, shape, size). Includes reference-counting metadata and structured logging via slog.LogValuer.
Usage
Foundation type for the entire MLX Go binding layer. Every tensor operation, model weight, and computation result flows through this Array type.
Code Reference
Source Location
- Repository: Ollama
- File: x/mlxrunner/mlx/array.go
- Lines: 1-274
Signature
type Array struct {
ctx C.mlx_array
desc tensorDesc
}
func New(name string, inputs ...*Array) *Array
func FromValue[T scalarTypes](t T) *Array
func FromValues[S ~[]E, E arrayTypes](s S, shape ...int) *Array
func (t *Array) Set(other *Array)
func (t *Array) Clone() *Array
func (t *Array) Valid() bool
func (t *Array) DType() DType
func (t *Array) Dims() []int
func (t *Array) Dim(i int) int
func (t *Array) Size() int
func (t *Array) Int() int
func (t *Array) Float32() float32
func Free(arrays ...*Array)
Import
import "github.com/ollama/ollama/x/mlxrunner/mlx"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | Debug name for tensor tracing |
| inputs | ...*Array | No | Input arrays this tensor depends on |
Outputs
| Name | Type | Description |
|---|---|---|
| *Array | *Array | Initialized MLX array wrapper |
Usage Examples
// Create scalar
scalar := mlx.FromValue(float32(3.14))
// Create tensor from slice
data := []float32{1, 2, 3, 4, 5, 6}
tensor := mlx.FromValues(data, 2, 3) // shape [2, 3]
// Inspect properties
fmt.Println(tensor.Dims()) // [2, 3]
fmt.Println(tensor.DType()) // DTypeFloat32
// Clone and free
clone := tensor.Clone()
mlx.Free(tensor, clone)