From fd2cc996fe0f99cd1471b95ce4f75509371e241c Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Sun, 29 Oct 2023 20:13:09 -0700 Subject: [PATCH] refac: update sourcery depency version to 4.4.0 --- fpm.toml | 2 +- .../activation_strategy_m.f90 | 2 +- src/inference_engine/hyperparameters_m.f90 | 26 ++----- src/inference_engine/hyperparameters_s.f90 | 76 +++++++++---------- src/inference_engine/inference_engine_m_.f90 | 3 +- src/inference_engine/inference_engine_s.f90 | 6 +- .../network_configuration_m.f90 | 39 ++++++++++ .../network_configuration_s.f90 | 54 +++++++++++++ src/inference_engine/neuron_m.f90 | 2 +- src/inference_engine/neuron_s.f90 | 8 +- src/inference_engine/relu_m.f90 | 2 +- src/inference_engine/sigmoid_m.f90 | 2 +- src/inference_engine/step_m.f90 | 2 +- src/inference_engine/swish_m.f90 | 2 +- test/asymmetric_engine_test_m.f90 | 4 +- test/inference_engine_test_m.f90 | 5 +- test/netCDF_file_test_m.f90 | 4 +- training_parameters.json | 9 --- 18 files changed, 153 insertions(+), 95 deletions(-) create mode 100644 src/inference_engine/network_configuration_m.f90 create mode 100644 src/inference_engine/network_configuration_s.f90 delete mode 100644 training_parameters.json diff --git a/fpm.toml b/fpm.toml index 3042622a5..bdc742364 100644 --- a/fpm.toml +++ b/fpm.toml @@ -6,5 +6,5 @@ maintainer = "rouson@lbl.gov" [dependencies] assert = {git = "https://github.com/sourceryinstitute/assert", tag = "1.5.0"} -sourcery = {git = "https://github.com/sourceryinstitute/sourcery", tag = "3.9.1"} +sourcery = {git = "https://github.com/sourceryinstitute/sourcery", tag = "4.4.0"} netcdf-interfaces = {git = "https://github.com/rouson/netcdf-interfaces.git", branch = "implicit-interfaces"} diff --git a/src/inference_engine/activation_strategy_m.f90 b/src/inference_engine/activation_strategy_m.f90 index cdb464852..dd888ee7c 100644 --- a/src/inference_engine/activation_strategy_m.f90 +++ b/src/inference_engine/activation_strategy_m.f90 @@ -4,7 +4,7 @@ module activation_strategy_m ! External dependencies use kind_parameters_m, only : rkind - use string_m, only : string_t + use sourcery_m, only : string_t implicit none private diff --git a/src/inference_engine/hyperparameters_m.f90 b/src/inference_engine/hyperparameters_m.f90 index f0e125ef7..dea9e6580 100644 --- a/src/inference_engine/hyperparameters_m.f90 +++ b/src/inference_engine/hyperparameters_m.f90 @@ -1,34 +1,22 @@ module hyperparameters_m - use sourcery_m, only : file_t + use sourcery_m, only : string_t, file_t implicit none private public :: hyperparameters_t - public :: initialization_parameters_t - public :: initialization_t - type initialization_parameters_t - real spread_ - end type - - type initialization_t - character(len=:), allocatable :: initialization_type_ - type(initialization_parameters_t) :: initialization_parameters_ - end type - type hyperparameters_t private - character(len=:), allocatable :: activation_ - integer mini_batch_size_ - integer, allocatable :: nodes_per_layer_(:) - type(initialization_t) initialization_ + integer :: mini_batches_ = 10 + real :: learning_rate_ = 1.5 + character(len=:), allocatable :: optimizer_ contains procedure :: to_json end type interface hyperparameters_t - pure module function construct_from_json_file(file_) result(hyperparameters) + pure module function from_json(file_) result(hyperparameters) implicit none type(file_t), intent(in) :: file_ type(hyperparameters_t) hyperparameters @@ -38,10 +26,10 @@ pure module function construct_from_json_file(file_) result(hyperparameters) interface - impure elemental module function to_json(self) result(json_file) + pure module function to_json(self) result(lines) implicit none class(hyperparameters_t), intent(in) :: self - type(file_t) json_file + type(string_t), allocatable :: lines(:) end function end interface diff --git a/src/inference_engine/hyperparameters_s.f90 b/src/inference_engine/hyperparameters_s.f90 index 29007b92c..c1f9aae94 100644 --- a/src/inference_engine/hyperparameters_s.f90 +++ b/src/inference_engine/hyperparameters_s.f90 @@ -1,54 +1,50 @@ submodule(hyperparameters_m) hyperparameters_s - use assert_m, only : assert, intrinsic_array_t - use sourcery_m, only : string_t + use assert_m, only : assert implicit none + character(len=*), parameter :: mini_batches_key = "mini-batches" + character(len=*), parameter :: learning_rate_key = "learning rate" + character(len=*), parameter :: optimizer_key = "optimizer" + contains - module procedure construct_from_json_file - integer l + module procedure from_json type(string_t), allocatable :: lines(:) + integer l + logical hyperparameters_key_found lines = file_%lines() - - l = 1 - call assert(adjustl(lines(l)%string())=="{", 'construct_from_json_file: adjustl(lines(l)%string())=="{"', lines(l)%string()) - - !{ - ! "activation" : "sigmoid", - ! "num_mini_batches" : 10, - ! "nodes per layer" : [2, 72, 2], - ! "initialization" : { - ! "type" : "perturbed identity", - ! "parameters" : [ { "spread" : 0.05 } ] - ! } - !} - - - l = l + 1 - call assert(adjustl(lines(l)%string())=="}", 'construct_from_json_file: adjustl(lines(l)%string())=="}"', lines(l)%string()) + hyperparameters_key_found = .false. + + loop_through_file: & + do l=1,size(lines) + if (lines(l)%get_json_key() == "hyperparameters") then + hyperparameters_key_found = .true. + hyperparameters%mini_batches_ = lines(l+1)%get_json_value(string_t(mini_batches_key), mold=0) + hyperparameters%learning_rate_ = lines(l+2)%get_json_value(string_t(learning_rate_key), mold=0.) + hyperparameters%optimizer_ = lines(l+3)%get_json_value(string_t(optimizer_key), mold=string_t("")) + return + end if + end do loop_through_file + + call assert(hyperparameters_key_found, "hyperparameters_s(from_json): hyperparameters_found") end procedure module procedure to_json - type(string_t), allocatable :: lines(:) - integer, parameter :: outer_object_braces = 2 - integer, parameter :: num_lines = outer_object_braces - integer l - - allocate(lines(num_lines)) - - l = 1 - lines(l) = string_t('{') - - l = l + 1 - !lines(line) = string_t(' "modelName": "' // & - !self%metadata_(findloc(key, "modelName", dim=1))%string() // '",') - - - - l = l + 1 - call assert(l == num_lines, "hyperparameters_s(to_json): l == num_lines", intrinsic_array_t([l,num_lines])) - lines(l) = string_t('}') + character(len=*), parameter :: indent = repeat(" ",ncopies=4) + integer, parameter :: max_digits = 12 + character(len=max_digits) mini_batches_string, learning_rate_string + + write(mini_batches_string,*) self%mini_batches_ + write(learning_rate_string,*) self%learning_rate_ + + lines = [ & + string_t(indent // '"hyperparameters": {'), & + string_t(indent // indent // '"' // mini_batches_key // '": ' // mini_batches_string ), & + string_t(indent // indent // '"' // learning_rate_key // '": ' // learning_rate_string ), & + string_t(indent // indent // '"' // optimizer_key // '": "' // self%optimizer_ // '"'), & + string_t(indent // '}') & + ] end procedure end submodule hyperparameters_s diff --git a/src/inference_engine/inference_engine_m_.f90 b/src/inference_engine/inference_engine_m_.f90 index cbab99256..358d59a93 100644 --- a/src/inference_engine/inference_engine_m_.f90 +++ b/src/inference_engine/inference_engine_m_.f90 @@ -2,9 +2,8 @@ ! Terms of use are as specified in LICENSE.txt module inference_engine_m_ !! Define an abstraction that supports inference operationsn on a neural network - use string_m, only : string_t use activation_strategy_m, only : activation_strategy_t - use file_m, only : file_t + use sourcery_m, only : file_t, string_t use kind_parameters_m, only : rkind use tensor_m, only : tensor_t use differentiable_activation_strategy_m, only :differentiable_activation_strategy_t diff --git a/src/inference_engine/inference_engine_s.f90 b/src/inference_engine/inference_engine_s.f90 index 559ef29ab..9e03b08df 100644 --- a/src/inference_engine/inference_engine_s.f90 +++ b/src/inference_engine/inference_engine_s.f90 @@ -1,16 +1,14 @@ ! Copyright (c), The Regents of the University of California ! Terms of use are as specified in LICENSE.txt submodule(inference_engine_m_) inference_engine_s - use assert_m, only : assert - use intrinsic_array_m, only : intrinsic_array_t + use assert_m, only : assert, intrinsic_array_t use step_m, only : step_t use swish_m, only : swish_t use sigmoid_m, only : sigmoid_t use relu_m, only : relu_t use layer_m, only : layer_t use neuron_m, only : neuron_t - use file_m, only : file_t - use formats_m, only : separated_values + use sourcery_m, only : separated_values implicit none interface assert_consistency diff --git a/src/inference_engine/network_configuration_m.f90 b/src/inference_engine/network_configuration_m.f90 new file mode 100644 index 000000000..6f057e7a6 --- /dev/null +++ b/src/inference_engine/network_configuration_m.f90 @@ -0,0 +1,39 @@ +module network_configuration_m + use sourcery_m, only : file_t + implicit none + + private + public :: network_configuration_t + + type network_configuration_t + private + type(string_t) activation_function_ + integer, allocatable :: nodes_per_layer_(:) + logical skip_connections_ + contains + procedure :: to_json + end type + + end type + + interface network_configuration_t + + elemental module function from_json(file_) result(network_configuration) + implicit none + type(file_t), intent(in) :: file_ + type(network_configuration_t) network_configuration + end function + + end interface + + interface + + elemental module function to_json(self) result(json_file) + implicit none + class(network_configuration_t), intent(in) :: self + type(file_t) json_file + end function + + end interface + +end module diff --git a/src/inference_engine/network_configuration_s.f90 b/src/inference_engine/network_configuration_s.f90 new file mode 100644 index 000000000..43249fcc4 --- /dev/null +++ b/src/inference_engine/network_configuration_s.f90 @@ -0,0 +1,54 @@ +submodule(network_configuration_m) network_configuration_s + use assert_m, only : assert + use sourcery_m, only : string_t + implicit none + + character(len=*), parameter :: activation_function_key = "activation function" + character(len=*), parameter :: nodes_per_layer_key = "nodes per layer" + character(len=*), parameter :: skip_connections_key = "skip connections" + +contains + + module procedure from_json + type(string_t), allocatable :: lines(:) + integer l + logical network configuration_key_found + + lines = file_%lines() + network configuration_key_found = .false. + + loop_through_file: & + do l=1,size(lines) + if (line(l)%get_key() == "network configuration") then + network configuration_key_found = .true. + self%activation_function_ = line(l+1)%get_json_value(activation_function_key, mold=string("")) + self%nodes_per_layer_ = line(l+2)%get_json_value(nodes_per_layer_key , mold=[integer::]) + self%skip_connections_ = line(l+2)%get_json_value(skip_connetions_key , mold=.true.) + return + end if + end do loop_through_file + + call assert(network configuration_found, "network configuration_s(from_json): network configuration_found") + end procedure + + module procedure to_json + character(len=:), parameter :: indent = repeat(" ",ncopies=4) + integer, parameter :: max_digits = 12, max_length=5 + character(len=max_digits) activation_function_string, nodes_per_layer_string, skip_connections_string + character(len=max_length) skip_connections_string + + + write(activation_function_string,*) self%activation_function_ + write(nodes_per_layer_string ,*) self%nodes_per_layer_ + write(skip_connections_string ,*) merge("true ","false", self%skip_connections_) + + lines = [ & + string_t(indent // '"network configuration": {'), & + string_t(indent // indent // '"' // activation_function_key //'": ' // activation_function_string ), & + string_t(indent // indent // '"' // nodes_per_layer_key //'": ' // nodes_per_layer_string ), & + string_t(indent // indent // '"' // skip_connections_key //'": "' // skip_connections_string // '"'), & + string_t(indent // '}') & + ] + end procedure + +end submodule network_configuration_s diff --git a/src/inference_engine/neuron_m.f90 b/src/inference_engine/neuron_m.f90 index 03bf2a54a..dca1c45e5 100644 --- a/src/inference_engine/neuron_m.f90 +++ b/src/inference_engine/neuron_m.f90 @@ -1,7 +1,7 @@ ! Copyright (c), The Regents of the University of California ! Terms of use are as specified in LICENSE.txt module neuron_m - use string_m, only : string_t + use sourcery_m, only : string_t use kind_parameters_m, only : rkind implicit none diff --git a/src/inference_engine/neuron_s.f90 b/src/inference_engine/neuron_s.f90 index 2b25bd7fc..115675af3 100644 --- a/src/inference_engine/neuron_s.f90 +++ b/src/inference_engine/neuron_s.f90 @@ -11,11 +11,11 @@ character(len=:), allocatable :: line integer i - call assert(adjustl(neuron_lines(start)%string())=='{', "read_json: neuron object start", neuron_lines(start)%string()) + call assert(adjustl(neuron_lines(start)%string())=='{', "neuron_s(construct): neuron object start",neuron_lines(start)%string()) line = neuron_lines(start+1)%string() associate(colon => index(line, ":")) - call assert(adjustl(line(:colon-1))=='"weights"', "read_json: neuron weights", line) + call assert(adjustl(line(:colon-1))=='"weights"', "neuron_s(construct): neuron weights", line) associate(opening_bracket => colon + index(line(colon+1:), "[")) associate(closing_bracket => opening_bracket + index(line(opening_bracket+1:), "]")) associate(commas => count("," == [(line(i:i), i=opening_bracket+1,closing_bracket-1)])) @@ -30,12 +30,12 @@ line = neuron_lines(start+2)%string() associate(colon => index(line, ":")) - call assert(adjustl(line(:colon-1))=='"bias"', "read_json: neuron bias", line) + call assert(adjustl(line(:colon-1))=='"bias"', "neuron_s(construct): neuron bias", line) read(line(colon+1:), fmt=*) neuron%bias_ end associate line = adjustl(neuron_lines(start+3)%string()) - call assert(line(1:1)=='}', "read_json: neuron object end", line) + call assert(line(1:1)=='}', "neuron_s(construct): neuron object end", line) line = adjustr(neuron_lines(start+3)%string()) if (line(len(line):len(line)) == ",") neuron%next = construct(neuron_lines, start+4) diff --git a/src/inference_engine/relu_m.f90 b/src/inference_engine/relu_m.f90 index 2108e7a78..0fae43e22 100644 --- a/src/inference_engine/relu_m.f90 +++ b/src/inference_engine/relu_m.f90 @@ -3,7 +3,7 @@ module relu_m use differentiable_activation_strategy_m, only : differentiable_activation_strategy_t use kind_parameters_m, only : rkind - use string_m, only : string_t + use sourcery_m, only : string_t implicit none private diff --git a/src/inference_engine/sigmoid_m.f90 b/src/inference_engine/sigmoid_m.f90 index d66324560..3f27b3c81 100644 --- a/src/inference_engine/sigmoid_m.f90 +++ b/src/inference_engine/sigmoid_m.f90 @@ -3,7 +3,7 @@ module sigmoid_m use differentiable_activation_strategy_m, only : differentiable_activation_strategy_t use kind_parameters_m, only : rkind - use string_m, only : string_t + use sourcery_m, only : string_t implicit none private diff --git a/src/inference_engine/step_m.f90 b/src/inference_engine/step_m.f90 index e3661770c..297a99473 100644 --- a/src/inference_engine/step_m.f90 +++ b/src/inference_engine/step_m.f90 @@ -3,7 +3,7 @@ module step_m use activation_strategy_m, only : activation_strategy_t use kind_parameters_m, only : rkind - use string_m, only : string_t + use sourcery_m, only : string_t implicit none private diff --git a/src/inference_engine/swish_m.f90 b/src/inference_engine/swish_m.f90 index d0c24c64e..64edac8dd 100644 --- a/src/inference_engine/swish_m.f90 +++ b/src/inference_engine/swish_m.f90 @@ -3,7 +3,7 @@ module swish_m use differentiable_activation_strategy_m, only : differentiable_activation_strategy_t use kind_parameters_m, only : rkind - use string_m, only : string_t + use sourcery_m, only : string_t implicit none private diff --git a/test/asymmetric_engine_test_m.f90 b/test/asymmetric_engine_test_m.f90 index 9b1f8e9bb..932f07b9b 100644 --- a/test/asymmetric_engine_test_m.f90 +++ b/test/asymmetric_engine_test_m.f90 @@ -5,9 +5,7 @@ module asymmetric_engine_test_m ! External dependencies use assert_m, only : assert - use string_m, only : string_t - use test_m, only : test_t - use test_result_m, only : test_result_t + use sourcery_m, only : string_t, test_t, test_result_t ! Internal dependencies use inference_engine_m, only : inference_engine_t, tensor_t diff --git a/test/inference_engine_test_m.f90 b/test/inference_engine_test_m.f90 index 7f66aa333..44f40a25b 100644 --- a/test/inference_engine_test_m.f90 +++ b/test/inference_engine_test_m.f90 @@ -6,10 +6,7 @@ module inference_engine_test_m ! External dependencies use assert_m, only : assert use kind_parameters_m, only : rkind - use string_m, only : string_t - use test_m, only : test_t - use test_result_m, only : test_result_t - use file_m, only : file_t + use sourcery_m, only : string_t, test_t, test_result_t, file_t ! Internal dependencies use inference_engine_m, only : inference_engine_t, tensor_t, difference_t diff --git a/test/netCDF_file_test_m.f90 b/test/netCDF_file_test_m.f90 index f01c1b75c..6cbfc84c8 100644 --- a/test/netCDF_file_test_m.f90 +++ b/test/netCDF_file_test_m.f90 @@ -8,9 +8,7 @@ module NetCDF_file_test_m ! External dependencies use assert_m, only : assert - use string_m, only : string_t - use test_m, only : test_t - use test_result_m, only : test_result_t + use sourcery_m, only : string_t, test_t, test_result_t use netcdf, only : & nf90_create, nf90_def_dim, nf90_def_var, nf90_enddef, nf90_put_var, nf90_inquire_dimension, & ! functions nf90_close, nf90_open, nf90_inq_varid, nf90_get_var, nf90_inquire_variable, & diff --git a/training_parameters.json b/training_parameters.json deleted file mode 100644 index 37b5701f6..000000000 --- a/training_parameters.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "activation" : "sigmoid", - "num_mini_batches" : 10, - "nodes per layer" : [2, 72, 2], - "initialization" : { - "type" : "perturbed identity", - "parameters" : [ { "spread" : 0.05 } ] - } -}