Skip to main content

Convert StableHLO models into Apple Core ML format

Project description

Convert StableHLO models into Apple Core ML format

This repo is currently experimental!

Only a subset of the StableHLO operations have been implemented, and some of them may have restrictions.

Due to the current dot_general op implementation, it is only possible to target iOS >= 18.

Look in the tests directory, to see what has currently been tested.

The package is published to PyPi as stablehlo-coreml-experimental.

Converting a model

To convert a StableHLO module, do the following:

import coremltools as ct
from stablehlo_coreml.converter import convert

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)

For a Jax project, the hlo_module can be obtained the following way:

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.

For additional examples see the tests directory.

Notes

  • coremltools supports up to python 3.11. Do not run hatch with a newer version. Can be controlled using fx export HATCH_PYTHON=python3.11
  • 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_experimental-0.0.2.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file stablehlo_coreml_experimental-0.0.2.tar.gz.

File metadata

File hashes

Hashes for stablehlo_coreml_experimental-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b6f95e20aecce1fd5cd7be8820526e4d8398e1fa46ea5d7035b55b620ec716ef
MD5 4f22cf06efdbf5a31aea781eff02eb1a
BLAKE2b-256 c30d6e866838adda71c0ead4ee4659f81e30f36df3cabf5395cbc16b39c968f0

See more details on using hashes here.

File details

Details for the file stablehlo_coreml_experimental-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for stablehlo_coreml_experimental-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9504c157ea8b851893565f8ea2485053c4c3ad4c0c69bf09467407d19cc6de62
MD5 8cab4891e8e4f2ab666bb5e73e6eb46f
BLAKE2b-256 1b699714dbb6a4225ff890b3356ab1aece62d8e3c4d4df5240a352bf201f4569

See more details on using hashes here.

Supported by

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