GCC Code Coverage Report


Directory: src/athena/
File: src/athena/athena_misc_types.f90
Date: 2026-04-15 16:08:59
Exec Total Coverage
Lines: 0 1 0.0%
Functions: 0 0 -%
Branches: 0 74 0.0%

Line Branch Exec Source
1 module athena__misc_types
2 !! Module containing custom derived types and interfaces for ATHENA
3 !!
4 !! This module contains interfaces and derived types for
5 !! activation functions, initialisers, arrays, and facets.
6 !! The activation and initialiser types are abstract types that are used
7 !! to define the activation functions and initialisers for the
8 !! weights and biases in the neural network. The array type is an
9 !! abstract type that is used to define the operations that can be performed
10 !! on the arrays used in the neural network. The facets type is used to store
11 !! the faces, edges, and corners of the arrays for padding.
12 use coreutils, only: real32
13 use diffstruc, only: array_type
14 implicit none
15
16
17 private
18
19 public :: base_actv_type
20 public :: base_init_type
21 public :: facets_type
22 public :: onnx_attribute_type, onnx_node_type, onnx_initialiser_type, &
23 onnx_tensor_type
24
25
26
27
28
29
30 !-------------------------------------------------------------------------------
31 ! Attributes type (for ONNX export)
32 !-------------------------------------------------------------------------------
33 type :: onnx_attribute_type
34 !! Type for storing attributes for ONNX export
35 character(64), allocatable :: name
36 !! Name of the attribute
37 character(10), allocatable :: type
38 !! Type of the attribute (e.g. 'int', 'float', 'string')
39 character(len=:), allocatable :: val
40 !! Value of the attribute as a string
41 !! This allows for flexible storage of different types
42 !! of attributes without needing to define a specific type
43 end type onnx_attribute_type
44
45 interface onnx_attribute_type
46 pure module function create_attribute(name, type, val) result(attribute)
47 !! Function to create an ONNX attribute
48 character(*), intent(in) :: name
49 !! Name of the attribute
50 character(*), intent(in) :: type
51 !! Type of the attribute
52 character(len=*), intent(in) :: val
53 !! Value of the attribute as a string
54 type(onnx_attribute_type) :: attribute
55 !! Resulting ONNX attribute
56 end function create_attribute
57 end interface
58 !-------------------------------------------------------------------------------
59
60 !-------------------------------------------------------------------------------
61 ! ONNX node type
62 !-------------------------------------------------------------------------------
63 type :: onnx_node_type
64 character(128) :: op_type = ''
65 character(128) :: name = ''
66 character(128), allocatable, dimension(:) :: inputs
67 character(128), allocatable, dimension(:) :: outputs
68 type(onnx_attribute_type), allocatable, dimension(:) :: attributes
69 character(4096) :: attributes_json = ''
70 !! Pre-formatted JSON for attributes block (empty = no attributes)
71 integer :: num_inputs = 0
72 integer :: num_outputs = 0
73 end type onnx_node_type
74 !-------------------------------------------------------------------------------
75
76 !-------------------------------------------------------------------------------
77 ! ONNX initialiser type
78 !-------------------------------------------------------------------------------
79 type :: onnx_initialiser_type
80 character(128) :: name = ''
81 integer :: data_type = 1
82 !! 1=float32, 7=int64
83 integer, allocatable, dimension(:) :: dims
84 real(real32), allocatable, dimension(:) :: data
85 integer, allocatable, dimension(:) :: int_data
86 end type onnx_initialiser_type
87 !-------------------------------------------------------------------------------
88
89 !-------------------------------------------------------------------------------
90 ! ONNX tensor type
91 !-------------------------------------------------------------------------------
92 type :: onnx_tensor_type
93 character(128) :: name = ''
94 integer :: elem_type = 1
95 integer, allocatable, dimension(:) :: dims
96 character(64), allocatable, dimension(:) :: dim_params
97 !! If dim_params(i) /= '', it is a symbolic dimension name
98 end type onnx_tensor_type
99 !-------------------------------------------------------------------------------
100
101
102
103 !------------------------------------------------------------------------------!
104 ! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * !
105 !------------------------------------------------------------------------------!
106
107
108
109 !-------------------------------------------------------------------------------
110 ! Activation (aka transfer) function base type
111 !-------------------------------------------------------------------------------
112 type, abstract :: base_actv_type
113 !! Abstract type for activation functions
114 character(10) :: name
115 !! Name of the activation function
116 real(real32) :: scale = 1._real32
117 !! Scale of the activation function
118 real(real32) :: threshold
119 !! Threshold of the activation function
120 logical :: apply_scaling = .false.
121 !! Boolean to apply scaling or not
122 contains
123 procedure (apply_actv), deferred, pass(this) :: apply
124 !! Abstract procedure for 5D derivative of activation function
125 procedure(reset_actv), deferred, pass(this) :: reset
126 !! Reset activation function attributes and variables
127 procedure(apply_attributes_actv), deferred, pass(this) :: apply_attributes
128 !! Set up ONNX attributes
129 procedure(export_attributes_actv), deferred, pass(this) :: export_attributes
130 !! Export ONNX attributes
131 procedure, pass(this) :: print_to_unit => print_to_unit_actv
132 end type base_actv_type
133
134 ! Interface for activation function
135 !-----------------------------------------------------------------------------
136 abstract interface
137 subroutine reset_actv(this)
138 !! Interface for resetting activation function attributes and variables
139 import base_actv_type
140 class(base_actv_type), intent(inout) :: this
141 !! Instance of the activation type
142 end subroutine reset_actv
143
144 function apply_actv(this, val) result(output)
145 !! Interface for activation function
146 import base_actv_type, real32, array_type
147 class(base_actv_type), intent(in) :: this
148 type(array_type), intent(in) :: val
149 type(array_type), pointer :: output
150 end function apply_actv
151
152 subroutine apply_attributes_actv(this, attributes)
153 !! Interface for loading ONNX attributes
154 import base_actv_type, onnx_attribute_type
155 class(base_actv_type), intent(inout) :: this
156 !! Instance of the activation type
157 type(onnx_attribute_type), dimension(:), intent(in) :: attributes
158 !! ONNX attributes
159 end subroutine apply_attributes_actv
160
161 pure function export_attributes_actv(this) result(attributes)
162 !! Interface for exporting ONNX attributes
163 import base_actv_type, onnx_attribute_type
164 class(base_actv_type), intent(in) :: this
165 !! Instance of the activation type
166 type(onnx_attribute_type), allocatable, dimension(:) :: attributes
167 end function export_attributes_actv
168 end interface
169
170 interface
171 module subroutine print_to_unit_actv(this, unit, identifier)
172 !! Interface for printing activation function details
173 class(base_actv_type), intent(in) :: this
174 !! Instance of the activation type
175 integer, intent(in) :: unit
176 !! Unit number for output
177 character(len=*), intent(in), optional :: identifier
178 !! Optional identifier for the activation function
179 end subroutine print_to_unit_actv
180 end interface
181 !-------------------------------------------------------------------------------
182
183
184 !------------------------------------------------------------------------------!
185 ! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * !
186 !------------------------------------------------------------------------------!
187
188
189 !-------------------------------------------------------------------------------
190 ! Weights and biases initialiser base type
191 !-------------------------------------------------------------------------------
192 type, abstract :: base_init_type
193 !! Abstract type for initialising weights and biases
194 character(len=20) :: name
195 !! Name of the initialiser
196 real(real32) :: scale = 1._real32, mean = 1._real32, std = 0.01_real32
197 !! Scale, mean, and standard deviation of the initialiser
198 contains
199 procedure (initialiser_subroutine), deferred, pass(this) :: initialise
200 !! Abstract procedure for initialising weights and biases
201 end type base_init_type
202
203 ! Interface for initialiser function
204 !-----------------------------------------------------------------------------
205 abstract interface
206 !! Interface for initialiser function
207 subroutine initialiser_subroutine(this, input, fan_in, fan_out, spacing)
208 !! Interface for initialiser function
209 import base_init_type, real32
210 class(base_init_type), intent(inout) :: this
211 !! Instance of the initialiser type
212 real(real32), dimension(..), intent(out) :: input
213 !! Array to initialise
214 integer, optional, intent(in) :: fan_in, fan_out
215 !! Number of input and output units
216 integer, dimension(:), optional, intent(in) :: spacing
217 !! Spacing of the array
218 end subroutine initialiser_subroutine
219 end interface
220 !-------------------------------------------------------------------------------
221
222
223 !------------------------------------------------------------------------------!
224 ! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * !
225 !------------------------------------------------------------------------------!
226
227
228 !-------------------------------------------------------------------------------
229 ! Facet type (for storing faces, edges, and corners for padding)
230 !-------------------------------------------------------------------------------
231 type :: facets_type
232 !! Type for storing faces, edges, and corners for padding
233 integer :: num
234 !! Number of facets
235 integer :: rank
236 !! Number of dimensions of the shape
237 integer :: nfixed_dims
238 !! Number of fixed dimensions
239 character(6) :: type
240 !! Type of facet, i.e. face, edge, corner
241 integer, dimension(:), allocatable :: dim
242 !! Dimension the facet is in, i.e.
243 integer, dimension(:,:,:), allocatable :: orig_bound
244 !! Original bounds of the facet (2, nfixed_dims, num)
245 integer, dimension(:,:,:), allocatable :: dest_bound
246 !! Destination bounds of the facet (2, nfixed_dims, num)
247 contains
248 procedure, pass(this) :: setup_bounds
249 !! Procedure for setting up bounds
250 end type facets_type
251
252 interface
253 !! Interface for setting up bounds
254 module subroutine setup_bounds(this, length, pad, imethod)
255 !! Procedure for setting up bounds
256 class(facets_type), intent(inout) :: this
257 !! Instance of the facets type
258 integer, dimension(this%rank), intent(in) :: length, pad
259 !! Length of the shape and padding
260 integer, intent(in) :: imethod
261 !! Method for setting up bounds
262 end subroutine setup_bounds
263 end interface
264 !-------------------------------------------------------------------------------
265
266 end module athena__misc_types
267