Skip to main content

Convert StableHLO models into Apple Core ML format

Project description

stablehlo-coreml

Convert StableHLO models into Apple Core ML format.

StableHLO is the portability layer used by ML frameworks like JAX and PyTorch. This library converts StableHLO programs into Apple's Core ML format via coremltools, enabling deployment on Apple hardware (iOS, macOS, etc.).

Installation

pip install stablehlo-coreml

Requires Python 3.9+ and targets iOS/macOS 18+.

Supported Frameworks

Models can be exported from any framework that produces StableHLO:

  • JAX / Flax / Equinox — via jax.export
  • PyTorch — via torchax to trace the model into JAX, then jax.export to StableHLO

The test suite validates against a broad set of models, including full HuggingFace Transformers such as TinyLlama, T5, DistilBERT, GPT-2, BERT, and Whisper, as well as vision models like ResNet, EfficientNet, ViT, ConvNeXt, and more.

For a real-world example, see gemma-coreml-chat, which exports Google's Gemma 4 model to Core ML using this library.

Converting a Model

To convert a StableHLO module:

import coremltools as ct
from stablehlo_coreml.converter import convert
from stablehlo_coreml import DEFAULT_HLO_PIPELINE

mil_program = convert(hlo_module, minimum_deployment_target=ct.target.iOS18)
cml_model = ct.convert(
    mil_program,
    source="milinternal",
    minimum_deployment_target=ct.target.iOS18,
    pass_pipeline=DEFAULT_HLO_PIPELINE,
)

Obtaining a StableHLO Module from JAX

import jax
from jax._src.lib.mlir import ir
from jax._src.interpreters import mlir as jax_mlir
from jax.export import export

import jax.numpy as jnp

def jax_function(a, b):
    return jnp.einsum("ij,jk -> ik", a, b)

context = jax_mlir.make_ir_context()
input_shapes = (jnp.zeros((2, 4)), jnp.zeros((4, 3)))
jax_exported = export(jax.jit(jax_function))(*input_shapes)
hlo_module = ir.Module.parse(jax_exported.mlir_module(), context=context)

For the JAX example to work, you will additionally need to install absl-py and flatbuffers as dependencies.

Examples in the test suite

The tests/ directory has end-to-end export and conversion examples:

Development

  • coremltools supports up to Python 3.13. Do not run hatch with a newer version. Can be controlled using e.g. export HATCH_PYTHON=python3.13
  • Run tests using hatch run test:pytest tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stablehlo_coreml-0.1.0.tar.gz (48.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stablehlo_coreml-0.1.0-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

Details for the file stablehlo_coreml-0.1.0.tar.gz.

File metadata

  • Download URL: stablehlo_coreml-0.1.0.tar.gz
  • Upload date:
  • Size: 48.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.12.3 HTTPX/0.28.1

File hashes

Hashes for stablehlo_coreml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a30a4ee6ceec0b146deb15e18cc905c71c3fd257c7f728ae58533101d036647c
MD5 f26d23e5e1e1f633086e0a35d9f2b71e
BLAKE2b-256 a77979fb61eb0d23b05f9819d5c2aad22a8b5e25763564ad84d8ced8dd5b11c8

See more details on using hashes here.

File details

Details for the file stablehlo_coreml-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: stablehlo_coreml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 33.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.12.3 HTTPX/0.28.1

File hashes

Hashes for stablehlo_coreml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d3dafbd6fd59277811efd8702cf988eafc5cee1af709cde93e2d7d9bed5de21
MD5 d4dc7cf35e39608dd22d88ea0a0ce5e2
BLAKE2b-256 35dad507d801d04b276ba079e2774e19aecdb6b9562fea6ccd9bed4d45262833

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page