athena__onnx_utils Module

Shared utility routines for ONNX JSON export

Contains base64 encoding, node emission helpers, col→row transpose, and activation/attribute-building utilities used by both the main write_onnx procedure and layer-specific emit_onnx_nodes overrides.



Functions

public function onnx_to_athena_activation(optype) result(name)

Convert an ONNX activation op_type string to the Athena activation name

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: optype

Return Value character(len=64)


Subroutines

public subroutine base64_encode_bytes(bytes, nbytes, output)

Core base64 encoder (allocatable output)

Arguments

Type IntentOptional Attributes Name
integer(kind=int8), intent(in) :: bytes(:)
integer, intent(in) :: nbytes
character(len=:), intent(out), allocatable :: output

public subroutine base64_encode_bytes_fixed(bytes, nbytes, output)

Core base64 encoder (fixed-length output)

Arguments

Type IntentOptional Attributes Name
integer(kind=int8), intent(in) :: bytes(:)
integer, intent(in) :: nbytes
character(len=256), intent(out) :: output

public subroutine build_attributes_json(layer, op_type, attr_json)

Build JSON string for layer attributes

Arguments

Type IntentOptional Attributes Name
class(base_layer_type), intent(in) :: layer

Layer supplying ONNX attribute metadata

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

ONNX operation type used to handle special cases

character(len=4096), intent(out) :: attr_json

Serialized JSON fragment containing the emitted attributes

public subroutine col_to_row_major_2d(data_in, data_out, m, n)

Convert flat column-major [m,n] to flat row-major [m,n] Fortran stores arrays column-major; ONNX rawData expects row-major.

Arguments

Type IntentOptional Attributes Name
real(kind=real32), intent(in) :: data_in(m*n)
real(kind=real32), intent(out) :: data_out(m*n)
integer, intent(in) :: m
integer, intent(in) :: n

public subroutine decode_base64_to_float32(input, values, n)

Decode base64 string to float32 array

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input
real(kind=real32), intent(out), allocatable :: values(:)
integer, intent(out) :: n

public subroutine decode_base64_to_int64(input, values, n)

Decode base64 string to integer array (from 8-byte int64 encoding)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input
integer, intent(out), allocatable :: values(:)
integer, intent(out) :: n

public subroutine emit_activation_node(name, prefix, input_override, nodes, num_nodes, max_nodes)

Emit an activation function node

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Activation name, node prefix, and optional override for the input name

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

Activation name, node prefix, and optional override for the input name

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

Activation name, node prefix, and optional override for the input name

type(onnx_node_type), intent(inout), dimension(:) :: nodes

Node accumulator array

integer, intent(inout) :: num_nodes

Current number of populated nodes

integer, intent(in) :: max_nodes

Maximum number of nodes available in the accumulator

public subroutine emit_constant_float(name, values, dims, nodes, num_nodes, inits, num_inits)

Emit a Constant node producing a float32 tensor

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Constant node and output tensor name

real(kind=real32), intent(in) :: values(:)

Float32 constant values to embed in the node

integer, intent(in) :: dims(:)

Tensor dimensions for the constant value

type(onnx_node_type), intent(inout), dimension(:) :: nodes

Node accumulator array

integer, intent(inout) :: num_nodes

Current number of populated nodes

type(onnx_initialiser_type), intent(inout), dimension(:) :: inits

Initialiser accumulator array (unused, kept for interface symmetry)

integer, intent(inout) :: num_inits

Current number of populated initialisers (unused)

public subroutine emit_constant_int64(name, values, dims, nodes, num_nodes, inits, num_inits)

Emit a Constant node producing an int64 tensor

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Constant node and output tensor name

integer, intent(in) :: values(:)

Constant values and tensor dimensions

integer, intent(in) :: dims(:)

Constant values and tensor dimensions

type(onnx_node_type), intent(inout), dimension(:) :: nodes

Node accumulator array

integer, intent(inout) :: num_nodes

Current number of populated nodes

type(onnx_initialiser_type), intent(inout), dimension(:) :: inits

Initialiser accumulator array (unused, kept for interface symmetry)

integer, intent(inout) :: num_inits

Current number of populated initialisers (unused)

public subroutine emit_constant_of_shape_float(name, shape_input, value, output, nodes, num_nodes, inits, num_inits)

Emit a ConstantOfShape node

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Node name, shape tensor input, and output tensor name

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

Node name, shape tensor input, and output tensor name

real(kind=real32), intent(in) :: value

Fill value to use for the generated tensor

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

Node name, shape tensor input, and output tensor name

type(onnx_node_type), intent(inout), dimension(:) :: nodes

Node accumulator array

integer, intent(inout) :: num_nodes

Current number of populated nodes

type(onnx_initialiser_type), intent(inout), dimension(:) :: inits

Initialiser accumulator array (unused, kept for interface symmetry)

integer, intent(inout) :: num_inits

Current number of populated initialisers (unused)

public subroutine emit_initialisers(layer, prefix, inits, num_inits, max_inits)

Emit initialisers for a learnable layer

Arguments

Type IntentOptional Attributes Name
class(learnable_layer_type), intent(in) :: layer

Learnable layer containing parameter tensors and shape metadata

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

Name prefix used to generate exported initialiser names

type(onnx_initialiser_type), intent(inout), dimension(:) :: inits

Initialiser accumulator array

integer, intent(inout) :: num_inits

Current number of populated initialisers

integer, intent(in) :: max_inits

Maximum number of initialisers available in the accumulator

public subroutine emit_node(op_type, name, out1, attr_json, nodes, num_nodes, in1, in2, in3)

Emit a simple ONNX node (individual string interface) Avoids gfortran array constructor issues

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: op_type

ONNX operation type, node name, output name, and attribute JSON

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

ONNX operation type, node name, output name, and attribute JSON

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

ONNX operation type, node name, output name, and attribute JSON

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

ONNX operation type, node name, output name, and attribute JSON

type(onnx_node_type), intent(inout), dimension(:) :: nodes

Node accumulator array

integer, intent(inout) :: num_nodes

Current number of populated nodes

character(len=*), intent(in), optional :: in1

Optional input tensor names

character(len=*), intent(in), optional :: in2

Optional input tensor names

character(len=*), intent(in), optional :: in3

Optional input tensor names

public subroutine emit_squeeze_node(name, input, axes_input, output, nodes, num_nodes)

Emit a Squeeze node (ONNX opset 13+: axes as input)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: name

Node name, data input, axes input, and output tensor name

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

Node name, data input, axes input, and output tensor name

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

Node name, data input, axes input, and output tensor name

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

Node name, data input, axes input, and output tensor name

type(onnx_node_type), intent(inout), dimension(:) :: nodes

Node accumulator array

integer, intent(inout) :: num_nodes

Current number of populated nodes

public subroutine encode_float32_base64(values, n, output)

Encode float32 array as base64 string (fixed-length output)

Arguments

Type IntentOptional Attributes Name
real(kind=real32), intent(in) :: values(:)
integer, intent(in) :: n
character(len=256), intent(out) :: output

public subroutine encode_float32_base64_alloc(values, n, output)

Encode float32 array as base64 string (allocatable output)

Arguments

Type IntentOptional Attributes Name
real(kind=real32), intent(in) :: values(:)
integer, intent(in) :: n
character(len=:), intent(out), allocatable :: output

public subroutine encode_int64_base64(values, output)

Encode integer array as base64 int64 string (fixed-length output)

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: values(:)
character(len=256), intent(out) :: output

public subroutine encode_int64_base64_alloc(values, n, output)

Encode integer array as base64 int64 string (allocatable output)

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: values(:)
integer, intent(in) :: n
character(len=:), intent(out), allocatable :: output

public subroutine encode_string_base64(str, output)

Encode a string as base64

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
character(len=256), intent(out) :: output

public subroutine parse_space_separated_ints(str, values)

Parse space-separated integers from a string into an allocatable array

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
integer, intent(out), allocatable :: values(:)

public subroutine row_to_col_major_2d(data_in, data_out, m, n)

Convert flat row-major [m,n] to flat column-major [m,n] Inverse of col_to_row_major_2d.

Arguments

Type IntentOptional Attributes Name
real(kind=real32), intent(in) :: data_in(m*n)
real(kind=real32), intent(out) :: data_out(m*n)
integer, intent(in) :: m
integer, intent(in) :: n

public subroutine write_json_initialisers(unit, inits, num_inits)

Write initialisers array to JSON with base64-encoded rawData

Arguments

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

Output unit receiving the JSON text

type(onnx_initialiser_type), intent(in), dimension(:) :: inits

Initialiser collection to serialise

integer, intent(in) :: num_inits

Number of populated initialisers in the collection

public subroutine write_json_nodes(unit, nodes, num_nodes)

Write nodes array to JSON

Arguments

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

Output unit receiving the JSON text

type(onnx_node_type), intent(in), dimension(:) :: nodes

Node collection to serialise

integer, intent(in) :: num_nodes

Number of populated nodes in the collection

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

private subroutine base64_decode_bytes(input, bytes, nbytes)

Core base64 decoder

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input
integer(kind=int8), intent(out), allocatable :: bytes(:)
integer, intent(out) :: nbytes