| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | module athena__maxpool1d_layer | ||
| 2 | !! Module containing implementation of a 1D max pooling layer | ||
| 3 | !! | ||
| 4 | !! This module implements 1D max pooling for downsampling sequential data | ||
| 5 | !! by selecting maximum values within pooling windows. | ||
| 6 | !! | ||
| 7 | !! Mathematical operation: | ||
| 8 | !! output[i,k] = max_{m∈[0,pool_size)} input[i*stride+m, k] | ||
| 9 | !! | ||
| 10 | !! where: | ||
| 11 | !! i is the output position along sequence | ||
| 12 | !! k is the channel index | ||
| 13 | !! pool_size is the pooling window length | ||
| 14 | !! stride controls the step size | ||
| 15 | !! | ||
| 16 | !! Shape: (length, channels) -> (length//stride, channels) | ||
| 17 | use coreutils, only: real32, stop_program | ||
| 18 | use athena__base_layer, only: pool_layer_type, base_layer_type | ||
| 19 | use athena__pad1d_layer, only: pad1d_layer_type | ||
| 20 | use diffstruc, only: array_type | ||
| 21 | use athena__misc_types, only: & | ||
| 22 | onnx_node_type, onnx_initialiser_type, onnx_tensor_type | ||
| 23 | use athena__diffstruc_extd, only: maxpool1d | ||
| 24 | implicit none | ||
| 25 | |||
| 26 | |||
| 27 | private | ||
| 28 | |||
| 29 | public :: maxpool1d_layer_type | ||
| 30 | public :: read_maxpool1d_layer | ||
| 31 | |||
| 32 | |||
| 33 | type, extends(pool_layer_type) :: maxpool1d_layer_type | ||
| 34 | !! Type for 1D max pooling layer with overloaded procedures | ||
| 35 | contains | ||
| 36 | procedure, pass(this) :: set_hyperparams => set_hyperparams_maxpool1d | ||
| 37 | !! Set hyperparameters for 1D max pooling layer | ||
| 38 | procedure, pass(this) :: read => read_maxpool1d | ||
| 39 | !! Read 1D max pooling layer from file | ||
| 40 | |||
| 41 | procedure, pass(this) :: forward => forward_maxpool1d | ||
| 42 | !! Forward propagation derived type handler | ||
| 43 | |||
| 44 | end type maxpool1d_layer_type | ||
| 45 | |||
| 46 | interface maxpool1d_layer_type | ||
| 47 | !! Interface for setting up the 1D max pooling layer | ||
| 48 | module function layer_setup( input_shape, & | ||
| 49 | pool_size, stride, padding, verbose ) result(layer) | ||
| 50 | !! Set up the 1D max pooling layer | ||
| 51 | integer, dimension(:), optional, intent(in) :: input_shape | ||
| 52 | !! Input shape | ||
| 53 | integer, dimension(..), optional, intent(in) :: pool_size | ||
| 54 | !! Pool size | ||
| 55 | integer, dimension(..), optional, intent(in) :: stride | ||
| 56 | !! Stride | ||
| 57 | character(*), optional, intent(in) :: padding | ||
| 58 | !! Padding | ||
| 59 | integer, optional, intent(in) :: verbose | ||
| 60 | !! Verbosity level | ||
| 61 | type(maxpool1d_layer_type) :: layer | ||
| 62 | !! Instance of the 1D max pooling layer | ||
| 63 | end function layer_setup | ||
| 64 | end interface maxpool1d_layer_type | ||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | contains | ||
| 69 | |||
| 70 | !############################################################################### | ||
| 71 | − | module function layer_setup( & | |
| 72 | − | input_shape, & | |
| 73 | − | pool_size, stride, padding, verbose) result(layer) | |
| 74 | !! Set up the 1D max pooling layer | ||
| 75 | implicit none | ||
| 76 | |||
| 77 | ! Arguments | ||
| 78 | integer, dimension(:), optional, intent(in) :: input_shape | ||
| 79 | !! Input shape | ||
| 80 | integer, dimension(..), optional, intent(in) :: pool_size | ||
| 81 | !! Pool size | ||
| 82 | integer, dimension(..), optional, intent(in) :: stride | ||
| 83 | !! Stride | ||
| 84 | character(*), optional, intent(in) :: padding | ||
| 85 | !! Padding | ||
| 86 | integer, optional, intent(in) :: verbose | ||
| 87 | !! Verbosity level | ||
| 88 | |||
| 89 | type(maxpool1d_layer_type) :: layer | ||
| 90 | !! Instance of the 1D max pooling layer | ||
| 91 | |||
| 92 | ! Local variables | ||
| 93 | integer :: verbose_ = 0 | ||
| 94 | !! Verbosity level | ||
| 95 | integer, dimension(1) :: pool_size_, stride_ | ||
| 96 | !! Pool size and stride | ||
| 97 | character(len=20) :: padding_ | ||
| 98 | !! Padding | ||
| 99 | |||
| 100 | − | if(present(verbose)) verbose_ = verbose | |
| 101 | |||
| 102 | |||
| 103 | !--------------------------------------------------------------------------- | ||
| 104 | ! Set up pool size | ||
| 105 | !--------------------------------------------------------------------------- | ||
| 106 | − | if(present(pool_size))then | |
| 107 | − | select rank(pool_size) | |
| 108 | rank(0) | ||
| 109 | − | pool_size_ = pool_size | |
| 110 | rank(1) | ||
| 111 | − | pool_size_ = pool_size | |
| 112 | end select | ||
| 113 | else | ||
| 114 | − | pool_size_ = 2 | |
| 115 | end if | ||
| 116 | |||
| 117 | |||
| 118 | !--------------------------------------------------------------------------- | ||
| 119 | ! Set up stride | ||
| 120 | !--------------------------------------------------------------------------- | ||
| 121 | − | if(present(stride))then | |
| 122 | − | select rank(stride) | |
| 123 | rank(0) | ||
| 124 | − | stride_ = stride | |
| 125 | rank(1) | ||
| 126 | − | stride_ = stride | |
| 127 | end select | ||
| 128 | else | ||
| 129 | − | stride_ = 2 | |
| 130 | end if | ||
| 131 | |||
| 132 | |||
| 133 | !--------------------------------------------------------------------------- | ||
| 134 | ! Set up padding | ||
| 135 | !--------------------------------------------------------------------------- | ||
| 136 | − | if(present(padding))then | |
| 137 | − | padding_ = padding | |
| 138 | else | ||
| 139 | − | padding_ = "valid" | |
| 140 | end if | ||
| 141 | |||
| 142 | |||
| 143 | !--------------------------------------------------------------------------- | ||
| 144 | ! Set hyperparameters | ||
| 145 | !--------------------------------------------------------------------------- | ||
| 146 | call layer%set_hyperparams( & | ||
| 147 | pool_size=pool_size_, stride=stride_, & | ||
| 148 | padding=padding_, verbose=verbose_ & | ||
| 149 | − | ) | |
| 150 | |||
| 151 | |||
| 152 | !--------------------------------------------------------------------------- | ||
| 153 | ! Initialise layer shape | ||
| 154 | !--------------------------------------------------------------------------- | ||
| 155 | − | if(present(input_shape)) call layer%init(input_shape=input_shape) | |
| 156 | |||
| 157 | − | end function layer_setup | |
| 158 | !############################################################################### | ||
| 159 | |||
| 160 | |||
| 161 | !############################################################################### | ||
| 162 | − | subroutine set_hyperparams_maxpool1d( & | |
| 163 | this, pool_size, stride, padding, verbose & | ||
| 164 | ) | ||
| 165 | !! Set hyperparameters for 1D max pooling layer | ||
| 166 | use coreutils, only: to_lower | ||
| 167 | implicit none | ||
| 168 | |||
| 169 | ! Arguments | ||
| 170 | class(maxpool1d_layer_type), intent(inout) :: this | ||
| 171 | !! Instance of the 1D max pooling layer | ||
| 172 | integer, dimension(1), intent(in) :: pool_size | ||
| 173 | !! Pool size | ||
| 174 | integer, dimension(1), intent(in) :: stride | ||
| 175 | !! Stride | ||
| 176 | character(*), optional, intent(in) :: padding | ||
| 177 | !! Padding | ||
| 178 | integer, optional, intent(in) :: verbose | ||
| 179 | !! Verbosity level | ||
| 180 | |||
| 181 | ! Local variables | ||
| 182 | character(len=20) :: padding_ | ||
| 183 | |||
| 184 | − | this%name = "maxpool1d" | |
| 185 | − | this%type = "pool" | |
| 186 | − | this%subtype = "max" | |
| 187 | − | this%input_rank = 2 | |
| 188 | − | this%output_rank = 2 | |
| 189 | − | if(allocated(this%pool)) deallocate(this%pool) | |
| 190 | − | if(allocated(this%strd)) deallocate(this%strd) | |
| 191 | allocate( & | ||
| 192 | − | this%pool(this%input_rank-1), & | |
| 193 | − | this%strd(this%input_rank-1) & | |
| 194 | − | ) | |
| 195 | − | this%pool = pool_size | |
| 196 | − | this%strd = stride | |
| 197 | |||
| 198 | ! Handle padding | ||
| 199 | − | if(present(padding))then | |
| 200 | − | padding_ = trim(adjustl(padding)) | |
| 201 | else | ||
| 202 | − | padding_ = "valid" | |
| 203 | end if | ||
| 204 | |||
| 205 | − | select case(trim(adjustl(to_lower(padding_)))) | |
| 206 | case("valid", "none", "") | ||
| 207 | case default | ||
| 208 | this%pad_layer = pad1d_layer_type( & | ||
| 209 | − | padding = [ (this%pool-1)/2 ], & | |
| 210 | method = padding_ & | ||
| 211 | − | ) | |
| 212 | end select | ||
| 213 | |||
| 214 | − | end subroutine set_hyperparams_maxpool1d | |
| 215 | !############################################################################### | ||
| 216 | |||
| 217 | |||
| 218 | !##############################################################################! | ||
| 219 | ! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ! | ||
| 220 | !##############################################################################! | ||
| 221 | |||
| 222 | |||
| 223 | !############################################################################### | ||
| 224 | − | subroutine read_maxpool1d(this, unit, verbose) | |
| 225 | !! Read 1D max pooling layer from file | ||
| 226 | use athena__tools_infile, only: assign_val, assign_vec | ||
| 227 | use coreutils, only: to_lower, to_upper, icount | ||
| 228 | implicit none | ||
| 229 | |||
| 230 | ! Arguments | ||
| 231 | class(maxpool1d_layer_type), intent(inout) :: this | ||
| 232 | !! Instance of the 1D max pooling layer | ||
| 233 | integer, intent(in) :: unit | ||
| 234 | !! File unit | ||
| 235 | integer, optional, intent(in) :: verbose | ||
| 236 | !! Verbosity level | ||
| 237 | |||
| 238 | ! Local variables | ||
| 239 | integer :: verbose_ = 0 | ||
| 240 | !! Verbosity level | ||
| 241 | integer :: stat | ||
| 242 | !! File status | ||
| 243 | integer :: itmp1 | ||
| 244 | !! Temporary integer | ||
| 245 | integer, dimension(1) :: pool_size, stride | ||
| 246 | !! Pool size and stride | ||
| 247 | integer, dimension(2) :: input_shape | ||
| 248 | !! Input shape | ||
| 249 | character(256) :: buffer, tag, err_msg | ||
| 250 | !! Buffer for reading lines, tag for identifying lines, error message | ||
| 251 | |||
| 252 | |||
| 253 | ! Initialise optional arguments | ||
| 254 | !--------------------------------------------------------------------------- | ||
| 255 | − | if(present(verbose)) verbose_ = verbose | |
| 256 | |||
| 257 | ! Loop over tags in layer card | ||
| 258 | !--------------------------------------------------------------------------- | ||
| 259 | − | tag_loop: do | |
| 260 | |||
| 261 | ! Check for end of file | ||
| 262 | !------------------------------------------------------------------------ | ||
| 263 | − | read(unit,'(A)',iostat=stat) buffer | |
| 264 | − | if(stat.ne.0)then | |
| 265 | write(err_msg,'("file encountered error (EoF?) before END ",A)') & | ||
| 266 | − | to_upper(this%name) | |
| 267 | − | call stop_program(err_msg) | |
| 268 | − | return | |
| 269 | end if | ||
| 270 | − | if(trim(adjustl(buffer)).eq."") cycle tag_loop | |
| 271 | |||
| 272 | ! Check for end of layer card | ||
| 273 | !------------------------------------------------------------------------ | ||
| 274 | − | if(trim(adjustl(buffer)).eq."END "//to_upper(trim(this%name)))then | |
| 275 | − | backspace(unit) | |
| 276 | − | exit tag_loop | |
| 277 | end if | ||
| 278 | |||
| 279 | − | tag=trim(adjustl(buffer)) | |
| 280 | − | if(scan(buffer,"=").ne.0) tag=trim(tag(:scan(tag,"=")-1)) | |
| 281 | |||
| 282 | ! Read parameters from save file | ||
| 283 | !------------------------------------------------------------------------ | ||
| 284 | − | select case(trim(tag)) | |
| 285 | case("INPUT_SHAPE") | ||
| 286 | − | call assign_vec(buffer, input_shape, itmp1) | |
| 287 | case("POOL_SIZE") | ||
| 288 | − | call assign_vec(buffer, pool_size, itmp1) | |
| 289 | case("STRIDE") | ||
| 290 | − | call assign_vec(buffer, stride, itmp1) | |
| 291 | case default | ||
| 292 | ! Don't look for "e" due to scientific notation of numbers | ||
| 293 | ! ... i.e. exponent (E+00) | ||
| 294 | − | if(scan(to_lower(trim(adjustl(buffer))),& | |
| 295 | 'abcdfghijklmnopqrstuvwxyz').eq.0)then | ||
| 296 | − | cycle tag_loop | |
| 297 | − | elseif(tag(:3).eq.'END')then | |
| 298 | − | cycle tag_loop | |
| 299 | end if | ||
| 300 | write(err_msg,'("Unrecognised line in input file: ",A)') & | ||
| 301 | − | trim(adjustl(buffer)) | |
| 302 | − | call stop_program(err_msg) | |
| 303 | − | return | |
| 304 | end select | ||
| 305 | end do tag_loop | ||
| 306 | |||
| 307 | |||
| 308 | ! Set hyperparameters and initialise layer | ||
| 309 | !--------------------------------------------------------------------------- | ||
| 310 | − | call this%set_hyperparams(pool_size=pool_size, stride=stride) | |
| 311 | − | call this%init(input_shape = input_shape) | |
| 312 | |||
| 313 | |||
| 314 | ! Check for end of layer card | ||
| 315 | !--------------------------------------------------------------------------- | ||
| 316 | − | read(unit,'(A)') buffer | |
| 317 | − | if(trim(adjustl(buffer)).ne."END "//to_upper(trim(this%name)))then | |
| 318 | − | write(0,*) trim(adjustl(buffer)) | |
| 319 | − | write(err_msg,'("END ",A," not where expected")') to_upper(this%name) | |
| 320 | − | call stop_program(err_msg) | |
| 321 | − | return | |
| 322 | end if | ||
| 323 | |||
| 324 | end subroutine read_maxpool1d | ||
| 325 | !############################################################################### | ||
| 326 | |||
| 327 | |||
| 328 | !############################################################################### | ||
| 329 | − | function read_maxpool1d_layer(unit, verbose) result(layer) | |
| 330 | !! Read 1D max pooling layer from file and return layer | ||
| 331 | implicit none | ||
| 332 | |||
| 333 | ! Arguments | ||
| 334 | integer, intent(in) :: unit | ||
| 335 | !! File unit | ||
| 336 | integer, optional, intent(in) :: verbose | ||
| 337 | !! Verbosity level | ||
| 338 | class(base_layer_type), allocatable :: layer | ||
| 339 | !! Instance of the 1D max pooling layer | ||
| 340 | |||
| 341 | ! Local variables | ||
| 342 | integer :: verbose_ = 0 | ||
| 343 | !! Verbosity level | ||
| 344 | |||
| 345 | − | if(present(verbose)) verbose_ = verbose | |
| 346 | − | allocate(layer, source=maxpool1d_layer_type()) | |
| 347 | − | call layer%read(unit, verbose=verbose_) | |
| 348 | |||
| 349 | − | end function read_maxpool1d_layer | |
| 350 | !############################################################################### | ||
| 351 | |||
| 352 | |||
| 353 | !############################################################################### | ||
| 354 | − | subroutine build_from_onnx_maxpool1d( & | |
| 355 | − | this, node, initialisers, value_info, verbose & | |
| 356 | ) | ||
| 357 | !! Read ONNX attributes for 1D max pooling layer | ||
| 358 | implicit none | ||
| 359 | |||
| 360 | ! Arguments | ||
| 361 | class(maxpool1d_layer_type), intent(inout) :: this | ||
| 362 | !! Instance of the 1D max pooling layer | ||
| 363 | type(onnx_node_type), intent(in) :: node | ||
| 364 | !! ONNX node information | ||
| 365 | type(onnx_initialiser_type), dimension(:), intent(in) :: initialisers | ||
| 366 | !! ONNX initialiser information | ||
| 367 | type(onnx_tensor_type), dimension(:), intent(in) :: value_info | ||
| 368 | !! ONNX value info | ||
| 369 | integer, intent(in) :: verbose | ||
| 370 | !! Verbosity level | ||
| 371 | |||
| 372 | ! Local variables | ||
| 373 | integer :: i | ||
| 374 | !! Loop index | ||
| 375 | integer, dimension(1) :: stride, pool_size, padding | ||
| 376 | !! Stride, kernel size, and padding | ||
| 377 | character(256) :: val | ||
| 378 | !! Attribute value | ||
| 379 | character(20) :: padding_method | ||
| 380 | !! Padding method | ||
| 381 | |||
| 382 | ! Set default values | ||
| 383 | − | stride = 1 | |
| 384 | − | pool_size = 2 | |
| 385 | − | padding = 0 | |
| 386 | |||
| 387 | − | do i = 1, size(node%attributes) | |
| 388 | − | val = node%attributes(i)%val | |
| 389 | − | select case(trim(adjustl(node%attributes(i)%name))) | |
| 390 | case("kernel_shape") | ||
| 391 | − | read(val,*) pool_size | |
| 392 | case("strides") | ||
| 393 | − | read(val,*) stride | |
| 394 | case("pads") | ||
| 395 | − | read(val,*) padding | |
| 396 | case default | ||
| 397 | ! Do nothing | ||
| 398 | − | write(0,*) "WARNING: Unrecognised attribute in ONNX MAXPOOL1D ", & | |
| 399 | − | "layer: ", trim(adjustl(node%attributes(i)%name)) | |
| 400 | end select | ||
| 401 | end do | ||
| 402 | |||
| 403 | ! Check size of initialisers is zero | ||
| 404 | − | if(size(initialisers).ne.0)then | |
| 405 | − | write(0,*) "WARNING: initialisers not used for ONNX MAXPOOL1D layer" | |
| 406 | end if | ||
| 407 | |||
| 408 | ! Convert integer padding to character method | ||
| 409 | − | if(any(padding.gt.0))then | |
| 410 | − | padding_method = "constant" | |
| 411 | else | ||
| 412 | − | padding_method = "valid" | |
| 413 | end if | ||
| 414 | |||
| 415 | call this%set_hyperparams( & | ||
| 416 | stride = stride, & | ||
| 417 | pool_size = pool_size, & | ||
| 418 | padding = padding_method & | ||
| 419 | − | ) | |
| 420 | |||
| 421 | − | end subroutine build_from_onnx_maxpool1d | |
| 422 | !############################################################################### | ||
| 423 | |||
| 424 | |||
| 425 | !##############################################################################! | ||
| 426 | ! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ! | ||
| 427 | !##############################################################################! | ||
| 428 | |||
| 429 | |||
| 430 | !############################################################################### | ||
| 431 | − | subroutine forward_maxpool1d(this, input) | |
| 432 | !! Forward propagation | ||
| 433 | implicit none | ||
| 434 | |||
| 435 | ! Arguments | ||
| 436 | class(maxpool1d_layer_type), intent(inout) :: this | ||
| 437 | !! Instance of the 1D max pooling layer | ||
| 438 | class(array_type), dimension(:,:), intent(in) :: input | ||
| 439 | !! Input values | ||
| 440 | |||
| 441 | ! Local variables | ||
| 442 | type(array_type), pointer :: ptr | ||
| 443 | !! Pointer array | ||
| 444 | |||
| 445 | |||
| 446 | − | call this%output(1,1)%zero_grad() | |
| 447 | − | select case(allocated(this%pad_layer)) | |
| 448 | case(.true.) | ||
| 449 | − | call this%pad_layer%forward(input) | |
| 450 | − | ptr => maxpool1d(this%pad_layer%output(1,1), this%pool(1), this%strd(1)) | |
| 451 | case default | ||
| 452 | − | ptr => maxpool1d(input(1,1), this%pool(1), this%strd(1)) | |
| 453 | end select | ||
| 454 | − | call this%output(1,1)%assign_and_deallocate_source(ptr) | |
| 455 | − | this%output(1,1)%is_temporary = .false. | |
| 456 | |||
| 457 | − | end subroutine forward_maxpool1d | |
| 458 | !############################################################################### | ||
| 459 | |||
| 460 | − | end module athena__maxpool1d_layer | |
| 461 |