Files
Jesse Gross 2e036e7cdf mlx, mlxrunner: move the MLX engine out of x/
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.
2026-09-16 14:06:08 -07:00

75 lines
2.6 KiB
Go

package create
import (
"bytes"
"fmt"
"io"
"github.com/ollama/ollama/fs/safetensors"
)
// applyByteTransform produces a TensorSpec's output tensor from its resolved
// source tensors using only byte-level (non-MLX) operations. The MLX transform
// (decode_fp8) and quantization are handled separately by the MLX writer path.
func applyByteTransform(ts TensorSpec, sources []*safetensors.TensorData) (*safetensors.TensorData, error) {
switch ts.Transform {
case TransformNone:
if len(sources) != 1 {
return nil, fmt.Errorf("transform none expects 1 source, got %d", len(sources))
}
return sources[0].WithName(ts.Name), nil
case TransformRepackFP4, TransformRelabelU8:
// Both relabel the header (dtype, and for the fp4 repack the last
// dimension); the bytes are unchanged, so the reader is reused.
if len(sources) != 1 {
return nil, fmt.Errorf("transform %s expects 1 source, got %d", ts.Transform, len(sources))
}
td := sources[0].WithName(ts.Name)
if ts.OutDtype != "" {
td.Dtype = ts.OutDtype
}
if ts.OutShape != nil {
td.Shape = append([]int32(nil), ts.OutShape...)
}
return td, nil
case TransformScalarF32:
if len(sources) != 1 {
return nil, fmt.Errorf("transform scalar_f32 expects 1 source, got %d", len(sources))
}
return validateScalarFloat32TensorData(sources[0], ts.Name)
case TransformReciprocalF32:
if len(sources) != 1 {
return nil, fmt.Errorf("transform reciprocal_f32 expects 1 source, got %d", len(sources))
}
return invertScalarFloat32TensorData(sources[0], ts.Name)
case TransformStackExperts:
return stackExpertTensors(ts.Name, ts.OutDtype, ts.OutShape, sources)
default:
return nil, fmt.Errorf("transform %q requires the MLX writer path", ts.Transform)
}
}
// stackExpertTensors concatenates per-expert tensors (in the given order) into
// one [experts, ...] tensor. Row-major layout means the stacked bytes are
// exactly the per-expert byte blocks back to back.
func stackExpertTensors(name, dtype string, shape []int32, sources []*safetensors.TensorData) (*safetensors.TensorData, error) {
if len(sources) == 0 {
return nil, fmt.Errorf("stack_experts expects at least one source")
}
var buf bytes.Buffer
for i, s := range sources {
if s.Dtype != sources[0].Dtype {
return nil, fmt.Errorf("stack_experts source %d dtype %s != %s", i, s.Dtype, sources[0].Dtype)
}
if _, err := io.Copy(&buf, s.Reader()); err != nil {
return nil, fmt.Errorf("stack_experts read source %d (%s): %w", i, s.Name, err)
}
}
return safetensors.NewTensorDataFromBytes(name, dtype, append([]int32(nil), shape...), buf.Bytes()), nil
}