get_partial_pad1d_val Subroutine

pure subroutine get_partial_pad1d_val(this, upstream_grad, output)

Get the partial derivative for the pad1d operation - raw array version

Arguments

Type IntentOptional Attributes Name
class(array_type), intent(in) :: this
real(kind=real32), intent(in), dimension(:,:) :: upstream_grad
real(kind=real32), intent(out), dimension(:,:) :: output

Source Code

  pure subroutine get_partial_pad1d_val(this, upstream_grad, output)
    !! Get the partial derivative for the pad1d operation - raw array version
    implicit none

    ! Arguments
    class(array_type), intent(in) :: this
    real(real32), dimension(:,:), intent(in) :: upstream_grad
    real(real32), dimension(:,:), intent(out) :: output

    ! Local variables
    integer :: i, m, s
    integer :: idx_in, idx_out
    integer :: input_size, output_size
    integer :: num_samples, num_features
    integer, dimension(3) :: input_shape

    input_shape = [ this%left_operand%shape, size(upstream_grad, dim=2) ]
    num_samples = input_shape(3)
    num_features = input_shape(2)
    input_size = input_shape(1)
    output_size = input_size + 2 * this%indices(2)

    output = 0._real32

    ! Main gradient extraction
    do concurrent( &
         s = 1:num_samples, &
         m = 1:num_features, &
         i = 1:input_size)
       idx_in = i + (m-1) * input_size
       idx_out = i + this%indices(2) + (m-1) * output_size
       output(idx_in, s) = upstream_grad(idx_out, s)
    end do

    ! Handle edge gradients for special padding modes
    if(this%indices(1) .ge. 3 .and. this%indices(1) .le. 5)then
       call accumulate_edge_gradients_1d_val( &
            upstream_grad, output, input_shape, this%indices, this%adj_ja &
       )
    end if

  end subroutine get_partial_pad1d_val