[WIP] Permute shape in torch_tensor_from_blob#415
Conversation
…llow shape to be adjusted according to `layout` in order to create a row-major 'transpose' in Torch.
|
Take the data in memory as: This comes from a (column-major) Fortran array with shape Calling This means the 'same' Tensor appears in Torch as it does in Fortran, but requiring strides in memory to move along rows. Calling This 'row-major' access to underlying data preserves the Fortran shape. A more realistic scenario is that users want to use the same array in Torch as in Fortran, but with 'row-major' access to the data instead of Fortran's column-major layout/access. This can be achieved with the new i.e. the transpose of the Fortran array accessed in a 'row-major' way. To conclude: Suppose a user wants to access the Fortran array using the same shape and indexing from Torch, but with row-major layout in memory for internal (to Torch) efficiency. They start with a (column-major) Fortran array with shape appearing in memory as They first transpose the array in Fortran: to appear in Memory as: and then call Exactly as would be expected had we declared this tensor from first principles in Torch. |
|
Just though I will comment since having a look at FTorch for a first time only recently I may be not a bad stand-in for a 'user' ;-) And I confirm that I was expecting However that being said I would also argue that in general the argument Basically to obtain a contiguous view of a shape type(torch_tensor) :: tensor
real, target :: data(2,3,4)
real, target :: data_t(4,3,2)
! Perform transpose on fortran code
data_t = reshape(data, [4,3,2], order=[3,2,1])
! Always use default layout when building a tensor from array
! Creates non-contiguous tensor of shape [4,3,2]
call torch_tensor_from_array(tensor, data_t, torch_kCPU)
! We reshape the tensor to contiguous view of shape [2,3,4] stored on data_t
call torch_tensor_shallow_copy(tensor, torch_tensor_permute(tensor, [3,2,1])The above is of course a bit of a sketch and some details need to be confirmed (i.e. would everything be going ok with finalisation of tensors produced as a result of the function etc.). To summarise: I have been just thinking if we could keep the array construction as simple as possible and instead enable extra features (such as row-major tensors), by exposing more |
|
Pushing this back to v1.2 as not urgent. |
Exploring the functionality of layout following query raised in #412
The aim is to generate row-major tensors with a 'transposed' shape when layout
[n, n-1, ..., 2, 1]is passed.To make this backwards-compatible (non-breaking) I have added a
permute_shapeargument.However, there could be some debate about making this the default, since
permute_shape=.false.with a non-monotonically increasinglayoutarguably produces a 'useless' tensor (see impending description below).It is arguably the expectation of the user (principle of least surprise) that the use of layout should 'transpose' the shape as well as the indexing.
This is complicated by the fact we use an ambiguous example in the docs (section 3) with a 2x2 array where the result of both operations is identical.
IIRC this was always the original intent (though users always had to be explicit when passing the desired shape in) but perhaps got lost in changes to move away from separate
FortranandCroutines and abstracting details from users that occurred before we had explicit tests to catch this sort of thing.TODO:
layout = [2, 1]andpermute_shape=.true.permute_shapeshould be (see above)There is perhaps some discussion to be had around "should we require users to transpose the data themselves before calling this routine, as is the current implementation, or should we instead make the Fortran transpose part of the internals?"
I.e. providing
torch_tensor_from_array()instead with arow-major=.true.argument that will perform the Fortran transpose ([m, n] -> [n, m]) and then calculate appropriate shape ([m, n]) and strides ([n, 1]) to appear in the same in Torch as it does in Fortran.