Metadata-Version: 2.4
Name: onnx2coreml
Version: 1.0.0
Summary: Convert ONNX models to Apple Core ML — .mlpackage (ML Program) and .mlmodel (NeuralNetwork) — built on coremltools' MIL builder, with numerical-parity verification.
Author-email: devin-lai <markauto75@gmail.com>
License-Expression: BSD-3-Clause
Project-URL: Homepage, https://github.com/devin-lai/onnx2coreml
Project-URL: Repository, https://github.com/devin-lai/onnx2coreml
Project-URL: Issues, https://github.com/devin-lai/onnx2coreml/issues
Keywords: onnx,core-ml,coreml,mlpackage,mlmodel,mlprogram,apple,model-conversion,machine-learning,ios,macos,neural-engine,apple-silicon,on-device,inference,coremltools
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Operating System :: MacOS
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: <3.14,>=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: coremltools>=8
Requires-Dist: onnx>=1.16
Requires-Dist: numpy
Provides-Extra: verify
Requires-Dist: onnxruntime; extra == "verify"
Provides-Extra: test
Requires-Dist: onnxruntime; extra == "test"
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-xdist; extra == "test"
Provides-Extra: docs
Requires-Dist: sphinx>=7.0; extra == "docs"
Requires-Dist: shibuya; extra == "docs"
Requires-Dist: myst-parser; extra == "docs"
Provides-Extra: dev
Requires-Dist: build>=1.0; extra == "dev"
Requires-Dist: twine>=5.0; extra == "dev"
Requires-Dist: ruff>=0.12.0; extra == "dev"
Requires-Dist: pre-commit>=4.0; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Dynamic: license-file

# onnx2coreml

Convert ONNX models to Apple Core ML — **`.mlpackage`** (ML Program / MIL, primary) and
**`.mlmodel`** (NeuralNetwork, secondary) — built on coremltools' MIL builder, with
numerical-parity verification against ONNX Runtime.

- **ML Program first** for modern on-device performance (fp16 and Apple Neural Engine),
  with full NeuralNetwork support for older-OS reach.
- **Maintainable as Core ML evolves**: all coremltools coupling is isolated behind two
  facades, so spec/format changes touch two files, not the operator lowerings.
- **Verified, not assumed**: every operator has numerical-parity tests run against the live
  Core ML runtime on macOS.

> Status: v1.0.0. Design: `docs/specs/`.

## Install

```bash
uv pip install -e ".[dev,verify,test]"   # development
# or, once published:  uv pip install onnx2coreml
```

Requires Python 3.11–3.13 and macOS for prediction/verification (model *building* works
anywhere coremltools installs).

## Quickstart

```python
import onnx2coreml as o2c

# ML Program (.mlpackage) — the default, fp16, iOS17+
mlmodel = o2c.convert("model.onnx", format="mlpackage", minimum_deployment_target="iOS17")
mlmodel.save("model.mlpackage")

# NeuralNetwork (.mlmodel)
o2c.convert("model.onnx", format="mlmodel").save("model.mlmodel")

# Check coverage before converting
report = o2c.analyze("model.onnx")
print(report.convertible, report.unsupported)

# Numerical parity vs ONNX Runtime
print(o2c.verify("model.onnx", "model.mlpackage"))
```

`convert()` returns a coremltools `MLModel`. Options: `format`,
`minimum_deployment_target`, `compute_precision` (`fp16`/`fp32`), `compute_units`,
`fuse`, `verify`.

## CLI

```bash
onnx2coreml convert model.onnx -o model.mlpackage --format mlpackage --verify
onnx2coreml inspect model.onnx          # op histogram + per-format convertibility
onnx2coreml verify  model.onnx model.mlpackage
onnx2coreml schema                      # version, supported op count, error codes
```

All subcommands accept `--json`.

## Supported operators (105 ops)

| Family | Operators |
|--------|-----------|
| Elementwise / math | Add, Sub, Mul, Div, Pow, Sqrt, Exp, Log, Abs, Erf, Reciprocal, Neg, Floor, Ceil, Round, Sign, Sin, Cos, Tan, Asin, Acos, Atan, Sinh, Cosh, Atanh, Min, Max, Clip, Mod, Equal, Greater, Less, GreaterOrEqual, LessOrEqual, And, Or, Xor, Not, IsNaN, Where |
| Activations | Relu, LeakyRelu, PRelu, Sigmoid, Tanh, Gelu, Softmax, LogSoftmax, Softplus, Elu, HardSigmoid, HardSwish |
| Convolution / pooling | Conv, ConvTranspose, MaxPool, AveragePool, GlobalAveragePool, GlobalMaxPool |
| Normalization | BatchNormalization, LayerNormalization, InstanceNormalization, GroupNormalization |
| Linear | MatMul, Gemm, Inverse (`com.microsoft`) |
| Shape / movement | Reshape, Transpose, Flatten, Squeeze, Unsqueeze, Concat, Split, Pad, Cast, Identity, DepthToSpace, GridSample, Resize, Upsample |
| Indexing / gather-scatter | Gather, GatherND, GatherElements, ScatterND, ScatterElements, NonZero, Slice, Expand, Tile, Shape, ConstantOfShape |
| Reduction | ReduceMean, ReduceSum, ReduceMax, ReduceMin, ReduceProd, ReduceL1, ReduceL2, ReduceLogSum, ReduceLogSumExp, ReduceSumSquare, ArgMax, ArgMin, TopK |
| Recurrent | LSTM |
| Attention | ScaledDotProductAttention (produced by the attention fusion pass) |

`onnx2coreml inspect <model>` reports exactly which ops a given model needs and whether
each is covered. Unsupported ops raise a single aggregated `UnsupportedOpError`; register
a custom lowering with `Converter().register("OpType")` as an escape hatch.

## How it works

```
ONNX → load → passes (opset/shape/fold/cleanup) → fusion (attention→SDPA) →
coverage-gate → lower to MIL (mb.*) → coremltools backend → .mlpackage / .mlmodel → verify
```

See `docs/specs/2026-06-15-onnx2coreml-design.md` for the full design and `AGENTS.md` for
the developer guide.

## Known limitations (v1.0.0)

- Fixed input shapes.
- A full all-axes reduction with `keepdims=0` yields shape `(1,)` (Core ML has no rank-0
  scalar).
- Resize `linear`+`half_pixel` via explicit sizes is `.mlpackage`-only (the NeuralNetwork
  backend lacks the sampling mode); the scales path covers it on both formats.

## License

BSD-3-Clause.
