-
Notifications
You must be signed in to change notification settings - Fork 133
Implement BandedDot
Op
#1416
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
base: main
Are you sure you want to change the base?
Implement BandedDot
Op
#1416
Changes from 8 commits
bbf3141
2282161
ae8eff6
b2f68a5
e64d4d3
c979e9d
f1066a9
0302fac
f47d88b
157345c
7d109b9
c18f095
607a871
f6f12aa
e8fe5e3
5344c27
31e9a29
0505c57
2b5c51d
519c933
5754f93
481814f
30fece4
4bd259c
497721e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
import numpy as np | ||
import scipy.linalg as scipy_linalg | ||
from numpy import diag, zeros | ||
from numpy.exceptions import ComplexWarning | ||
|
||
import pytensor | ||
|
@@ -1669,6 +1670,70 @@ def block_diag(*matrices: TensorVariable): | |
return _block_diagonal_matrix(*matrices) | ||
|
||
|
||
class BandedDot(Op): | ||
jessegrabowski marked this conversation as resolved.
Show resolved
Hide resolved
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. Put in blas.py? 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. I saw your message, fine 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. You mean in |
||
__props__ = ("lower_diags", "upper_diags") | ||
gufunc_signature = "(m,n),(n)->(m)" | ||
|
||
def __init__(self, lower_diags, upper_diags): | ||
self.lower_diags = lower_diags | ||
self.upper_diags = upper_diags | ||
|
||
def make_node(self, A, b): | ||
A = as_tensor_variable(A) | ||
B = as_tensor_variable(b) | ||
|
||
out_dtype = pytensor.scalar.upcast(A.dtype, B.dtype) | ||
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. I suspect this is wrong for integer types |
||
output = b.type.clone(dtype=out_dtype)() | ||
|
||
return pytensor.graph.basic.Apply(self, [A, B], [output]) | ||
|
||
def infer_shape(self, fgraph, nodes, shapes): | ||
return [shapes[0][:-1]] | ||
jessegrabowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def perform(self, node, inputs, outputs_storage): | ||
A, b = inputs | ||
m, n = A.shape | ||
|
||
kl = self.lower_diags | ||
ku = self.upper_diags | ||
|
||
A_banded = zeros((kl + ku + 1, n), dtype=A.dtype, order="C") | ||
|
||
for i, k in enumerate(range(ku, -kl - 1, -1)): | ||
A_banded[i, slice(k, None) if k >= 0 else slice(None, n + k)] = diag(A, k=k) | ||
|
||
fn = scipy_linalg.get_blas_funcs("gbmv", dtype=A.dtype) | ||
outputs_storage[0][0] = fn(m=m, n=n, kl=kl, ku=ku, alpha=1, a=A_banded, x=b) | ||
|
||
|
||
def banded_dot(A: TensorLike, b: TensorLike, lower_diags: int, upper_diags: int): | ||
""" | ||
Specialized matrix-vector multiplication for cases when A is a banded matrix | ||
|
||
No type-checking is done on A at runtime, so all data in A off the banded diagonals will be ignored. This will lead | ||
to incorrect results if A is not actually a banded matrix. | ||
|
||
Unlike dot, this function is only valid if b is a vector. | ||
|
||
Parameters | ||
---------- | ||
A: Tensorlike | ||
Matrix to perform banded dot on. | ||
b: Tensorlike | ||
Vector to perform banded dot on. | ||
lower_diags: int | ||
Number of nonzero lower diagonals of A | ||
upper_diags: int | ||
Number of nonzero upper diagonals of A | ||
|
||
Returns | ||
------- | ||
out: Tensor | ||
The matrix multiplication result | ||
""" | ||
return Blockwise(BandedDot(lower_diags, upper_diags))(A, b) | ||
|
||
|
||
__all__ = [ | ||
"cholesky", | ||
"solve", | ||
|
@@ -1683,4 +1748,5 @@ def block_diag(*matrices: TensorVariable): | |
"lu", | ||
"lu_factor", | ||
"lu_solve", | ||
"banded_dot", | ||
] |
Uh oh!
There was an error while loading. Please reload this page.