Tensor Slice Views?

Making the swap from TF to pyTorch im really liking how view is implemented on the back end, but im having trouble scouring the source for the advanced indexing implementation.

Does pytorch make new tensors according to a tensor[slice] or is it also a like viewing mask? If the former, can you point me to the source files where i could change the standard to the latter?


The .view() calls basically only change the metadata of the tensor (size and strides).
This means that the data in memory are completely untouched, only the way you interpret (read) them is changed.
Does that answer your question?

hey, yes i am aware of that. I am wondering whether tensor slices like tensorA[32:64] are implemented in a similar way to view(), or if slices do actually copy the data in tensorA.

I want “view slices” for my model, not real numpy copy slices as im doing a lot of tensor slicing.

So the advance indexing does it’s best to avoid copy, but sometimes it has to do.
If you absolutely want to avoid copies, you can use .select() and .narrow() to make sure you will always have copy-free subsets of your tensor.
I don’t remember where the advance indexing code is, but it’s a few hundred lines of cpp somewhere :smiley:


Hi, I have a similar issue than the author, 2 years later :slight_smile:

I wanted to know it it’s possible to index a tensor (on cuda device), without copy.

That is do something like x2 = x[idx] where idx is a sequence of int.

From my understanding, narrow() can’t handle this case if the sequence of int is something like idx=[1, 5, 9].


A Tensor can only be backed with a continuous chunk of memory and with a single stride per dimension.
So unless your indices are spaced in a very specific way, you won’t be able to do that :confused:

1 Like

Hi @albanD ,I have been trying to do weight clustering and have also posed a question for the same at https://discuss.pytorch.org/t/weight-clustering/142155. However,as Patrick rightly points out that, the model size won’t reduce (which is my ultimate goal) because each weight is saved individually. Is there a way of using view to utilize the same memory storage to achieve this? Could you possibly direct me to a way or an example that can help achieve this in reference to the question I posed? As this thread was somewhat relevant with views , I thought of asking it here.