torch_tensor_to_array_int8_2d Subroutine

public subroutine torch_tensor_to_array_int8_2d(tensor, data_out, sizes)

Return the array data associated with a Torch tensor of rank 2 and data type int8

Arguments

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

Returned tensor

integer(kind=int8), intent(out), pointer :: data_out(:,:)

Pointer to tensor data

integer, intent(in), optional :: sizes(2)

Number of entries for each rank


Source Code

  subroutine torch_tensor_to_array_int8_2d(tensor, data_out, sizes)
    use, intrinsic :: iso_c_binding, only : c_int, c_int64_t, c_loc
    use, intrinsic :: iso_fortran_env, only : int8, int64
    type(torch_tensor), intent(in) :: tensor !! Returned tensor
    integer(kind=int8), pointer, intent(out) :: data_out(:,:) !! Pointer to tensor data
    integer, optional, intent(in) :: sizes(2) !! Number of entries for each rank
    integer(kind=int64), allocatable :: my_shape(:) !! Number of entries for each rank

    ! Local data
    integer(c_int), parameter :: c_dtype = torch_kInt8 !! Data type
    type(c_ptr) :: cptr

    my_shape = tensor%get_shape()

    if (present(sizes)) then
      if (.not. all(my_shape == sizes)) then
        write(*,*) 'Error :: sizes argument does not match shape of tensor'
        write(*,'(A, 2(I0, " "), A)') 'sizes        :: [ ', sizes(:), ']'
        write(*,'(A, 2(I0, " "), A)') 'tensor shape :: [ ', my_shape(:), ']'
        stop 1
      end if
    end if

    ! Have the data_out array point to the Tensor data
    cptr = torch_to_blob_c(tensor%p, c_dtype)
    call c_f_pointer(cptr, data_out, my_shape)

  end subroutine torch_tensor_to_array_int8_2d