torch_tensor_from_array_int8_1d Subroutine

public subroutine torch_tensor_from_array_int8_1d(tensor, data_in, layout, device_type, device_index, requires_grad)

Return a Torch tensor pointing to data_in array of rank 1 containing data of type int8

Arguments

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

Returned tensor

integer(kind=int8), intent(in), target :: data_in(:)

Input data that tensor will point at

integer(kind=ftorch_int), intent(in) :: layout(1)

Control order of indices

integer(kind=c_int), intent(in) :: device_type

Device type the tensor will live on (torch_kCPU or torch_kCUDA)

integer(kind=c_int), intent(in), optional :: device_index

device index to use for torch_kCUDA case

logical, intent(in), optional :: requires_grad

Whether gradients need to be computed for the created tensor


Source Code

  subroutine torch_tensor_from_array_int8_1d(tensor, data_in, layout, &
                                                        device_type, device_index, requires_grad)
    use, intrinsic :: iso_c_binding, only : c_bool, c_float, c_int, c_int64_t, c_loc
    use, intrinsic :: iso_fortran_env, only : int8

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

    ! inputs
    integer(kind=int8), intent(in), target :: data_in(:)  !! Input data that tensor will point at
    integer(ftorch_int), intent(in)      :: layout(1)  !! Control order of indices
    integer(c_int), intent(in)           :: device_type    !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`)
    integer(c_int), optional, intent(in) :: device_index   !! device index to use for `torch_kCUDA` case
    logical, optional, intent(in)        :: requires_grad  !! Whether gradients need to be computed for the created tensor

    ! local data
    integer(c_int64_t)        :: tensor_shape(1)            !! Shape of the tensor
    integer(c_int), parameter :: dtype = torch_kInt8  !! Data type
    integer(c_int64_t)        :: strides(1)                 !! Strides for accessing data
    integer(c_int), parameter :: ndims = 1                  !! Number of dimension of input data
    integer(ftorch_int)       :: i
    integer(c_int)            :: device_index_value
    logical :: requires_grad_value  !! Whether gradients need to be computed for the created tensor

    ! Process optional arguments
    if (present(device_index)) then
      device_index_value = device_index
    else if (device_type == torch_kCPU) then
      device_index_value = -1
    else
      device_index_value = 0
    endif

    if (.not. present(requires_grad)) then
      requires_grad_value = .false.
    else
      requires_grad_value = requires_grad
    end if

    tensor_shape = shape(data_in)

    strides(layout(1)) = 1
    do i = 2, ndims
      strides(layout(i)) = strides(layout(i - 1)) * tensor_shape(layout(i - 1))
    end do

    tensor%p = torch_from_blob_c(c_loc(data_in), ndims, tensor_shape,          &
                                 strides, dtype, device_type,                  &
                                 device_index_value,                           &
                                 logical(requires_grad_value, c_bool))

  end subroutine torch_tensor_from_array_int8_1d