BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
layer_traits.h
Go to the documentation of this file.
1 /*
2  * Layer_Traits.h
3  *
4  * Created on: Jul 20, 2019
5  * Author: joseph
6  */
7 
8 #ifndef BLACKCAT_TENSORS_LAYER_TRAITS_H_
9 #define BLACKCAT_TENSORS_LAYER_TRAITS_H_
10 
11 namespace bc {
12 namespace nn {
13 namespace detail {
14 
15 template<class T> using query_forward_requires_inputs = typename T::forward_requires_inputs;
16 template<class T> using query_forward_requires_outputs = typename T::forward_requires_outputs;
17 template<class T> using query_forward_requires_extra_cache = typename T::forward_requires_extra_cache;
18 template<class T> using query_backward_requires_inputs = typename T::backward_requires_inputs;
19 template<class T> using query_backward_requires_outputs = typename T::backward_requires_outputs;
20 template<class T> using query_backward_requires_extra_cache = typename T::backward_requires_extra_cache;
21 template<class T> using query_input_tensor_dim = typename T::input_tensor_dim;
22 template<class T> using query_output_tensor_dim = typename T::output_tensor_dim;
23 template<class T> using query_greedy_evaluate_delta = typename T::greedy_evaluate_delta;
24 
25 //If true we cache the delta- into a matrix/vector. This is not stored in recurrent layers.
26 //It is used for things like feedforward backprop which require using the 'deltaY' error multiple times
27 //This tag is used to prevent recalculating the same error values multiple times (and saving on reallocations)
29 
30 template<class T> using query_requires_extra_cache = typename T::requires_extra_cache;
31 template<class T> using query_extra_batched_cache_args = typename T::extra_batched_cache_args;
32 
33 template<class T>
34 using query_defines_single_predict = typename T::defines_single_predict;
35 
36 template<class T>
37 using query_defines_predict = typename T::defines_predict;
38 
39 } // ns detail
40 
41 template<class T>
55  using system_tag = typename T::system_tag;
56 
57  using value_type =
60  typename system_tag::default_floating_point_type>;
61 
62  using allocator_type =
66 
68  detail::query_requires_extra_cache, T, std::false_type>;
69 
72 
75 
78 
81 
84 
87 
90 
93 
96 
99 
100 
101  template<class... Args>
102  static auto select_on_predict(T& layer, Args&&... args) {
103  using detected =
105  bc::traits::is_detected_v<
107  return select_on_predict(detected(), layer, std::forward<Args>(args)...);
108  }
109 
110  template<class... Args>
111  static auto select_on_single_predict(T& layer, Args&&... args) {
112  using detected =
114  bc::traits::is_detected_v<
116  return select_on_single_predict(detected(), layer, std::forward<Args>(args)...);
117  }
118 
119 
120 private:
121 
122  template<class... Args>
123  static auto select_on_predict(std::true_type, T& layer, Args&&... args) {
124  return layer.predict(args...);
125  }
126 
127  template<class... Args>
128  static auto select_on_predict(std::false_type, T& layer, Args&&... args) {
129  return layer.forward_propagation(args...);
130  }
131 
132  template<class... Args>
133  static auto select_on_single_predict(std::true_type, T& layer, Args&&... args) {
134  return layer.single_predict(args...);
135  }
136 
137  template<class... Args>
138  static auto select_on_single_predict(std::false_type, T& layer, Args&&... args) {
139  return select_on_predict(layer, args...);
140  }
141 
142 
143 };
144 } // namespace nn
145 } // namespace BC
146 
147 
148 
149 #endif /* LAYER_TRAITS_H_ */
typename T::greedy_evaluate_delta query_greedy_evaluate_delta
Definition: layer_traits.h:23
bc::traits::conditional_detected_t< detail::query_backward_requires_outputs, T, std::false_type > backward_delta_should_be_cached
Definition: layer_traits.h:92
Definition: constexpr_int.h:14
bc::traits::conditional_detected_t< detail::query_greedy_evaluate_delta, T, std::false_type > greedy_evaluate_delta
Definition: layer_traits.h:98
Definition: type_traits.h:190
typename T::backward_requires_extra_cache query_backward_requires_extra_cache
Definition: layer_traits.h:20
typename T::backward_requires_inputs query_backward_requires_inputs
Definition: layer_traits.h:18
bc::traits::conditional_detected_t< bc::traits::query_value_type, T, typename system_tag::default_floating_point_type > value_type
Definition: layer_traits.h:60
typename T::input_tensor_dim query_input_tensor_dim
Definition: layer_traits.h:21
typename T::system_tag system_tag
Layers have the function: backward_propagate(Args...); – The arguments supplied are based upon these...
Definition: layer_traits.h:55
typename T::requires_extra_cache query_requires_extra_cache
Definition: layer_traits.h:30
Definition: layer_traits.h:42
bc::traits::conditional_detected_t< detail::query_forward_requires_extra_cache, T, std::false_type > forward_requires_extra_cache
Definition: layer_traits.h:83
typename T::defines_predict query_defines_predict
Definition: layer_traits.h:37
typename T::extra_batched_cache_args query_extra_batched_cache_args
Definition: layer_traits.h:31
bc::traits::conditional_detected_t< detail::query_forward_requires_outputs, T, std::false_type > forward_requires_outputs
Definition: layer_traits.h:80
typename T::backward_requires_outputs query_backward_requires_outputs
Definition: layer_traits.h:19
bc::traits::conditional_detected_t< bc::traits::query_allocator_type, T, bc::Allocator< value_type, system_tag > > allocator_type
Definition: layer_traits.h:65
typename T::value_type query_value_type
Definition: type_traits.h:173
typename T::output_tensor_dim query_output_tensor_dim
Definition: layer_traits.h:22
typename T::defines_single_predict query_defines_single_predict
Definition: layer_traits.h:34
typename T::forward_requires_extra_cache query_forward_requires_extra_cache
Definition: layer_traits.h:17
bc::traits::conditional_detected_t< detail::query_requires_extra_cache, T, std::false_type > requires_extra_cache
Definition: layer_traits.h:68
Definition: allocators.h:20
bc::traits::conditional_detected_t< detail::query_backward_requires_extra_cache, T, std::false_type > backward_requires_extra_cache
Definition: layer_traits.h:95
typename conditional_detected< func, TestType, DefaultType >::type conditional_detected_t
Definition: type_traits.h:87
typename T::forward_requires_outputs query_forward_requires_outputs
Definition: layer_traits.h:16
bc::traits::conditional_detected_t< detail::query_output_tensor_dim, T, input_tensor_dim > output_tensor_dim
Definition: layer_traits.h:74
static auto select_on_single_predict(T &layer, Args &&... args)
Definition: layer_traits.h:111
bc::traits::conditional_detected_t< detail::query_forward_requires_inputs, T, std::true_type > forward_requires_inputs
Definition: layer_traits.h:77
typename T::forward_requires_inputs query_forward_requires_inputs
Definition: layer_traits.h:15
conditional_t< Bool, true_type, false_type > truth_type
Definition: type_traits.h:49
static auto select_on_predict(T &layer, Args &&... args)
Definition: layer_traits.h:102
bc::traits::conditional_detected_t< detail::query_input_tensor_dim, T, bc::traits::Integer< 1 > > input_tensor_dim
Definition: layer_traits.h:71
bc::traits::conditional_detected_t< detail::query_backward_requires_inputs, T, std::true_type > backward_requires_inputs
Definition: layer_traits.h:86
bc::traits::conditional_detected_t< detail::query_backward_requires_outputs, T, std::false_type > backward_requires_outputs
Definition: layer_traits.h:89
typename T::query_backwards_delta_should_be_cached query_backwards_delta_should_be_cached
Definition: layer_traits.h:28
typename T::allocator_type query_allocator_type
Definition: type_traits.h:176
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22