Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dot kron rewrite #1090

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tanish1729
Copy link
Contributor

@tanish1729 tanish1729 commented Nov 14, 2024

Description

Adds a rewrite for kron(a,b) @ c -> (b @ c.reshape @ a.T).ravel

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

📚 Documentation preview 📚: https://pytensor--1090.org.readthedocs.build/en/1090/

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.11%. Comparing base (33a4d48) to head (c1567c3).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1090   +/-   ##
=======================================
  Coverage   82.11%   82.11%           
=======================================
  Files         183      183           
  Lines       47959    47972   +13     
  Branches     8635     8636    +1     
=======================================
+ Hits        39381    39394   +13     
  Misses       6411     6411           
  Partials     2167     2167           
Files with missing lines Coverage Δ
pytensor/tensor/rewriting/linalg.py 91.66% <100.00%> (+0.29%) ⬆️

pytensor/tensor/rewriting/linalg.py Show resolved Hide resolved
@register_canonicalize
@register_stabilize
@node_rewriter([Dot])
def rewrite_dot_kron(fgraph, node):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a docstring and typehints

tests/tensor/rewriting/test_linalg.py Show resolved Hide resolved
@@ -906,3 +906,29 @@ def test_rewrite_cholesky_diag_to_sqrt_diag_not_applied():
f_rewritten = function([x], z_cholesky, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
assert any(isinstance(node.op, Cholesky) for node in nodes)


def test_dot_kron_rewrite():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test with batch dims. I am worried the use of e.g. ravel in the rewrite will cause problems.

@jessegrabowski
Copy link
Member

The name I found for the identity we're exploiting here is vec-trick, so we can use that in the naming instead of "clever way", which doesn't say anything

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.

Add rewrite to optimize dot(kron(a, b), c)
2 participants