Documentation
¶
Overview ¶
Package coremlcompiler compiles CoreML model packages (.mlpackage) into compiled model bundles (.mlmodelc) without requiring xcrun or Apple's proprietary coremlcompiler binary.
The primary 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)
- type ArrayDataType
- type ArrayFeatureType
- type DataType
- type FeatureDescription
- type FeatureType
- type Model
- type ModelDescription
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.
Currently supports mlprogram models. NeuralNetwork models will be supported in a future release.
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.
Types ¶
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 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 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 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