Alchemize: Transpile PyMC to Rust for 3-7x speed-up

Excited to release Alchemize: It takes a PyMC model and emits optimized Rust. But the interesting part is what’s not in the codebase: a compiler.

The biggest complexity in PyMC hides in PyTensor: the computational library that takes your high-level Python model description and compiles it to a low-level backend like C, JAX or Numba. Adding a new low-level target like Rust takes a lot of work: every mathematical operation (addition, log, exp) requires its own Rust implementation. PyTensor currently sits at ~150k LOC. Adding a new backend is usually months of work.

But what if instead you let AI write that low-level code directly, bypassing PyTensor all-together? That’s what Alchemize does: About 500 lines of Python scaffolding, a markdown skill file describing how PyMC models map to Rust, and a reference check that compiles the agent’s output, runs it, and compares log-probabilities and gradients against PyMC. The agent writes Rust, validates, self-corrects until the numbers match. Converges in 3-4 iterations. Final Rust runs 3-7x faster than what highly optimized PyTensor produces.

The speedup isn’t the interesting part. The architecture is: Alchemize puts Claude Code (Agent SDK) into a loop that only exits once the compiled Rust produces the exact same outputs. New hardware targets like CUDA or Apple Accelerate don’t need code changes. They only need a new skill file describing what libraries to use.

The compiler isn’t gone. It’s just written in English now.

Code: GitHub - pymc-labs/alchemize: LLM-based, self-correcting transpiler. Supports JAX, PyTorch, Rust, PyMC, Stan. · GitHub
Blog: https://dub.sh/po1I6uu

3 Likes

The links don’t seem to work.. Here is a direct link to pymc labs blog post :LLM Transpiler: Using AI Agents as Compilers for PyMC and Rust.

Interesting idea! I do wonder if a model could be fine tuned on examples generated by the “transpiler” to create a smaller cheaper model specialized in this use case, but that might not be worth the effort.

2 Likes

Are there performance benefits to generating Rust for each model individually rather than having a Rust compiler, or was it just development speed? Will Claude regenerate the Rust code for a normal distribution for every model or is there some mechanism to reuse common features? Are there plans to develop a similar performance backend that doesn’t cost tokens? It seems like you’ve created a pay-for-performance premium PyMC tier, but the money goes to Anthropic instead of PyMC. Does PyMC get anything out of this deal?

1 Like

@bob-carpenter wrote his perspective on the statmodeling blog: https://statmodeling.stat.columbia.edu/2026/05/14/alchemize-pymcs-model-to-replace-stan-pymc-etc-with-an-llm/

Are there performance benefits to generating Rust for each model individually rather than having a Rust compiler, or was it just development speed?

The main motivation was development speed, but it’s cool that it’s also faster.

Will Claude regenerate the Rust code for a normal distribution for every model or is there some mechanism to reuse common features?

Yes, the skills are essentially a caching mechanism.

Are there plans to develop a similar performance backend that doesn’t cost tokens?

Not currently.

It seems like you’ve created a pay-for-performance premium PyMC tier, but the money goes to Anthropic instead of PyMC. Does PyMC get anything out of this deal?

No.

Thanks for linking, Thomas. Whenever anything AI-related comes up on Gelman’s blog, there’s a knee-jerk reaction of anti-AI sentiment and requests to stop anthropomorphizing. I get the fear around AI and think both short term and long term that’s worth taking seriously. But I don’t get the skepticism given how useful they are for almost everything I do.

2 Likes

Same, it has revolutionized the way I work and I think it has tremendous potential to democratize the Bayesian causal approach we all agree is uniquely powerful, but also uniquely difficult to get right, to ultimately build very powerful decision intelligence systems that can be used by stake-holders. And it doesn’t have to be for building models, we can leave that to the experts (that are also elevated by agents), but just inquiring into the model and getting the right answers.