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.
Interface for setting up the add layer
Setup a add layer
| Type | Intent | Optional | 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 |
Type for add layer with overloaded procedures
| 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 |
Interface for setting up the add layer
| private module function layer_setup (input_layer_ids, input_rank, verbose) | Setup a add layer |
| 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 |
| 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 |
Read add layer from file and return layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | unit |
Unit number |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the add layer
Calculate input shape based on shapes of input layers
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(add_layer_type), | intent(in) | :: | this |
Instance of the layer |
||
| integer, | intent(in), | dimension(:,:) | :: | input_shapes |
Input shapes |
Calculated input shape
Setup a add layer
| Type | Intent | Optional | 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 |
Instance of the add layer
Forward propagation for 2D input
| Type | Intent | Optional | 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 |
Initialise add layer
| Type | Intent | Optional | 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 |
Print add layer to unit
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(add_layer_type), | intent(in) | :: | this |
Instance of the add layer |
||
| integer, | intent(in) | :: | unit |
File unit |
Read add layer from file
| Type | Intent | Optional | 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 |
Set the hyperparameters for add layer
| Type | Intent | Optional | 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 |