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
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,
)

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.12. Do not run hatch with a newer version. Can be controlled using fx export HATCH_PYTHON=python3.12
  • 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.12.tar.gz (32.1 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_experimental-0.0.12-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for stablehlo_coreml_experimental-0.0.12.tar.gz
Algorithm Hash digest
SHA256 cabc54c74bd48ab6535c05da84ee31be7f0f511bf216f152b996456ffd72d31b
MD5 03d76c4a417cbf42613dc29d6b071ff8
BLAKE2b-256 774a1b14e8ce086e907ebb53411d4a9aca02113fab8df340990289da72fa437b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for stablehlo_coreml_experimental-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 5ead04f097ddcbe49bfb4e1431e79614d4f8ef7bb0113fd6ea91fe2aa3f5aa2f
MD5 2933765258c79afb14b1375703a35c21
BLAKE2b-256 00fff984483aa55c7c0d6b775fe7204ee004ad9ad4397fa4826f3b28dd271d39

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