Skip to content

Sparse TensorVariables are not using the Sparse Elemwise methods #272

Open
@ricardoV94

Description

@ricardoV94

Description

I think during the merging of SparseVariable and TensorVariables Type classes still in Aesara aesara-devs/aesara#766, there was a mistake where the Structured methods were not borrowed, instead we always convert to dense with a warning

import numpy as np
import pytensor.tensor as pt
import pytensor.sparse as ps


X_sp = ps.csc_from_dense(pt.arange(5))
X_sp.sin()  # Method sin is not implemented for sparse variables. The variable will be converted to dense.

Either way, we have a sparse Sin implemented, so we should use them:

out = ps.sin(X_sp)
out.eval()  # <1x5 sparse matrix of type '<class 'numpy.float64'>'

def structured_monoid(tensor_op):
# Generic operation to perform many kinds of monoid element-wise
# operations on the non-zeros of a sparse matrix.
# The first parameter must always be a sparse matrix. The other parameters
# must be scalars which will be passed as argument to the tensor_op.
def decorator(f):
def wrapper(*args):
x = as_sparse_variable(args[0])
assert x.format in ("csr", "csc")
xs = [aes.as_scalar(arg) for arg in args[1:]]
data, ind, ptr, _shape = csm_properties(x)
data = tensor_op(data, *xs)
return CSM(x.format)(data, ind, ptr, _shape)
wrapper.__name__ = str(tensor_op.scalar_op)
return wrapper
return decorator

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