alchemize-ai 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- alchemize_ai-0.1.0/.github/workflows/ci.yml +64 -0
- alchemize_ai-0.1.0/.github/workflows/workflow.yml +50 -0
- alchemize_ai-0.1.0/.gitignore +22 -0
- alchemize_ai-0.1.0/LICENSE +201 -0
- alchemize_ai-0.1.0/PKG-INFO +30 -0
- alchemize_ai-0.1.0/README.md +263 -0
- alchemize_ai-0.1.0/alchemize/__init__.py +142 -0
- alchemize_ai-0.1.0/alchemize/analysis.py +425 -0
- alchemize_ai-0.1.0/alchemize/benchmark.py +416 -0
- alchemize_ai-0.1.0/alchemize/cli.py +255 -0
- alchemize_ai-0.1.0/alchemize/compiler.py +1598 -0
- alchemize_ai-0.1.0/alchemize/exporter.py +647 -0
- alchemize_ai-0.1.0/alchemize/formatting.py +25 -0
- alchemize_ai-0.1.0/alchemize/jax_exporter.py +247 -0
- alchemize_ai-0.1.0/alchemize/jax_pytorch_transpiler.py +877 -0
- alchemize_ai-0.1.0/alchemize/nutpie_bridge.py +249 -0
- alchemize_ai-0.1.0/alchemize/pytorch_exporter.py +230 -0
- alchemize_ai-0.1.0/alchemize/pytorch_rust_transpiler.py +1141 -0
- alchemize_ai-0.1.0/alchemize/skills/enzyme.md +182 -0
- alchemize_ai-0.1.0/alchemize/skills/gp.md +190 -0
- alchemize_ai-0.1.0/alchemize/skills/gp_accelerate.md +301 -0
- alchemize_ai-0.1.0/alchemize/skills/gp_cuda.md +258 -0
- alchemize_ai-0.1.0/alchemize/skills/jax_to_pytorch.md +178 -0
- alchemize_ai-0.1.0/alchemize/skills/pymc_optimization.md +277 -0
- alchemize_ai-0.1.0/alchemize/skills/pytorch_to_jax.md +186 -0
- alchemize_ai-0.1.0/alchemize/skills/pytorch_to_rust.md +287 -0
- alchemize_ai-0.1.0/alchemize/skills/pytorch_to_rust_burn.md +405 -0
- alchemize_ai-0.1.0/alchemize/skills/stan.md +217 -0
- alchemize_ai-0.1.0/alchemize/skills/stan_to_pymc.md +952 -0
- alchemize_ai-0.1.0/alchemize/skills/zerosumnormal.md +181 -0
- alchemize_ai-0.1.0/alchemize/stan_compiler.py +911 -0
- alchemize_ai-0.1.0/alchemize/stan_exporter.py +394 -0
- alchemize_ai-0.1.0/alchemize/stan_to_pymc.py +716 -0
- alchemize_ai-0.1.0/bench_runner/Cargo.toml +7 -0
- alchemize_ai-0.1.0/bench_runner/src/lib.rs +58 -0
- alchemize_ai-0.1.0/benchmarks/results.json +958 -0
- alchemize_ai-0.1.0/benchmarks/run_benchmarks.py +929 -0
- alchemize_ai-0.1.0/compiled_models/celeri/Cargo.toml +19 -0
- alchemize_ai-0.1.0/compiled_models/celeri/geologic_bounds_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/src/bench.rs +40 -0
- alchemize_ai-0.1.0/compiled_models/celeri/src/data.rs +31 -0
- alchemize_ai-0.1.0/compiled_models/celeri/src/generated.rs +278 -0
- alchemize_ai-0.1.0/compiled_models/celeri/src/lib.rs +2 -0
- alchemize_ai-0.1.0/compiled_models/celeri/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/celeri/station_velocity_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/x_0_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/x_1_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/x_2_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/x_3_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/x_4_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/x_5_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/x_6_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/celeri/x_7_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp/Cargo.toml +20 -0
- alchemize_ai-0.1.0/compiled_models/gp/src/bench.rs +43 -0
- alchemize_ai-0.1.0/compiled_models/gp/src/data.rs +13 -0
- alchemize_ai-0.1.0/compiled_models/gp/src/ffi.rs +43 -0
- alchemize_ai-0.1.0/compiled_models/gp/src/generated.rs +221 -0
- alchemize_ai-0.1.0/compiled_models/gp/src/lib.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/gp/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/gp/x_0_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp/x_1_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp/x_2_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp/y_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/Cargo.toml +19 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/build.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/src/bench.rs +44 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/src/data.rs +13 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/src/generated.rs +251 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/src/lib.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/x_0_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/x_1_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/x_2_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_accelerate_200/y_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/Cargo.toml +20 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/src/bench.rs +44 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/src/data.rs +13 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/src/generated.rs +247 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/src/lib.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/x_0_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/x_1_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/x_2_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_cpu_200/y_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/Cargo.toml +20 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/src/bench.rs +44 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/src/data.rs +13 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/src/generated.rs +222 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/src/lib.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/x_0_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/x_1_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/x_2_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/gp_mlx_test/y_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/Cargo.toml +19 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/results.tsv +14 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/src/bench.rs +44 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/src/data.rs +10 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/src/ffi.rs +43 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/src/generated.rs +174 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/src/lib.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/x_0_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/x_1_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/hierarchical/y_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/linreg/Cargo.toml +19 -0
- alchemize_ai-0.1.0/compiled_models/linreg/src/bench.rs +43 -0
- alchemize_ai-0.1.0/compiled_models/linreg/src/data.rs +7 -0
- alchemize_ai-0.1.0/compiled_models/linreg/src/ffi.rs +43 -0
- alchemize_ai-0.1.0/compiled_models/linreg/src/generated.rs +129 -0
- alchemize_ai-0.1.0/compiled_models/linreg/src/lib.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/linreg/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/linreg/x_0_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/linreg/y_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/normal/Cargo.toml +19 -0
- alchemize_ai-0.1.0/compiled_models/normal/results.tsv +14 -0
- alchemize_ai-0.1.0/compiled_models/normal/src/bench.rs +44 -0
- alchemize_ai-0.1.0/compiled_models/normal/src/data.rs +4 -0
- alchemize_ai-0.1.0/compiled_models/normal/src/ffi.rs +43 -0
- alchemize_ai-0.1.0/compiled_models/normal/src/generated.rs +154 -0
- alchemize_ai-0.1.0/compiled_models/normal/src/lib.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/normal/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/normal/y_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/Cargo.toml +19 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/src/bench.rs +40 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/src/data.rs +13 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/src/ffi.rs +43 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/src/generated.rs +348 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/src/lib.rs +3 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/src/validate.rs +35 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/x_0_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/x_1_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/x_2_data.npy +0 -0
- alchemize_ai-0.1.0/compiled_models/zerosumnormal/y_data.npy +0 -0
- alchemize_ai-0.1.0/examples/01_normal.py +53 -0
- alchemize_ai-0.1.0/examples/02_linear_regression.py +54 -0
- alchemize_ai-0.1.0/examples/03_hierarchical.py +84 -0
- alchemize_ai-0.1.0/examples/04_zerosumnormal.py +216 -0
- alchemize_ai-0.1.0/examples/05_celeri_simplified.py +211 -0
- alchemize_ai-0.1.0/examples/bench_logp.py +146 -0
- alchemize_ai-0.1.0/examples/generate_blog_plots.py +385 -0
- alchemize_ai-0.1.0/examples/jax_to_pytorch_mlp.py +66 -0
- alchemize_ai-0.1.0/examples/mingpt_enzyme/.gitignore +2 -0
- alchemize_ai-0.1.0/examples/mingpt_enzyme/Cargo.toml +15 -0
- alchemize_ai-0.1.0/examples/mingpt_enzyme/src/bench_grad.rs +50 -0
- alchemize_ai-0.1.0/examples/mingpt_enzyme/src/generated.rs +315 -0
- alchemize_ai-0.1.0/examples/mingpt_enzyme/src/main.rs +80 -0
- alchemize_ai-0.1.0/examples/mingpt_enzyme/validate_pytorch.py +207 -0
- alchemize_ai-0.1.0/examples/mingpt_rust_output/bench.rs +35 -0
- alchemize_ai-0.1.0/examples/mingpt_rust_output/generated.rs +362 -0
- alchemize_ai-0.1.0/examples/mingpt_to_rust.py +346 -0
- alchemize_ai-0.1.0/examples/pytorch_to_jax_mlp.py +70 -0
- alchemize_ai-0.1.0/examples/pytorch_to_rust_mlp.py +76 -0
- alchemize_ai-0.1.0/examples/run_benchmark.py +220 -0
- alchemize_ai-0.1.0/examples/stan_01_normal.py +45 -0
- alchemize_ai-0.1.0/examples/stan_02_hierarchical.py +50 -0
- alchemize_ai-0.1.0/examples/stan_pymc_01_normal.py +58 -0
- alchemize_ai-0.1.0/examples/stan_pymc_02_hierarchical.py +73 -0
- alchemize_ai-0.1.0/notebooks/overview.py +488 -0
- alchemize_ai-0.1.0/plots/benchmark_comparison.png +0 -0
- alchemize_ai-0.1.0/plots/opt_progress_hierarchical.png +0 -0
- alchemize_ai-0.1.0/plots/opt_progress_normal.png +0 -0
- alchemize_ai-0.1.0/plots/opt_timeline_hierarchical.png +0 -0
- alchemize_ai-0.1.0/plots/opt_timeline_normal.png +0 -0
- alchemize_ai-0.1.0/plots/opt_waterfall_hierarchical.png +0 -0
- alchemize_ai-0.1.0/plots/opt_waterfall_normal.png +0 -0
- alchemize_ai-0.1.0/pyproject.toml +47 -0
- alchemize_ai-0.1.0/python +53 -0
- alchemize_ai-0.1.0/rust_template/Cargo.toml +15 -0
- alchemize_ai-0.1.0/rust_template/src/main.rs +72 -0
- alchemize_ai-0.1.0/test_mlx.py +57 -0
- alchemize_ai-0.1.0/test_mlx_vs_cpu.py +66 -0
- alchemize_ai-0.1.0/tests/__init__.py +0 -0
- alchemize_ai-0.1.0/tests/conftest.py +61 -0
- alchemize_ai-0.1.0/tests/test_compiler.py +472 -0
- alchemize_ai-0.1.0/tests/test_exporter.py +386 -0
- alchemize_ai-0.1.0/tests/test_jax_pytorch.py +358 -0
- alchemize_ai-0.1.0/tests/test_pytorch_rust.py +471 -0
- alchemize_ai-0.1.0/uv.lock +2786 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
concurrency:
|
|
10
|
+
group: ${{ github.workflow }}-${{ github.ref }}
|
|
11
|
+
cancel-in-progress: true
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
lint:
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Install uv
|
|
20
|
+
uses: astral-sh/setup-uv@v4
|
|
21
|
+
with:
|
|
22
|
+
version: "latest"
|
|
23
|
+
|
|
24
|
+
- name: Set up Python
|
|
25
|
+
uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: "3.12"
|
|
28
|
+
|
|
29
|
+
- name: Install dependencies
|
|
30
|
+
run: uv sync --group dev
|
|
31
|
+
|
|
32
|
+
- name: Lint with ruff
|
|
33
|
+
run: |
|
|
34
|
+
uv run ruff check .
|
|
35
|
+
uv run ruff format --check .
|
|
36
|
+
|
|
37
|
+
test:
|
|
38
|
+
runs-on: ubuntu-latest
|
|
39
|
+
strategy:
|
|
40
|
+
matrix:
|
|
41
|
+
python-version: ["3.11", "3.12"]
|
|
42
|
+
steps:
|
|
43
|
+
- uses: actions/checkout@v4
|
|
44
|
+
|
|
45
|
+
- name: Install uv
|
|
46
|
+
uses: astral-sh/setup-uv@v4
|
|
47
|
+
with:
|
|
48
|
+
version: "latest"
|
|
49
|
+
|
|
50
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
51
|
+
uses: actions/setup-python@v5
|
|
52
|
+
with:
|
|
53
|
+
python-version: ${{ matrix.python-version }}
|
|
54
|
+
|
|
55
|
+
- name: Install Rust toolchain
|
|
56
|
+
uses: dtolnay/rust-toolchain@stable
|
|
57
|
+
|
|
58
|
+
- name: Install dependencies
|
|
59
|
+
run: uv sync --group dev
|
|
60
|
+
|
|
61
|
+
- name: Run tests
|
|
62
|
+
env:
|
|
63
|
+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
|
|
64
|
+
run: uv run pytest tests/ -v --tb=short -x
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*'
|
|
7
|
+
workflow_dispatch:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
build:
|
|
11
|
+
name: Build distribution
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: Set up Python
|
|
17
|
+
uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: '3.12'
|
|
20
|
+
|
|
21
|
+
- name: Install build
|
|
22
|
+
run: python -m pip install --upgrade build
|
|
23
|
+
|
|
24
|
+
- name: Build wheel and sdist
|
|
25
|
+
run: python -m build
|
|
26
|
+
|
|
27
|
+
- name: Upload distributions
|
|
28
|
+
uses: actions/upload-artifact@v4
|
|
29
|
+
with:
|
|
30
|
+
name: dist
|
|
31
|
+
path: dist/
|
|
32
|
+
|
|
33
|
+
publish:
|
|
34
|
+
name: Publish to PyPI
|
|
35
|
+
needs: build
|
|
36
|
+
runs-on: ubuntu-latest
|
|
37
|
+
environment:
|
|
38
|
+
name: pypi
|
|
39
|
+
url: https://pypi.org/p/alchemize-ai
|
|
40
|
+
permissions:
|
|
41
|
+
id-token: write
|
|
42
|
+
steps:
|
|
43
|
+
- name: Download distributions
|
|
44
|
+
uses: actions/download-artifact@v4
|
|
45
|
+
with:
|
|
46
|
+
name: dist
|
|
47
|
+
path: dist/
|
|
48
|
+
|
|
49
|
+
- name: Publish to PyPI
|
|
50
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: alchemize-ai
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Alchemize: an AI agent that transpiles between any computational framework via LLM
|
|
5
|
+
Author-email: Thomas Wiecki <thomas.wiecki@pymc-labs.com>
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Requires-Dist: anthropic>=0.52
|
|
9
|
+
Requires-Dist: bridgestan>=2.7.0
|
|
10
|
+
Requires-Dist: click>=8.0
|
|
11
|
+
Requires-Dist: cmdstanpy>=1.3.0
|
|
12
|
+
Requires-Dist: marimo>=0.20.4
|
|
13
|
+
Requires-Dist: numpy
|
|
14
|
+
Requires-Dist: nutpie>=0.16
|
|
15
|
+
Requires-Dist: pymc>=5.28
|
|
16
|
+
Requires-Dist: torch>=2.10.0
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
19
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
20
|
+
Provides-Extra: dl
|
|
21
|
+
Requires-Dist: jax>=0.4; extra == 'dl'
|
|
22
|
+
Requires-Dist: jaxlib>=0.4; extra == 'dl'
|
|
23
|
+
Requires-Dist: torch>=2.0; extra == 'dl'
|
|
24
|
+
Provides-Extra: jax
|
|
25
|
+
Requires-Dist: jax>=0.4; extra == 'jax'
|
|
26
|
+
Requires-Dist: jaxlib>=0.4; extra == 'jax'
|
|
27
|
+
Provides-Extra: test
|
|
28
|
+
Requires-Dist: pytest; extra == 'test'
|
|
29
|
+
Provides-Extra: torch
|
|
30
|
+
Requires-Dist: torch>=2.0; extra == 'torch'
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# Transalchemy
|
|
2
|
+
|
|
3
|
+
An AI agent that acts as a compiler for computational models. It transpiles between probabilistic programming languages (PyMC, Stan), deep learning frameworks (JAX, PyTorch), and compiles to optimized Rust — with numerical validation at every step.
|
|
4
|
+
|
|
5
|
+
**[Read the blog post →](https://twiecki.io/blog/2026/03/10/alchemize/)**
|
|
6
|
+
|
|
7
|
+
## How it works
|
|
8
|
+
|
|
9
|
+
The agent doesn't translate ops mechanically. It reasons about the full computational graph and applies the optimizations a domain expert would: loop fusion, memory pre-allocation, cache-friendly access patterns.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
Source (PyMC/Stan/JAX/PyTorch) → Extract outputs + validation points → Claude agent loop → Target (Rust/PyMC/PyTorch/JAX) → Verify
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
1. **Extract**: Read model to get parameters, transforms, logp graph, and reference values
|
|
16
|
+
2. **Generate**: Claude (as an agent with tools) generates the target implementation
|
|
17
|
+
3. **Verify**: Validate logp + gradients against reference values
|
|
18
|
+
4. **Iterate**: If validation fails, the agent reads errors, inspects data, and fixes code autonomously
|
|
19
|
+
|
|
20
|
+
The agent has four tools: `write_rust_code`, `cargo_build`, `validate_logp`, and `read_file`. It loops until the output compiles and validates correctly. Model-specific "skills" are detected automatically:
|
|
21
|
+
|
|
22
|
+
- **GP (CPU)**: Linear algebra via faer (Cholesky, solves, inverses)
|
|
23
|
+
- **GP (Accelerate)**: Apple Accelerate framework (AMX coprocessor) for Apple Silicon
|
|
24
|
+
- **GP (CUDA)**: GPU-accelerated via cudarc + cuSOLVER for NVIDIA GPUs
|
|
25
|
+
- **ZeroSumNormal**: ZeroSum transform formulas and constraint handling
|
|
26
|
+
- **Stan → Rust**: Stan model extraction via BridgeStan
|
|
27
|
+
- **Stan → PyMC**: Distribution mappings, idiom translation, constraint handling
|
|
28
|
+
- **JAX → PyTorch**: Functional-to-stateful translation, op mapping, weight transposition
|
|
29
|
+
- **PyTorch → JAX**: Stateful-to-functional translation, pure function extraction
|
|
30
|
+
- **PyTorch → Rust**: Neural net to zero-dependency Rust binary, forward + gradient validation
|
|
31
|
+
|
|
32
|
+
Hardware is auto-detected: CUDA → Accelerate (Apple Silicon) → CPU fallback.
|
|
33
|
+
|
|
34
|
+
## Benchmarks
|
|
35
|
+
|
|
36
|
+
### Compilation (Claude Sonnet 4)
|
|
37
|
+
|
|
38
|
+
| Model | Params | Tool Calls | Tokens | Result |
|
|
39
|
+
|---|---|---|---|---|
|
|
40
|
+
| Normal | 2 | 4 | 40K | First try |
|
|
41
|
+
| Linear Regression | 3 | 4 | 54K | First try |
|
|
42
|
+
| Hierarchical | 12 | 8 | 153K | Fixed gradients in 1 retry |
|
|
43
|
+
| GP (ExpQuad) | 3 | 11 | 467K | Passed (GP skill) |
|
|
44
|
+
| ZeroSumNormal | 142 | 9 | 484K | Passed (ZeroSumNormal skill) |
|
|
45
|
+
|
|
46
|
+
### Runtime: logp+dlogp evaluation speed
|
|
47
|
+
|
|
48
|
+
Rust vs nutpie's Numba backend (500K evaluations, lower is better):
|
|
49
|
+
|
|
50
|
+
| Model | Numba (us/eval) | Rust (us/eval) | Speedup |
|
|
51
|
+
|---|---|---|---|
|
|
52
|
+
| Normal (2 params) | 0.96 | 0.14 | **6.8x** |
|
|
53
|
+
| LinReg (3 params) | 1.60 | 0.33 | **4.9x** |
|
|
54
|
+
| Hierarchical (12 params) | 2.63 | 0.76 | **3.5x** |
|
|
55
|
+
| GP regression (3 params) | 116.57 | 35.31 | **3.3x** |
|
|
56
|
+
|
|
57
|
+
Numba column = `numba.cfunc` called from Rust in a tight loop (how nutpie actually works). The AI-compiled Rust is 3-7x faster across all models.
|
|
58
|
+
|
|
59
|
+
### Apple Accelerate (AMX coprocessor) acceleration
|
|
60
|
+
|
|
61
|
+
For GP models on Apple Silicon, the compiler auto-detects the platform and uses Apple's Accelerate framework via direct LAPACK FFI (`dpotrf`, `dpotrs`, `dpotri`). This leverages the AMX coprocessor for hardware-accelerated matrix operations in full f64 precision. Benchmarks on M4 Max, N=200 GP:
|
|
62
|
+
|
|
63
|
+
| Backend | µs/eval | Speedup vs faer |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| Rust + faer (pure Rust) | 487 | 1.0x |
|
|
66
|
+
| Rust + Accelerate (AMX) | 366 | **1.33x** |
|
|
67
|
+
|
|
68
|
+
No extra crate dependencies needed — Accelerate is linked via `build.rs` to the system framework.
|
|
69
|
+
|
|
70
|
+
### Neural network inference: PyTorch → Rust
|
|
71
|
+
|
|
72
|
+
minGPT-nano (3 layers, 3 heads, 48-dim embeddings) transpiled to zero-dependency Rust:
|
|
73
|
+
|
|
74
|
+
| Backend | µs/call | Speedup |
|
|
75
|
+
|---|---|---|
|
|
76
|
+
| PyTorch (eager) | 660 | 1.0x |
|
|
77
|
+
| Rust (transpiled) | 284 | **2.3x** |
|
|
78
|
+
| Rust + Enzyme (forward+backward) | 899 | **3.1x** vs PyTorch fwd+bwd (2777 µs) |
|
|
79
|
+
|
|
80
|
+
Forward pass numerical accuracy: max_diff = 5.36e-07. Enzyme gradients match PyTorch autograd to ~5e-7 (f32 precision).
|
|
81
|
+
|
|
82
|
+
## Install
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
pip install alchemize-ai
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
> Note: the PyPI distribution name is `alchemize-ai` (the `alchemize` name was taken by an abandoned package), but the import name is still `alchemize`:
|
|
89
|
+
>
|
|
90
|
+
> ```python
|
|
91
|
+
> from alchemize import compile_model
|
|
92
|
+
> ```
|
|
93
|
+
|
|
94
|
+
The CLI command is also `alchemize`.
|
|
95
|
+
|
|
96
|
+
## Quick start
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
export ANTHROPIC_API_KEY=sk-ant-...
|
|
100
|
+
pip install alchemize-ai # or, for development: uv sync / pip install -e .
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
import pymc as pm
|
|
105
|
+
from alchemize import compile_model
|
|
106
|
+
|
|
107
|
+
with pm.Model() as model:
|
|
108
|
+
mu = pm.Normal("mu", 0, 10)
|
|
109
|
+
sigma = pm.HalfNormal("sigma", 5)
|
|
110
|
+
y = pm.Normal("y", mu=mu, sigma=sigma, observed=data)
|
|
111
|
+
|
|
112
|
+
result = compile_model(model)
|
|
113
|
+
|
|
114
|
+
if result.success:
|
|
115
|
+
print(f"Compiled in {result.n_attempts} build(s), {result.token_usage['total_tokens']} tokens")
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Examples
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
# Single models
|
|
122
|
+
python examples/01_normal.py
|
|
123
|
+
python examples/02_linear_regression.py
|
|
124
|
+
python examples/03_hierarchical.py
|
|
125
|
+
|
|
126
|
+
# Full benchmark suite
|
|
127
|
+
python examples/run_benchmark.py
|
|
128
|
+
|
|
129
|
+
# logp+dlogp evaluation benchmark (Rust vs nutpie/Numba)
|
|
130
|
+
python examples/bench_logp.py
|
|
131
|
+
|
|
132
|
+
# Stan → PyMC transpilation
|
|
133
|
+
python examples/stan_pymc_01_normal.py
|
|
134
|
+
python examples/stan_pymc_02_hierarchical.py
|
|
135
|
+
|
|
136
|
+
# JAX ↔ PyTorch transpilation
|
|
137
|
+
python examples/jax_to_pytorch_mlp.py
|
|
138
|
+
python examples/pytorch_to_jax_mlp.py
|
|
139
|
+
|
|
140
|
+
# PyTorch → Rust (zero-dependency inference binary)
|
|
141
|
+
python examples/pytorch_to_rust_mlp.py
|
|
142
|
+
|
|
143
|
+
# minGPT → Rust (transformer inference)
|
|
144
|
+
python examples/mingpt_to_rust.py
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Architecture
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
alchemize/
|
|
151
|
+
├── exporter.py # Extract parameters, transforms, logp graph from pm.Model()
|
|
152
|
+
├── compiler.py # Agentic loop: Claude API → Rust code → build → validate
|
|
153
|
+
├── stan_exporter.py # Extract Stan model context via BridgeStan
|
|
154
|
+
├── stan_compiler.py # Stan → Rust agentic compiler
|
|
155
|
+
├── stan_to_pymc.py # Stan → PyMC agentic transpiler
|
|
156
|
+
├── jax_exporter.py # Extract model info from JAX functions
|
|
157
|
+
├── pytorch_exporter.py # Extract model info from PyTorch modules
|
|
158
|
+
├── jax_pytorch_transpiler.py # JAX ↔ PyTorch agentic transpiler
|
|
159
|
+
├── pytorch_rust_transpiler.py # PyTorch → Rust agentic transpiler (zero-dep inference)
|
|
160
|
+
├── nutpie_bridge.py # nutpie integration: compiled Rust → nutpie.sample()
|
|
161
|
+
├── benchmark.py # logp eval benchmarks: Rust vs Numba (jit + cfunc)
|
|
162
|
+
└── skills/ # Model-specific knowledge for the AI agent
|
|
163
|
+
├── gp.md # CPU GP (faer Cholesky)
|
|
164
|
+
├── gp_accelerate.md # Apple Silicon GP (Accelerate LAPACK / AMX)
|
|
165
|
+
├── gp_cuda.md # NVIDIA GPU GP (cudarc + cuSOLVER)
|
|
166
|
+
├── zerosumnormal.md
|
|
167
|
+
├── stan.md # Stan → Rust translation knowledge
|
|
168
|
+
├── stan_to_pymc.md # Stan → PyMC translation knowledge
|
|
169
|
+
├── jax_to_pytorch.md # JAX → PyTorch op mapping + idioms
|
|
170
|
+
├── pytorch_to_jax.md # PyTorch → JAX op mapping + idioms
|
|
171
|
+
└── pytorch_to_rust.md # PyTorch → Rust: matmul, activations, backprop
|
|
172
|
+
|
|
173
|
+
rust_template/ # Template Rust project (Cargo.toml, data loading, validation)
|
|
174
|
+
bench_runner/ # Rust lib for calling Numba cfunc from Rust (like nutpie)
|
|
175
|
+
compiled_models/ # Pre-compiled models (normal, linreg, hierarchical, GP, ...)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Stan → PyMC Transpiler
|
|
179
|
+
|
|
180
|
+
The same agentic architecture works for language-to-language translation. Claude generates PyMC code, validates logp against BridgeStan reference values, and iterates until the models match numerically. Used to translate all 120 models from [posteriordb](https://github.com/stan-dev/posteriordb) from Stan to PyMC.
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
from alchemize import transpile_stan_to_pymc
|
|
184
|
+
|
|
185
|
+
stan_code = """
|
|
186
|
+
data { int<lower=0> N; array[N] real y; }
|
|
187
|
+
parameters { real mu; real<lower=0> sigma; }
|
|
188
|
+
model { mu ~ normal(0, 10); sigma ~ normal(0, 5); y ~ normal(mu, sigma); }
|
|
189
|
+
"""
|
|
190
|
+
|
|
191
|
+
result = transpile_stan_to_pymc(stan_code, data={"N": 100, "y": [...]})
|
|
192
|
+
if result.success:
|
|
193
|
+
model = result.get_model(data) # returns a pm.Model
|
|
194
|
+
print(result.pymc_code) # generated Python code
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## JAX ↔ PyTorch Transpiler
|
|
198
|
+
|
|
199
|
+
The same agentic architecture generalizes to deep learning frameworks. Claude translates between JAX's functional style and PyTorch's stateful modules, validating forward pass outputs and gradients at multiple test points.
|
|
200
|
+
|
|
201
|
+
### JAX → PyTorch
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
import jax.numpy as jnp
|
|
205
|
+
from alchemize import transpile_jax_to_pytorch
|
|
206
|
+
|
|
207
|
+
def forward(params, x):
|
|
208
|
+
x = jax.nn.relu(x @ params["w1"] + params["b1"])
|
|
209
|
+
return x @ params["w2"] + params["b2"]
|
|
210
|
+
|
|
211
|
+
params = {"w1": jnp.ones((4, 8)), "b1": jnp.zeros(8),
|
|
212
|
+
"w2": jnp.ones((8, 2)), "b2": jnp.zeros(2)}
|
|
213
|
+
|
|
214
|
+
result = transpile_jax_to_pytorch(forward, params, sample_input=jnp.ones((1, 4)))
|
|
215
|
+
if result.success:
|
|
216
|
+
model = result.get_model(params) # returns a torch.nn.Module
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### PyTorch → JAX
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
import torch.nn as nn
|
|
223
|
+
from alchemize import transpile_pytorch_to_jax
|
|
224
|
+
|
|
225
|
+
class MLP(nn.Module):
|
|
226
|
+
def __init__(self):
|
|
227
|
+
super().__init__()
|
|
228
|
+
self.fc1 = nn.Linear(4, 8)
|
|
229
|
+
self.fc2 = nn.Linear(8, 2)
|
|
230
|
+
|
|
231
|
+
def forward(self, x):
|
|
232
|
+
return self.fc2(torch.relu(self.fc1(x)))
|
|
233
|
+
|
|
234
|
+
result = transpile_pytorch_to_jax(MLP(), sample_input=torch.randn(1, 4))
|
|
235
|
+
if result.success:
|
|
236
|
+
jax_params, forward_fn = result.get_model(param_data)
|
|
237
|
+
output = forward_fn(jax_params, jnp.ones((1, 4)))
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## PyTorch → Rust Transpiler
|
|
241
|
+
|
|
242
|
+
The killer feature for inference deployment. Takes a PyTorch `nn.Module` and generates a **zero-dependency Rust binary** — no ML framework, no Python runtime, just raw f32 math compiled to native code. Parameters are baked in as `const` arrays.
|
|
243
|
+
|
|
244
|
+
The agent uses the same agentic architecture with tools: `write_code` → `cargo_build` → `validate_model` (forward pass + gradient matching). The generated Rust code includes both `forward()` and manual backpropagation for gradient validation.
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
import torch.nn as nn
|
|
248
|
+
from alchemize import transpile_pytorch_to_rust
|
|
249
|
+
|
|
250
|
+
class MLP(nn.Module):
|
|
251
|
+
def __init__(self):
|
|
252
|
+
super().__init__()
|
|
253
|
+
self.fc1 = nn.Linear(4, 8)
|
|
254
|
+
self.fc2 = nn.Linear(8, 2)
|
|
255
|
+
|
|
256
|
+
def forward(self, x):
|
|
257
|
+
return self.fc2(torch.relu(self.fc1(x)))
|
|
258
|
+
|
|
259
|
+
result = transpile_pytorch_to_rust(MLP(), sample_input=torch.randn(1, 4))
|
|
260
|
+
if result.success:
|
|
261
|
+
print(f"Binary at: {result.binary_path}") # Zero-dependency native binary
|
|
262
|
+
result.save("model.rs") # Save the generated Rust code
|
|
263
|
+
```
|