-
Notifications
You must be signed in to change notification settings - Fork 187
Add numba dispatch for SVD #2136
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
Open
jessegrabowski
wants to merge
1
commit into
pymc-devs:main
Choose a base branch
from
jessegrabowski:numba-svd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,10 @@ | |
| schur_complex, | ||
| schur_real, | ||
| ) | ||
| from pytensor.link.numba.dispatch.linalg.decomposition.svd import ( | ||
| _svd_gesdd_full, | ||
| _svd_gesdd_no_uv, | ||
| ) | ||
| from pytensor.tensor.linalg.decomposition.cholesky import Cholesky | ||
| from pytensor.tensor.linalg.decomposition.eigen import Eig, Eigh, Eigvalsh | ||
| from pytensor.tensor.linalg.decomposition.lu import LU, LUFactor, PivotToPermutations | ||
|
|
@@ -61,14 +65,14 @@ def numba_funcify_SVD(op, node, **kwargs): | |
| if discrete_input and config.compiler_verbose: | ||
| print("SVD requires casting discrete input to float") # noqa: T201 | ||
|
|
||
| # np.linalg.svd always returns real-valued singular values, even for complex input. | ||
| # The Op may declare s as complex (matching input dtype), but numba returns the real | ||
| # component dtype, so we must match that to avoid type unification errors. | ||
| # Casting discrete input to float allocates a new buffer, so in-place is moot. | ||
| effective_overwrite_a = op.overwrite_a and not discrete_input | ||
|
|
||
| matrix_dtype = out_dtype | ||
| if out_dtype.kind == "c": | ||
| s_dtype = np.dtype(f"f{out_dtype.itemsize // 2}") | ||
| else: | ||
| s_dtype = out_dtype | ||
| # SVD declares S with the real component dtype via linalg_real_output_dtype, | ||
| # so the s output's own dtype is the right answer for both real and complex | ||
| # input. | ||
| s_dtype = np.dtype(node.outputs[1 if compute_uv else 0].dtype) | ||
|
|
||
| if not compute_uv: | ||
|
|
||
|
|
@@ -80,8 +84,7 @@ def svd(x): | |
| return np.zeros((k,), dtype=s_dtype) | ||
| if discrete_input: | ||
| x = x.astype(out_dtype) | ||
| _, ret, _ = np.linalg.svd(x, full_matrices) | ||
| return ret | ||
| return _svd_gesdd_no_uv(x, overwrite_a=effective_overwrite_a) | ||
|
|
||
| else: | ||
|
|
||
|
|
@@ -90,8 +93,8 @@ def svd(x): | |
| if x.size == 0: | ||
| m, n = x.shape | ||
| k = min(m, n) | ||
| # The LAPACK dispatch returns matrices in fortran order. To match this for the empty cases, | ||
| # build flip the shape inputs to np.zeros and transpose. | ||
| # LAPACK returns matrices in fortran order; build the empty | ||
| # returns with reversed shape + transpose to match. | ||
| if full_matrices: | ||
| return ( | ||
| np.zeros((m, m), dtype=matrix_dtype).T, | ||
|
|
@@ -106,9 +109,13 @@ def svd(x): | |
| ) | ||
| if discrete_input: | ||
| x = x.astype(out_dtype) | ||
| return np.linalg.svd(x, full_matrices) | ||
| return _svd_gesdd_full( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth a (one time) bench we are doing better than the numba dispatch? |
||
| x, | ||
| full_matrices=full_matrices, | ||
| overwrite_a=effective_overwrite_a, | ||
| ) | ||
|
|
||
| cache_version = 1 | ||
| cache_version = 2 | ||
| return svd, cache_version | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hum? if you skip overwrite a in integer inputs you do two copies? the one to float and the one for the output?
you could say that if you cast you can always in place because it's a fresh buffer? opposite of what you did.
(does astype accept order=F). if not we should maybe implement our own version that does.
Actually I've missed this opt in other places?
For another time, should these cast be part of the graph? like expand dims that elemwise adds? then they 1) could be fused or rendered useless or what have you and 2) the numba dispatchers never need to worry about it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me check