torch_tensor_subtract Function

public function torch_tensor_subtract(tensor1, tensor2) result(output)

Uses

  • proc~~torch_tensor_subtract~~UsesGraph proc~torch_tensor_subtract torch_tensor_subtract iso_c_binding iso_c_binding proc~torch_tensor_subtract->iso_c_binding

Overloads subtraction operator for two tensors.

Arguments

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

First tensor for the subtraction

type(torch_tensor), intent(in) :: tensor2

Second tensor for the subtraction

Return Value type(torch_tensor)

Tensor to hold the difference


Calls

proc~~torch_tensor_subtract~~CallsGraph proc~torch_tensor_subtract torch_tensor_subtract proc~torch_tensor_empty torch_tensor_empty proc~torch_tensor_subtract->proc~torch_tensor_empty proc~torch_tensor_get_device_index torch_tensor%torch_tensor_get_device_index proc~torch_tensor_subtract->proc~torch_tensor_get_device_index proc~torch_tensor_get_device_type torch_tensor%torch_tensor_get_device_type proc~torch_tensor_subtract->proc~torch_tensor_get_device_type proc~torch_tensor_get_dtype torch_tensor%torch_tensor_get_dtype proc~torch_tensor_subtract->proc~torch_tensor_get_dtype proc~torch_tensor_get_rank torch_tensor%torch_tensor_get_rank proc~torch_tensor_subtract->proc~torch_tensor_get_rank proc~torch_tensor_get_shape torch_tensor%torch_tensor_get_shape proc~torch_tensor_subtract->proc~torch_tensor_get_shape proc~torch_tensor_requires_grad torch_tensor%torch_tensor_requires_grad proc~torch_tensor_subtract->proc~torch_tensor_requires_grad proc~torch_tensor_get_shape->proc~torch_tensor_get_rank

Called by

proc~~torch_tensor_subtract~~CalledByGraph proc~torch_tensor_subtract torch_tensor_subtract interface~operator (-) operator (-) interface~operator (-)->proc~torch_tensor_subtract

Source Code

  function torch_tensor_subtract(tensor1, tensor2) result(output)
    use, intrinsic :: iso_c_binding, only : c_associated
    type(torch_tensor), intent(in) :: tensor1  !! First tensor for the subtraction
    type(torch_tensor), intent(in) :: tensor2  !! Second tensor for the subtraction
    type(torch_tensor) :: output               !! Tensor to hold the difference

    interface
      subroutine torch_tensor_subtract_c(output_c, tensor1_c, tensor2_c) &
          bind(c, name = 'torch_tensor_subtract')
        use, intrinsic :: iso_c_binding, only : c_ptr
        implicit none
        type(c_ptr), value, intent(in) :: output_c
        type(c_ptr), value, intent(in) :: tensor1_c
        type(c_ptr), value, intent(in) :: tensor2_c
      end subroutine torch_tensor_subtract_c
    end interface

    if (tensor1%get_device_type() /= tensor2%get_device_type()) then
      write(*,*) "Error :: cannot subtract tensors with different device types"
      stop 1
    end if

    if (.not. c_associated(output%p)) then
      call torch_tensor_empty(output, tensor1%get_rank(), tensor1%get_shape(), &
                              tensor1%get_dtype(), tensor1%get_device_type(), &
                              device_index=tensor1%get_device_index(), &
                              requires_grad=tensor1%requires_grad())
    end if
    call torch_tensor_subtract_c(output%p, tensor1%p, tensor2%p)
  end function torch_tensor_subtract