athena__onnx_read_submodule Submodule

Submodule containing the ONNX import procedures.

This submodule contains the routines that parse the JSON ONNX representation and rebuild ATHENA networks from it.


Uses


Variables

Type Visibility Attributes Name Initial
integer, private, parameter :: MAX_GNN_METADATA = 100
integer, private, parameter :: MAX_ITEMS = 500

Derived Types

Components

Type Visibility Attributes Name Initial
integer, public :: data_type = 1

ONNX dataType enum value

integer, public, allocatable :: dims(:)

Parsed tensor dimensions

logical, public :: in_dims_array = .false.

Whether parser is currently inside a multiline dims array

logical, public :: in_object = .false.

Whether parser is currently inside an initialiser object

character(len=128), public :: name = ''

Initialiser tensor name

character(len=:), public, allocatable :: raw_data

Base64 payload from rawData field

Components

Type Visibility Attributes Name Initial
character(len=16), public :: active_string_array = ''

Currently active multiline string array: input or output

integer, public :: attribute_bracket_depth = 0

Current square-bracket nesting depth inside a multiline attribute block

type(onnx_attribute_type), public, allocatable :: attrs(:)

Temporary parsed node attributes

logical, public :: in_attribute = .false.

Whether parser is currently inside an attribute array

logical, public :: in_object = .false.

Whether parser is currently inside a node object

character(len=128), public, allocatable :: inputs(:)

Temporary node input names

character(len=256), public :: name = ''

Node name parsed from JSON

integer, public :: num_attrs = 0

Number of valid attribute entries

integer, public :: num_inputs = 0

Number of valid input names

integer, public :: num_outputs = 0

Number of valid output names

character(len=256), public :: op_type = ''

Node opType parsed from JSON

character(len=128), public, allocatable :: outputs(:)

Temporary node output names

Components

Type Visibility Attributes Name Initial
type(onnx_initialiser_type), public :: inits(MAX_ITEMS)

Parsed ONNX initialisers

type(onnx_tensor_type), public :: inputs(MAX_ITEMS)

Parsed graph input tensors

character(len=256), public :: meta_keys(MAX_GNN_METADATA)

Metadata keys read from metadataProps

character(len=256), public :: meta_values(MAX_GNN_METADATA)

Metadata values read from metadataProps

type(onnx_node_type), public :: nodes(MAX_ITEMS)

Parsed ONNX nodes

integer, public :: num_inits = 0

Number of valid entries in inits

integer, public :: num_inputs = 0

Number of valid entries in inputs

integer, public :: num_meta = 0

Number of valid metadata key/value pairs

integer, public :: num_nodes = 0

Number of valid entries in nodes

integer, public :: num_outputs = 0

Number of valid entries in outputs

type(onnx_tensor_type), public :: outputs(MAX_ITEMS)

Parsed graph output tensors

Components

Type Visibility Attributes Name Initial
type(json_initialiser_state_type), public :: initialiser

Reusable initialiser parser state

type(json_tensor_state_type), public :: input_tensor

Reusable input tensor parser state

type(json_node_state_type), public :: node

Reusable node parser state

type(json_tensor_state_type), public :: output_tensor

Reusable output tensor parser state

character(len=32), public :: section = ''

Active top-level section name

Components

Type Visibility Attributes Name Initial
integer, public, allocatable :: dim_values(:)

Parsed tensor dimensions (-1 for dimParam)

integer, public :: elem_type = 1

ONNX element type enum value

logical, public :: in_object = .false.

Whether parser is currently inside a tensor object

character(len=128), public :: name = ''

Tensor name

integer, public :: object_depth = 0

Nested JSON object depth within this tensor block


Functions

function extract_onnx_expanded_layer_prefix(node_name) result(prefix)

Extract the layerN prefix from an expanded-ONNX node name.

Arguments

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

Node name such as /layer1/MatMul

Return Value character(len=32)

Extracted layer prefix without leading slash

function find_activation_node_for_layer_id(nodes, num_nodes, layer_id) result(actv_index)

Return node index for activation attached to node_, or 0.

Arguments

Type IntentOptional Attributes Name
type(onnx_node_type), intent(in) :: nodes(:)

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Number of valid nodes and target layer id

integer, intent(in) :: layer_id

Number of valid nodes and target layer id

Return Value integer

Index of the found activation node

function find_metadata_for_layer_id(meta_keys, num_meta, layer_id) result(meta_index)

Return metadata index for a given layer id, or 0 if absent.

Arguments

Type IntentOptional Attributes Name
character(len=256), intent(in) :: meta_keys(:)

Metadata keys list

integer, intent(in) :: num_meta

Number of metadata entries and target layer id

integer, intent(in) :: layer_id

Number of metadata entries and target layer id

Return Value integer

Index of the found metadata entry, or 0 if not found

function find_primary_node_for_layer_id(nodes, num_nodes, layer_id) result(node_index)

Return node index for primary node_, or 0 if not found.

Arguments

Type IntentOptional Attributes Name
type(onnx_node_type), intent(in) :: nodes(:)

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Number of valid nodes and target layer id

integer, intent(in) :: layer_id

Number of valid nodes and target layer id

Return Value integer

Index of the found primary node, or 0 if not found

function is_activation_op_type(op_type) result(is_activation)

Return true for ONNX activation nodes emitted by ATHENA export.

Arguments

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

ONNX operation type string

Return Value logical

Whether op_type matches an activation emitted by ATHENA export

function is_json_object_start(line) result(is_start)

Return true for section object lines like {.

Arguments

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

Current JSON line to classify

Return Value logical

Whether this line is the start of a JSON object

function is_onnx_expanded_gnn_graph(nodes, num_nodes) result(output)

Return true when the parsed ONNX graph contains expanded-ONNX GNN patterns that ATHENA can collapse back into native message passing layers.

Arguments

Type IntentOptional Attributes Name
type(onnx_node_type), intent(in) :: nodes(:)

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Number of valid node entries

Return Value logical

Whether the graph contains recognizable expanded-ONNX GNN patterns

function is_onnx_expanded_nop_graph(nodes, num_nodes) result(output)

Return true when the parsed ONNX graph is a supported expanded-ONNX NOP decomposition that ATHENA can collapse back into native NOP layers.

Arguments

Type IntentOptional Attributes Name
type(onnx_node_type), intent(in) :: nodes(:)

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Number of valid node entries

Return Value logical

Whether the graph matches expanded-ONNX NOP patterns


Subroutines

subroutine add_gnn_layer_from_metadata(network, meta_key, meta_value, inits, num_inits, verbose_)

Create one GNN or NOP layer from metadata and append it to the network.

Arguments

Type IntentOptional Attributes Name
type(network_type), intent(inout) :: network

Network receiving the created layer

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

Metadata key/value pair describing one layer

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

Metadata key/value pair describing one layer

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

Parsed ONNX initialisers

integer, intent(in) :: num_inits

Number of initialisers and effective verbosity level

integer, intent(in) :: verbose_

Number of initialisers and effective verbosity level

subroutine add_standard_layer_from_onnx(network, layer_id, node_index, nodes, num_nodes, inits, num_inits, verbose_)

Create standard (non-GNN) layers for a given layer_id using the registered ONNX creator framework (list_of_onnx_layer_creators).

Read more…

Arguments

Type IntentOptional Attributes Name
type(network_type), intent(inout) :: network

Network receiving the created layer(s)

integer, intent(in) :: layer_id

Layer id, primary node index, node count and initialiser count

integer, intent(in) :: node_index

Layer id, primary node index, node count and initialiser count

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

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Layer id, primary node index, node count and initialiser count

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

Parsed ONNX initialisers

integer, intent(in) :: num_inits

Layer id, primary node index, node count and initialiser count

integer, intent(in) :: verbose_

Effective verbosity level

subroutine append_json_int_string_item(line, values)

Append one integer value stored as a quoted JSON string.

Arguments

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

Current JSON line inside a multiline integer array

integer, intent(inout), allocatable :: values(:)

Mutable integer array buffer updated in-place

subroutine append_json_string_array_item(line, active_array, inputs, num_inputs, outputs, num_outputs)

Append one string element from a multiline JSON string array.

Arguments

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

Current JSON line inside a multiline string array

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

Array currently being accumulated: input or output

character(len=128), intent(inout) :: inputs(:)

Mutable node input/output buffers

integer, intent(inout) :: num_inputs

Counts of valid input/output entries

character(len=128), intent(inout) :: outputs(:)

Mutable node input/output buffers

integer, intent(inout) :: num_outputs

Counts of valid input/output entries

subroutine append_unique_layer_id_from_meta_key(meta_key, ids)

Append a layer id parsed from athena_gnn_node_ or athena_nop_node_ if not already present.

Arguments

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

Metadata key potentially containing a layer id

integer, intent(inout), allocatable :: ids(:)

Unique set of discovered layer ids

subroutine append_unique_onnx_expanded_prefix(prefix, prefixes)

Append a /layerN prefix to a list if it is not already present.

Arguments

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

Prefix to append

character(len=32), intent(inout), allocatable :: prefixes(:)

Prefix list updated in-place

subroutine append_unique_primary_layer_id(node_name, ids)

Append a layer id parsed from a primary node name node_.

Arguments

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

Node name potentially containing a primary layer id

integer, intent(inout), allocatable :: ids(:)

Unique set of discovered layer ids

subroutine build_network_from_json_gnn(network, nodes, num_nodes, inits, num_inits, inputs, num_inputs, outputs, num_outputs, meta_keys, meta_values, num_meta, verbose_)

Build a network containing GNN layers from parsed JSON data.

Read more…

Arguments

Type IntentOptional Attributes Name
type(network_type), intent(inout) :: network

Network to populate from parsed ONNX content

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

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Number of valid entries in nodes

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

Parsed ONNX initialisers

integer, intent(in) :: num_inits

Number of valid entries in inits

type(onnx_tensor_type), intent(in) :: inputs(:)

Parsed graph input tensors

integer, intent(in) :: num_inputs

Number of valid entries in inputs

type(onnx_tensor_type), intent(in) :: outputs(:)

Parsed graph output tensors

integer, intent(in) :: num_outputs

Number of valid entries in outputs

character(len=256), intent(in) :: meta_keys(:)

Metadata keys and values from metadataProps

character(len=256), intent(in) :: meta_values(:)

Metadata keys and values from metadataProps

integer, intent(in) :: num_meta

Number of valid metadata entries

integer, intent(in) :: verbose_

Effective verbosity level

subroutine build_network_from_json_onnx_expanded_gnn(network, nodes, num_nodes, inits, num_inits, inputs, num_inputs, verbose_)

Reconstruct ATHENA GNN layers from an expanded-ONNX graph when metadata is absent.

Read more…

Arguments

Type IntentOptional Attributes Name
type(network_type), intent(inout) :: network

Network receiving the reconstructed layers

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

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Number of valid node entries

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

Parsed ONNX initialisers

integer, intent(in) :: num_inits

Number of valid initialiser entries

type(onnx_tensor_type), intent(in) :: inputs(:)

Parsed ONNX graph input tensors

integer, intent(in) :: num_inputs

Number of valid graph input entries

integer, intent(in) :: verbose_

Effective verbosity level

subroutine build_network_from_json_onnx_expanded_nop(network, nodes, num_nodes, inits, num_inits, verbose_)

Reconstruct ATHENA NOP layers from an expanded-ONNX decomposed graph.

Read more…

Arguments

Type IntentOptional Attributes Name
type(network_type), intent(inout) :: network

Network receiving the reconstructed layers

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

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Number of valid node entries

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

Parsed ONNX initialisers

integer, intent(in) :: num_inits

Number of valid initialiser entries

integer, intent(in) :: verbose_

Effective verbosity level

subroutine build_network_from_json_standard(network, nodes, num_nodes, inits, num_inits, inputs, num_inputs, verbose_)

Build a standard, non-GNN network from parsed JSON data.

Read more…

Arguments

Type IntentOptional Attributes Name
type(network_type), intent(inout) :: network

Network to populate from parsed ONNX content

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

Parsed ONNX nodes

integer, intent(in) :: num_nodes

Number of valid entries in nodes

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

Parsed ONNX initialisers

integer, intent(in) :: num_inits

Number of valid entries in inits

type(onnx_tensor_type), intent(in) :: inputs(:)

Parsed graph input tensors

integer, intent(in) :: num_inputs

Number of valid entries in inputs

integer, intent(in) :: verbose_

Effective verbosity level

subroutine detect_json_section(line, parser)

Detect the active top-level graph section.

Arguments

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

Current trimmed JSON line

type(json_parser_state_type), intent(inout) :: parser

Parser state with mutable active section

subroutine extract_gnn_subtype(meta_value, gnn_subtype)

Extract the subtype=... token from one metadata value string.

Arguments

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

Metadata payload string

character(len=*), intent(out) :: gnn_subtype

Extracted subtype token

subroutine extract_json_int(line, key, value)

Extract an integer value from a JSON key-value pair.

Arguments

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

Source line and key token to find

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

Source line and key token to find

integer, intent(out) :: value

Extracted integer value

subroutine extract_json_string(line, key, value)

Extract a string value from a JSON key-value pair.

Arguments

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

Source line and key token to find

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

Source line and key token to find

character(len=*), intent(out) :: value

Extracted string value

subroutine initialise_json_parser(parser)

Initialise the reusable parser state objects.

Arguments

Type IntentOptional Attributes Name
type(json_parser_state_type), intent(out) :: parser

Parser state container to initialise

subroutine parse_any_node_layer_id(node_name, layer_id, found)

Parse layer id from node_X or node_X_* names.

Arguments

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

Candidate ONNX node name

integer, intent(out) :: layer_id

Parsed layer id when present

integer, intent(out) :: found

Positive when parsing succeeded, zero otherwise

subroutine parse_initialiser_section_line(line, state, parsed, section)

Parse one line from the initialiser section.

Arguments

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

Current JSON line to parse

type(json_initialiser_state_type), intent(inout) :: state

Mutable parser state for the active initialiser object

type(json_parse_result_type), intent(inout) :: parsed

Parsed ONNX content accumulated so far

character(len=32), intent(inout) :: section

Current top-level JSON section name

subroutine parse_json_attribute(line, attrs, n_attrs)

Parse one or more JSON attribute objects from a line.

Arguments

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

Source line containing one or more JSON attribute objects

type(onnx_attribute_type), intent(inout), allocatable :: attrs(:)

Destination list of parsed attributes

integer, intent(inout) :: n_attrs

Number of valid attributes in attrs

subroutine parse_json_int_array_from_strings(line, values)

Parse a JSON array of string-encoded integers.

Arguments

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

Source line containing a JSON array

integer, intent(inout), allocatable :: values(:)

Parsed integer values

subroutine parse_json_string_array(line, key, values, n)

Parse a JSON string array from one line.

Arguments

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

Source line and array key token

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

Source line and array key token

character(len=128), intent(inout) :: values(:)

Destination array for parsed values

integer, intent(inout) :: n

Number of valid parsed values

subroutine parse_meta_layer_id(meta_key, layer_id, found)

Parse athena_gnn_node_ or athena_nop_node_ metadata key layer id.

Arguments

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

Metadata key potentially containing a layer id

integer, intent(out) :: layer_id

Parsed layer id value

logical, intent(out) :: found

Whether parsing succeeded

subroutine parse_metadata_line(line, parsed, section)

Parse one metadataProps line.

Arguments

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

Current metadata JSON line

type(json_parse_result_type), intent(inout) :: parsed

Parsed ONNX content accumulated so far

character(len=32), intent(inout) :: section

Current top-level JSON section name

subroutine parse_node_section_line(line, state, parsed, section)

Parse one line from the node section.

Arguments

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

Current JSON line to parse

type(json_node_state_type), intent(inout) :: state

Mutable node parser state

type(json_parse_result_type), intent(inout) :: parsed

Parsed ONNX content accumulated so far

character(len=32), intent(inout) :: section

Current top-level JSON section name

subroutine parse_primary_layer_id(node_name, layer_id, is_primary)

Parse node_ names and mark true only for primary layer nodes.

Arguments

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

Candidate ONNX node name

integer, intent(out) :: layer_id

Parsed layer id when present

logical, intent(out) :: is_primary

Whether node_name matches primary pattern node_

subroutine parse_single_json_attribute(line, attrs, n_attrs)

Parse a single JSON attribute object.

Arguments

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

Source line for one JSON attribute object

type(onnx_attribute_type), intent(inout), allocatable :: attrs(:)

Destination list of parsed attributes

integer, intent(inout) :: n_attrs

Number of valid attributes in attrs

subroutine parse_tensor_section_line(line, state, tensors, num_tensors, section)

Parse one line from the input or output tensor section.

Arguments

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

Current JSON line to parse

type(json_tensor_state_type), intent(inout) :: state

Mutable tensor parser state

type(onnx_tensor_type), intent(inout) :: tensors(:)

Parsed tensor destination array

integer, intent(inout) :: num_tensors

Number of valid tensor entries in tensors

character(len=32), intent(inout) :: section

Current top-level JSON section name

subroutine reset_initialiser_state(state)

Reset the reusable initialiser parser state.

Arguments

Type IntentOptional Attributes Name
type(json_initialiser_state_type), intent(inout) :: state

Initialiser parser state to reset

subroutine reset_node_state(state)

Reset the reusable node parser state.

Arguments

Type IntentOptional Attributes Name
type(json_node_state_type), intent(inout) :: state

Node parser state to reset

subroutine reset_tensor_state(state)

Reset the reusable tensor parser state.

Arguments

Type IntentOptional Attributes Name
type(json_tensor_state_type), intent(inout) :: state

Tensor parser state to reset

subroutine sort_int_array(values)

Sort an integer array in ascending order.

Arguments

Type IntentOptional Attributes Name
integer, intent(inout), allocatable :: values(:)

Integer array sorted in ascending order in-place

subroutine store_initialiser_state(state, parsed)

Copy the current initialiser state into the parsed result collection.

Arguments

Type IntentOptional Attributes Name
type(json_initialiser_state_type), intent(in) :: state

Completed initialiser parse state to copy into the result object

type(json_parse_result_type), intent(inout) :: parsed

Parsed ONNX content accumulated so far

subroutine store_node_state(state, parsed)

Copy the current node state into the parsed result collection.

Arguments

Type IntentOptional Attributes Name
type(json_node_state_type), intent(in) :: state

Completed node parser state

type(json_parse_result_type), intent(inout) :: parsed

Parsed ONNX content accumulated so far

subroutine store_tensor_state(state, tensors, num_tensors)

Copy the current tensor state into the parsed result collection.

Arguments

Type IntentOptional Attributes Name
type(json_tensor_state_type), intent(in) :: state

Completed tensor parser state

type(onnx_tensor_type), intent(inout) :: tensors(:)

Parsed tensor destination array

integer, intent(inout) :: num_tensors

Number of valid tensor entries in tensors

subroutine update_array_bracket_depth(line, depth)

Update square-bracket nesting depth for multiline JSON arrays.

Arguments

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

Current JSON line inside a multiline array

integer, intent(inout) :: depth

Mutable array nesting depth

subroutine update_object_depth(line, object_depth)

Update a nested object depth counter from one JSON line.

Arguments

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

Current JSON line

integer, intent(inout) :: object_depth

Mutable object depth counter


Module Functions

module function read_onnx(file, verbose) result(network)

Import a network from ONNX JSON format.

Read more…

Arguments

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

File to import the network from

integer, intent(in), optional :: verbose

Verbosity level

Return Value type(network_type)

Resulting network instance