-
Notifications
You must be signed in to change notification settings - Fork 132
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 5 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 |
---|---|---|
|
@@ -1669,6 +1669,98 @@ def block_diag(*matrices: TensorVariable): | |
return _block_diagonal_matrix(*matrices) | ||
|
||
|
||
def _to_banded_form(A, kl, ku): | ||
""" | ||
Convert a full matrix A to LAPACK banded form for gbmv. | ||
|
||
Parameters | ||
---------- | ||
A: np.ndarray | ||
(m, n) banded matrix with nonzero values on the diagonals | ||
kl: int | ||
Number of nonzero lower diagonals of A | ||
ku: int | ||
Number of nonzero upper diagonals of A | ||
|
||
Returns | ||
------- | ||
ab: np.ndarray | ||
(kl + ku + 1, n) banded matrix suitable for LAPACK | ||
""" | ||
A = np.asarray(A) | ||
m, n = A.shape | ||
ab = np.zeros((kl + ku + 1, n), dtype=A.dtype, order="C") | ||
|
||
for i, k in enumerate(range(ku, -kl - 1, -1)): | ||
col_slice = slice(k, None) if k >= 0 else slice(None, n + k) | ||
ab[i, col_slice] = np.diag(A, k=k) | ||
|
||
return ab | ||
|
||
|
||
_dgbmv = scipy_linalg.get_blas_funcs("gbmv", dtype="float64") | ||
_sgbmv = scipy_linalg.get_blas_funcs("gbmv", dtype="float32") | ||
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. This will cause import time overhead to PyTensor. I'm okay paying the extra 3us at runtime instead since virtually nobody will ever use this (or use it in a case where they need those extra us) 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 thought about this as well. It won't stay in the final verison. 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 can exploit 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. Or you can sidestep |
||
|
||
|
||
class BandedDot(Op): | ||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
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)->(n)" | ||
|
||
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().astype(out_dtype) | ||
ricardoV94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return pytensor.graph.basic.Apply(self, [A, B], [output]) | ||
|
||
def perform(self, node, inputs, outputs_storage): | ||
A, b = inputs | ||
m, n = A.shape | ||
alpha = 1 | ||
jessegrabowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
kl = self.lower_diags | ||
ku = self.upper_diags | ||
|
||
A_banded = _to_banded_form(A, kl, ku) | ||
|
||
fn = _dgbmv if A.dtype == "float64" else _sgbmv | ||
outputs_storage[0][0] = fn(m, n, kl, ku, alpha, A_banded, 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 +1775,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.