Skip to content

Optimize matmuls involving block diagonal matrices #1493

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
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
78 changes: 74 additions & 4 deletions pytensor/tensor/rewriting/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
cast,
constant,
get_underlying_scalar_constant_value,
join,
moveaxis,
ones_like,
register_infer_shape,
split,
switch,
zeros_like,
)
Expand Down Expand Up @@ -99,6 +101,7 @@
)
from pytensor.tensor.rewriting.elemwise import apply_local_dimshuffle_lift
from pytensor.tensor.shape import Shape, Shape_i
from pytensor.tensor.slinalg import BlockDiagonal
from pytensor.tensor.subtensor import Subtensor
from pytensor.tensor.type import (
complex_dtypes,
Expand Down Expand Up @@ -167,6 +170,77 @@
return [constant_zero]


@register_stabilize
@node_rewriter([Blockwise])
def local_block_diag_dot_to_dot_block_diag(fgraph, node):
r"""
Perform the rewrite ``dot(block_diag(A, B), C) -> concat(dot(A, C), dot(B, C))``

BlockDiag results in the creation of a matrix of shape ``(n1 * n2, m1 * m2)``. Because dot has complexity
of approximately O(n^3), it's always better to perform two dot products on the smaller matrices, rather than
a single dot on the larger matrix.
"""
if not isinstance(node.op.core_op, BlockDiagonal):
return

def check_for_block_diag(x):
return x.owner and (
isinstance(x.owner.op, BlockDiagonal)
or isinstance(x.owner.op, Blockwise)
and isinstance(x.owner.op.core_op, BlockDiagonal)
)

# Check that the BlockDiagonal is an input to a Dot node:
for client in get_clients_at_depth(fgraph, node, depth=1):
if not isinstance(client.op, Dot):
continue

op = client.op
x, y = client.inputs

if not (check_for_block_diag(x) or check_for_block_diag(y)):
continue

Check warning on line 202 in pytensor/tensor/rewriting/math.py

View check run for this annotation

Codecov / codecov/patch

pytensor/tensor/rewriting/math.py#L202

Added line #L202 was not covered by tests

# Case 1: Only one input is BlockDiagonal. In this case, multiply all components of the block-diagonal with the
# non-block diagonal, and return a new block diagonal
if check_for_block_diag(x) and not check_for_block_diag(y):
components = x.owner.inputs
y_splits = split(
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this and the join along the 0th axis assuming a BlockwiseBlockDiagonal without batch dims?

Also not sure why you look for Dot but not Blockwise of _matrix_matrix_matmul. It doesn't always get rewritten as a Dot.

Copy link
Member

@ricardoV94 ricardoV94 Jun 26, 2025

Choose a reason for hiding this comment

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

I guess because you don't look for batch dot you can only have a BlockDiagonal without batch dims. That's fine but maybe a bit implicit. You can also wait for the useless BlockwiseBlockdiagonal to be rewritten as BlockDiagonal and only track that.

More importantly because you track a regular dot and not the matmul you may have a vector * matrix or matrix * vector product. Does the rewrite handle these correctly?

Copy link
Member Author

Choose a reason for hiding this comment

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

If this is what's implied it's not on purpose. I'll modify it to account for blockwise dot.

There's no canonical dot form we rewrite to in an intermediate step to make reasoning about graphs easier? It seems nuts to have to to look for a bunch of different _matrix_matrix_matmul or _matrix_vec_matmul or dot22 or whatever.

Copy link
Member

Choose a reason for hiding this comment

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

I'm simplifying every blockwise as blockwise 2x2 dot (ie matmul) in #1471

The dot22 and dot22scalar are stuff from the blas pipeline and I've been hesitant to touch it. As first steps I would like to move them after specialize and to get rid of dot22scalar (should just be gemm). Those blas stuff should also work with blockwise but they currently don't.

Anyway if you target blockwise and core 2x2dot in this PR that should be the most robust going forward even if it misses some cases now. I suggest you explicitly exclude the vector matrix dots for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

skipping matrix-vector is a bit of a bummer

Copy link
Member

Choose a reason for hiding this comment

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

then make sure the rewrite works or wait for the PR

Copy link
Member Author

Choose a reason for hiding this comment

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

you're like the teacher from that movie about jazz drumming u-u

Copy link
Member

Choose a reason for hiding this comment

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

which movie? whiplash?

y,
splits_size=[component.shape[-1] for component in components],
n_splits=len(components),
)
new_components = [
op(component, y_split)
for component, y_split in zip(components, y_splits)
]
new_output = join(0, *new_components)

elif not check_for_block_diag(x) and check_for_block_diag(y):
components = y.owner.inputs
x_splits = split(
x,
splits_size=[component.shape[0] for component in components],
n_splits=len(components),
axis=1,
)

new_components = [
op(x_split, component)
for component, x_split in zip(components, x_splits)
]
new_output = join(1, *new_components)

# Case 2: Both inputs are BlockDiagonal. Do nothing
else:
# TODO: If shapes are statically known and all components have equal shapes, we could rewrite
# this case to block_diag(*[dot(comp_1, comp_2) for comp_1, comp_2 in zip(x.owner.inputs, y.owner.inputs)])
continue

Check warning on line 238 in pytensor/tensor/rewriting/math.py

View check run for this annotation

Codecov / codecov/patch

pytensor/tensor/rewriting/math.py#L238

Added line #L238 was not covered by tests

copy_stack_trace(node.outputs[0], new_output)
return {client.outputs[0]: new_output}


@register_canonicalize
@node_rewriter([DimShuffle])
def local_lift_transpose_through_dot(fgraph, node):
Expand Down Expand Up @@ -2496,7 +2570,6 @@
name="add_canonizer_group",
)


register_canonicalize(local_add_canonizer, "shape_unsafe", name="local_add_canonizer")


Expand Down Expand Up @@ -3619,7 +3692,6 @@
)
register_stabilize(logdiffexp_to_log1mexpdiff, name="logdiffexp_to_log1mexpdiff")


# log(sigmoid(x) / (1 - sigmoid(x))) -> x
# i.e logit(sigmoid(x)) -> x
local_logit_sigmoid = PatternNodeRewriter(
Expand All @@ -3633,7 +3705,6 @@
register_canonicalize(local_logit_sigmoid)
register_specialize(local_logit_sigmoid)


# sigmoid(log(x / (1-x)) -> x
# i.e., sigmoid(logit(x)) -> x
local_sigmoid_logit = PatternNodeRewriter(
Expand Down Expand Up @@ -3674,7 +3745,6 @@

register_specialize(local_polygamma_to_tri_gamma)


local_log_kv = PatternNodeRewriter(
# Rewrite log(kv(v, x)) = log(kve(v, x) * exp(-x)) -> log(kve(v, x)) - x
# During stabilize -x is converted to -1.0 * x
Expand Down
81 changes: 81 additions & 0 deletions tests/tensor/rewriting/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
simplify_mul,
)
from pytensor.tensor.shape import Reshape, Shape_i, SpecifyShape, specify_shape
from pytensor.tensor.slinalg import BlockDiagonal
from pytensor.tensor.type import (
TensorType,
cmatrix,
Expand Down Expand Up @@ -4654,3 +4655,83 @@ def test_local_dot_to_mul(batched, a_shape, b_shape):
out.eval({a: a_test, b: b_test}, mode=test_mode),
rewritten_out.eval({a: a_test, b: b_test}, mode=test_mode),
)


@pytest.mark.parametrize("left_multiply", [True, False], ids=["left", "right"])
def test_local_block_diag_dot_to_dot_block_diag(left_multiply):
"""
Test that dot(block_diag(x, y,), z) is rewritten to concat(dot(x, z[:n]), dot(y, z[n:]))
"""
a = tensor("a", shape=(4, 2))
b = tensor("b", shape=(2, 4))
c = tensor("c", shape=(4, 4))
d = tensor("d", shape=(10, 10))
e = tensor("e", shape=(10, 10))

x = pt.linalg.block_diag(a, b, c)

# Test multiple clients are all rewritten
if left_multiply:
out = [x @ d, x @ e]
else:
out = [d @ x, e @ x]

fn = pytensor.function([a, b, c, d, e], out, mode=rewrite_mode)
assert not any(
isinstance(node.op, BlockDiagonal) for node in fn.maker.fgraph.toposort()
)

fn_expected = pytensor.function(
[a, b, c, d, e],
out,
mode=rewrite_mode.excluding("local_block_diag_dot_to_dot_block_diag"),
)

rng = np.random.default_rng()
a_val = rng.normal(size=a.type.shape).astype(a.type.dtype)
b_val = rng.normal(size=b.type.shape).astype(b.type.dtype)
c_val = rng.normal(size=c.type.shape).astype(c.type.dtype)
d_val = rng.normal(size=d.type.shape).astype(d.type.dtype)
e_val = rng.normal(size=e.type.shape).astype(e.type.dtype)

np.testing.assert_allclose(
fn(a_val, b_val, c_val, d_val, e_val),
fn_expected(a_val, b_val, c_val, d_val, e_val),
atol=1e-6 if config.floatX == "float32" else 1e-12,
rtol=1e-6 if config.floatX == "float32" else 1e-12,
)


@pytest.mark.parametrize("rewrite", [True, False], ids=["rewrite", "no_rewrite"])
@pytest.mark.parametrize("size", [10, 100, 1000], ids=["small", "medium", "large"])
def test_block_diag_dot_to_dot_concat_benchmark(benchmark, size, rewrite):
rng = np.random.default_rng()
a_size = int(rng.uniform(0, size))
b_size = int(rng.uniform(0, size - a_size))
c_size = size - a_size - b_size

a = tensor("a", shape=(a_size, a_size))
b = tensor("b", shape=(b_size, b_size))
c = tensor("c", shape=(c_size, c_size))
d = tensor("d", shape=(size,))

x = pt.linalg.block_diag(a, b, c)
out = x @ d

mode = get_default_mode()
if not rewrite:
mode = mode.excluding("local_block_diag_dot_to_dot_block_diag")
fn = pytensor.function([a, b, c, d], out, mode=mode)

a_val = rng.normal(size=a.type.shape).astype(a.type.dtype)
b_val = rng.normal(size=b.type.shape).astype(b.type.dtype)
c_val = rng.normal(size=c.type.shape).astype(c.type.dtype)
d_val = rng.normal(size=d.type.shape).astype(d.type.dtype)

benchmark(
fn,
a_val,
b_val,
c_val,
d_val,
)