Files
ollama/mlx/gpu_kernel.go
Daniel Hiltgen 0171099db2 inkling2: speed up Inkling 2 Mini on Metal (#20127)
* mlx: export a kernel API and move Inkling's kernels into inkling2

Model packages can now define their own custom GPU kernels. Package mlx
keeps the kernel engine and exports a thin authoring layer over it:
NewKernel builds a Kernel from a KernelSpec (Metal and CUDA sources,
strided bindings, atomic outputs, a graph fallback), Run and ApplyMetal
launch it with a KernelLaunch (template arguments, outputs, grid, inputs,
an optional zero fill), and Spec returns the spec for deriving variants.
The engine's launch-argument types alias the exported ones, so a launch
converts without copying. The device queries kernels tune on
(KernelArchitecture, AppleGPU, IsAppleGPU, HasMetalTensorOps) are
exported with it.

Inkling's kernels, used only by the inkling2 model, move with their tests
from mlx into mlxrunner/model/inkling2 onto that API. Their entry points
become package-local and their kernel names gain an inkling2_ prefix. The
NVFP4 quantize tuning and the Metal rounding snippets that the moved
expert epilogue shares with mlx's activation quantizers are exported from
mlx. Tests that disabled a kernel by setting its internal state now swap
in a kernel with no backend source.

Kernel sources, launch shapes and outputs are unchanged.

* inkling2: speed up Inkling 2 Mini on Metal

Generation folds the small convolutions into the attention output, expert
sum and K/V inputs, runs the router top-k in one launch, tunes the expert
decode per GPU generation, and serves top-k sampling from a 4-bit scoring
pass plus the exact product for the leading candidates. Prompt processing
gets a restructured attention prologue, larger tiles and inline position
bias where the device plan picks them, and grouped expert GEMMs on
M2-class GPUs. Inkling also gains DFlash2 draft support. Each kernel
matches the path it replaces and falls back to it elsewhere.

* address comments

* address comments

* inkling2: run the attention projections on 8-bit weights

Read 8-bit affine attention projection and output weights (group 64)
from packages that carry them, quantized offline from the BF16 weights;
packages with BF16 weights keep the exact path, and nothing is quantized
at load. Short decode and verify tiles read the 8-bit weights through
8-bit twins of the short matmul, grouped short matmul and fused
projection-conv kernels, padded to eight rows so a row's result does not
depend on the batch width. Longer prompt tiles use MLX's quantized
matmul. The importer copies Inkling's 8-bit attention weights as is and
rejects 8-bit weights on any other tensor.

Decode reads half the projection bytes, so generation is faster and the
runner holds less memory, at a small prompt-rate cost from the wider
quantized matmul.

* advertise only Inkling's distinct thinking levels

* tighten up unit tests
2026-12-04 14:26:15 -08:00

413 lines
12 KiB
Go

package mlx
// #include <stdlib.h>
// #include "generated.h"
import "C"
import (
"log/slog"
"sync"
"unsafe"
)
// gpuSource is one backend's implementation of a kernel.
type gpuSource struct {
source string
header string
}
// gpuKernel is a custom kernel with per-backend sources and a graph
// fallback. Either backend may be absent; a backend that cannot be created
// or launched disables itself permanently. Contract checks belong to the
// caller, before run: run itself cannot fail.
type gpuKernel struct {
name string
inputs []string
outputs []string
// metal compiles when first evaluated, past any fallback, so a kernel that
// can launch on macOS 14 or 15 must compile with that runtime's Metal 3
// compiler (no lambdas). Kernels that use Metal 4 tensor ops (the M5
// neural accelerators) gate on hasMetalTensorOps.
metal gpuSource
cuda gpuSource
// Strided kernels index their inputs through <name>_shape and
// <name>_strides, so views are bound without a contiguous copy on either
// backend. A kernel that leaves this false is handed row-contiguous inputs.
strided bool
// atomicOutputs binds every Metal output as device atomic<T>, for
// kernels that accumulate across threadgroups.
atomicOutputs bool
// fallback computes the same outputs with graph ops when no GPU
// backend can run the launch.
fallback func(launch gpuLaunch) []*Array
metalOnce sync.Once
metalKernel C.mlx_fast_metal_kernel
metalDisabled bool
cudaOnce sync.Once
cudaKernel C.mlx_fast_cuda_kernel
cudaDisabled bool
}
// Launch arguments share the exported types, so a KernelLaunch converts to a
// gpuLaunch without copying.
type (
gpuDTypeArg = KernelDTypeArg
gpuIntArg = KernelIntArg
gpuOutputSpec = KernelOutput
)
// gpuLaunch is the per-call configuration for gpuKernel.run. Grid and
// thread-group units are shared across backends.
type gpuLaunch struct {
dtypes []gpuDTypeArg
ints []gpuIntArg
outputs []gpuOutputSpec
grid [3]int
threadGroup [3]int
inputs []*Array
// zeroOutputs fills every output with zero before the launch.
zeroOutputs bool
}
func cStringVector(values []string) (C.mlx_vector_string, func(), error) {
vec := C.mlx_vector_string_new()
if err := mlxError(vec); err != nil {
return C.mlx_vector_string{}, nil, err
}
for _, s := range values {
cs := C.CString(s)
err := mlxError(C.mlx_vector_string_append_value(vec, cs))
C.free(unsafe.Pointer(cs))
if err != nil {
mlxCheck(C.mlx_vector_string_free(vec))
return C.mlx_vector_string{}, nil, err
}
}
cleanup := func() {
mlxCheck(C.mlx_vector_string_free(vec))
}
return vec, cleanup, nil
}
// run executes the kernel with the first backend that works, in CUDA,
// Metal, fallback order. It panics if no variant can run the launch.
func (k *gpuKernel) run(launch gpuLaunch) []*Array {
if outs, ok := k.applyCUDA(launch); ok {
return outs
}
if outs, ok := k.applyMetal(launch); ok {
return outs
}
if k.fallback == nil {
panic("mlx: kernel " + k.name + " has no usable implementation")
}
outs := k.fallback(launch)
if len(outs) != len(k.outputs) {
panic("mlx: kernel " + k.name + " fallback returned wrong output count")
}
return outs
}
func (k *gpuKernel) disableMetal(reason string, err error) {
k.metalDisabled = true
args := []any{"kernel", k.name, "backend", "metal", "reason", reason}
if err != nil {
args = append(args, "error", err)
}
slog.Warn("custom GPU kernel backend disabled", args...)
}
func (k *gpuKernel) disableCUDA(reason string, err error) {
k.cudaDisabled = true
args := []any{"kernel", k.name, "backend", "cuda", "reason", reason}
if err != nil {
args = append(args, "error", err)
}
slog.Warn("custom GPU kernel backend disabled", args...)
}
func (k *gpuKernel) getMetal() (C.mlx_fast_metal_kernel, bool) {
k.metalOnce.Do(func() {
if !MetalIsAvailable() {
k.metalDisabled = true
return
}
if k.metal.source == "" {
k.disableMetal("no source", nil)
return
}
inputs, freeInputs, err := cStringVector(k.inputs)
if err != nil {
k.disableMetal("creating input names failed", err)
return
}
defer freeInputs()
outputs, freeOutputs, err := cStringVector(k.outputs)
if err != nil {
k.disableMetal("creating output names failed", err)
return
}
defer freeOutputs()
cName := C.CString(k.name)
defer C.free(unsafe.Pointer(cName))
cSource := C.CString(k.metal.source)
defer C.free(unsafe.Pointer(cSource))
cHeader := C.CString(k.metal.header)
defer C.free(unsafe.Pointer(cHeader))
k.metalKernel = C.mlx_fast_metal_kernel_new(
cName,
inputs,
outputs,
cSource,
cHeader,
// ensure_row_contiguous unless the kernel reads strides.
C.bool(!k.strided),
C.bool(k.atomicOutputs),
)
if err := mlxError(k.metalKernel); err != nil {
k.disableMetal("creating kernel failed", err)
}
})
return k.metalKernel, !k.metalDisabled
}
func (k *gpuKernel) applyMetal(launch gpuLaunch) ([]*Array, bool) {
if k.metalDisabled {
return nil, false
}
kernel, ok := k.getMetal()
if !ok {
return nil, false
}
cfg := C.mlx_fast_metal_kernel_config_new()
defer C.mlx_fast_metal_kernel_config_free(cfg)
if err := mlxError(cfg); err != nil {
k.disableMetal("creating config failed", err)
return nil, false
}
for _, arg := range launch.dtypes {
name := C.CString(arg.Name)
err := mlxError(C.mlx_fast_metal_kernel_config_add_template_arg_dtype(cfg, name, C.mlx_dtype(arg.DType)))
C.free(unsafe.Pointer(name))
if err != nil {
k.disableMetal("setting dtype template arg failed", err)
return nil, false
}
}
for _, arg := range launch.ints {
name := C.CString(arg.Name)
err := mlxError(C.mlx_fast_metal_kernel_config_add_template_arg_int(cfg, name, C.int(arg.Value)))
C.free(unsafe.Pointer(name))
if err != nil {
k.disableMetal("setting int template arg failed", err)
return nil, false
}
}
for _, out := range launch.outputs {
shape := make([]C.int, len(out.Shape))
for i, d := range out.Shape {
shape[i] = C.int(d)
}
if err := mlxError(C.mlx_fast_metal_kernel_config_add_output_arg(cfg, unsafe.SliceData(shape), C.size_t(len(shape)), C.mlx_dtype(out.DType))); err != nil {
k.disableMetal("adding output failed", err)
return nil, false
}
}
if launch.zeroOutputs {
if err := mlxError(C.mlx_fast_metal_kernel_config_set_init_value(cfg, C.float(0))); err != nil {
k.disableMetal("setting init value failed", err)
return nil, false
}
}
if err := mlxError(C.mlx_fast_metal_kernel_config_set_grid(cfg, C.int(launch.grid[0]), C.int(launch.grid[1]), C.int(launch.grid[2]))); err != nil {
k.disableMetal("setting grid failed", err)
return nil, false
}
if err := mlxError(C.mlx_fast_metal_kernel_config_set_thread_group(cfg, C.int(launch.threadGroup[0]), C.int(launch.threadGroup[1]), C.int(launch.threadGroup[2]))); err != nil {
k.disableMetal("setting thread group failed", err)
return nil, false
}
inputs := make([]C.mlx_array, len(launch.inputs))
for i, in := range launch.inputs {
inputs[i] = in.ctx
}
inVec := C.mlx_vector_array_new_data(unsafe.SliceData(inputs), C.size_t(len(inputs)))
if err := mlxError(inVec); err != nil {
k.disableMetal("creating input vector failed", err)
return nil, false
}
defer freeVectorArray(inVec)
outVec := C.mlx_vector_array_new()
if err := mlxError(outVec); err != nil {
k.disableMetal("creating output vector failed", err)
return nil, false
}
defer freeVectorArray(outVec)
if err := mlxError(C.mlx_fast_metal_kernel_apply(&outVec, kernel, inVec, cfg, DefaultStream().ctx)); err != nil {
k.disableMetal("launching failed", err)
return nil, false
}
if int(mlxCheck(C.mlx_vector_array_size(outVec))) < len(launch.outputs) {
return nil, false
}
outs := make([]*Array, len(launch.outputs))
for i, out := range launch.outputs {
outs[i] = New(out.Name)
mlxCheck(C.mlx_vector_array_get(&outs[i].ctx, outVec, C.size_t(i)))
}
return outs, true
}
func (k *gpuKernel) getCUDA() (C.mlx_fast_cuda_kernel, bool) {
k.cudaOnce.Do(func() {
if !CUDAIsAvailable() {
k.cudaDisabled = true
return
}
if k.cuda.source == "" {
k.disableCUDA("no source", nil)
return
}
inputs, freeInputs, err := cStringVector(k.inputs)
if err != nil {
k.disableCUDA("creating input names failed", err)
return
}
defer freeInputs()
outputs, freeOutputs, err := cStringVector(k.outputs)
if err != nil {
k.disableCUDA("creating output names failed", err)
return
}
defer freeOutputs()
cName := C.CString(k.name)
defer C.free(unsafe.Pointer(cName))
cSource := C.CString(k.cuda.source)
defer C.free(unsafe.Pointer(cSource))
cHeader := C.CString(k.cuda.header)
defer C.free(unsafe.Pointer(cHeader))
k.cudaKernel = C.mlx_fast_cuda_kernel_new(
cName,
inputs,
outputs,
cSource,
cHeader,
// ensure_row_contiguous unless the kernel reads strides.
C.bool(!k.strided),
C.int(0),
)
if err := mlxError(k.cudaKernel); err != nil {
k.disableCUDA("creating kernel failed", err)
}
})
return k.cudaKernel, !k.cudaDisabled
}
func (k *gpuKernel) applyCUDA(launch gpuLaunch) ([]*Array, bool) {
if k.cudaDisabled {
return nil, false
}
kernel, ok := k.getCUDA()
if !ok {
return nil, false
}
cfg := C.mlx_fast_cuda_kernel_config_new()
defer C.mlx_fast_cuda_kernel_config_free(cfg)
if err := mlxError(cfg); err != nil {
k.disableCUDA("creating config failed", err)
return nil, false
}
for _, arg := range launch.dtypes {
name := C.CString(arg.Name)
err := mlxError(C.mlx_fast_cuda_kernel_config_add_template_arg_dtype(cfg, name, C.mlx_dtype(arg.DType)))
C.free(unsafe.Pointer(name))
if err != nil {
k.disableCUDA("setting dtype template arg failed", err)
return nil, false
}
}
for _, arg := range launch.ints {
name := C.CString(arg.Name)
err := mlxError(C.mlx_fast_cuda_kernel_config_add_template_arg_int(cfg, name, C.int(arg.Value)))
C.free(unsafe.Pointer(name))
if err != nil {
k.disableCUDA("setting int template arg failed", err)
return nil, false
}
}
for _, out := range launch.outputs {
shape := make([]C.int, len(out.Shape))
for i, d := range out.Shape {
shape[i] = C.int(d)
}
if err := mlxError(C.mlx_fast_cuda_kernel_config_add_output_arg(cfg, unsafe.SliceData(shape), C.size_t(len(shape)), C.mlx_dtype(out.DType))); err != nil {
k.disableCUDA("adding output failed", err)
return nil, false
}
}
if launch.zeroOutputs {
if err := mlxError(C.mlx_fast_cuda_kernel_config_set_init_value(cfg, C.float(0))); err != nil {
k.disableCUDA("setting init value failed", err)
return nil, false
}
}
if err := mlxError(C.mlx_fast_cuda_kernel_config_set_grid(cfg, C.int(launch.grid[0]), C.int(launch.grid[1]), C.int(launch.grid[2]))); err != nil {
k.disableCUDA("setting grid failed", err)
return nil, false
}
if err := mlxError(C.mlx_fast_cuda_kernel_config_set_thread_group(cfg, C.int(launch.threadGroup[0]), C.int(launch.threadGroup[1]), C.int(launch.threadGroup[2]))); err != nil {
k.disableCUDA("setting thread group failed", err)
return nil, false
}
inputs := make([]C.mlx_array, len(launch.inputs))
for i, in := range launch.inputs {
inputs[i] = in.ctx
}
inVec := C.mlx_vector_array_new_data(unsafe.SliceData(inputs), C.size_t(len(inputs)))
if err := mlxError(inVec); err != nil {
k.disableCUDA("creating input vector failed", err)
return nil, false
}
defer freeVectorArray(inVec)
outVec := C.mlx_vector_array_new()
if err := mlxError(outVec); err != nil {
k.disableCUDA("creating output vector failed", err)
return nil, false
}
defer freeVectorArray(outVec)
if err := mlxError(C.mlx_fast_cuda_kernel_apply(&outVec, kernel, inVec, cfg, DefaultStream().ctx)); err != nil {
k.disableCUDA("launching failed", err)
return nil, false
}
if int(mlxCheck(C.mlx_vector_array_size(outVec))) < len(launch.outputs) {
return nil, false
}
outs := make([]*Array, len(launch.outputs))
for i, out := range launch.outputs {
outs[i] = New(out.Name)
mlxCheck(C.mlx_vector_array_get(&outs[i].ctx, outVec, C.size_t(i)))
}
return outs, true
}