Skip to content

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

4.0 KB
Raw Blame
package mlx

import (
	"testing"

	"github.com/ollama/ollama/mlx/mlxthread/mlxthreadtest"
)

func TestCompileFusion(t *testing.T) {
	withMLXThread(t, func(t *mlxthreadtest.T) {
		testCompileFusion(t)
	})
}

func testCompileFusion(t *mlxthreadtest.T) {
	// Compile fuses the ops inside a function body into a single kernel,
	// eliminating intermediate buffers. Use a diamond-shaped graph where
	// two branches must be materialized simultaneously without fusion,
	// then compare peak memory against the compiled version which fuses
	// everything into one kernel with no intermediates.
	const n = 1024 * 1024 // 4MB per float32 array
	data := make([]float32, n)
	for i := range data {
		data[i] = float32(i + 1)
	}

	// Diamond: both a*b and a+b must be live for the final multiply.
	// Without fusion: peak includes both intermediates (~8MB extra).
	// With fusion: single kernel, no intermediates.
	body := func(a, b *Array) *Array {
		return a.Multiply(b).Multiply(a.Add(b))
	}

	a := FromValues(data, n)
	b := FromValues(data, n)

	// Compiled: ops fused into a single kernel.
	EnableCompile()
	fn := Compile2("diamond", body, Shapeless())
	Scoped(func() { Eval(fn(a, b)) })
	ClearCache()
	ResetPeakMemory()
	var compiledPeak int
	Scoped(func() {
		Eval(fn(a, b))
		compiledPeak = PeakMemory()
	})

	// Uncompiled: ops evaluated individually, intermediates materialized.
	ClearCache()
	ResetPeakMemory()
	var uncompiledPeak int
	Scoped(func() {
		Eval(body(a, b))
		uncompiledPeak = PeakMemory()
	})

	if compiledPeak == 0 && uncompiledPeak == 0 {
		t.Skip("peak memory tracking not available")
	}

	t.Logf("peak memory: compiled=%d uncompiled=%d", compiledPeak, uncompiledPeak)

	if compiledPeak >= uncompiledPeak {
		t.Fatalf("compilation did not reduce peak memory: compiled=%d uncompiled=%d", compiledPeak, uncompiledPeak)
	}
}

func TestCompileNested(t *testing.T) {
	withMLXThread(t, func(t *mlxthreadtest.T) {
		testCompileNested(t)
	})
}

func testCompileNested(t *mlxthreadtest.T) {
	// A compiled function that calls another compiled function should
	// produce correct results. The inner function inlines via isTracing()
	// during the outer's trace.
	inner := Compile1("silu", func(a *Array) *Array {
		return a.Multiply(a.Sigmoid())
	}, Shapeless())

	outer := Compile2("swiglu", func(gate, up *Array) *Array {
		return inner(gate).Multiply(up)
	}, Shapeless())

	gate := FromValues([]float32{0, 1, 2}, 3)
	up := FromValues([]float32{1, 1, 1}, 3)

	y := outer(gate, up)
	Eval(y)

	// silu(x) = x * sigmoid(x); for x=0 -> 0, x=1 -> ~0.7311, x=2 -> ~1.7616
	got := y.Floats()
	want := []float32{0, 0.7310586, 1.7615942}
	for i, v := range got {
		if v-want[i] > 1e-4 || want[i]-v > 1e-4 {
			t.Fatalf("got[%d]=%v want %v", i, v, want[i])
		}
	}
}

func TestCompileCallbackPanicRecovers(t *testing.T) {
	withMLXThread(t, func(t *mlxthreadtest.T) {
		testCompileCallbackPanicRecovers(t)
	})
}

func testCompileCallbackPanicRecovers(t *mlxthreadtest.T) {
	boom := Compile1("boom", func(a *Array) *Array {
		panic("intentional test panic")
	})

	x := FromValues([]float32{1}, 1)

	defer func() {
		r := recover()
		if r == nil {
			t.Fatal("expected panic from Call, got none")
		}
		if _, ok := r.(error); !ok {
			t.Fatalf("expected error panic, got %T: %v", r, r)
		}
	}()
	boom(x)
}

func TestCompileNoTrackingGrowth(t *testing.T) {
	withMLXThread(t, func(t *mlxthreadtest.T) {
		testCompileNoTrackingGrowth(t)
	})
}

func testCompileNoTrackingGrowth(t *mlxthreadtest.T) {
	// Repeated invocations of a compiled kernel should not grow the
	// tracked-arrays list; the callback's scope collects intermediates
	// during tracing and frees them when the callback returns.
	fn := Compile2("mul_add", func(a, b *Array) *Array {
		return a.Multiply(b).Add(b)
	})

	a := FromValues([]float32{1, 2}, 2)
	b := FromValues([]float32{3, 4}, 2)

	before := len(currentScope.arrays)

	for range 100 {
		Scoped(func() { _ = fn(a, b) })
	}

	after := len(currentScope.arrays)
	if after > before+2 {
		t.Fatalf("tracked arrays grew from %d to %d across 100 calls (includes initial trace)", before, after)
	}
}