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",
// })
}
Output:
Index ¶
- func Compile(inputPath, outputPath string) error
- func CompileMILText(milText string, specVersion int32, desc ModelDescription, ...) error
- func CompileMLProgram(modelProto []byte, weightDir, outputPath string) error
- func CompileToTemp(inputPath string) (string, error)
- func EncodeModel(m *Model) []byte
- func WriteMLPackage(dir string, modelProto []byte, weightSrc string) error
- type Argument
- type ArrayDataType
- type ArrayFeatureType
- type Binding
- type BlobFileValue
- type Block
- type DataType
- type Dimension
- type FeatureDescription
- type FeatureType
- type Function
- type ImmediateValue
- type Model
- type ModelDescription
- type NamedValueType
- type Operation
- type Program
- type StateType
- type TensorType
- type TensorValue
- type Value
- type ValueType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compile ¶
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)
}
}
Output:
func CompileMLProgram ¶
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 ¶
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
EncodeModel encodes a Model to protobuf wire format.
func WriteMLPackage ¶ added in v0.5.0
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 ¶
Binding is either a name reference or an inline value. Proto: MILSpec.Argument.Binding
type BlobFileValue ¶
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 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 )
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 ¶
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 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