coreml
25 memberslucid.coreml`lucid.coreml` — export Lucid models to Core ML, and run them on the ANE.
Lucid computes on Accelerate (CPU) and MLX (Metal). Neither targets the Neural Engine, so a whole processor on every machine Lucid supports was unreachable; the same gap meant a model trained here could not ship inside an iOS or macOS app. Core ML is the only public route to either.
No third-party dependency. The package format is written by
lucid/_C/coreml — the MIL protobuf, the weight blob, the bundle —
and executed through Apple's own CoreML.framework, which stands beside
Accelerate and Metal rather than beside a pip package. Nothing under
lucid/ imports anything external (H4).
Two things worth knowing before trusting an export.
The Neural Engine only runs float16. A float32 program asked for with
ComputeUnits.CPU_AND_NE does not warn, does not error, and runs at CPU
speed — measured: zero of its operations are scheduled on the ANE. Pass
Precision.FLOAT16 to actually reach it, and expect the ~1e-4 that
half precision costs. CoreMLModel.compute_plan reports where the
operations really landed, so this is checkable rather than inferred from a
stopwatch.
Coverage is narrow and loud. The mapped operations are the ones real
models were measured to emit; anything else raises
UnsupportedOp naming the gap, because a package quietly missing a
layer still loads and still returns plausible numbers.
Smaller packages. weights= stores the weights compressed —
WeightPrecision for eight bits with a scale per output channel,
Palettize for one to eight bits through a lookup table,
Sparsify for the survivors plus a mask. Measured on a trained
ResNet-50, against its top-1 over five inputs: int8 is 3.9x smaller and
keeps 4/5, six-bit palettization 4.3x and 4/5. Below six bits
post-training palettization changes every prediction, and no export
setting recovers it — which is what CompressionAware is for.
Models from lucid.quantization export. A model prepared for
quantization-aware training carries observers that record what passes
through them, and tracing is not calibration, so they are paused around
it and put back; its weights were trained onto their grid, so storing
them there costs nothing — measured at 0.00e+00 against the eager model
where the same network quantized afterwards lands at 7e-04. A converted
model — quantize_dynamic, or convert — holds a packed MLX linear
whose Metal-only kernel the tracer cannot follow, and is exported through
that form's own dequantize-to-float reference path, which is the same
arithmetic to 5e-07 rather than an approximation of it.
Activations decides whether a prepared model's activation
quantization is carried into the package as arithmetic or left out; Core
ML quantizes weights only, so carrying it is faithful and is work the
accelerator did not ask for.
Classes
ShapeNotFlexible1 methodsAn operation's configuration was derived from the input's size.
StatefulModel1 methodsThe model changed its own buffers while being traced.
UnsupportedOp1 methodsA traced op has no MIL translation, or a refused one.
UnsupportedRank1 methodsA tensor exceeds the rank Core ML's program dialect allows.
CoreMLModel12 methodsA Core ML package written by Lucid, loaded and ready to run.
Latency1 methodsWhat one prediction costs, with the settings that produced it.
PlacementSummary5 methodsWhere a model's operations are scheduled.
CompressionAware5 methodsA model that trains against the compression it will be exported with.
ActivationsWhat to do with a quantization-aware model's activation quantization.
DeploymentTarget1 methodsOldest operating system the exported package will run on.
DrawsWhat to do about a model that draws random numbers in forward.
Palettize1 methodsStore each weight as an index into a small table of values.
Sparsify1 methodsKeep only the largest weights, and a bit saying where they were.
Classifier1 methodsTurn the exported scores into labels Core ML knows how to name.
State1 methodsA value the package carries from one prediction to the next.
ColorSpacePixel layout Core ML should hand the model.
ComputeUnitsProcessors Core ML may schedule the model on.
ImageInput1 methodsPresent an input as an image, with the normalisation it expects.
Metadata1 methodsWhat the package says about itself.
PrecisionWeight and activation precision of the exported program.
WeightPrecisionHow a weight is stored, as distinct from how the body computes.
Functions
export→ CoreMLModelTrace model, write a .mlpackage at path, and load it.
export_functions→ dict[str, CoreMLModel]Write several entry points into one package, sharing its weights.
precision_cost→ dict[str, float]What each precision costs this model, measured rather than assumed.
load→ CoreMLModelLoad an existing .mlpackage.