Files
ollama/create/prequant_test.go
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

184 lines
5.7 KiB
Go

package create
import (
"slices"
"testing"
)
func specByName(specs []BlobSpec, name string) (BlobSpec, bool) {
for _, s := range specs {
if s.Name == name {
return s, true
}
}
return BlobSpec{}, false
}
func inputByOutput(spec BlobSpec, outputName string) (TensorSpec, bool) {
for _, ts := range spec.Tensors {
if ts.Name == outputName {
return ts, true
}
}
return TensorSpec{}, false
}
// sourceName returns the (single) source tensor name for a TensorSpec.
func sourceName(ts TensorSpec) string {
if len(ts.Sources) == 0 {
return ""
}
return ts.Sources[0].Name
}
func specNames(specs []BlobSpec) []string {
names := make([]string, len(specs))
for i, s := range specs {
names[i] = s.Name
}
return names
}
func TestPlanPrequantizedMLX(t *testing.T) {
cfg := sourceModelConfig{Quantization: sourceQuantization{Bits: 4, Mode: "affine", GroupSize: 32}}
inv := newInventory(cfg, map[string]string{
"l.weight": "U32",
"l.scales": "BF16",
"l.biases": "BF16",
"norm.weight": "BF16",
})
specs, err := Plan(inv, Classification{Kind: SourcePrequantized}, defaultQuantPolicy{})
if err != nil {
t.Fatalf("Plan() error = %v", err)
}
// l.weight (fused with scales+biases) and norm.weight (pass-through).
if len(specs) != 2 {
t.Fatalf("got %d specs %v, want 2", len(specs), specNames(specs))
}
w, ok := specByName(specs, "l.weight")
if !ok {
t.Fatal("missing l.weight blob")
}
for _, want := range []string{"l.weight", "l.weight.scale", "l.weight.bias"} {
in, ok := inputByOutput(w, want)
if !ok {
t.Fatalf("l.weight blob missing input %q", want)
}
if in.Transform != TransformNone {
t.Errorf("%s transform = %q, want none", want, in.Transform)
}
}
if w.Metadata["quant_type"] != "int4" || w.Metadata["group_size"] != "32" {
t.Errorf("metadata = %v, want quant_type=int4 group_size=32 from config", w.Metadata)
}
if _, ok := specByName(specs, "norm.weight"); !ok {
t.Error("norm.weight should pass through as its own blob")
}
}
func TestPlanPrequantizedModelOptNVFP4(t *testing.T) {
inv := newInventory(sourceModelConfig{}, map[string]string{
"l.weight": "U8",
"l.weight_scale": "F8_E4M3",
"l.weight_scale_2": "F32",
})
specs, err := Plan(inv, Classification{Kind: SourcePrequantized}, defaultQuantPolicy{})
if err != nil {
t.Fatalf("Plan() error = %v", err)
}
if len(specs) != 1 {
t.Fatalf("got %d specs %v, want 1", len(specs), specNames(specs))
}
w := specs[0]
if w.Name != "l.weight" {
t.Fatalf("blob name = %q, want l.weight", w.Name)
}
weightIn, _ := inputByOutput(w, "l.weight")
if weightIn.Transform != TransformRepackFP4 || weightIn.OutDtype != "U32" || !slices.Equal(weightIn.OutShape, []int32{128, 32}) {
t.Errorf("weight input = %+v, want repack to U32 [128 32]", weightIn)
}
scaleIn, _ := inputByOutput(w, "l.weight.scale")
if scaleIn.Transform != TransformRelabelU8 || scaleIn.OutDtype != "U8" {
t.Errorf("scale input = %+v, want relabel to U8", scaleIn)
}
globalIn, ok := inputByOutput(w, "l.weight.global_scale")
if !ok || globalIn.Transform != TransformScalarF32 {
t.Errorf("global_scale input = %+v ok=%v, want scalar_f32 (stored as-is)", globalIn, ok)
}
if w.Metadata["quant_type"] != "nvfp4" {
t.Errorf("quant_type = %q, want nvfp4", w.Metadata["quant_type"])
}
if _, ok := w.Metadata["group_size"]; ok {
t.Errorf("ModelOpt should not default group_size: %v", w.Metadata)
}
}
func TestPlanPrequantizedModelOptDropsActivationScale(t *testing.T) {
// ModelOpt ships per-weight activation scales (.input_scale and, in some
// variants, .input_global_scale) that are unused for weight-only
// inference. They must be consumed, not emitted as their own blobs.
inv := newInventory(sourceModelConfig{}, map[string]string{
"l.weight": "U8",
"l.weight_scale": "F8_E4M3",
"l.weight_scale_2": "F32",
"l.input_scale": "F32",
"l.input_global_scale": "F32",
})
specs, err := Plan(inv, Classification{Kind: SourcePrequantized}, defaultQuantPolicy{})
if err != nil {
t.Fatalf("Plan() error = %v", err)
}
if len(specs) != 1 {
t.Fatalf("got %d specs %v, want 1 (activation scales must not become blobs)", len(specs), specNames(specs))
}
w := specs[0]
for _, act := range []string{"l.input_scale", "l.input_global_scale"} {
if _, leaked := inputByOutput(w, act); leaked {
t.Errorf("activation scale %s leaked into the fused blob", act)
}
for _, s := range specs {
if s.Name == act {
t.Errorf("activation scale %s emitted as its own blob", act)
}
}
}
}
func TestPlanPrequantizedCompressedNVFP4(t *testing.T) {
inv := newInventory(sourceModelConfig{}, map[string]string{
"l.weight_packed": "U8",
"l.weight_scale": "F8_E4M3",
"l.weight_global_scale": "F32",
"l.input_global_scale": "F32",
})
specs, err := Plan(inv, Classification{Kind: SourcePrequantized}, defaultQuantPolicy{})
if err != nil {
t.Fatalf("Plan() error = %v", err)
}
if len(specs) != 1 {
t.Fatalf("got %d specs %v, want 1 (input_global_scale must be consumed)", len(specs), specNames(specs))
}
w := specs[0]
if w.Name != "l.weight" {
t.Fatalf("blob name = %q, want l.weight", w.Name)
}
weightIn, _ := inputByOutput(w, "l.weight")
if sourceName(weightIn) != "l.weight_packed" || weightIn.Transform != TransformRepackFP4 {
t.Errorf("weight input = %+v, want source l.weight_packed repacked", weightIn)
}
globalIn, ok := inputByOutput(w, "l.weight.global_scale")
if !ok || globalIn.Transform != TransformReciprocalF32 {
t.Errorf("global_scale input = %+v ok=%v, want reciprocal_f32", globalIn, ok)
}
if w.Metadata["quant_type"] != "nvfp4" || w.Metadata["group_size"] != "16" {
t.Errorf("metadata = %v, want quant_type=nvfp4 group_size=16", w.Metadata)
}
}