coremlcompiler

package
v0.6.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package coremlcompiler compiles CoreML model packages (.mlpackage) into compiled model bundles (.mlmodelc) without requiring xcrun or Apple's proprietary coremlcompiler binary.

Only mlprogram models (spec version 5+) are supported. Legacy NeuralNetwork models must first be converted to mlprogram format using coremltools (convert_to="mlprogram").

The compilation path converts mlprogram models: the protobuf-encoded MIL program is serialized to MIL text, weights are copied byte-for-byte, and a coremldata.bin header is generated. The result can be loaded directly by CoreML, the ANE runtime, or x/ane.

Three entry points cover different use cases:

Compile takes a .mlpackage directory or .mlmodel file on disk and produces a compiled .mlmodelc bundle. This is the simplest path and handles reading the protobuf, locating weights, and writing all output files:

err := coremlcompiler.Compile("model.mlpackage", "model.mlmodelc")

CompileMLProgram accepts raw model protobuf bytes and a weight directory. Use this when you already have the serialized model in memory or need to supply the weight directory separately:

err := coremlcompiler.CompileMLProgram(protoBytes, weightDir, "model.mlmodelc")

CompileMILText takes pre-built MIL text, a ModelDescription, and an optional weight root directory. Use this when you generate MIL text programmatically rather than decoding it from a protobuf:

err := coremlcompiler.CompileMILText(milText, 8, desc, weightRoot, "model.mlmodelc")
Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/coremlcompiler"
)

func main() {
	// Compile a CoreML model package to a compiled bundle.
	err := coremlcompiler.Compile("model.mlpackage", "model.mlmodelc")
	if err != nil {
		fmt.Println(err)
	}

	// The compiled bundle can be loaded by CoreML or x/ane:
	//
	//   rt, _ := ane.Open()
	//   k, _ := rt.Compile(ane.CompileOptions{
	//       ModelType:   ane.ModelTypePackage,
	//       PackagePath: "model.mlmodelc",
	//   })
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compile

func Compile(inputPath, outputPath string) error

Compile compiles a CoreML model package (.mlpackage) or model file (.mlmodel) into a compiled bundle (.mlmodelc) at outputPath.

Only mlprogram models (spec version 5+) are supported. Legacy NeuralNetwork models must be converted to the mlprogram format before compilation (e.g. via coremltools.convert with convert_to="mlprogram").

func CompileMILText

func CompileMILText(milText string, specVersion int32, desc ModelDescription, weightRoot, outputPath string) error

CompileMILText compiles an already-emitted mlprogram MIL text into a compiled bundle at outputPath.

desc names the model inputs, outputs, and states stored in coremldata.bin and metadata.json. weightRoot, when non-empty, is copied into the bundle preserving relative paths, so BLOBFILE paths like "@model_path/weights/..." resolve correctly at runtime.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/tmc/apple/x/coremlcompiler"
)

func main() {
	tmpDir, err := os.MkdirTemp("", "coremlcompiler-example-*")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer os.RemoveAll(tmpDir)

	milText := `program(1.3) {
    func main<ios18>(tensor<fp16, [1, 8]> x) {
    } -> (x);
}`
	desc := coremlcompiler.ModelDescription{
		Inputs:  []coremlcompiler.FeatureDescription{{Name: "x"}},
		Outputs: []coremlcompiler.FeatureDescription{{Name: "x"}},
	}
	err = coremlcompiler.CompileMILText(milText, 8, desc, "", filepath.Join(tmpDir, "model.mlmodelc"))
	if err != nil {
		fmt.Println(err)
	}
}

func CompileMLModelAtURL added in v0.5.4

func CompileMLModelAtURL(sourcePath string) (string, error)

CompileMLModelAtURL uses [MLModel compileModelAtURL:error:] to compile a .mlpackage or .mlmodel to a .mlmodelc directory. Only available on darwin.

func CompileMLProgram

func CompileMLProgram(modelProto []byte, weightDir, outputPath string) error

CompileMLProgram compiles an mlprogram model from already-parsed components. This is useful when you have the model proto bytes and weight directory available separately.

func CompileToTemp

func CompileToTemp(inputPath string) (string, error)

CompileToTemp compiles a model to a temporary directory, returning the path to the compiled .mlmodelc bundle. The directory is placed under os.TempDir() and named by a hash of the input path for implicit caching.

func EncodeModel added in v0.5.0

func EncodeModel(m *Model) []byte

EncodeModel encodes a Model to protobuf wire format.

func WriteMILBlob added in v0.5.4

func WriteMILBlob(entries []BlobEntry) (data []byte, offsets []uint64)

WriteMILBlob builds a MIL Blob Storage v2 weight file from the given entries. Returns the complete file bytes and the BLOBFILE offsets (one per entry) that should be used in MIL text.

func WriteMLPackage added in v0.5.0

func WriteMLPackage(dir string, modelProto []byte, weightSrc string) error

WriteMLPackage creates a .mlpackage directory at dir containing the given model protobuf and optional weights. The directory can be read back by readMLPackage or passed to MLModel.compileModelAtURL:.

The layout matches Apple's coremltools format:

dir/
├── Manifest.json
└── Data/
    └── com.apple.CoreML/
        ├── model.mlmodel     (modelProto bytes)
        └── weights/          (copied from weightSrc if provided)

Manifest paths use "com.apple.CoreML/..." (without Data/ prefix); the Data/ directory is implicit when resolving paths on disk.

If weightSrc is non-empty and is a directory, its contents are copied into the package's CoreML directory preserving relative paths. If weightSrc is a single file, it is copied as weights/weight.bin.

Types

type Argument

type Argument struct {
	Bindings []Binding // field 1
}

Argument is an Operation input (list of bindings). Proto: MILSpec.Argument

type ArrayDataType

type ArrayDataType int32

ArrayDataType identifies Core ML multi-array element types.

const (
	ArrayDataTypeInvalid ArrayDataType = 0
	ArrayDataTypeFloat16 ArrayDataType = 65552
	ArrayDataTypeFloat32 ArrayDataType = 65568
	ArrayDataTypeDouble  ArrayDataType = 65600
	ArrayDataTypeInt32   ArrayDataType = 131104
)

type ArrayFeatureType

type ArrayFeatureType struct {
	Shape    []int64
	DataType ArrayDataType
}

ArrayFeatureType describes a Core ML multi-array feature.

type Binding

type Binding struct {
	Name  string // field 1 (oneof)
	Value *Value // field 2 (oneof)
}

Binding is either a name reference or an inline value. Proto: MILSpec.Argument.Binding

type BlobDataType added in v0.5.4

type BlobDataType uint32

BlobDataType identifies element types in MIL blob storage.

const (
	BlobDataTypeFloat16    BlobDataType = 1
	BlobDataTypeFloat32    BlobDataType = 2
	BlobDataTypeUInt8      BlobDataType = 3
	BlobDataTypeInt8       BlobDataType = 4
	BlobDataTypeBFloat16   BlobDataType = 5
	BlobDataTypeInt16      BlobDataType = 6
	BlobDataTypeUInt16     BlobDataType = 7
	BlobDataTypeInt4       BlobDataType = 8
	BlobDataTypeUInt1      BlobDataType = 9
	BlobDataTypeUInt2      BlobDataType = 10
	BlobDataTypeUInt4      BlobDataType = 11
	BlobDataTypeUInt3      BlobDataType = 12
	BlobDataTypeUInt6      BlobDataType = 13
	BlobDataTypeInt32      BlobDataType = 14
	BlobDataTypeUInt32     BlobDataType = 15
	BlobDataTypeFloat8E4M3 BlobDataType = 16
	BlobDataTypeFloat8E5M2 BlobDataType = 17
)

func DataTypeToBlobDataType added in v0.5.4

func DataTypeToBlobDataType(dt DataType) (BlobDataType, error)

MILBlobDataTypeName returns the MIL DataType enum for a BlobDataType.

type BlobEntry added in v0.5.4

type BlobEntry struct {
	DType BlobDataType
	Data  []byte
}

BlobEntry describes a single tensor to write into a MILBlob weight file.

type BlobFileValue

type BlobFileValue struct {
	FileName string // field 1
	Offset   uint64 // field 2
}

BlobFileValue references weight data in a blob file. Proto: MILSpec.BlobFileValue

type Block

type Block struct {
	Inputs     []NamedValueType // field 1
	Outputs    []string         // field 2
	Operations []*Operation     // field 3
}

Block is a sequence of operations. Proto: MILSpec.Block

type CoreMLModel added in v0.5.4

type CoreMLModel struct{}

CoreMLModel wraps a loaded MLModel for inference. Only functional on darwin.

func LoadCoreMLModel added in v0.5.4

func LoadCoreMLModel(modelcPath string) (*CoreMLModel, error)

LoadCoreMLModel loads a compiled .mlmodelc from disk. Only available on darwin.

func (*CoreMLModel) Close added in v0.5.4

func (m *CoreMLModel) Close()

Close releases the CoreML model reference.

func (*CoreMLModel) Predict added in v0.5.4

func (m *CoreMLModel) Predict(inputs []PredictInput, outputName string) (*PredictOutput, error)

Predict runs inference. Only available on darwin.

type DataType

type DataType int32

DataType identifies element types in MIL. Proto: MILSpec.DataType

const (
	DataTypeFloat16  DataType = 10
	DataTypeFloat32  DataType = 11
	DataTypeFloat64  DataType = 12
	DataTypeBFloat16 DataType = 13
	DataTypeInt8     DataType = 21
	DataTypeInt16    DataType = 22
	DataTypeInt32    DataType = 23
	DataTypeInt64    DataType = 24
	DataTypeInt4     DataType = 25
	DataTypeUInt8    DataType = 31
	DataTypeUInt16   DataType = 32
	DataTypeUInt32   DataType = 33
	DataTypeUInt64   DataType = 34
	DataTypeBool     DataType = 1
	DataTypeString   DataType = 2
)

func (DataType) String

func (dt DataType) String() string

String returns the MIL text name for the data type.

type Dimension

type Dimension struct {
	Constant uint64 // from ConstantDimension.size (field 1.1)
	Unknown  bool   // true if UnknownDimension
}

Dimension is a tensor Dimension (constant or unknown). Proto: MILSpec.Dimension

type FeatureDescription

type FeatureDescription struct {
	Name string       // field 1
	Type *FeatureType // field 3
}

FeatureDescription is a named, typed model feature. Proto: CoreML.Specification.FeatureDescription

type FeatureType

type FeatureType struct {
	MultiArrayType *ArrayFeatureType
	IsOptional     bool
}

FeatureType describes a Core ML model feature type.

type Function

type Function struct {
	Inputs               []NamedValueType  // field 1
	OpSet                string            // field 2
	BlockSpecializations map[string]*Block // field 3
	Attributes           map[string]*Value // field 4
}

Function is an MIL Function. Proto: MILSpec.Function

type ImmediateValue

type ImmediateValue struct {
	// Exactly one of these is set.
	Tensor *TensorValue // field 1
}

ImmediateValue holds inline constant data. Proto: MILSpec.ImmediateValue

type Model

type Model struct {
	SpecVersion int32            // field 1
	Description ModelDescription // field 2
	MLProgram   *Program         // field 502 (oneof)
	// contains filtered or unexported fields
}

Model is the top-level CoreML model container. Proto: CoreML.Specification.Model

type ModelDescription

type ModelDescription struct {
	Inputs  []FeatureDescription // field 1
	Outputs []FeatureDescription // field 10
	States  []FeatureDescription // field 13
}

ModelDescription describes model inputs, outputs, and metadata. Proto: CoreML.Specification.ModelDescription

type NamedValueType

type NamedValueType struct {
	Name string     // field 1
	Type *ValueType // field 2
}

NamedValueType is a (name, type) pair. Proto: MILSpec.NamedValueType

type Operation

type Operation struct {
	Type       string               // field 1
	Inputs     map[string]*Argument // field 2
	Outputs    []NamedValueType     // field 3
	Blocks     []*Block             // field 4
	Attributes map[string]*Value    // field 5
}

Operation is a single MIL Operation. Proto: MILSpec.Operation

type PredictInput added in v0.5.4

type PredictInput struct {
	Name    string
	Data    unsafe.Pointer
	Shape   []int
	Strides []int
	DType   coreml.MLMultiArrayDataType
}

PredictInput is a named tensor input for CoreML prediction.

type PredictOutput added in v0.5.4

type PredictOutput struct {
	Bytes []byte // copied from CoreML output buffer
	Shape []int
	DType coreml.MLMultiArrayDataType
}

PredictOutput holds the result of a CoreML prediction.

type Program

type Program struct {
	Version    int64                // field 1
	Functions  map[string]*Function // field 2
	Attributes map[string]*Value    // field 4
}

Program is an MIL Program. Proto: MILSpec.Program

type StateType

type StateType struct {
	WrappedType *ValueType // field 1
}

StateType wraps a ValueType for stateful operations. Proto: MILSpec.StateType

type TensorType

type TensorType struct {
	DataType   DataType    // field 1
	Rank       int64       // field 2
	Dimensions []Dimension // field 3
}

TensorType describes a tensor's element type and shape. Proto: MILSpec.TensorType

type TensorValue

type TensorValue struct {
	// Exactly one of these is set.
	Floats  []float32 // field 1
	Ints    []int32   // field 2
	Bools   []bool    // field 3
	Strings []string  // field 4
	Longs   []int64   // field 5
	Doubles []float64 // field 6
	Bytes   []byte    // field 7
}

TensorValue holds tensor data inline. Proto: MILSpec.TensorValue

type Value

type Value struct {
	Type *ValueType // field 2

	// Exactly one of these is set.
	Immediate *ImmediateValue // field 3
	BlobFile  *BlobFileValue  // field 5
}

value is an MIL value (immediate or blob reference). Proto: MILSpec.Value

type ValueType

type ValueType struct {
	// Exactly one of these is set.
	TensorType *TensorType // field 1
	StateType  *StateType  // field 5

}

ValueType describes a MIL value's type. Proto: MILSpec.ValueType

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL