Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions include/compound_assertions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
! Copyright (c), The Regents of the University of California
! Terms of use are as specified in LICENSE.txt

! This file provides preprocessor-based macros to call procedures that exist
! solely to make calls to 'assert' and that therefore will be eliminated completely
! whenever the ASSERTIONS macro is defined to a value other than 0.

! Enable repeated includes to toggle assertions based on current settings:
#undef call_assert_consistency
#undef call_assert_conformable

#ifndef ASSERTIONS
! Assertions are off by default
#define ASSERTIONS 0
#endif

#if ASSERTIONS
# define call_assert_consistency(obj) call assert_consistency(obj)
# define call_assert_conformable(lhs,rhs) call assert_conformable(lhs,rhs)
#else
# define call_assert_consistency(obj)
# define call_assert_conformable(lhs,rhs)
#endif
3 changes: 3 additions & 0 deletions src/fiats/activation_m.f90
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
! Copyright (c), The Regents of the University of California
! Terms of use are as specified in LICENSE.txt

module activation_m
use iso_c_binding, only : c_int
use julienne_m, only : string_t
Expand Down
5 changes: 4 additions & 1 deletion src/fiats/activation_s.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
! Copyright (c), The Regents of the University of California
! Terms of use are as specified in LICENSE.txt

submodule(activation_m) activation_s
use assert_m, only : assert
use assert_m
implicit none

real , parameter :: pi = 3.141592653589793
Expand Down
15 changes: 8 additions & 7 deletions src/fiats/hyperparameters_s.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
! Copyright (c), The Regents of the University of California
! Terms of use are as specified in LICENSE.txt

#include "assert_macros.h"

submodule(hyperparameters_m) hyperparameters_s
use assert_m, only : assert
use assert_m
implicit none

character(len=*), parameter :: mini_batches_key = "mini-batches"
Expand All @@ -26,8 +29,7 @@

real, parameter :: tolerance = 1.E-08

call assert(allocated(lhs%optimizer_) .and. allocated(rhs%optimizer_), &
"hyperparameters_s(default_real_equals): allocated optimizers")
call_assert(allocated(lhs%optimizer_) .and. allocated(rhs%optimizer_))

lhs_equals_rhs = &
lhs%mini_batches_ == rhs%mini_batches_ .and. &
Expand All @@ -40,8 +42,7 @@

double precision, parameter :: tolerance = 1.D-15

call assert(allocated(lhs%optimizer_) .and. allocated(rhs%optimizer_), &
"hyperparameters_s(double_precisionequals): allocated optimizers")
call_assert(allocated(lhs%optimizer_) .and. allocated(rhs%optimizer_))

lhs_equals_rhs = &
lhs%mini_batches_ == rhs%mini_batches_ .and. &
Expand All @@ -66,7 +67,7 @@
end if
end do

call assert(hyperparameters_key_found, "hyperparameters_s(default_real_from_json): hyperparameters_found")
call_assert(hyperparameters_key_found)
end procedure

module procedure double_precision_from_json
Expand All @@ -85,7 +86,7 @@
end if
end do

call assert(hyperparameters_key_found, "hyperparameters_s(double_precision_from_json): hyperparameters_found")
call_assert(hyperparameters_key_found)
end procedure

module procedure default_real_to_json
Expand Down
9 changes: 6 additions & 3 deletions src/fiats/layer_s.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
! Copyright (c), The Regents of the University of California
! Terms of use are as specified in LICENSE.txt

#include "assert_macros.h"

submodule(layer_m) layer_s
use assert_m, only : assert
use assert_m
implicit none

contains
Expand Down Expand Up @@ -77,7 +80,7 @@
num_hidden_layers => hidden_layers%count_layers(), &
num_output_layers => output_layer%count_layers() &
)
call assert(num_output_layers==1, "neural_network_s(default_real_neural_network): 1 output layer", num_output_layers)
call_assert(num_output_layers==1)

associate(nodes => [num_inputs, neurons_per_hidden_layer, num_outputs])
associate(n_max => maxval(nodes))
Expand Down Expand Up @@ -149,7 +152,7 @@
num_hidden_layers => hidden_layers%count_layers(), &
num_output_layers => output_layer%count_layers() &
)
call assert(num_output_layers==1, "neural_network_s(double_precision_neural_network): 1 output layer", num_output_layers)
call_assert(num_output_layers==1)

associate(nodes => [num_inputs, neurons_per_hidden_layer, num_outputs])
associate(n_max => maxval(nodes))
Expand Down
9 changes: 6 additions & 3 deletions src/fiats/metadata_s.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
! Copyright (c), The Regents of the University of California
! Terms of use are as specified in LICENSE.txt

#include "assert_macros.h"

submodule(metadata_m) metadata_s
use assert_m, only : assert
use assert_m
implicit none

contains
Expand All @@ -25,7 +28,7 @@
module procedure from_json
integer l

call assert(lines(1)%get_json_key() == "metadata", "metadata_s(from_json): metadata found")
call_assert(lines(1)%get_json_key() == "metadata")

do l = 2, size(lines)-1
associate(key => lines(l)%get_json_key())
Expand All @@ -52,7 +55,7 @@
module procedure double_precision_from_json
integer l

call assert(lines(1)%get_json_key() == "metadata", "metadata_s(double_precision_from_json): metadata found")
call_assert(lines(1)%get_json_key() == "metadata")

do l = 2, size(lines)-1
associate(key => lines(l)%get_json_key())
Expand Down
13 changes: 7 additions & 6 deletions src/fiats/network_configuration_s.F90
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
! Copyright (c), The Regents of the University of California
! Terms of use are as specified in LICENSE.txt

#include "assert_macros.h"

submodule(network_configuration_m) network_configuration_s
use assert_m, only : assert
use assert_m
use julienne_formats_m, only : csv
implicit none

Expand All @@ -19,8 +22,7 @@

module procedure equals

call assert(allocated(lhs%activation_name_) .and. allocated(rhs%activation_name_), &
"network_configuration_s(equals): allocated({lhs,rhs}%activation_name_)")
call_assert(allocated(lhs%activation_name_) .and. allocated(rhs%activation_name_))

lhs_equals_rhs = &
lhs%skip_connections_ .eqv. rhs%skip_connections_ .and. &
Expand All @@ -45,7 +47,7 @@
end if
end do

call assert(network_configuration_key_found, "network_configuration_s(from_json): network_configuration_found")
call_assert(network_configuration_key_found)
end procedure

module procedure from_double_precision_string_json
Expand All @@ -64,8 +66,7 @@
end if
end do

call assert(network_configuration_key_found, &
"network_configuration_s(from_double_precision_string_json): network_configuration_found")
call_assert(network_configuration_key_found)
end procedure

module procedure to_json
Expand Down
21 changes: 5 additions & 16 deletions src/fiats/neural_network_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module neural_network_m
public :: neural_network_t
public :: unmapped_network_t
public :: workspace_t
public :: assert_consistency

type neural_network_t(k)
!! Encapsulate the information needed to perform inference
Expand All @@ -35,12 +36,9 @@ module neural_network_m
generic :: num_inputs => default_real_num_inputs, double_precision_num_inputs
generic :: num_outputs => default_real_num_outputs, double_precision_num_outputs
generic :: nodes_per_layer => default_real_nodes_per_layer, double_precision_nodes_per_layer
generic :: assert_conformable_with => default_real_assert_conformable_with, double_precision_assert_conformable_with
generic :: skip => default_real_skip, double_precision_skip
generic :: activation_function_name => default_real_activation_name, double_precision_activation_name
generic :: learn => default_real_learn
generic :: assert_consistency => default_real_consistency, double_precision_consistency
procedure, private, non_overridable :: default_real_consistency, double_precision_consistency
procedure, private, non_overridable :: default_real_approximately_equal, double_precision_approximately_equal
procedure, private, non_overridable :: default_real_infer, double_precision_infer
procedure, private, non_overridable :: default_real_learn
Expand All @@ -51,7 +49,6 @@ module neural_network_m
procedure, private, non_overridable :: default_real_num_inputs, double_precision_num_inputs
procedure, private, non_overridable :: default_real_num_outputs, double_precision_num_outputs
procedure, private, non_overridable :: default_real_nodes_per_layer, double_precision_nodes_per_layer
procedure, private, non_overridable :: default_real_assert_conformable_with, double_precision_assert_conformable_with
procedure, private, non_overridable :: default_real_skip, double_precision_skip
procedure, private, non_overridable :: default_real_activation_name, double_precision_activation_name
end type
Expand Down Expand Up @@ -190,18 +187,6 @@ impure elemental module function double_precision_to_json(self) result(json_file
type(file_t) json_file
end function

elemental module subroutine default_real_assert_conformable_with(self, neural_network)
implicit none
class(neural_network_t), intent(in) :: self
type(neural_network_t), intent(in) :: neural_network
end subroutine

elemental module subroutine double_precision_assert_conformable_with(self, neural_network)
implicit none
class(neural_network_t(double_precision)), intent(in) :: self
type(neural_network_t(double_precision)), intent(in) :: neural_network
end subroutine

elemental module function default_real_infer(self, inputs) result(outputs)
implicit none
class(neural_network_t), intent(in) :: self
Expand Down Expand Up @@ -298,6 +283,10 @@ pure module subroutine default_real_learn(self, mini_batches_arr, cost, adam, le
type(workspace_t), intent(inout) :: workspace
end subroutine

end interface

interface assert_consistency

pure module subroutine default_real_consistency(self)
implicit none
class(neural_network_t), intent(in) :: self
Expand Down
Loading