torch_tensor_ones Subroutine

public subroutine torch_tensor_ones(tensor, ndims, tensor_shape, dtype, device_type, device_index, requires_grad)

Uses

  • proc~~torch_tensor_ones~~UsesGraph proc~torch_tensor_ones torch_tensor_ones iso_c_binding iso_c_binding proc~torch_tensor_ones->iso_c_binding

Returns a tensor filled with the scalar value 1.

Arguments

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

Returned tensor

integer(kind=int32), intent(in) :: ndims

Number of dimensions of the tensor

integer(kind=int64), intent(in) :: tensor_shape(ndims)

Shape of the tensor

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

Data type of the tensor

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

logical, intent(in), optional :: requires_grad

Whether gradients need to be computed for the created tensor


Source Code

  subroutine torch_tensor_ones(tensor, ndims, tensor_shape, dtype, &
                               device_type, device_index, requires_grad)
    use, intrinsic :: iso_c_binding, only : c_bool, c_int, c_int64_t
    type(torch_tensor), intent(out) :: tensor     !! Returned tensor
    integer(int32), intent(in)      :: ndims      !! Number of dimensions of the tensor
    integer(int64), intent(in)      :: tensor_shape(ndims)   !! Shape of the tensor
    integer(c_int), intent(in)      :: dtype        !! Data type of the tensor
    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
    logical, optional, intent(in) :: requires_grad
        !! Whether gradients need to be computed for the created tensor
    integer(c_int)                  :: ndims_c_int  !! C-type ndims
    integer(c_int64_t)              :: tensor_shape_c_int(ndims)  !! C-type tensor_shape
    integer(c_int)                  :: device_index_value    !! device index used
    logical(c_bool)                 :: requires_grad_value
        !! Whether gradients need to be computed for the created tensor

    interface
      function torch_ones_c(ndims_c, tensor_shape_c, dtype_c, &
                            device_type_c, device_index_c, requires_grad_c) result(tensor_c) &
          bind(c, name = "torch_ones")
        use, intrinsic :: iso_c_binding, only : c_bool, c_int, c_int64_t, c_ptr

        implicit none

        integer(c_int), value, intent(in) :: ndims_c
        integer(c_int64_t), intent(in)    :: tensor_shape_c(*)
        integer(c_int), value, intent(in) :: dtype_c
        integer(c_int), value, intent(in) :: device_type_c
        integer(c_int), value, intent(in) :: device_index_c
        logical(c_bool), value, intent(in) :: requires_grad_c
        type(c_ptr)                       :: tensor_c
      end function torch_ones_c
    end interface

    ! 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
    end if

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

    ! Convert public arguments to C-types
    ndims_c_int = ndims
    tensor_shape_c_int(:) = tensor_shape(:)

    tensor%p = torch_ones_c(ndims_c_int, tensor_shape_c_int, dtype, device_type,      &
                            device_index_value, requires_grad_value)
  end subroutine torch_tensor_ones