| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | module athena__container_layer | ||
| 2 | !! Module containing types and interfaces for the container type | ||
| 3 | !! | ||
| 4 | !! This module contains the container layer type which is a container for an | ||
| 5 | !! individual layer. | ||
| 6 | use coreutils, only: real32 | ||
| 7 | use athena__base_layer, only: base_layer_type | ||
| 8 | use athena__misc_types, only: & | ||
| 9 | onnx_node_type, onnx_initialiser_type, onnx_tensor_type | ||
| 10 | implicit none | ||
| 11 | |||
| 12 | |||
| 13 | private | ||
| 14 | |||
| 15 | public :: container_layer_type | ||
| 16 | public :: read_layer_container | ||
| 17 | public :: list_of_layer_types | ||
| 18 | public :: allocate_list_of_layer_types | ||
| 19 | public :: onnx_create_layer_container | ||
| 20 | public :: list_of_onnx_layer_creators | ||
| 21 | public :: allocate_list_of_onnx_layer_creators | ||
| 22 | public :: onnx_meta_create_layer_container | ||
| 23 | public :: list_of_onnx_meta_layer_creators | ||
| 24 | public :: allocate_list_of_onnx_meta_layer_creators | ||
| 25 | public :: onnx_expanded_nop_create_layer_container | ||
| 26 | public :: list_of_onnx_expanded_nop_layer_creators | ||
| 27 | public :: allocate_list_of_onnx_expanded_nop_layer_creators | ||
| 28 | public :: onnx_expanded_gnn_create_layer_container | ||
| 29 | public :: list_of_onnx_expanded_gnn_layer_creators | ||
| 30 | public :: allocate_list_of_onnx_expanded_gnn_layer_creators | ||
| 31 | #if defined(GFORTRAN) | ||
| 32 | public :: container_reduction | ||
| 33 | #endif | ||
| 34 | |||
| 35 | |||
| 36 | type :: container_layer_type | ||
| 37 | !! Container for a layer | ||
| 38 | class(base_layer_type), allocatable :: layer | ||
| 39 | !! Layer | ||
| 40 | contains | ||
| 41 | #if defined(GFORTRAN) | ||
| 42 | procedure, pass(this) :: reduce => container_reduction | ||
| 43 | !! Reduce two layers via summation | ||
| 44 | final :: finalise_container_layer | ||
| 45 | !! Finalise the container layer | ||
| 46 | #endif | ||
| 47 | end type container_layer_type | ||
| 48 | |||
| 49 | |||
| 50 | #if defined(GFORTRAN) | ||
| 51 | interface | ||
| 52 | module subroutine container_reduction(this, rhs) | ||
| 53 | !! Reduce two layers via summation | ||
| 54 | class(container_layer_type), intent(inout) :: this | ||
| 55 | !! Present layer container | ||
| 56 | class(container_layer_type), intent(in) :: rhs | ||
| 57 | !! Input layer container | ||
| 58 | end subroutine | ||
| 59 | end interface | ||
| 60 | #endif | ||
| 61 | |||
| 62 | |||
| 63 | type :: read_layer_container | ||
| 64 | !! Type containing information needed to read a layer | ||
| 65 | character(20) :: name | ||
| 66 | !! Name of the layer | ||
| 67 | procedure(read_layer), nopass, pointer :: read_ptr => null() | ||
| 68 | !! Pointer to the specific layer read function | ||
| 69 | end type read_layer_container | ||
| 70 | type(read_layer_container), dimension(:), allocatable :: & | ||
| 71 | list_of_layer_types | ||
| 72 | !! List of layer names and their associated read functions | ||
| 73 | |||
| 74 | type :: onnx_create_layer_container | ||
| 75 | !! Type containing information needed to create a layer from ONNX | ||
| 76 | character(20) :: op_type | ||
| 77 | !! Name of the layer | ||
| 78 | procedure(create_from_onnx_layer), nopass, pointer :: create_ptr => null() | ||
| 79 | !! Pointer to the specific layer creation function from ONNX | ||
| 80 | end type onnx_create_layer_container | ||
| 81 | type(onnx_create_layer_container), dimension(:), allocatable :: & | ||
| 82 | list_of_onnx_layer_creators | ||
| 83 | !! List of layer names and their associated ONNX creation functions | ||
| 84 | |||
| 85 | abstract interface | ||
| 86 | function create_gnn_from_onnx_layer(meta_key, meta_value, inits, verbose) & | ||
| 87 | result(layer) | ||
| 88 | !! Create a GNN layer from ONNX metadata and initialisers | ||
| 89 | import :: base_layer_type, onnx_initialiser_type | ||
| 90 | character(*), intent(in) :: meta_key | ||
| 91 | !! GNN metadata key (e.g. "athena_gnn_node_1") | ||
| 92 | character(*), intent(in) :: meta_value | ||
| 93 | !! Semicolon-separated GNN metadata value string | ||
| 94 | type(onnx_initialiser_type), dimension(:), intent(in) :: inits | ||
| 95 | !! ONNX initialisers (valid slice only) | ||
| 96 | integer, optional, intent(in) :: verbose | ||
| 97 | !! Verbosity level | ||
| 98 | class(base_layer_type), allocatable :: layer | ||
| 99 | !! Constructed GNN layer | ||
| 100 | end function create_gnn_from_onnx_layer | ||
| 101 | end interface | ||
| 102 | |||
| 103 | type :: onnx_meta_create_layer_container | ||
| 104 | !! Type containing information needed to create a metadata layer from ONNX | ||
| 105 | character(30) :: layer_subtype | ||
| 106 | !! Metadata subtype name (e.g. "duvenaud", "dynamic_lno") | ||
| 107 | procedure(create_gnn_from_onnx_layer), nopass, pointer :: & | ||
| 108 | create_ptr => null() | ||
| 109 | !! Pointer to the metadata layer creation function | ||
| 110 | end type onnx_meta_create_layer_container | ||
| 111 | type(onnx_meta_create_layer_container), dimension(:), allocatable :: & | ||
| 112 | list_of_onnx_meta_layer_creators | ||
| 113 | !! List of metadata subtypes and their associated ONNX creation functions | ||
| 114 | |||
| 115 | abstract interface | ||
| 116 | logical function classify_onnx_expanded_nop_layer(prefix, nodes, & | ||
| 117 | num_nodes) | ||
| 118 | !! Return true when this creator handles the given | ||
| 119 | !! expanded-ONNX NOP prefix. | ||
| 120 | import :: onnx_node_type | ||
| 121 | character(*), intent(in) :: prefix | ||
| 122 | !! Expanded-ONNX layer prefix (e.g. "layer1") | ||
| 123 | type(onnx_node_type), intent(in) :: nodes(:) | ||
| 124 | !! Parsed ONNX nodes | ||
| 125 | integer, intent(in) :: num_nodes | ||
| 126 | !! Number of valid node entries | ||
| 127 | end function classify_onnx_expanded_nop_layer | ||
| 128 | |||
| 129 | function build_onnx_expanded_nop_layer( & | ||
| 130 | prefix, nodes, num_nodes, inits, & | ||
| 131 | num_inits) result(layer) | ||
| 132 | !! Build one expanded-ONNX NOP layer from a node cluster. | ||
| 133 | import :: base_layer_type, onnx_node_type, onnx_initialiser_type | ||
| 134 | character(*), intent(in) :: prefix | ||
| 135 | !! Expanded-ONNX layer prefix (e.g. "layer1") | ||
| 136 | type(onnx_node_type), intent(in) :: nodes(:) | ||
| 137 | !! Parsed ONNX nodes | ||
| 138 | integer, intent(in) :: num_nodes | ||
| 139 | !! Number of valid node entries | ||
| 140 | type(onnx_initialiser_type), intent(in) :: inits(:) | ||
| 141 | !! Parsed ONNX initialisers | ||
| 142 | integer, intent(in) :: num_inits | ||
| 143 | !! Number of valid initialiser entries | ||
| 144 | class(base_layer_type), allocatable :: layer | ||
| 145 | !! Constructed layer | ||
| 146 | end function build_onnx_expanded_nop_layer | ||
| 147 | end interface | ||
| 148 | |||
| 149 | type :: onnx_expanded_nop_create_layer_container | ||
| 150 | !! Registration entry for one expanded-ONNX NOP layer type | ||
| 151 | character(30) :: nop_subtype | ||
| 152 | !! Subtype name used for diagnostics (e.g. "dynamic_lno") | ||
| 153 | procedure(classify_onnx_expanded_nop_layer), nopass, pointer :: & | ||
| 154 | classify_ptr => null() | ||
| 155 | !! Pointer to the classifier that recognises this layer type | ||
| 156 | procedure(build_onnx_expanded_nop_layer), nopass, pointer :: & | ||
| 157 | build_ptr => null() | ||
| 158 | !! Pointer to the builder that constructs the layer | ||
| 159 | end type onnx_expanded_nop_create_layer_container | ||
| 160 | type(onnx_expanded_nop_create_layer_container), dimension(:), allocatable :: & | ||
| 161 | list_of_onnx_expanded_nop_layer_creators | ||
| 162 | !! List of expanded-ONNX NOP creators registered for pattern-matched ONNX | ||
| 163 | !! import | ||
| 164 | |||
| 165 | abstract interface | ||
| 166 | logical function classify_onnx_expanded_gnn_layer( & | ||
| 167 | prefix, nodes, num_nodes) | ||
| 168 | !! Return true when this creator handles the given | ||
| 169 | !! expanded-ONNX GNN prefix. | ||
| 170 | import :: onnx_node_type | ||
| 171 | character(*), intent(in) :: prefix | ||
| 172 | !! Expanded-ONNX layer prefix (e.g. "node_2") | ||
| 173 | type(onnx_node_type), intent(in) :: nodes(:) | ||
| 174 | !! Parsed ONNX nodes | ||
| 175 | integer, intent(in) :: num_nodes | ||
| 176 | !! Number of valid node entries | ||
| 177 | end function classify_onnx_expanded_gnn_layer | ||
| 178 | |||
| 179 | function build_onnx_expanded_gnn_layer( & | ||
| 180 | prefix, nodes, num_nodes, inits, & | ||
| 181 | num_inits, inputs, num_inputs) & | ||
| 182 | result(layer) | ||
| 183 | !! Build one expanded-ONNX GNN layer from a node cluster. | ||
| 184 | import :: base_layer_type, onnx_node_type, & | ||
| 185 | onnx_initialiser_type, onnx_tensor_type | ||
| 186 | character(*), intent(in) :: prefix | ||
| 187 | !! Expanded-ONNX layer prefix (e.g. "node_2") | ||
| 188 | type(onnx_node_type), intent(in) :: nodes(:) | ||
| 189 | !! Parsed ONNX nodes | ||
| 190 | integer, intent(in) :: num_nodes | ||
| 191 | !! Number of valid node entries | ||
| 192 | type(onnx_initialiser_type), intent(in) :: inits(:) | ||
| 193 | !! Parsed ONNX initialisers | ||
| 194 | integer, intent(in) :: num_inits | ||
| 195 | !! Number of valid initialiser entries | ||
| 196 | type(onnx_tensor_type), intent(in) :: inputs(:) | ||
| 197 | !! Parsed ONNX graph input tensors | ||
| 198 | integer, intent(in) :: num_inputs | ||
| 199 | !! Number of valid graph input entries | ||
| 200 | class(base_layer_type), allocatable :: layer | ||
| 201 | !! Constructed layer | ||
| 202 | end function build_onnx_expanded_gnn_layer | ||
| 203 | end interface | ||
| 204 | |||
| 205 | type :: onnx_expanded_gnn_create_layer_container | ||
| 206 | !! Registration entry for one expanded-ONNX GNN layer type | ||
| 207 | character(30) :: gnn_subtype | ||
| 208 | !! Subtype name (e.g. "kipf", "duvenaud") | ||
| 209 | procedure(classify_onnx_expanded_gnn_layer), & | ||
| 210 | nopass, pointer :: classify_ptr => null() | ||
| 211 | !! Pointer to the classifier | ||
| 212 | procedure(build_onnx_expanded_gnn_layer), & | ||
| 213 | nopass, pointer :: build_ptr => null() | ||
| 214 | !! Pointer to the builder | ||
| 215 | end type onnx_expanded_gnn_create_layer_container | ||
| 216 | type(onnx_expanded_gnn_create_layer_container), & | ||
| 217 | dimension(:), allocatable :: & | ||
| 218 | list_of_onnx_expanded_gnn_layer_creators | ||
| 219 | !! List of expanded-ONNX GNN creators | ||
| 220 | |||
| 221 | interface | ||
| 222 | module function read_layer(unit, verbose) result(layer) | ||
| 223 | !! Read a layer from a file | ||
| 224 | integer, intent(in) :: unit | ||
| 225 | !! Unit number | ||
| 226 | integer, intent(in), optional :: verbose | ||
| 227 | !! Verbosity level | ||
| 228 | class(base_layer_type), allocatable :: layer | ||
| 229 | !! Instance of a layer | ||
| 230 | end function read_layer | ||
| 231 | |||
| 232 | module function create_from_onnx_layer( & | ||
| 233 | nodes, initialisers, value_info, verbose & | ||
| 234 | ) result(layer) | ||
| 235 | !! Create a layer from ONNX nodes and initialisers | ||
| 236 | type(onnx_node_type), intent(in) :: nodes | ||
| 237 | !! ONNX nodes | ||
| 238 | type(onnx_initialiser_type), dimension(:), intent(in) :: initialisers | ||
| 239 | !! ONNX initialisers | ||
| 240 | type(onnx_tensor_type), dimension(:), intent(in) :: value_info | ||
| 241 | !! ONNX value info | ||
| 242 | integer, intent(in), optional :: verbose | ||
| 243 | !! Verbosity level | ||
| 244 | class(base_layer_type), allocatable :: layer | ||
| 245 | !! Instance of a layer | ||
| 246 | end function create_from_onnx_layer | ||
| 247 | end interface | ||
| 248 | |||
| 249 | interface | ||
| 250 | module subroutine allocate_list_of_layer_types(addit_list) | ||
| 251 | !! Allocate the list of layer types | ||
| 252 | type(read_layer_container), dimension(:), intent(in), optional :: & | ||
| 253 | addit_list | ||
| 254 | !! Additional list of layer types | ||
| 255 | end subroutine allocate_list_of_layer_types | ||
| 256 | |||
| 257 | module subroutine allocate_list_of_onnx_layer_creators(addit_list) | ||
| 258 | !! Allocate the list of ONNX layer creation procedures | ||
| 259 | type(onnx_create_layer_container), dimension(:), intent(in), optional :: & | ||
| 260 | addit_list | ||
| 261 | !! Additional list of ONNX layer creation procedures | ||
| 262 | end subroutine allocate_list_of_onnx_layer_creators | ||
| 263 | |||
| 264 | module subroutine allocate_list_of_onnx_meta_layer_creators(addit_list) | ||
| 265 | !! Allocate the list of metadata-based ONNX layer creation procedures | ||
| 266 | type(onnx_meta_create_layer_container), & | ||
| 267 | dimension(:), intent(in), optional :: addit_list | ||
| 268 | !! Additional list of metadata-based ONNX layer creation procedures | ||
| 269 | end subroutine allocate_list_of_onnx_meta_layer_creators | ||
| 270 | |||
| 271 | module subroutine allocate_list_of_onnx_expanded_nop_layer_creators( & | ||
| 272 | addit_list) | ||
| 273 | !! Allocate the list of expanded-ONNX NOP layer creation procedures | ||
| 274 | type(onnx_expanded_nop_create_layer_container), & | ||
| 275 | dimension(:), intent(in), optional :: addit_list | ||
| 276 | !! Additional list of expanded-ONNX NOP layer creation procedures | ||
| 277 | end subroutine allocate_list_of_onnx_expanded_nop_layer_creators | ||
| 278 | |||
| 279 | module subroutine allocate_list_of_onnx_expanded_gnn_layer_creators( & | ||
| 280 | addit_list) | ||
| 281 | !! Allocate the list of expanded-ONNX GNN layer creation procedures | ||
| 282 | type(onnx_expanded_gnn_create_layer_container), & | ||
| 283 | dimension(:), intent(in), optional :: addit_list | ||
| 284 | !! Additional list of expanded-ONNX GNN layer creation procedures | ||
| 285 | end subroutine allocate_list_of_onnx_expanded_gnn_layer_creators | ||
| 286 | end interface | ||
| 287 | |||
| 288 | interface | ||
| 289 | module subroutine finalise_container_layer(this) | ||
| 290 | !! Finalise the container layer | ||
| 291 | class(container_layer_type), intent(inout) :: this | ||
| 292 | !! Present layer container | ||
| 293 | end subroutine finalise_container_layer | ||
| 294 | end interface | ||
| 295 | |||
| 296 | ✗ | end module athena__container_layer | |
| 297 |