Skip to content

Faster NB LL computation if data is known to be sparse #1038

Open

Description

@adamgayoso

We should add an is_sparse param to our distributions (at least NB, with LL computation here)

As an example in Pyro for Poisson, see here

If X is known to be sparse, we'd only have to compute the middle term for those 0 entries (r is inv. disp., m is mean, k is x).

Screen Shot 2021-04-25 at 8 11 43 PM

Activity

  1. LiudengZhang commented on Mar 18, 2026

    @LiudengZhang

    Hi, I'd be interested in picking this up if it's still desired in the current codebase. The approach would be to add an is_sparse flag to log_nb_positive and log_zinb_positive that short-circuits the lgamma(x + theta) computation for zero entries (since it simplifies to lgamma(theta) and cancels with the existing -lgamma(theta) term), following the pattern in the Pyro implementation. Is this optimization still relevant given how the codebase has evolved since 2021?

  2. ori-kron-wis commented on Mar 19, 2026

    @ori-kron-wis
    Collaborator

    Hey @LiudengZhang ,
    Im not sure this is still relevant in the current codebase, unless we do a major refactor:
    By default, the data is already dense by the time it hits log_nb_positive, and there are many places in the codebase where we do .toarray(), which converts sparse scipy arrays to dense numpy arrays if needed, before making them as tensors.

    We do have the input of load_sparse_tensor which should take care of it, so perhaps you can exploit it to the change in the distribution function as well (instead of another flag), but even then, you need to probably take care of the indexing (masking) of zero values and not zero values, which makes things more complicated, like https://github.com/scverse/scvi-tools/pull/1052/changes suggested. Perhaps it will be fine for the CPU run, but it's not typical, as we use GPU's. Not sure about the time advantage. Also then need to validate all models and all scenarios (jax, pyro and so on), as all use it.

    The issue with using sparse tensors for training is an ongoing challenge, and there are groups that are trying to solve it, including scverse Anndata. If it is solved there, we will adjust as well. e.g: https://github.com/rusty1s/pytorch_sparse) but again will need a major refactor.

    Having said that, you can suggest your fix in a PR and prove it gives us an advantage with a relevant small refactor and without affecting the user interface - if it will be the case, we can definitely consider it.

  3. canergen commented on Mar 19, 2026

    @canergen
    Member

    Hi @ori-kron-wis I think you're mixing things up here. Sparse tensor and simplified computation of reconstruction loss if expression is zero are separate things.
    I'm not sure about the speed-up though and whether implementation with torch.mask will be efficient (pyro internally uses this simplified computation if values are zero). Please create a PR if you want to dig into it @LiudengZhang, we can have a look at benchmarking then.

  4. ori-kron-wis commented on Mar 19, 2026

    @ori-kron-wis
    Collaborator

    @canergen If there will be a good solution to run sparse tensors on GPUs efficiently, with anndata support, I expect our loss calculation to be tuned as well. But that's not the point of this issue.

  5. LiudengZhang commented on Mar 19, 2026

    @LiudengZhang

    Thanks for the thorough context, both of you. After digging into the data flow I can see this is a bigger lift than I initially scoped — the dense conversion before the loss function means a simple flag won't cut it. Going to hold off on a PR for now, but I may circle back if I find a clean approach that shows a real speedup.

  6. ori-kron-wis commented on Mar 19, 2026

    @ori-kron-wis
    Collaborator

    yep, makes sense. Will keep it open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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