Skip to content

Feat: change internal handling of Zero in hstack/vstack#702

Merged
mrava87 merged 2 commits into
PyLops:devfrom
mrava87:feat-zerostack1
Oct 14, 2025
Merged

Feat: change internal handling of Zero in hstack/vstack#702
mrava87 merged 2 commits into
PyLops:devfrom
mrava87:feat-zerostack1

Conversation

@mrava87
Copy link
Copy Markdown
Collaborator

@mrava87 mrava87 commented Oct 12, 2025

Motivation

PyLops' stacking operators allow one to pass PyLops multiple LinearOperators or numpy/scipy matrices. However, in many practical scenarios, especially for the Block Operator, one may have some zero blocks (either many small ones or few large ones). Currently, one must rely on pylops.Zero to fill those zero blocks. However the inner working of pylops.Zero is such that every time matvec/rmatvec is called a zero array is instantiated. For stacking operators, this is actually redundant as one could simply skip filling part of the output vector. The current approach is particularly problematic when using the CuPy backend as instantiating new arrays on GPU memory is tremendously slow and unefficient (compared to performing arithmetic operations).

To solve this issue, this PR proposes to add a check prior to calling the matvec/rmatvec of an operator and skip it if the operator is of instance Zero

Definition of Done

  • Modify HStack
  • Modify VStack
  • Add tests

⏰ This is a much simpler solution to the one initially proposed in #699

@mrava87 mrava87 mentioned this pull request Oct 12, 2025
5 tasks
@mrava87 mrava87 marked this pull request as ready for review October 12, 2025 21:00
@mrava87 mrava87 requested a review from cako October 13, 2025 17:59
Copy link
Copy Markdown
Collaborator

@cako cako left a comment

Choose a reason for hiding this comment

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

LGTM! I will leave it as approved but I suggest including this behavior in the class documentation and/or the gallery examples. It is important for people to know that if they use the proper operators (Zero as opposed to e.g., MatrixMult) they will get performance benefits.

Also, seems like no need for me to review #699?

@mrava87
Copy link
Copy Markdown
Collaborator Author

mrava87 commented Oct 13, 2025

Thanks!

Sure, let me add something to the example like I had done inhttps://github.com//pull/699

And yes, no need to look at #699... after I implemented that way I started to feel it was an overkill given the minimal overhead of Zero at initialization time, what we want to avoid is calling the matvec/rmatvec.

@mrava87 mrava87 merged commit da4adaa into PyLops:dev Oct 14, 2025
17 checks passed
@mrava87 mrava87 deleted the feat-zerostack1 branch October 14, 2025 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants