Skip to content

Sparse MulSD Op gradient raises when dense is scalar #1338

Open
@jessegrabowski

Description

@jessegrabowski

Description

Reported here on the forum. Reproducible example:

import pytensor.tensor as pt
from pytensor import sparse

W_pt = sparse.csc_from_dense(pt.eye(5))
x = pt.dscalar('x')
z = (W_pt * x)
gz = pt.grad(z.sum(), x)

Traceback:

[<ipython-input-6-e4b66837fcca>](https://localhost:8080/#) in <cell line: 0>()
      6 x = pt.dscalar('x')
      7 z = (W_pt * x)
----> 8 gz = pt.grad(z.sum(), x)

3 frames

[/usr/local/lib/python3.11/dist-packages/pytensor/gradient.py](https://localhost:8080/#) in access_grad_cache(var)
   1511 
   1512                         if hasattr(var, "ndim") and term.ndim != var.ndim:
-> 1513                             raise ValueError(
   1514                                 f"{node.op}.grad returned a term with"
   1515                                 f" {int(term.ndim)} dimensions, but {int(var.ndim)} are required."

ValueError: MulSD.grad returned a term with 2 dimensions, but 0 are required.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions