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

Implement destructive in-place rewrites for Cholesky and Solve Ops #577

Closed
wants to merge 3 commits into from

Conversation

jessegrabowski
Copy link
Member

@jessegrabowski jessegrabowski commented Jan 6, 2024

Description

All scipy.linalg functions offer an overwrite_a and/or overwrite_b argument that can enhance performance by re-using the input memory for the outputs. This PR implements re-writes that will set these flags to True at compile time.

These rewrites are also a nice example for the in-place docs here, so I'll update them with an example as a later commit to this PR.

We added a general inplace functionality to Blockwise, that automatically makes the core_op inplace. This will translate to the core_op if the Blockwise is later removed due to being useless (no batch dims actually exist).

Related Issue

Checklist

Type of change

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

pytensor/tensor/slinalg.py Outdated Show resolved Hide resolved
pytensor/tensor/slinalg.py Outdated Show resolved Hide resolved
Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

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

Great! I think we made our lives easier going forward

@ricardoV94
Copy link
Member

Can the Numba Cholesky make use of overwrite?

@jessegrabowski
Copy link
Member Author

jessegrabowski commented Jan 6, 2024

I think so? I added a check to copy the input matrix or not. I need to test it more carefully to make sure it does what I want it to do.

@ricardoV94
Copy link
Member

Numba seems to be computing in integer? Anyway I guess this PR now depends on #578?

@jessegrabowski
Copy link
Member Author

jessegrabowski commented Jan 7, 2024

I had removed a .astype(input.dtype) to the output of Cholesky().perform, so it made the integer test fail. Everything should pass now. #578 is just a general numba speedup, the two shouldn't clash (I hope).

@codecov-commenter
Copy link

codecov-commenter commented Jan 7, 2024

Codecov Report

Attention: Patch coverage is 81.72043% with 17 lines in your changes missing coverage. Please review.

Project coverage is 81.73%. Comparing base (1c2bc8f) to head (cb0ec82).
Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
pytensor/tensor/slinalg.py 77.96% 7 Missing and 6 partials ⚠️
pytensor/tensor/blockwise.py 50.00% 1 Missing and 1 partial ⚠️
pytensor/tensor/rewriting/blockwise.py 92.85% 1 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #577      +/-   ##
==========================================
- Coverage   81.73%   81.73%   -0.01%     
==========================================
  Files         183      183              
  Lines       47734    47814      +80     
  Branches    11611    11635      +24     
==========================================
+ Hits        39016    39080      +64     
- Misses       6523     6532       +9     
- Partials     2195     2202       +7     
Files with missing lines Coverage Δ
pytensor/graph/op.py 88.02% <100.00%> (+0.12%) ⬆️
pytensor/scan/rewriting.py 81.52% <ø> (ø)
pytensor/sparse/rewriting.py 76.15% <ø> (ø)
pytensor/tensor/random/rewriting/basic.py 93.19% <ø> (ø)
pytensor/tensor/rewriting/blas.py 88.21% <ø> (ø)
pytensor/tensor/rewriting/blas_scipy.py 82.35% <ø> (ø)
pytensor/tensor/rewriting/elemwise.py 90.84% <ø> (ø)
pytensor/tensor/rewriting/subtensor.py 90.12% <ø> (ø)
pytensor/typed_list/rewriting.py 100.00% <ø> (ø)
pytensor/tensor/blockwise.py 81.21% <50.00%> (-0.65%) ⬇️
... and 2 more

... and 3 files with indirect coverage changes

@ricardoV94
Copy link
Member

If you are a pro: each commit corresponds to a relevant logical change

You marked that, but the commits seem "dirty". Do you mind if I squash merge?

@jessegrabowski
Copy link
Member Author

I don't mind at all, but I'd rather you explained it to me so I can do it right next time.

I read the link, but it suggested I write War and Peace in each commit message.

@ricardoV94
Copy link
Member

ricardoV94 commented Jan 8, 2024

I read the link, but it suggested I write War and Peace in each commit message.

Fair enough. The biggest point is each commit should be a self-contained logical change, so that you could in theory revert or checkout any of them and the codebase would still make sense as is. Usually stuff like fix typo, run-pre commit, add suggestions from review, intermediate changes that were ultimately not needed, disappear after "cleaning"

@jessegrabowski
Copy link
Member Author

OK let me take a stab at cleaning it up

@ricardoV94
Copy link
Member

ricardoV94 commented Jan 8, 2024

OK let me take a stab at cleaning it up

Advice: backup the branch before you try :)

@jessegrabowski
Copy link
Member Author

I just did an interactive rebase and squashed things together. Is that all I needed to do?

@bwengals
Copy link

bwengals commented Jan 9, 2024

sorry for the delay, ya'll are fast! Taking a look now

@bwengals
Copy link

bwengals commented Jan 9, 2024

Works great for me (running the test you wrote)! Are you planning on doing all of them in this PR? the solves, eig's, etc?

pytensor/tensor/slinalg.py Outdated Show resolved Hide resolved
pytensor/tensor/rewriting/basic.py Outdated Show resolved Hide resolved
@ricardoV94 ricardoV94 changed the title Implement destructive in-place rewrites for linear algebra functions Implement destructive in-place rewrites for Blockwise Ops Jan 19, 2024
pytensor/tensor/slinalg.py Outdated Show resolved Hide resolved
@ricardoV94 ricardoV94 force-pushed the inplace-linalg branch 3 times, most recently from e448896 to 259d3f3 Compare August 30, 2024 14:29
@ricardoV94 ricardoV94 changed the title Implement destructive in-place rewrites for Blockwise Ops Implement destructive in-place rewrites for Cholesky and Solve Ops Aug 30, 2024
@ricardoV94 ricardoV94 force-pushed the inplace-linalg branch 4 times, most recently from 9f7db08 to 48721f4 Compare August 30, 2024 14:54
@ricardoV94 ricardoV94 force-pushed the inplace-linalg branch 3 times, most recently from 119f2e0 to 0a37de5 Compare October 7, 2024 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Add destructive rewrites for Cholesky Op
4 participants