pydata / pydata/sparse

Memory usage - coords waste

Open
#335 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

support
Dominant language
Python
Stars
668
Forks
141
Avg merge
2d 8h
Merged PRs (30d)
4

Description

Dear developers,

Description
In my code, I'm using sparse for handling large data ( > 10 GB). I noticed a larger memory usage by the sparse library than I expected. Comparing 2D matrix with scipy.sparse I realized that sparse is using a significantly larger amount of memory than scipy.sparse. Below you can find the memory consumption of the small example code included at the bottom (obtained with the memory_profiler library)

Line #    Mem usage    Increment   Line Contents
================================================
 7     99.5 MiB     99.5 MiB   @profile
 8                             def check_conv(N1, N2, N3):
 9    226.4 MiB    126.9 MiB       A = sp.random(N1, N2, density=0.12, format="coo")
10                             
11    447.0 MiB    220.6 MiB       B = sparse.COO.from_scipy_sparse(A)
12    636.3 MiB    189.3 MiB       return B.reshape((N3, N2, N2))

We see a usage of 220 MB by sparse.COO while scipy.sparse uses only 127 MB.
Investigating the memory usage in sparse.COO, I found a large amount of memory used by the lines

246    415.6 MiB    126.1 MiB           self.coords = self.coords.astype(np.intp, copy=False)

and

276    510.1 MiB     94.3 MiB               self._sort_indices()

If I comment line 246 in the file sparse/_coo/core.py then the memory usage is significantly smaller.

Line #    Mem usage    Increment   Line Contents
================================================
 7     99.2 MiB     99.2 MiB   @profile
 8                             def check_conv(N1, N2, N3):
 9    226.3 MiB    127.1 MiB       A = sp.random(N1, N2, density=0.12, format="coo")
10                             
11    383.8 MiB    157.5 MiB       B = sparse.COO.from_scipy_sparse(A)
12    573.1 MiB    189.2 MiB       return B.reshape((N3, N2, N2))

A gain of around 60 MB. My question is, why line 246 in sparse/_coo/core.py seems to copy the memory, while copy=False and how can I avoid it?
Also, do there is a way to avoid the sorting of index in line 276 when converting the matrix from scipy.sparse?

Example Code

from __future__ import division
import numpy as np
import scipy.sparse as sp
import sparse
from memory_profiler import profile

@profile
def check_conv(N1, N2, N3):
    A = sp.random(N1, N2, density=0.12, format="coo")

    B = sparse.COO.from_scipy_sparse(A)
    return B.reshape((N3, N2, N2))

check_conv(453264, 152, 2982)

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

Start with sparse/_coo/core.py around the coords astype call at line 246 and _sort_indices at line 276, then reproduce the reported memory profile using the example with scipy.sparse and sparse.COO.from_scipy_sparse. Determine whether the conversions create avoidable copies or sorting work, and consider the issue complete when the conversion preserves correctness with the reported memory overhead addressed or the behavior is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.