Skip to content

docs: guide to PyTensor compilation for PyMC users [first draft / RFC] - #2220

Closed
drbenvincent wants to merge 4 commits into
pymc-devs:mainfrom
drbenvincent:docs/compilation-for-pymc-users
Closed

docs: guide to PyTensor compilation for PyMC users [first draft / RFC]#2220
drbenvincent wants to merge 4 commits into
pymc-devs:mainfrom
drbenvincent:docs/compilation-for-pymc-users

Conversation

@drbenvincent

Copy link
Copy Markdown
Contributor

⚠️ Status: first draft — seeking high-level feedback

Putting this up early to get first impressions and direction before I invest in polishing. Feedback on scope, framing, and accuracy is more useful than wording at this stage; I'm happy to restructure significantly.

Summary

A new user-guide page, "Understanding PyTensor compilation (a guide for PyMC users)", for people who reach PyTensor through PyMC rather than using it directly. It builds a mental model of the compilation pipeline and answers two recurring PyMC questions: "why does sampling take a while to start?" (compilation) and "why is the first run slow but later runs faster?" (caching).

What's included

  • A Mermaid system diagram of the full pipeline (PyMC layer → graph prep → Stage 1 rewriting → backend selection → Stage 2 linking → sampler runtime), with the PyTensor/​sampler boundary made explicit.
  • Stage 1 (rewriting) explained pass by pass — merge/useless, canonicalize, stabilize, specialize/fusion, backend-specific + in-place — in plain language, with collapsible "Under the hood" notes linking to the implementing modules/functions.
  • Stage 2 (linking) explained backend by backend — Numba (default), C/CVM, JAX, PyTorch, MLX, Python VM.
  • How the backend is chosen (backend= / compile_kwargs / config.linker) vs. how the sampler is chosen (nuts_sampler=: pymc/nutpie/numpyro/blackjax), and why they're mostly-independent choices.
  • Toctree entry plus the doc-build deps (sphinx-design, sphinxcontrib-mermaid) and conf.py mermaid config.

Open questions for reviewers

  • Is the user guide the right home, and is the "opinionated, PyMC-first" framing acceptable for the docs?
  • Is the level of internal detail right (the folds reference specific modules/functions/positions)? Too much, too little?
  • Anything technically off — especially the Numba-default story, the sampler/backend coupling, and the caching / "Stage 1 isn't cached" note?

Notes

  • Docs-only change; no code paths touched.
  • Builds locally with python -m sphinx -b html ./doc ./html; cross-references (FunctionGraph, Mode, jax.jit, torch.compile) resolve and the Mermaid diagram + collapsible notes render.

Made with Cursor

drbenvincent and others added 4 commits June 13, 2026 21:29
Add an opinionated User Guide page explaining PyTensor's two-stage
compilation pipeline (graph rewriting then backend linking) from the
perspective of a PyMC user, including a Mermaid system diagram covering
the Numba/C/JAX/PyTorch/MLX/VM backends and the runtime logp+gradient
callable consumed by samplers (PyMC NUTS, nutpie).

Enable the sphinxcontrib-mermaid extension (and render plain ```mermaid
fences via myst_fence_as_directive) so the diagram renders in the docs
build, on GitHub, and in editor previews.

Co-authored-by: Cursor <cursoragent@cursor.com>
Rework the PyMC-user compilation guide to be readable without prior PyTensor
knowledge: explain each Stage 1 rewrite pass (merge/useless, canonicalize,
stabilize, specialize/fusion, backend-specific + in-place) and each Stage 2
backend in plain language with analogies, and move precise module/function
references into collapsible "Under the hood" notes. Clarify how the backend
(backend= / compile_kwargs / config.linker) and the sampler (nuts_sampler=:
pymc/nutpie/numpyro/blackjax) are chosen, and make the PyTensor/sampler
boundary explicit in the diagram.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ricardoV94

ricardoV94 commented Jun 13, 2026

Copy link
Copy Markdown
Member

sounds like a PyMC documentation not PyTensor. There's already one doc answering the "graph" approach part https://www.pymc.io/projects/docs/en/stable/learn/core_notebooks/pymc_pytensor.html

@drbenvincent

Copy link
Copy Markdown
Contributor Author

Thanks @ricardoV94 — agreed, this belongs in the PyMC docs alongside the existing PyMC and PyTensor notebook rather than here.

I've moved it over and reframed it as a companion to that notebook (placed right after it in the core-notebooks toctree): pymc-devs/pymc#8326. Closing this one in favour of that PR.

@drbenvincent
drbenvincent deleted the docs/compilation-for-pymc-users branch June 13, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants