pymc-devs / pymc-devs/pytensor

Add helper for constructing block matrices

Open
#1,100 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement feature request help wanted linalg NumPy compatibility
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

Description

Numpy has np.block, which is gives a nice shorthand for repeated concatenations. For example, say I want to make a block lower-triangle matrix:

$$
D = \begin{bmatrix} A & 0 \
B & C \end{bmatrix}
$$

I can do:

import numpy 
A, B, C = np.ones((3, 4, 4))
B *= 2
C *= 3

zeros = np.zeros((4, 4))
D = np.block([[A, zeros], [B, C]])
print(D)

# Result:
[[1. 1. 1. 1. 0. 0. 0. 0.]
 [1. 1. 1. 1. 0. 0. 0. 0.]
 [1. 1. 1. 1. 0. 0. 0. 0.]
 [1. 1. 1. 1. 0. 0. 0. 0.]
 [2. 2. 2. 2. 3. 3. 3. 3.]
 [2. 2. 2. 2. 3. 3. 3. 3.]
 [2. 2. 2. 2. 3. 3. 3. 3.]
 [2. 2. 2. 2. 3. 3. 3. 3.]]

Obviously you can do this in a million different ways, with np.hstack, np.vstack, np.c_, np.r_r, np.concat, np.stack, etc, etc. But this one is concise and readable.

In the content of pytensor, it's related to rewrites in the same vein as #1044. We could very easily break apart these block matrices and do the matmul in chunks, especially if we see there are zero matrices among the blocks.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading this proposal alongside the rewrite work referenced in issue #1044. Define the helper's supported block-matrix shapes and how it should expose zero blocks for chunked matrix multiplication; done should include the helper and the related rewrite behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.