Expression Templates Library (ETL)
pool_upsample_2d_expr.hpp
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
8 #pragma once
9 
10 #include "etl/expr/base_temporary_expr.hpp"
11 
12 //Get the implementations
13 #include "etl/impl/std/max_pooling_upsample.hpp"
14 #include "etl/impl/std/avg_pooling_upsample.hpp"
15 #include "etl/impl/cudnn/pooling_upsample.hpp"
16 
17 namespace etl {
18 
25 template <etl_expr A, same_dimensions<A> B, same_dimensions<A> C, size_t C1, size_t C2, size_t S1, size_t S2, size_t P1, size_t P2, bool Max>
26 struct pool_upsample_2d_expr : base_temporary_expr_tern<pool_upsample_2d_expr<A, B, C, C1, C2, S1, S2, P1, P2, Max>, A, B, C> {
31 
32  static constexpr auto storage_order = sub_traits::storage_order;
33 
38  static constexpr bool gpu_computable = cudnn_enabled && all_floating<A, B> && all_homogeneous<A, B>;
39 
40  friend struct etl_traits<pool_upsample_2d_expr>;
41 
46  pool_upsample_2d_expr(A a, B b, C c) : base_type(a, b, c) {
47  //Nothing else to init
48  }
49 
55  template <same_dimensions<A> R>
56  static void check([[maybe_unused]] const A& a, [[maybe_unused]] const B& b, [[maybe_unused]] const C& c, [[maybe_unused]] const R& result) {
57  static constexpr size_t D = etl::decay_traits<A>::dimensions();
58 
59  if constexpr (all_fast<A, B, C, R>) {
60  static_assert(etl::decay_traits<R>::size() == etl::decay_traits<A>::size(), "max_pool_upsample_2d:A and R must have the same size");
61  static_assert(etl::decay_traits<B>::size() == etl::decay_traits<C>::size(), "max_pool_upsample_2d:B and C must have the same size");
62 
63  static_assert(etl::decay_traits<A>::template dim<D - 2>() == S1 * (etl::decay_traits<B>::template dim<D - 2>() - 1) + C1 - 2 * P1,
64  "Invalid pooling dimensions for max_pool_upsample_2d");
65  static_assert(etl::decay_traits<A>::template dim<D - 1>() == S2 * (etl::decay_traits<B>::template dim<D - 1>() - 1) + C2 - 2 * P2,
66  "Invalid pooling dimensions for max_pool_upsample_2d");
67  } else {
68  cpp_assert(etl::size(result) == etl::size(a), "max_pool_upsample_2d:A and R must have the same size");
69  cpp_assert(etl::size(b) == etl::size(c), "max_pool_upsample_2d:B and C must have the same size");
70 
71  cpp_assert(etl::dim<D - 2>(a) == S1 * (etl::dim<D - 2>(b) - 1) + C1 - 2 * P1, "Invalid pooling dimensions for max_pool_upsample_2d");
72  cpp_assert(etl::dim<D - 1>(a) == S2 * (etl::dim<D - 1>(b) - 1) + C2 - 2 * P2, "Invalid pooling dimensions for max_pool_upsample_2d");
73  }
74  }
75 
83  template <typename R>
84  static constexpr etl::pool_impl select_default_impl(bool no_gpu) {
85  if (cudnn_enabled && all_floating<A, B, C, R> && !no_gpu) {
86  return etl::pool_impl::CUDNN;
87  }
88 
89  return etl::pool_impl::STD;
90  }
91 
92 #ifdef ETL_MANUAL_SELECT
93 
98  template <typename R>
99  static etl::pool_impl select_impl() {
100  if (local_context().pool_selector.forced) {
101  auto forced = local_context().pool_selector.impl;
102 
103  switch (forced) {
104  // CUDNN cannot always be used
105  case pool_impl::CUDNN:
106  if (!cudnn_enabled || !all_floating<A, B, C, R> || local_context().cpu) { //COVERAGE_EXCLUDE_LINE
107  std::cerr << "Forced selection to CUDNN pool implementation, but not possible for this expression" << std::endl; //COVERAGE_EXCLUDE_LINE
108  return select_default_impl<R>(local_context().cpu); //COVERAGE_EXCLUDE_LINE
109  } //COVERAGE_EXCLUDE_LINE
110 
111  return forced;
112 
113  //In other cases, simply use the forced impl
114  default:
115  return forced;
116  }
117  }
118 
119  return select_default_impl<R>(local_context().cpu);
120  }
121 
122 #else
123 
129  template <typename R>
130  static constexpr etl::pool_impl select_impl() {
131  return select_default_impl<R>(false);
132  }
133 
134 #endif
135 
136  // Assignment functions
137 
142  template <etl_expr R>
143  void assign_to(R&& result) const {
144  inc_counter("temp:assign");
145 
146  auto& a = this->a();
147  auto& b = this->b();
148  auto& c = this->c();
149 
150  check(a, b, c, result);
151 
152  constexpr_select auto impl = select_impl<R>();
153 
154  if constexpr (Max) {
155  if
156  constexpr_select(impl == pool_impl::STD) {
157  inc_counter("impl:std");
158  impl::standard::max_pool_upsample_2d::apply<C1, C2, S1, S2, P1, P2>(smart_forward(a), smart_forward(b), smart_forward(c), result);
159  }
160  else if
161  constexpr_select(impl == pool_impl::CUDNN) {
162  inc_counter("impl:cudnn");
164  }
165  else {
166  cpp_unreachable("Invalid pool implementation");
167  }
168  } else {
169  if
170  constexpr_select(impl == pool_impl::STD) {
171  inc_counter("impl:std");
172  impl::standard::avg_pool_upsample_2d::apply<C1, C2, S1, S2, P1, P2>(smart_forward(a), smart_forward(b), smart_forward(c), result);
173  }
174  else if
175  constexpr_select(impl == pool_impl::CUDNN) {
176  inc_counter("impl:cudnn");
178  }
179  else {
180  cpp_unreachable("Invalid pool implementation");
181  }
182  }
183  }
184 
189  template <typename L>
190  void assign_add_to(L&& lhs) const {
191  std_add_evaluate(*this, lhs);
192  }
193 
198  template <typename L>
199  void assign_sub_to(L&& lhs) const {
200  std_sub_evaluate(*this, lhs);
201  }
202 
207  template <typename L>
208  void assign_mul_to(L&& lhs) const {
209  std_mul_evaluate(*this, lhs);
210  }
211 
216  template <typename L>
217  void assign_div_to(L&& lhs) const {
218  std_div_evaluate(*this, lhs);
219  }
220 
225  template <typename L>
226  void assign_mod_to(L&& lhs) const {
227  std_mod_evaluate(*this, lhs);
228  }
229 
236  friend std::ostream& operator<<(std::ostream& os, const pool_upsample_2d_expr& expr) {
237  return os << "max_pool_upsample2(" << expr._a << ", " << expr._b << ", " << expr._c << ")";
238  }
239 };
240 
245 template <typename A, typename B, typename C, size_t C1, size_t C2, size_t S1, size_t S2, size_t P1, size_t P2, bool Max>
246 struct etl_traits<etl::pool_upsample_2d_expr<A, B, C, C1, C2, S1, S2, P1, P2, Max>> {
248  using sub_expr_t = std::decay_t<A>;
251 
252  static constexpr bool is_etl = true;
253  static constexpr bool is_transformer = false;
254  static constexpr bool is_view = false;
255  static constexpr bool is_magic_view = false;
256  static constexpr bool is_fast = sub_traits::is_fast;
257  static constexpr bool is_linear = false;
258  static constexpr bool is_thread_safe = true;
259  static constexpr bool is_value = false;
260  static constexpr bool is_direct = true;
261  static constexpr bool is_generator = false;
262  static constexpr bool is_padded = false;
263  static constexpr bool is_aligned = true;
264  static constexpr bool is_temporary = true;
265  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
266  static constexpr order storage_order = sub_traits::storage_order;
267 
273  template <vector_mode_t V>
274  static constexpr bool vectorizable = true;
275 
280  template <size_t DD>
281  static constexpr size_t dim() {
282  return decay_traits<A>::template dim<DD>();
283  }
284 
291  static size_t dim(const expr_t& e, size_t d) {
292  return etl::dim(e.a(), d);
293  }
294 
300  static size_t size(const expr_t& e) {
301  return etl::size(e.a());
302  }
303 
308  static constexpr size_t size() {
309  return decay_traits<A>::size();
310  }
311 
316  static constexpr size_t dimensions() {
317  return sub_traits::dimensions();
318  }
319 
324  static constexpr int complexity() noexcept {
325  return -1;
326  }
327 };
328 
337 template <size_t C1, size_t C2, size_t S1 = C1, size_t S2 = C2, size_t P1 = 0, size_t P2 = 0, etl_expr A, etl_expr B, etl_expr C>
339  B&& output,
340  C&& errors) {
341  return {input, output, errors};
342 }
343 
352 template <size_t C1, size_t C2, size_t S1 = C1, size_t S2 = C2, size_t P1 = 0, size_t P2 = 0, etl_expr A, etl_expr B, etl_expr C>
353 pool_upsample_2d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, C1, C2, S1, S2, P1, P2, false> avg_pool_upsample_2d(A&& input,
354  B&& output,
355  C&& errors) {
356  return {input, output, errors};
357 }
358 
359 } //end of namespace etl
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:702
friend std::ostream & operator<<(std::ostream &os, const pool_upsample_2d_expr &expr)
Print a representation of the expression on the given stream.
Definition: pool_upsample_2d_expr.hpp:236
pool_upsample_2d_expr(A a, B b, C c)
Construct a new expression.
Definition: pool_upsample_2d_expr.hpp:46
static void apply([[maybe_unused]] A &&in, [[maybe_unused]] B &&out, [[maybe_unused]] C &&errors, [[maybe_unused]] M &m, [[maybe_unused]] size_t c1, [[maybe_unused]] size_t c2, [[maybe_unused]] size_t s1, [[maybe_unused]] size_t s2, [[maybe_unused]] size_t p1, [[maybe_unused]] size_t p2)
Apply the functor on sub and store the result in m.
Definition: pooling_upsample.hpp:263
pool_impl
Enumeration describing the different implementations of pooling.
Definition: pool_impl.hpp:21
Standard implementation.
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
static constexpr etl::pool_impl select_default_impl(bool no_gpu)
Select the pool implementation for an expression of type ABC->R.
Definition: pool_upsample_2d_expr.hpp:84
D D
The number of dimensions.
Definition: dyn_matrix_view.hpp:24
std::decay_t< A > sub_expr_t
The sub expression type.
Definition: pool_upsample_2d_expr.hpp:248
order
Storage order of a matrix.
Definition: order.hpp:15
constexpr bool cuda_enabled
Indicates if CUDA is available.
Definition: config.hpp:94
A _a
The first sub expression reference.
Definition: base_temporary_expr.hpp:638
void assign_to(R &&result) const
Assign to a matrix of the same storage order.
Definition: pool_upsample_2d_expr.hpp:143
static void check([[maybe_unused]] const A &a, [[maybe_unused]] const B &b, [[maybe_unused]] const C &c, [[maybe_unused]] const R &result)
Validate the transposition dimensions.
Definition: pool_upsample_2d_expr.hpp:56
static void apply([[maybe_unused]] A &&in, [[maybe_unused]] B &&out, [[maybe_unused]] C &&errors, [[maybe_unused]] M &m, [[maybe_unused]] size_t c1, [[maybe_unused]] size_t c2, [[maybe_unused]] size_t s1, [[maybe_unused]] size_t s2, [[maybe_unused]] size_t p1, [[maybe_unused]] size_t p2)
Apply the functor on sub and store the result in m.
Definition: pooling_upsample.hpp:213
static constexpr auto storage_order
The sub storage order.
Definition: pool_upsample_2d_expr.hpp:32
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: pool_upsample_2d_expr.hpp:324
value_t< A > value_type
The value type of the expression.
Definition: pool_upsample_2d_expr.hpp:250
std::add_lvalue_reference_t< A > a()
Returns the sub expression.
Definition: base_temporary_expr.hpp:686
constexpr bool is_fast
Traits to test if the given ETL expresion type is fast (sizes known at compile-time) ...
Definition: traits.hpp:588
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: pool_upsample_2d_expr.hpp:38
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr size_t dim()
Returns the DDth dimension of the expression.
Definition: pool_upsample_2d_expr.hpp:281
context & local_context()
Return the configuration context of the current thread.
Definition: context.hpp:50
static constexpr size_t size()
Returns the size of the expression.
Definition: pool_upsample_2d_expr.hpp:308
static constexpr size_t dimensions()
Return the number of dimensions of the expression.
Definition: traits_base.hpp:31
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: pool_upsample_2d_expr.hpp:199
auto dim(E &&value, size_t i) -> detail::identity_helper< E, dim_view< detail::build_identity_type< E >, D >>
Return a view representing the ith Dth dimension.
Definition: view_expression_builder.hpp:25
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: pool_upsample_2d_expr.hpp:217
std::conditional_t< is_etl_value< T >, const std::decay_t< T > &, std::decay_t< T > > build_type
Helper to build the type for a sub expression.
Definition: expression_helpers.hpp:24
constexpr bool cudnn_enabled
Indicates if the NVIDIA CUDNN library is available for ETL.
Definition: config.hpp:114
bool cpu
Force CPU evaluation.
Definition: context.hpp:29
GPU implementation.
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
Abstract base class for temporary ternary expression.
Definition: base_temporary_expr.hpp:634
void std_mul_evaluate(Expr &&expr, Result &&result)
Compound multiply evaluation of the expr into result.
Definition: evaluator.hpp:1233
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
decltype(auto) smart_forward_gpu(E &expr)
Smart forwarding for a temporary expression that will be computed in GPU.
Definition: helpers.hpp:343
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: pool_upsample_2d_expr.hpp:226
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: pool_upsample_2d_expr.hpp:316
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: pool_upsample_2d_expr.hpp:208
static constexpr bool is_fast
Indicates if T is a fast structure.
Definition: traits_base.hpp:25
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: pool_upsample_2d_expr.hpp:300
static size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: pool_upsample_2d_expr.hpp:291
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
decltype(auto) smart_forward(E &expr)
Smart forwarding for a temporary expression.
Definition: helpers.hpp:323
A derivative of the 2D max pooling (combine derivative and upsampling for performance) ...
Definition: pool_upsample_2d_expr.hpp:26
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
B _b
The second sub expression reference.
Definition: base_temporary_expr.hpp:639
C _c
The third sub expression reference.
Definition: base_temporary_expr.hpp:640
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: pool_upsample_2d_expr.hpp:190
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
void std_div_evaluate(Expr &&expr, Result &&result)
Compound divide evaluation of the expr into result.
Definition: evaluator.hpp:1252
void inc_counter([[maybe_unused]] const char *name)
Increase the given counter.
Definition: counters.hpp:25
value_t< A > value_type
The type of value of the expression.
Definition: pool_upsample_2d_expr.hpp:27
std::add_lvalue_reference_t< C > c()
Returns the sub expression.
Definition: base_temporary_expr.hpp:718
dyn_pool_upsample_2d_expr< detail::build_type< A >, detail::build_type< B >, detail::build_type< C >, true > max_pool_upsample_2d(A &&input, B &&output, C &&errors, size_t c1, size_t c2)
Derivative of the 2D Max Pooling of the given matrix expression and upsampling.
Definition: dyn_pool_upsample_2d_expr.hpp:325
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195
dyn_pool_upsample_2d_expr< detail::build_type< A >, detail::build_type< B >, detail::build_type< C >, false > avg_pool_upsample_2d(A &&input, B &&output, C &&errors, size_t c1, size_t c2)
Derivative of the 2D Average Pooling of the given matrix expression and upsampling.
Definition: dyn_pool_upsample_2d_expr.hpp:353
static constexpr etl::pool_impl select_impl()
Select the pool implementation for an expression of type ABC->R.
Definition: pool_upsample_2d_expr.hpp:130