Return a Torch tensor pointing to data_in array of rank 3 containing data of type real32
This subroutine is part of an interface and should be accessed through
torch_tensor_from_array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(torch_tensor), | intent(out) | :: | tensor |
Returned tensor |
||
| real(kind=real32), | intent(in), | pointer, contiguous | :: | data_in(:,:,:) |
Input data that tensor will point at |
|
| integer(kind=c_int), | intent(in) | :: | device_type |
Device type the tensor will live on ( |
||
| integer, | intent(in), | optional | :: | device_index |
Device index for GPU devices |
|
| integer(kind=int32), | intent(in), | optional | :: | permute_dims(3) |
Permutation of dimensions to be applied to Fortran data in the resulting tensor.
Takes the form of an array of length |
|
| logical, | intent(in), | optional | :: | requires_grad |
Whether gradients need to be computed for the created tensor |
subroutine torch_tensor_from_array_real32_3d(tensor, data_in, & device_type, device_index, permute_dims, & requires_grad) use, intrinsic :: iso_c_binding, only : c_int, c_loc use, intrinsic :: iso_fortran_env, only : real32 ! output tensor type(torch_tensor), intent(out) :: tensor !! Returned tensor ! inputs real(kind=real32), intent(in), pointer, contiguous :: data_in(:,:,:) !! Input data that tensor will point at integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or a GPU device type) integer, optional, intent(in) :: device_index !! Device index for GPU devices integer(int32), optional, intent(in) :: permute_dims(3) !! Permutation of dimensions to be applied to Fortran data in the resulting tensor. !! Takes the form of an array of length `n` with elements `1` to `n`, where `n` !! is the `rank`. Element `i` indicates which dimension of the Fortran array !! appears as dimension `i` on the Torch tensor. !! e.g. a Fortran array of shape [10, 20, 30] permuted by [2, 3, 1] will result !! in Torch shape [20, 30, 10]. This matches behaviour of `torch.permute()`, !! noting that this is Fortran so we index from 1! logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor ! local data integer(int64) :: fortran_shape(3) !! Shape of the unpermuted Fortran array integer(int64) :: fortran_strides(3) !! Strides for the Fortran array integer(int64) :: torch_shape(3) !! Shape of the Torch tensor integer(int64) :: torch_strides(3) !! Strides for the Torch tensor integer(c_int), parameter :: dtype = torch_kFloat32 !! Data type integer(int32), parameter :: ndims = 3 !! Number of dimension of input data logical :: permute_valid(3) !! Array to check supplied permutation is valid integer :: i fortran_shape = shape(data_in) ! Compute native Fortran strides do i = 1, ndims if (i == 1) then fortran_strides(1) = 1_int64 else fortran_strides(i) = fortran_strides(i - 1) * fortran_shape(i-1) end if end do if (present(permute_dims)) then ! Check that the supplied permutation is valid and raise an error if not. ! Should be of length 3 and contain numbers 1 to 3. permute_valid = .false. do i = 1, ndims if (permute_dims(i) < 1 .or. permute_dims(i) > ndims) then error stop "Invalid permute_dims: element out of range [1, 3]" end if if (permute_valid(permute_dims(i))) then error stop "Invalid permute_dims: duplicate dimension" end if permute_valid(permute_dims(i)) = .true. end do ! permute shape of torch tensor to match permutation requested ('transpose') do i = 1, ndims torch_shape(i) = fortran_shape(permute_dims(i)) torch_strides(i) = fortran_strides(permute_dims(i)) end do else ! Keep Torch shape and strides same as Fortran torch_shape = fortran_shape torch_strides = fortran_strides end if call torch_tensor_from_blob(tensor, c_loc(data_in), ndims, torch_shape, & torch_strides, dtype, device_type, device_index, & requires_grad) end subroutine torch_tensor_from_array_real32_3d