Compile PyTorch models into TensorFlow custom ops backed by Triton PTX kernels
Project description
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 mirai-compiler
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mirai_compiler-0.2.1.tar.gz.
File metadata
- Download URL: mirai_compiler-0.2.1.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b51773277790b4134cefa984bcfa9a9ed8cef85784577bd9609f16a4d8fac250
|
|
| MD5 |
b89989040c47d5c5072a972750955e42
|
|
| BLAKE2b-256 |
5b198dca091f328113ff0264dfd8c5159d9c1cdad5d2f613d06789886f8135cb
|
Provenance
The following attestation bundles were made for mirai_compiler-0.2.1.tar.gz:
Publisher:
publish.yml on dawnop/mirai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mirai_compiler-0.2.1.tar.gz -
Subject digest:
b51773277790b4134cefa984bcfa9a9ed8cef85784577bd9609f16a4d8fac250 - Sigstore transparency entry: 1356799425
- Sigstore integration time:
-
Permalink:
dawnop/mirai@2f933655ad7543a1834dbaca052dcf3c06a661c0 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/dawnop
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2f933655ad7543a1834dbaca052dcf3c06a661c0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file mirai_compiler-0.2.1-py3-none-any.whl.
File metadata
- Download URL: mirai_compiler-0.2.1-py3-none-any.whl
- Upload date:
- Size: 31.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
287b1f8d14c57b6e857199b7f6bdcc1a466c6eef547acfdf40e54a6450074b76
|
|
| MD5 |
2bf55a0e1d27629b37d32236fae660d8
|
|
| BLAKE2b-256 |
0513ef36a47cecf495ea6dc50d8862442de8b76d008b1652174b8506fabd03c4
|
Provenance
The following attestation bundles were made for mirai_compiler-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on dawnop/mirai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mirai_compiler-0.2.1-py3-none-any.whl -
Subject digest:
287b1f8d14c57b6e857199b7f6bdcc1a466c6eef547acfdf40e54a6450074b76 - Sigstore transparency entry: 1356799435
- Sigstore integration time:
-
Permalink:
dawnop/mirai@2f933655ad7543a1834dbaca052dcf3c06a661c0 -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/dawnop
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2f933655ad7543a1834dbaca052dcf3c06a661c0 -
Trigger Event:
push
-
Statement type: