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

Deprecate block_diag from math module in favor of PyTensor #7132

Merged
merged 6 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 9 additions & 54 deletions pymc/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import pytensor.sparse
import pytensor.tensor as pt
import pytensor.tensor.slinalg
import scipy as sp
import scipy.sparse

from pytensor.graph.basic import Apply
from pytensor.graph.op import Op
Expand Down Expand Up @@ -93,9 +91,8 @@
from pytensor.tensor.linalg import solve_triangular
from pytensor.tensor.nlinalg import matrix_inverse
from pytensor.tensor.special import log_softmax, softmax
from scipy.linalg import block_diag as scipy_block_diag

from pymc.pytensorf import floatX, ix_, largest_common_dtype
from pymc.pytensorf import floatX

__all__ = [
"abs",
Expand Down Expand Up @@ -513,55 +510,9 @@
raise ValueError("Input should be 2 or 3 dimensional")


class BlockDiagonalMatrix(Op):
__props__ = ("sparse", "format")

def __init__(self, sparse=False, format="csr"):
if format not in ("csr", "csc"):
raise ValueError(f"format must be one of: 'csr', 'csc', got {format}")
self.sparse = sparse
self.format = format

def make_node(self, *matrices):
if not matrices:
raise ValueError("no matrices to allocate")
matrices = list(map(pt.as_tensor, matrices))
if any(mat.type.ndim != 2 for mat in matrices):
raise TypeError("all data arguments must be matrices")
if self.sparse:
out_type = pytensor.sparse.matrix(self.format, dtype=largest_common_dtype(matrices))
else:
out_type = pytensor.tensor.matrix(dtype=largest_common_dtype(matrices))
return Apply(self, matrices, [out_type])

def perform(self, node, inputs, output_storage, params=None):
dtype = largest_common_dtype(inputs)
if self.sparse:
output_storage[0][0] = sp.sparse.block_diag(inputs, self.format, dtype)
else:
output_storage[0][0] = scipy_block_diag(*inputs).astype(dtype)

def grad(self, inputs, gout):
shapes = pt.stack([i.shape for i in inputs])
index_end = shapes.cumsum(0)
index_begin = index_end - shapes
slices = [
ix_(
pt.arange(index_begin[i, 0], index_end[i, 0]),
pt.arange(index_begin[i, 1], index_end[i, 1]),
)
for i in range(len(inputs))
]
return [gout[0][slc] for slc in slices]

def infer_shape(self, fgraph, nodes, shapes):
first, second = zip(*shapes)
return [(pt.add(*first), pt.add(*second))]


def block_diagonal(matrices, sparse=False, format="csr"):
r"""See scipy.sparse.block_diag or
scipy.linalg.block_diag for reference
def block_diagonal(*matrices, sparse=False, format="csr"):
Copy link
Member

Choose a reason for hiding this comment

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

We should keep the old signature, missed this before:

Suggested change
def block_diagonal(*matrices, sparse=False, format="csr"):
def block_diagonal(matrices, sparse=False, format="csr"):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated it!

r"""See pt.slinalg.block_diag or
pytensor.sparse.basic.block_diag for reference

Parameters
----------
Expand All @@ -577,4 +528,8 @@
"""
if len(matrices) == 1: # graph optimization
return matrices[0]
return BlockDiagonalMatrix(sparse=sparse, format=format)(*matrices)

Copy link
Member

Choose a reason for hiding this comment

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

Can you add a FutureWarning mentioning this is deprecated in favor of the pytensor function, so we can remove this thin wrapper sometime in the future?

if sparse:
return pytensor.sparse.basic.block_diag(*matrices, format=format)

Check warning on line 533 in pymc/math.py

View check run for this annotation

Codecov / codecov/patch

pymc/math.py#L532-L533

Added lines #L532 - L533 were not covered by tests
else:
return pt.slinalg.block_diag(*matrices)

Check warning on line 535 in pymc/math.py

View check run for this annotation

Codecov / codecov/patch

pymc/math.py#L535

Added line #L535 was not covered by tests
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ sphinx>=1.5
sphinxext-rediraffe
types-cachetools
typing-extensions>=3.7.4
watermark
watermark
Loading