Fix SparseDenseMultiply gradient when the dense input is a scalar - #2319
Open
Sanjays2402 wants to merge 2 commits into
Open
Fix SparseDenseMultiply gradient when the dense input is a scalar#2319Sanjays2402 wants to merge 2 commits into
Sanjays2402 wants to merge 2 commits into
Conversation
…calar
Multiplying a sparse matrix by a scalar broadcasts the scalar over every
entry, but SparseDenseMultiply.pullback returned the full dense gradient
for the second input. pt.grad then rejected it with
ValueError: SparseDenseMultiply.grad returned a term with 2
dimensions, but 0 are required.
Sum the contributions when the dense input is 0-d so the gradient has the
same number of dimensions as the input. Adds a regression test in
tests/sparse/test_math.py covering both csc and csr.
Closes pymc-devs#1338
ricardoV94
reviewed
Aug 2, 2026
| if y.type.ndim == 0: | ||
| # y was broadcast against every entry of x, so its gradient is the | ||
| # sum of the contributions of all entries. | ||
| gy = gy.sum() |
Member
There was a problem hiding this comment.
do we have a sparse sum we should use instead of summing on the dense?
Author
There was a problem hiding this comment.
Yes — sp_sum handles it, switched to that in 3fdf0ae:
if y.type.ndim == 0:
gy = sp_sum(x * gz, sparse_grad=True)
else:
gy = psb.dense_from_sparse(x * gz)x * gz is sparse, so the previous version densified an M x N array only to reduce it to a scalar. sp_sum(..., sparse_grad=True) reduces over x.data directly and its own grad stays structured, which matches what SparseDenseVectorMultiply.pullback already does one class down (sp_sum(x * gz, axis=0, sparse_grad=True)).
test_MulSD_scalar_grad still fails on the unpatched pullback with the original ValueError: ... returned a term with 2 dimensions, but 0 are required and passes with this. tests/sparse/test_math.py is 63 passed, 6 xfailed.
ricardoV94
approved these changes
Aug 2, 2026
Member
|
Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
SparseDenseMultiply.pullbackreturned the full dense gradient for its second input, but when that input is a scalar it was broadcast over every entry of the sparse matrix, so its gradient must be a scalar.pt.gradrejected the 2-d term withValueError: SparseDenseMultiply.grad returned a term with 2 dimensions, but 0 are required.The gradient is now summed when the dense input is 0-d; the 2-d case is unchanged.Related Issue
MulSDOp gradient raises when dense is scalar #1338Checklist
Type of change
test_MulSD_scalar_grad(csc and csr) fails onmainwith the reported ValueError and passes with the fix. This change was prepared with AI assistance; the regression test was run locally and fails without the fix.