Skip to main content

Convert PyTorch models to CoreAI format

Project description

Core AI PyTorch Extensions (coreai-torch)

Core AI PyTorch Extensions (coreai-torch) is a Python package that bridges PyTorch and Core AI. Use it to bring up an existing PyTorch model into Core AI IR, or to author Core AI models directly from PyTorch by composing the built-in composite op library (coreai_torch.composite_ops), authoring new ops via register_torch_lowering, and authoring inline Metal GPU kernels via TorchMetalKernel. The resulting IR can be compiled and executed efficiently by the Core AI inference stack.

🔗 Jump to: Getting started · Documentation · Contributing · Support · License

Overview

coreai-torch traverses a torch.export.ExportedProgram and produces Core AI IR — the same IR consumed by the Core AI compiler and runtime. The public entry point is TorchConverter, which lowers PyTorch operators to Core AI dialect operations, preserves location and module-stack information for debugging, and provides extension points for custom Metal kernels and submodule externalization.

Getting started

Installation

pip install coreai-torch

Or from source with uv:

uv sync

Usage

TorchConverter accepts models in two forms. Pick based on what you have and whether you need externalization:

You have Use
A decomposed ExportedProgram add_exported_program()
An nn.Module + need externalization add_pytorch_module() with externalize_modules
An nn.Module, no externalization Either method — add_exported_program() is more direct

From an ExportedProgram

You export and decompose the model yourself, then pass the ExportedProgram directly. Use get_decomp_table() so that composite ops (instance_norm, pixel_shuffle, scaled_dot_product_attention) are preserved for optimal runtime performance.

import torch
from coreai_torch import TorchConverter, get_decomp_table

model = ...  # your nn.Module
model.eval()

# Export and decompose — this is your responsibility
ep = torch.export.export(model, args=(torch.randn(1, 3, 224, 224),))
ep = ep.run_decompositions(get_decomp_table())

# Convert to Core AI IR
converter = TorchConverter().add_exported_program(ep)
coreai_program = converter.to_coreai()
coreai_program.optimize()

From an nn.Module

Pass your model and an export_fn that returns a decomposed ExportedProgram. This is equivalent to calling add_exported_program() with the result of export_fn.

import coreai_torch
from coreai_torch import TorchConverter

model = ...  # your nn.Module
model.eval()
sample = (torch.randn(1, 3, 224, 224),)

converter = TorchConverter().add_pytorch_module(
    model,
    export_fn=lambda m: torch.export.export(m, args=sample).run_decompositions(
        coreai_torch.get_decomp_table()
    ),
)
coreai_program = converter.to_coreai()
coreai_program.optimize()

Documentation

  • API reference and conversion guides — see the docs/ directory
  • Supported model types and ops — see the operator coverage notebooks under docs/

Building docs locally

uv sync --extra docs
uv run jupyter-book build docs/
open docs/_build/html/index.html

Development

Running tests

uv sync --extra test
uv run pytest tests/ -n auto

Testing notebooks

uv run pytest docs/ --nbmake -v

Contributing

We welcome contributions within a defined scope. Please read CONTRIBUTING.md carefully before opening a pull request or issue — particularly the section on contribution scope.

Support

Security and code of conduct

Security vulnerability reporting and the Code of Conduct for this project are governed at the org level via the Apple Open Source .github repository.

License

This project is licensed under the BSD 3-Clause License.

Related projects

  • Core AI — Apple's on-device AI inference stack
  • Core AI Optimization — model compression for deployment on Apple Silicon
  • Core AI Models — ready-to-run optimized models, Python reproduction scripts, and Swift utilities for on-device integration

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

coreai_torch-0.4.0.tar.gz (204.9 kB view details)

Uploaded Source

Built Distribution

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

coreai_torch-0.4.0-py3-none-any.whl (193.0 kB view details)

Uploaded Python 3

File details

Details for the file coreai_torch-0.4.0.tar.gz.

File metadata

  • Download URL: coreai_torch-0.4.0.tar.gz
  • Upload date:
  • Size: 204.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for coreai_torch-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9ced8d080f3dcdb804eaaa9cc471df94a9f754e7a8dda8720204b810e193f3e6
MD5 061f7b9fddcbe9a6741e660cd9b40ff3
BLAKE2b-256 abb7e1e6b3039c5e37f9602de8df2f2a07659ec7b946b2e537a1b4c824013edf

See more details on using hashes here.

File details

Details for the file coreai_torch-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: coreai_torch-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 193.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for coreai_torch-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b109c0090d83b0a8eb7302fea2314cf3cb8da957477f7b31b8ec36480a94a247
MD5 c46645b174ab68f294eb2da7eee255df
BLAKE2b-256 785dd84f5d62f2ab2588e0d8b4581d040b9e7fe97777a584d821a2ee103b88e8

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