read_activation_attributes Function

private function read_activation_attributes(unit, iline) result(attributes)

Uses

    • coreutils

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: unit

Unit number for input file

integer, intent(inout), optional :: iline

Indicator for inline reading

Return Value type(onnx_attribute_type), allocatable, dimension(:)

Array of ONNX attributes


Source Code

  function read_activation_attributes(unit, iline) result(attributes)
    use coreutils, only: stop_program, to_lower
    implicit none

    ! Arguments
    integer, intent(in) :: unit
    !! Unit number for input file
    integer, intent(inout), optional :: iline
    !! Indicator for inline reading

    type(onnx_attribute_type), allocatable, dimension(:) :: attributes
    !! Array of ONNX attributes

    ! Local variables
    integer :: i
    !! Loop variable
    character(256) :: buffer
    !! Buffer for reading lines
    character(256) :: err_msg
    !! Error message
    character(20) :: attr_name
    !! Attribute name
    character(20) :: attr_value
    !! Attribute value as string
    integer :: stat
    !! I/O status
    integer :: eq_pos
    !! Position of equals sign
    integer :: n_attrs
    !! Number of attributes
    type(onnx_attribute_type), allocatable, dimension(:) :: temp_attrs
    !! Temporary array for growing attributes
    character(20), dimension(:), allocatable :: names
    !! Array of attribute names
    integer :: iline_
    !! Line number


    ! Initialise empty attributes array
    allocate(attributes(0))
    allocate(names(0))
    iline_ = 0

    ! Read lines until END or END ACTIVATION
    read_loop: do
       read(unit,'(A)',iostat=stat) buffer
       if(stat.ne.0)then
          write(err_msg,'("File encountered error (EoF?) before END ACTIVATION")')
          call stop_program(err_msg)
          return
       end if
       iline_ = iline_ + 1

       ! Skip empty or comment lines
       if(trim(adjustl(buffer)).eq."") cycle read_loop
       if(index(trim(adjustl(buffer)),"#") .eq. 1) cycle read_loop
       if(index(trim(adjustl(buffer)),"!") .eq. 1) cycle read_loop

       ! Check for end of activation block
       if(trim(adjustl(buffer)).eq."END" .or. &
            trim(adjustl(buffer)).eq."END ACTIVATION")then
          exit read_loop
       end if

       ! Look for NAME = VALUE pattern
       eq_pos = scan(buffer, "=")


       if(eq_pos .gt. 0)then
          ! Extract name (everything before =)
          attr_name = to_lower(adjustl(buffer(:eq_pos-1)))
          ! Extract value (everything after =)
          attr_value = adjustl(buffer(eq_pos+1:))


          if(index(trim(adjustl(buffer)),"ACTIVATION") .eq. 1 .and. iline_ .eq. 1)then
             attributes = [ onnx_attribute_type("name", "string", trim(attr_value)) ]
             exit read_loop
          end if

          ! Check if attribute already exists
          if(any(names .eq. attr_name))then
             write(err_msg,'("Duplicate activation attribute name: ''",A,"''")') &
                  trim(attr_name)
             call stop_program(trim(err_msg))
             return
          end if

          ! Grow attributes array
          attributes = [ &
               attributes, &
               onnx_attribute_type( &
                    trim(attr_name), &
                    "float", &
                    trim(attr_value) &
               ) &
          ]
          names = [ names, attr_name ]

       end if
    end do read_loop

    if(present(iline)) iline = iline + iline_

  end function read_activation_attributes