FruitVinegar:

Now I’m restoring tensor with these decomposed components. It’s quite confusing. I’m looking for suitable pytorch API now(like masked_fill) but all I found is not suitable…
This is my current idea. and if you have a similar idea or knowledge about pytorch API, please answer how to restoring the tensor please.


That’s the pitfall I tried to mention in my last post.
I’m not sure, if there might be a clean way to reconstruct e.g. an array (tensor) with mixed precision data types, as the strides would change based on the current element type.
Let’s think about a plain tensor:
x = torch.tensor([[0., 1., 2., 3.],
                  [4., 5., 6., 7.]])
print(x.size())
> torch.Size([2, 4])
print(x.stride())
> (4, 1)
print(x.nelement())
> 8
print(x.element_size())
> 4

The stride shows, how many element_size() bytes you would have to skip in the contiguous tensor to get to the next index in the corresponding dimension.
E.g. to index the 5 in x, you would have to use x[1, 1], which corresponds to 4+1 elements offset from the beginning of the data.
Also, as you can see the element_size() is defined per tensor, which makes the indexing (and shape changes) via strides possible.
If you are using different data types in the tensor, you would e.g. need to access the size of each elements to index any value in it.
That being said, this approach won’t work for this strided approach using contiguous memory chunks. There might be some other data layout, I’m not aware of, so please let me know, if you find something.