athena__add_layer Module

Module containing implementation of an element-wise addition layer

This module implements a merge layer that performs element-wise addition of multiple input tensors. Commonly used for residual/skip connections.

Mathematical operation: output = sum(input_i) for i=1..N

where N is the number of input layers. All inputs must have identical shapes. Gradients are copied (not split) to all inputs during backpropagation.


Uses


Interfaces

public interface add_layer_type

Interface for setting up the add layer

  • private module function layer_setup(input_layer_ids, input_rank, verbose) result(layer)

    Setup a add layer

    Arguments

    Type IntentOptional Attributes Name
    integer, intent(in), dimension(:) :: input_layer_ids

    Input layer IDs

    integer, intent(in), optional :: input_rank

    Input rank

    integer, intent(in), optional :: verbose

    Verbosity level

    Return Value type(add_layer_type)


Derived Types

type, public, extends(merge_layer_type) ::  add_layer_type

Type for add layer with overloaded procedures

Components

Type Visibility Attributes Name Initial
type(graph_type), public, allocatable, dimension(:) :: graph

Graph structure of input data

integer, public :: id

Unique identifier

logical, public :: inference = .false.

Inference mode

integer, public, allocatable, dimension(:) :: input_layer_ids

IDs of input layers

integer, public :: input_rank = 0

Rank of input data

integer, public, allocatable, dimension(:) :: input_shape

Input shape

integer, public :: merge_mode = 1

Integer code for fundamental merge method 1 = pointwise 2 = concatenate 3 = reduction 4 = parametric (NOT IMPLEMENTED)

character(len=20), public :: method

Merge method

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

Layer name

integer, public :: num_input_layers = 0

Number of input layers

class(array_type), public, allocatable, dimension(:,:) :: output

Output

integer, public :: output_rank = 0

Rank of output data

integer, public, allocatable, dimension(:) :: output_shape

Output shape

character(len=20), public :: subtype = repeat(" ", 20)
character(len=4), public :: type = 'base'

Layer type

logical, public :: use_graph_input = .false.

Use graph input

logical, public :: use_graph_output = .false.

Use graph output

Constructor

Interface for setting up the add layer

private module function layer_setup (input_layer_ids, input_rank, verbose)

Setup a add layer

Type-Bound Procedures

procedure, public, pass(this) :: build_from_onnx => build_from_onnx_base

Build layer from ONNX node and initialiser

procedure, public, pass(this) :: calc_input_shape => calc_input_shape_add

Calculate input shape based on shapes of input layers

procedure, public, pass(this) :: combine => combine_add
procedure, public, pass(this) :: emit_onnx_graph_inputs => emit_onnx_graph_inputs_base

Emit graph input tensor declarations for this layer

procedure, public, pass(this) :: emit_onnx_nodes => emit_onnx_nodes_base

Emit ONNX JSON nodes for this layer (format-aware and polymorphic)

procedure, public, pass(this) :: extract_output => extract_output_base

Extract the output of the layer as a standard real array

procedure, public, pass(this) :: forward => forward_base

Forward pass of layer

procedure, public, pass(this) :: forward_eval => forward_eval_base

Forward pass of layer and return output for evaluation

procedure, public, pass(this) :: get_attributes => get_attributes_base

Get the attributes of the layer (for ONNX export)

procedure, public, pass(this) :: get_num_params => get_num_params_base

Get the number of parameters in the layer

procedure, public, pass(this) :: init => init_add

Initialise add layer

procedure, public, pass(this) :: nullify_graph => nullify_graph_base

Nullify the forward pass data of the layer to free memory

Read more…
procedure, public, pass(this) :: print => print_base

Print the layer to a file with additional information

procedure, public, pass(this) :: print_to_unit => print_to_unit_add

Print the layer to a file

procedure, public, pass(this) :: read => read_add

Read the layer from a file

procedure, public, pass(this) :: set_graph => set_graph_base

Set the graph structure of the input data !! this is adjacency and edge weighting

procedure, public, pass(this) :: set_hyperparams => set_hyperparams_add

Set the hyperparameters for add layer

procedure, public, pass(this) :: set_rank => set_rank_base

Set the input and output ranks of the layer

procedure, public, pass(this) :: set_shape => set_shape_base

Set the input shape of the layer


Functions

public function read_add_layer(unit, verbose) result(layer)

Read add layer from file and return layer

Arguments

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

Unit number

integer, intent(in), optional :: verbose

Verbosity level

Return Value class(base_layer_type), allocatable

Instance of the add layer

private function calc_input_shape_add(this, input_shapes) result(input_shape)

Calculate input shape based on shapes of input layers

Arguments

Type IntentOptional Attributes Name
class(add_layer_type), intent(in) :: this

Instance of the layer

integer, intent(in), dimension(:,:) :: input_shapes

Input shapes

Return Value integer, allocatable, dimension(:)

Calculated input shape

private module function layer_setup(input_layer_ids, input_rank, verbose) result(layer)

Setup a add layer

Arguments

Type IntentOptional Attributes Name
integer, intent(in), dimension(:) :: input_layer_ids

Input layer IDs

integer, intent(in), optional :: input_rank

Input rank

integer, intent(in), optional :: verbose

Verbosity level

Return Value type(add_layer_type)

Instance of the add layer


Subroutines

private subroutine combine_add(this, input_list)

Forward propagation for 2D input

Arguments

Type IntentOptional Attributes Name
class(add_layer_type), intent(inout) :: this

Instance of the add layer

type(array_ptr_type), intent(in), dimension(:) :: input_list

Input values

private subroutine init_add(this, input_shape, verbose)

Initialise add layer

Arguments

Type IntentOptional Attributes Name
class(add_layer_type), intent(inout) :: this

Instance of the add layer

integer, intent(in), dimension(:) :: input_shape

Input shape

integer, intent(in), optional :: verbose

Verbosity level

private subroutine print_to_unit_add(this, unit)

Print add layer to unit

Arguments

Type IntentOptional Attributes Name
class(add_layer_type), intent(in) :: this

Instance of the add layer

integer, intent(in) :: unit

File unit

private subroutine read_add(this, unit, verbose)

Read add layer from file

Arguments

Type IntentOptional Attributes Name
class(add_layer_type), intent(inout) :: this

Instance of the add layer

integer, intent(in) :: unit

Unit number

integer, intent(in), optional :: verbose

Verbosity level

private subroutine set_hyperparams_add(this, input_layer_ids, input_rank, verbose)

Set the hyperparameters for add layer

Arguments

Type IntentOptional Attributes Name
class(add_layer_type), intent(inout) :: this

Instance of the add layer

integer, intent(in), dimension(:) :: input_layer_ids

Input layer IDs

integer, intent(in) :: input_rank

Input rank

integer, intent(in), optional :: verbose

Verbosity level