emit_weight_initialiser_3d Subroutine

public subroutine emit_weight_initialiser_3d(name, nslices, nrows, ncols, weight_data, inits, num_inits)

Store a stacked bank of 2D weight matrices as one ONNX tensor.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name
integer, intent(in) :: nslices
integer, intent(in) :: nrows
integer, intent(in) :: ncols
real(kind=real32), intent(in) :: weight_data(:)
type(onnx_initialiser_type), intent(inout), dimension(:) :: inits
integer, intent(inout) :: num_inits

Source Code

  subroutine emit_weight_initialiser_3d( &
       name, nslices, nrows, ncols, weight_data, inits, num_inits)
    !! Store a stacked bank of 2D weight matrices as one ONNX tensor.
    implicit none

    ! Arguments
    character(*), intent(in) :: name
    integer, intent(in) :: nslices, nrows, ncols
    real(real32), intent(in) :: weight_data(:)
    type(onnx_initialiser_type), intent(inout), dimension(:) :: inits
    integer, intent(inout) :: num_inits

    num_inits = num_inits + 1
    inits(num_inits)%name = trim(name)
    inits(num_inits)%data_type = 1
    allocate(inits(num_inits)%dims(3))
    inits(num_inits)%dims = [ nslices, nrows, ncols ]
    allocate(inits(num_inits)%data(size(weight_data)))

    ! Transpose each 2D slice from column-major to row-major before export.
    block
      integer :: d, slice_size
      slice_size = nrows * ncols
      do d = 1, nslices
         call col_to_row_major_2d( &
              weight_data((d-1)*slice_size+1 : d*slice_size), &
              inits(num_inits)%data((d-1)*slice_size+1 : d*slice_size), &
              nrows, ncols)
      end do
    end block

  end subroutine emit_weight_initialiser_3d