pymc-devs / pymc-devs/pytensor
Equivalent dots not merged
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 644
- Forks
- 208
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 16
Description
Description
In the example below we end up computing 4 dots, whereas only 3 are needed
import pytensor
import pytensor.tensor as pt
from pytensor.compile.mode import get_default_mode
A = pt.dmatrix('A')
x = pt.col('x')
f = (x.T @ A @ x), A @ x, A.T @ x
fn = pytensor.function([A, x], f, mode=get_default_mode().excluding("BlasOpt"))
fn.dprint()
# dot [id A] 2
# ├─ dot [id B] 1
# │ ├─ Transpose{axes=[1, 0]} [id C] 'x.T' 0
# │ │ └─ x [id D]
# │ └─ A [id E]
# └─ x [id D]
# dot [id F] 3
# ├─ A [id E]
# └─ x [id D]
# dot [id G] 5
# ├─ Transpose{axes=[1, 0]} [id H] 'A.T' 4
# │ └─ A [id E]
# └─ x [id D]
We could use associativity to write (x.T @ A) @ x -> as x.T @ (A @ x), where the inner dot is equivalent to the second output, so they could be merged
Alternatively, we could use the transpose rule to write the third output A.T @ x -> (x.T @ A).T which is the innermost dot in the first output, so they could be merged (with an extra transpose which is just a cheap view anyway).
With associativity we may want to be careful as order can impact a lot on performance. If we know the static shapes we can optimize like einsum does (see also #https://github.com/pymc-devs/pytensor/issues/961), but if we are already computing it anyway then we can't possible be doing worse.
The example above can easily show up in the gradient of a quadratic form graph.
Contributor guide
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.
Research direction
Start with the supplied PyTensor graph example and inspect the optimizer passes that handle dot operations. Reproduce the graph with the shown function and compare its dprint output before and after optimization. Done means equivalent dot expressions are merged without introducing a worse multiplication order, with coverage for the quadratic-form case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- performance
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100