write_json_tensors Subroutine

public subroutine write_json_tensors(unit, section_name, tensors, num_tensors)

Write input/output tensor specifications to JSON

Arguments

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

Output unit receiving the JSON text

character(len=*), intent(in) :: section_name

JSON section name, e.g. input or output

type(onnx_tensor_type), intent(in), dimension(:) :: tensors

Tensor collection to serialise

integer, intent(in) :: num_tensors

Number of populated tensors in the collection


Source Code

  subroutine write_json_tensors(unit, section_name, tensors, num_tensors)
    !! Write input/output tensor specifications to JSON
    implicit none

    ! Arguments
    integer, intent(in) :: unit
    !! Output unit receiving the JSON text
    character(*), intent(in) :: section_name
    !! JSON section name, e.g. input or output
    type(onnx_tensor_type), intent(in), dimension(:) :: tensors
    !! Tensor collection to serialise
    integer, intent(in) :: num_tensors
    !! Number of populated tensors in the collection

    ! Local variables
    integer :: i, j
    !! Tensor and dimension index counters

    write(unit, '(A,A,A)') '    "', trim(section_name), '": ['
    do i = 1, num_tensors
       write(unit, '(A)') '      {'
       write(unit, '(A,A,A)') '        "name": "', &
            trim(adjustl(tensors(i)%name)), '",'
       write(unit, '(A)') '        "type": {'
       write(unit, '(A)') '          "tensorType": {'
       write(unit, '(A,I0,A)') '            "elemType": ', &
            tensors(i)%elem_type, ','
       write(unit, '(A)') '            "shape": {'
       write(unit, '(A)') '              "dim": ['
       if(allocated(tensors(i)%dims))then
          do j = 1, size(tensors(i)%dims)
             write(unit, '(A)') '                {'
             if(allocated(tensors(i)%dim_params))then
                if(len_trim(tensors(i)%dim_params(j)) .gt. 0)then
                   write(unit, '(A,A,A)') '                  "dimParam": "', &
                        trim(adjustl(tensors(i)%dim_params(j))), '"'
                else
                   write(unit, '(A,"""",I0,"""")') &
                        '                  "dimValue": ', &
                        tensors(i)%dims(j)
                end if
             else
                write(unit, '(A,"""",I0,"""")') &
                     '                  "dimValue": ', &
                     tensors(i)%dims(j)
             end if
             if(j .lt. size(tensors(i)%dims))then
                write(unit, '(A)') '                },'
             else
                write(unit, '(A)') '                }'
             end if
          end do
       end if
       write(unit, '(A)') '              ]'
       write(unit, '(A)') '            }'
       write(unit, '(A)') '          }'
       write(unit, '(A)') '        }'
       if(i .lt. num_tensors)then
          write(unit, '(A)') '      },'
       else
          write(unit, '(A)') '      }'
       end if
    end do
    write(unit, '(A,A,A)') '    ]'

  end subroutine write_json_tensors