gomodel

command
v0.6.20 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gomodel — a CoreML model authored, compiled, and run entirely from Go

No Python, no coremltools, no Xcode. One Go binary:

  1. defines an MIL program (y = relu(x·Wᵀ + b)) as a plain Go value,
  2. writes its weights as a MILBlob Storage v2 weight.bin,
  3. encodes the model to CoreML's protobuf (x/coremlcompiler.EncodeModel),
  4. writes an Apple-compatible .mlpackage (WriteMLPackage),
  5. compiles it to .mlmodelc — by default with the repo's pure-Go compiler (no Apple tool invoked, ~1ms); -applecompile routes through [MLModel compileModelAtURL:] instead, proving Apple's own toolchain accepts the Go-authored package,
  6. loads it with CoreML and runs a prediction, verified against the same math computed in Go.
go run ./examples/coreml/gomodel
go run ./examples/coreml/gomodel -applecompile
go run ./examples/coreml/gomodel -keep ~/tmp/gomodel   # inspect the products

Both compile arms verified 2026-08-11 (macOS 26.x, go1.26.3).

Spec gotchas this example encodes (found by running both arms)

  • const ops carry val as an attribute, not an input. The MIL-text emitter accepts either (it merges const inputs into attributes), so the pure-Go arm masks the mistake; Apple's protobuf parser rejects it.
  • The protobuf opset is CoreMLn, not iosNN. CoreML7 corresponds to ios18 in MIL text — the pure-Go compiler performs that rewrite when emitting model.mil. Apple's parser also requires the block_specializations key to equal the function's opset string exactly.
  • Spec version must match the opset: 9 for CoreML7. A mismatch fails Apple's validator with "not written in an opset that is supported".
  • Multi-dimensional immediate tensors didn't survive MIL text — the serializer emitted flat literals, which CoreML's parser reads as rank-1 ("Declared shape [3, 4] but expected shape 12"). FIXED same day: the emitter now nests literals to match the declared shape (TestCompileRank2ImmediateLoadable proves a rank-2 immediate loads). This example still routes weights through weight.bin (WriteMILBlob + BlobFileValue) because that is what coremltools does and what real models need.
  • WriteMLPackage's directory branch dropped the weights/ prefix (contents landed at com.apple.CoreML/ and the compiled model couldn't find them). FIXED same day: a directory weightSrc is now the weights directory itself and its contents land under weights/.

Documentation

Overview

Command gomodel authors, packages, compiles, and runs a CoreML model entirely from Go — no Python, no coremltools, no Xcode.

The pipeline, every step in this process:

  1. Define an MIL program in Go (y = relu(x·Wᵀ + b)) and write its weights as a MILBlob Storage v2 weight.bin.
  2. Encode it to CoreML's protobuf with x/coremlcompiler.EncodeModel.
  3. Write an Apple-compatible .mlpackage with WriteMLPackage.
  4. Compile it to .mlmodelc — by default with x/coremlcompiler's own pure-Go compiler; -applecompile uses [MLModel compileModelAtURL:] instead, proving Apple's toolchain accepts the Go-authored package.
  5. Load it with CoreML and run a prediction.

The prediction is verified against the same math computed in Go.

go run ./examples/coreml/gomodel
go run ./examples/coreml/gomodel -applecompile

Jump to

Keyboard shortcuts

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