torch_tensor_from_array_real64_4d Subroutine

public subroutine torch_tensor_from_array_real64_4d(tensor, data_in, device_type, device_index, permute_dims, requires_grad)

Uses

  • proc~~torch_tensor_from_array_real64_4d~~UsesGraph proc~torch_tensor_from_array_real64_4d torch_tensor_from_array_real64_4d iso_c_binding iso_c_binding proc~torch_tensor_from_array_real64_4d->iso_c_binding iso_fortran_env iso_fortran_env proc~torch_tensor_from_array_real64_4d->iso_fortran_env

Return a Torch tensor pointing to data_in array of rank 4 containing data of type real64 This subroutine is part of an interface and should be accessed through torch_tensor_from_array.

Arguments

Type IntentOptional Attributes Name
type(torch_tensor), intent(out) :: tensor

Returned tensor

real(kind=real64), 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 (torch_kCPU or a GPU device type)

integer, intent(in), optional :: device_index

Device index for GPU devices

integer(kind=int32), intent(in), optional :: permute_dims(4)

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, intent(in), optional :: requires_grad

Whether gradients need to be computed for the created tensor


Calls

proc~~torch_tensor_from_array_real64_4d~~CallsGraph proc~torch_tensor_from_array_real64_4d torch_tensor_from_array_real64_4d proc~torch_tensor_from_blob torch_tensor_from_blob proc~torch_tensor_from_array_real64_4d->proc~torch_tensor_from_blob interface~torch_from_blob_c torch_from_blob_c proc~torch_tensor_from_blob->interface~torch_from_blob_c

Called by

proc~~torch_tensor_from_array_real64_4d~~CalledByGraph proc~torch_tensor_from_array_real64_4d torch_tensor_from_array_real64_4d interface~torch_tensor_from_array torch_tensor_from_array interface~torch_tensor_from_array->proc~torch_tensor_from_array_real64_4d

Source Code

  subroutine torch_tensor_from_array_real64_4d(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 : real64

    ! output tensor
    type(torch_tensor), intent(out) :: tensor  !! Returned tensor

    ! inputs
    real(kind=real64), 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(4)
        !! 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(4)
        !! Shape of the unpermuted Fortran array
    integer(int64)        :: fortran_strides(4)
        !! Strides for the Fortran array
    integer(int64)        :: torch_shape(4)
        !! Shape of the Torch tensor
    integer(int64)        :: torch_strides(4)
        !! Strides for the Torch tensor
    integer(c_int), parameter :: dtype = torch_kFloat64  !! Data type
    integer(int32), parameter :: ndims = 4
        !! Number of dimension of input data
    logical                   :: permute_valid(4)
        !! 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 4 and contain numbers 1 to 4.
      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, 4]"
        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_real64_4d