You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Faster NB LL computation if data is known to be sparse #1038
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?
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.
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.
@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.
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.
Activity
LiudengZhang commentedon Mar 18, 2026
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_sparseflag tolog_nb_positiveandlog_zinb_positivethat short-circuits thelgamma(x + theta)computation for zero entries (since it simplifies tolgamma(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?ori-kron-wis commentedon Mar 19, 2026
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_tensorwhich 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.
canergen commentedon Mar 19, 2026
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.
ori-kron-wis commentedon Mar 19, 2026
@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.
LiudengZhang commentedon Mar 19, 2026
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.
ori-kron-wis commentedon Mar 19, 2026
yep, makes sense. Will keep it open