Speed and memory optimizations: rewrites, inplace ops, and gradient graphs
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
- Issue type
- Refactor
- Clarity
- Needs clarification
- Activity status
- Quiet
- Tech stack
- python
- Domain
- machine-learning, performance
Research direction
Begin with the ptgp-local rewrites and the unapproximated GP MLL forward+gradient graph; profile which PyTensor rewrites fire and how memory and inplace behavior change. Compare the optimized graph with the naive graph, focusing on Cholesky, MatrixInverse, solve, and intermediate allocations. Done requires narrowing this broad investigation to a scoped, benchmarked optimization and deciding whether it belongs in ptgp or PyTensor.
Written by the indexing model from the issue text.
Description
Collecting ideas for performance work. ptgp relies on PyTensor's rewrite system to choose efficient algorithms, but we haven't done a thorough audit of what's actually happening at compile time.
The ptgp-local rewrites have already made a significant difference. For the unapproximated GP MLL, the joint forward+gradient graph compiles down to exactly 1 Cholesky and 0 MatrixInverse ops (the naive graph before rewrites has multiple of each). The rewrites that achieve this include PSD-aware slogdet lowering, Cholesky reuse across the graph, and a composite-merge pass that prevents the fusion optimizer from duplicating cubic ops.
Rewrites that are generally useful should be upstreamed into PyTensor. Rewrites that are GP-specific can live locally in ptgp, or live there until they're mature enough to upstream.
Areas to investigate
-
Memory profiling and inplace operations — are Cholesky, solve, and other cubic ops getting inplace flags where they should? I haven't looked closely at whether PyTensor is handling this well. There may be missed inplace opportunities, especially when tensors pass through assumption annotations.
-
PyTensor rewrites — which rewrites are firing on our graphs and which aren't? Are there rewrites we should be registering ourselves (e.g. Woodbury for low-rank-plus-diagonal structure)?
-
Gradient graph quality — PyTensor generates gradient graphs via autodiff, but for known kernels (Matern, ExpQuad, etc.) there are analytic gradient forms. It would be worth comparing the autodiff graphs to the analytic forms to see if there are unnecessary intermediate allocations or missed simplifications.
-
Structured matrix operations — Kronecker, Toeplitz, banded, and block-diagonal solvers all exploit specific matrix structure to reduce the cost of Cholesky / solve below the dense O(N^3) baseline. Which of these structures show up in ptgp's graphs, and could PyTensor dispatch to them?
Advanced / subjective rewrites
PyTensor can arbitrarily manipulate graphs, and some potential rewrites are subjective and situation-specific. The simplest example is the Woodbury lemma: whether to apply it depends on the relative shapes of the matrices involved. A more ambitious example is choosing between Cholesky and iterative solvers (CG, Lanczos) for solve and slogdet (see #11). PyTensor can inspect the incoming matrix shapes at compile time (if the user specifies them) and could potentially choose the algorithm automatically.
Could this be done automatically, or should users choose the approach? Both?
Other libraries handle this at runtime rather than compile time:
- GPyTorch uses a
LinearOperatorclass where structured matrix types (DiagLinearOperator,LowRankRootLinearOperator, etc.) dispatch to specialized algorithms at runtime - GPJax uses CoLA (Compositional Linear Algebra), which combines a linear operator abstraction with over 70 dispatch rules that select algorithms based on detected structure (e.g.
solve[Sum[LowRank, Diagonal]]dispatches to Woodbury automatically)
ptgp's approach could be the compile-time version of the same idea: use shape info and assumption tags in the symbolic graph to choose algorithms after the initial PyTensor graph is created. How should this be implemented?
Open questions
- What tools or approaches are people using to profile PyTensor graph execution?
- Are there other optimization opportunities we should be looking at?
- Dominant language
- Jupyter Notebook
- Stars
- 16
- Forks
- 5
- Avg merge
- 2d 49m
- Merged PRs (30d)
- 6
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pymc-devs/ptgp
-
Difficulty 5/5 Over a week Newbie friendliness 22/100
-
Expose and document VGP variational parameters for fit diagnosticsPossibly taken @bwengals claimed this 58 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
Add VGP: variational Gaussian inference for non-Gaussian likelihoods without inducing pointsPossibly taken @bwengals claimed this 156 days ago. Open
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
pymc
Difficulty 5/5 Over a week Newbie friendliness 25/100
Similar issues
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over timePossibly taken @PabloRoque claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pymc-devs/pymc-extras#839 ·
Maintainers usually reply within 1 day
-
[Bug]: MLX text downloads omit the requested Hub token and cache directoryPossibly taken @abhijithneilabraham claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
vllm-project/vllm-metal#1423 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
CyberTimon/RapidRAW#2303 ·
Maintainers usually reply within 1 day
-
area:asr good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
dmunasingha/lingo-overlay#13 ·
-
wave-followup
Difficulty 2/5 1-3 hours Newbie friendliness 79/100