Skip to content

Commit

Permalink
refac: update sourcery depency version to 4.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rouson committed Oct 30, 2023
1 parent b024b70 commit fd2cc99
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 95 deletions.
2 changes: 1 addition & 1 deletion fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
2 changes: 1 addition & 1 deletion src/inference_engine/activation_strategy_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 7 additions & 19 deletions src/inference_engine/hyperparameters_m.f90
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
76 changes: 36 additions & 40 deletions src/inference_engine/hyperparameters_s.f90
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions src/inference_engine/inference_engine_m_.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/inference_engine/inference_engine_s.f90
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/inference_engine/network_configuration_m.f90
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions src/inference_engine/network_configuration_s.f90
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/inference_engine/neuron_m.f90
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/inference_engine/neuron_s.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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)]))
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/inference_engine/relu_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/inference_engine/sigmoid_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/inference_engine/step_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/inference_engine/swish_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/asymmetric_engine_test_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions test/inference_engine_test_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/netCDF_file_test_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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, &
Expand Down
Loading

0 comments on commit fd2cc99

Please sign in to comment.