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

181 lines
6.0 KiB
Go

package create
import (
"fmt"
"slices"
"strings"
)
// planBlockFP8 plans an HF block-FP8 source. MLX has no FP8 tensor type, so
// every FP8 weight is decoded to BF16 using its block scale and then quantized
// to the target (mxfp8); a weight the policy declines is still decoded and kept
// at BF16 (it is never stored as FP8). Everything else passes through at source
// precision.
func planBlockFP8(inv Inventory, target string, policy quantizePolicy) ([]BlobSpec, error) {
// The scale companion of each FP8 weight is folded into that weight's
// blob, so it is not emitted on its own.
consumed := make(map[string]bool)
for _, name := range sortedTensorNames(inv) {
if isFP8Weight(inv, name) {
if scale, ok := fp8ScaleFor(inv, name); ok {
consumed[scale] = true
}
}
}
groups := make(map[string][]SourceTensor)
fp8Groups := make(map[string][]SourceTensor)
specs := make([]BlobSpec, 0, len(inv.Tensors))
for _, name := range sortedTensorNames(inv) {
if consumed[name] {
continue
}
t := inv.Tensors[name]
if isFP8Weight(inv, name) {
// Disjoint per-expert FP8 weights are stacked, decoded, and
// quantized together by planFP8ExpertGroup; an already-stacked (3D)
// FP8 expert tensor falls through to the single-tensor decode below.
if gp, perExpert := perExpertGroup(name); perExpert {
fp8Groups[gp] = append(fp8Groups[gp], t)
continue
}
scaleName, ok := fp8ScaleFor(inv, name)
if !ok {
return nil, fmt.Errorf("fp8 weight %q has no scale companion", name)
}
specs = append(specs, BlobSpec{
Name: name,
Tensors: []TensorSpec{{
Name: name,
Sources: []SourceTensor{t, inv.Tensors[scaleName]},
Transform: TransformDecodeFP8,
Quantize: policy.quantizationType(name, t.Shape, target),
OutDtype: "BF16",
OutShape: t.Shape,
}},
})
continue
}
if gp, ok := perExpertGroup(name); ok {
groups[gp] = append(groups[gp], t)
continue
}
specs = append(specs, BlobSpec{
Name: name,
Tensors: []TensorSpec{{Name: name, Sources: []SourceTensor{t}}},
})
}
for _, gp := range sortedKeys(groups) {
groupSpecs, err := planExpertGroup(gp, groups[gp], "", policy)
if err != nil {
return nil, err
}
specs = append(specs, groupSpecs...)
}
for _, gp := range sortedKeys(fp8Groups) {
groupSpecs, err := planFP8ExpertGroup(gp, fp8Groups[gp], inv, target, policy)
if err != nil {
return nil, err
}
specs = append(specs, groupSpecs...)
}
return specs, nil
}
// planFP8ExpertGroup packs a layer's disjoint per-expert block-FP8 weights into
// one blob: the experts of each projection are stacked into [experts, out, in],
// dequantized from FP8 with their block scales, and quantized per the policy.
// The stacking, decode, and quantize all run on the MLX writer thread; the
// planner only groups and orders the source weights and their scale companions.
func planFP8ExpertGroup(groupPrefix string, tensors []SourceTensor, inv Inventory, target string, policy quantizePolicy) ([]BlobSpec, error) {
type expert struct {
weight SourceTensor
scale SourceTensor
}
byProj := make(map[string]map[int]expert)
for _, t := range tensors {
idx, proj, err := parseExpertTensor(groupPrefix, t.Name)
if err != nil {
return nil, err
}
scaleName, ok := fp8ScaleFor(inv, t.Name)
if !ok {
return nil, fmt.Errorf("fp8 expert weight %q has no scale companion", t.Name)
}
if byProj[proj] == nil {
byProj[proj] = make(map[int]expert)
}
if _, ok := byProj[proj][idx]; ok {
return nil, fmt.Errorf("expert group %s projection %s has duplicate expert index %d", groupPrefix, proj, idx)
}
byProj[proj][idx] = expert{weight: t, scale: inv.Tensors[scaleName]}
}
expertCount, err := validateExpertProjections(groupPrefix, byProj)
if err != nil {
return nil, err
}
var tensorSpecs []TensorSpec
for _, proj := range sortedKeys(byProj) {
experts := byProj[proj]
base := experts[0].weight
baseScale := experts[0].scale
// Sources are the N weights followed by the N scales, in expert order,
// matching what TransformDecodeStackFP8 expects.
sources := make([]SourceTensor, 0, 2*expertCount)
scales := make([]SourceTensor, 0, expertCount)
for i := range expertCount {
e := experts[i]
if e.weight.Dtype != base.Dtype || !slices.Equal(e.weight.Shape, base.Shape) {
return nil, fmt.Errorf("fp8 expert group %s projection %s has mismatched weight layout (%s %v vs %s %v)",
groupPrefix, proj, base.Dtype, base.Shape, e.weight.Dtype, e.weight.Shape)
}
if e.scale.Dtype != baseScale.Dtype || !slices.Equal(e.scale.Shape, baseScale.Shape) {
return nil, fmt.Errorf("fp8 expert group %s projection %s has mismatched scale layout (%s %v vs %s %v)",
groupPrefix, proj, baseScale.Dtype, baseScale.Shape, e.scale.Dtype, e.scale.Shape)
}
sources = append(sources, e.weight)
scales = append(scales, e.scale)
}
sources = append(sources, scales...)
stackedName := groupPrefix + "." + proj + ".weight"
stackedShape := append([]int32{int32(expertCount)}, base.Shape...)
tensorSpecs = append(tensorSpecs, TensorSpec{
Name: stackedName,
Sources: sources,
Transform: TransformDecodeStackFP8,
Quantize: policy.quantizationType(stackedName, stackedShape, target),
OutDtype: base.Dtype,
OutShape: stackedShape,
})
}
return homogeneousExpertBlobs(groupPrefix, tensorSpecs), nil
}
// isFP8Weight reports whether name is an F8_E4M3 weight with a block-scale
// companion (the form that must be decoded before use).
func isFP8Weight(inv Inventory, name string) bool {
t, ok := inv.Tensors[name]
if !ok || !strings.HasSuffix(name, ".weight") || !isE4M3Dtype(t.Dtype) {
return false
}
_, ok = fp8ScaleFor(inv, name)
return ok
}
// fp8ScaleFor returns the block-scale companion name for an FP8 weight,
// preferring "_scale_inv" over "_scale" (matching the source conventions).
func fp8ScaleFor(inv Inventory, weightName string) (string, bool) {
for _, suffix := range []string{"_scale_inv", "_scale"} {
if s := weightName + suffix; inv.Has(s) {
return s, true
}
}
return "", false
}