torch icon indicating copy to clipboard operation
torch copied to clipboard

Is it possible to detect if tensor is sparse or dense?

Open egillax opened this issue 3 years ago • 4 comments

I'm trying to make a custom collate_fn where some of my data is sparse and some dense. With the sparse data I want to stack and then use to_dense() but with the dense I only want to stack.

In pytorch there is the function is_sparse which relies on the tensor.layout property.

It doesn't seem like the layout property is exposed here. Is it possible to expose it so I can detect if the tensor is sparse? Or is there already some way to do it?

Regards, Egill

egillax avatar Nov 14 '21 15:11 egillax

@egillax sorry, this is currently not possible, but we will make sure to implement it for the version.

dfalbel avatar Nov 20 '21 12:11 dfalbel

It turns out this would be handy to have for autograd functional (#738 ), so I can add this functionality as part of the PR for that. It should be a pretty straightforward update to Lantern, since the Torch C++ API provides is_sparse() (so we just need a basic wrapper). Or would it be better to make a separate PR for this?

rdinnager avatar Jan 12 '22 19:01 rdinnager

Cool! No need to create a separate PR.

dfalbel avatar Jan 12 '22 19:01 dfalbel

Just adding a reference to the is_sparse() function in C++ so I don't forget where it is (curious that the Python interface doesn't use this and instead uses the layout property, but since R doesn't yet implement layout, going this route seems easier for now). https://github.com/pytorch/pytorch/blob/0032fa772588cd20f2b85324bdeb31ac497bbf29/c10/core/TensorImpl.h#L813-L820

rdinnager avatar Jan 12 '22 19:01 rdinnager