pymc-devs / pymc-devs/pytensor

Sparse TensorVariables are not using the Sparse Elemwise methods

Open
#272 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement sparse variables
Dominant language
Python
Stars
644
Forks
208
Avg merge
2d 14h
Merged PRs (30d)
16

Description

Description

I think during the merging of SparseVariable and TensorVariables Type classes still in Aesara https://github.com/aesara-devs/aesara/pull/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'>'

https://github.com/pymc-devs/pytensor/blob/0b632bdb64b9a5f8e0fe8e98491ce2a6f2bca27f/pytensor/sparse/basic.py#L3028-L3051

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

Reproduce the sparse TensorVariable example from the issue, then inspect pytensor/sparse/basic.py at lines 3028-3051 and the Structured methods mentioned in the report. Compare X_sp.sin() with ps.sin(X_sp); done means the method uses the sparse implementation, returns a sparse result, and no longer warns about dense conversion.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.