Skip to main content

Compile PyTorch models into TensorFlow custom ops backed by Triton PTX kernels

Project description

Mirai

A compiler that converts PyTorch models into deployable TensorFlow custom ops backed by Triton PTX kernels.

Mirai takes a decorated PyTorch function, runs torch.compile to generate optimized Triton kernels, extracts the PTX, and wraps everything into a TensorFlow custom op — with a single function call.

Quick Start

import torch
import mirai

@mirai.op(name="Pffn")
def pffn(inputs, w_gate, b_gate, w_up, b_up, w_down, b_down):
    inputs_t = inputs.transpose(0, 1).contiguous()
    gates = torch.bmm(inputs_t, w_gate) + b_gate.unsqueeze(1)
    gates = torch.nn.functional.silu(gates)
    vals = torch.bmm(inputs_t, w_up) + b_up.unsqueeze(1)
    outputs = torch.bmm(gates * vals, w_down) + b_down.unsqueeze(1)
    return outputs.transpose(0, 1).contiguous()

# One call: torch.compile → PTX extraction → C++ codegen → build script
mirai.build(pffn, sample_inputs=[inputs, w_gate, b_gate, w_up, b_up, w_down, b_down])

Output in ./generated/:

generated/
├── PffnFwd.cc          # TF custom op C++ source (forward)
├── PffnBwd.cc          # TF custom op C++ source (backward)
├── build.sh            # g++ compilation script
├── pffn_api.py         # TF Python API wrapper
└── tf32/               # PTX kernels and metadata
    ├── PffnFwd/
    └── PffnBwd/

Build the op:

cd generated && bash build.sh

Requirements

The pipeline spans two environments (typically on different machines):

Stage Environment Dependencies
PTX generation (Stage 1–3) PyTorch 2.x + Triton + CUDA GPU with ptxas torch>=2.0, jinja2>=3.0, Python >= 3.8
TF op compilation (build.sh) TensorFlow + CUDA toolkit + g++ tensorflow, CUDA headers

mirai.build() / python -m mirai runs in the PyTorch environment, producing generated/ artifacts. Copy them to the TF environment and run bash build.sh to compile into .so.

Install

pip install -e .

How It Works

@mirai.op decorated function
        │
        ▼
  torch.compile + execute        ← Stage 1: generate Triton kernels
        │
        ▼
  discover output_code.py        ← Stage 2: locate generated code
        │
        ▼
  AST patch → run → save PTX     ← Stage 3: extract PTX and shapes
        │
        ▼
  render C++ TF op               ← Stage 3: Jinja2 template → .cc
        │
        ▼
  generate build.sh              ← Stage 4
        │
        ▼
  generate TF API wrapper        ← Stage 5 (if backward exists)

Stage 1 uses torch.compile with max_autotune to find optimal kernel configurations. AUTOTUNE progress is shown during compilation.

Stage 3 patches inductor-generated code via AST transformers to intercept kernel launches and dump PTX binaries. The patched script runs in a subprocess with compile debug disabled.

Stage 5 generates a Python API wrapper that connects forward outputs to backward inputs, mapping user parameter names to internal tensor names.

CLI

For pre-existing output_code.py files (skips Stage 1):

python -m mirai \
  --model-name Pffn \
  --fwd-path /path/to/fwd_output_code.py \
  --bwd-path /path/to/bwd_output_code.py \
  --target ./generated \
  --version tf32

Logging

All output uses the mirai logger with [MIRAI INFO] prefix:

from mirai.log import setup_default_logging
import logging

setup_default_logging(logging.DEBUG)   # show everything including torch internals
setup_default_logging(logging.INFO)    # default: MIRAI logs + AUTOTUNE tables
setup_default_logging(logging.WARNING) # quiet mode

Project Structure

mirai/
├── __init__.py        # Public API: op, module, build
├── log.py             # Unified logging
├── decorator.py       # @mirai.op — contiguous IO wrapper
├── build.py           # mirai.build() entry point
├── __main__.py        # CLI entry point (python -m mirai)
├── discovery.py       # Auto-discover output_code.py
├── pipeline.py        # Per-kernel processing pipeline
├── codegen/
│   ├── transformer.py # AST transformers (ModuleInjector, KernelRunnerHook, MainStripper)
│   ├── render.py      # AST → C++ TF op via Jinja2
│   ├── builder.py     # Render build.sh
│   └── api_render.py  # Render TF API wrapper
└── templates/
    ├── kernel.cc.tpl  # C++ TF custom op template
    ├── build.sh.tpl   # Compilation script template
    └── api.py.tpl     # Python API wrapper template
examples/
└── pffn.py            # PFFN SwiGLU end-to-end example

Development

uv sync --extra dev
uv run black --check .
uv run pytest tests/ --ignore=tests/test_e2e.py   # unit tests (no GPU)
uv run pytest tests/test_e2e.py                    # e2e tests (GPU required)

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

mirai_compiler-0.1.0.tar.gz (26.6 kB view details)

Uploaded Source

Built Distribution

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

mirai_compiler-0.1.0-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mirai_compiler-0.1.0.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mirai_compiler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 426e4d014f0f6e0b3e07025dadb96b4343034cf53a9b637aea8076dd697898e0
MD5 7ffe94b9ac600feca200964b94ddd4a1
BLAKE2b-256 6f6fbb1185c0c222fa629cc053316f4df8575d8b311fd09d52e297742248bd92

See more details on using hashes here.

Provenance

The following attestation bundles were made for mirai_compiler-0.1.0.tar.gz:

Publisher: publish.yml on dawnop/mirai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: mirai_compiler-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mirai_compiler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf907c12dbdb423c5953ea604c617e4eab53362d25298b9ee82d0b6a7a101f1f
MD5 207872dafabda4eff24625f12ebe6478
BLAKE2b-256 f44d7b2082df1f628964ec5a7ea44780e35b30e53794c1040c5cc307be02137a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mirai_compiler-0.1.0-py3-none-any.whl:

Publisher: publish.yml on dawnop/mirai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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