Expression Templates Library (ETL)
pool_upsample_3d_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 C3, bool Max>
26 struct pool_upsample_3d_expr : base_temporary_expr_tern<pool_upsample_3d_expr<A, B, C, C1, C2, C3, 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_3d_expr>;
41 
46  pool_upsample_3d_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_3d:A and R must have the same size");
61  static_assert(etl::decay_traits<B>::size() == etl::decay_traits<C>::size(), "max_pool_upsample_3d:B and C must have the same size");
62 
63  static_assert(etl::decay_traits<A>::template dim<D - 3>() == C1 * etl::decay_traits<B>::template dim<D - 3>(),
64  "Invalid pooling dimensions for max_pool_upsample_2d");
65  static_assert(etl::decay_traits<A>::template dim<D - 2>() == C2 * etl::decay_traits<B>::template dim<D - 2>(),
66  "Invalid pooling dimensions for max_pool_upsample_2d");
67  static_assert(etl::decay_traits<A>::template dim<D - 1>() == C3 * etl::decay_traits<B>::template dim<D - 1>(),
68  "Invalid pooling dimensions for max_pool_upsample_2d");
69  } else {
70  cpp_assert(etl::size(result) == etl::size(a), "max_pool_upsample_3d:A and R must have the same size");
71  cpp_assert(etl::size(b) == etl::size(c), "max_pool_upsample_3d:B and C must have the same size");
72 
73  cpp_assert(etl::dim<D - 3>(a) == C1 * etl::dim<D - 3>(b), "Invalid pooling dimensions for max_pool_upsample_2d");
74  cpp_assert(etl::dim<D - 2>(a) == C2 * etl::dim<D - 2>(b), "Invalid pooling dimensions for max_pool_upsample_2d");
75  cpp_assert(etl::dim<D - 1>(a) == C3 * etl::dim<D - 1>(b), "Invalid pooling dimensions for max_pool_upsample_2d");
76  }
77  }
78 
79  // Assignment functions
80 
88  template <typename R>
89  static constexpr etl::pool_impl select_default_impl(bool no_gpu) {
90  if (cudnn_enabled && all_floating<A, B, C, R> && !no_gpu) {
91  return etl::pool_impl::CUDNN;
92  }
93 
94  return etl::pool_impl::STD;
95  }
96 
97 #ifdef ETL_MANUAL_SELECT
98 
103  template <typename R>
104  static etl::pool_impl select_impl() {
105  if (local_context().pool_selector.forced) {
106  auto forced = local_context().pool_selector.impl;
107 
108  switch (forced) {
109  // CUDNN cannot always be used
110  case pool_impl::CUDNN:
111  if (!cudnn_enabled || !all_floating<A, B, C, R> || local_context().cpu) { //COVERAGE_EXCLUDE_LINE
112  std::cerr << "Forced selection to CUDNN pool implementation, but not possible for this expression" << std::endl; //COVERAGE_EXCLUDE_LINE
113  return select_default_impl<R>(local_context().cpu); //COVERAGE_EXCLUDE_LINE
114  } //COVERAGE_EXCLUDE_LINE
115 
116  return forced;
117 
118  //In other cases, simply use the forced impl
119  default:
120  return forced;
121  }
122  }
123 
124  return select_default_impl<R>(local_context().cpu);
125  }
126 
127 #else
128 
134  template <typename R>
135  static constexpr etl::pool_impl select_impl() {
136  return select_default_impl<R>(false);
137  }
138 
139 #endif
140 
145  template <etl_expr R>
146  void assign_to(R&& result) const {
147  inc_counter("temp:assign");
148 
149  auto& a = this->a();
150  auto& b = this->b();
151  auto& c = this->c();
152 
153  check(a, b, c, result);
154 
155  constexpr_select auto impl = select_impl<R>();
156 
157  if constexpr (Max) {
158  if
159  constexpr_select(impl == pool_impl::STD) {
160  inc_counter("impl:std");
161  impl::standard::max_pool_upsample_3d::apply<C1, C2, C3>(smart_forward(a), smart_forward(b), smart_forward(c), result);
162  }
163  else if
164  constexpr_select(impl == pool_impl::CUDNN) {
165  inc_counter("impl:cudnn");
167  }
168  else {
169  cpp_unreachable("Invalid pool implementation");
170  }
171  } else {
172  if
173  constexpr_select(impl == pool_impl::STD) {
174  inc_counter("impl:std");
175  impl::standard::avg_pool_upsample_3d::apply<C1, C2, C3>(smart_forward(a), smart_forward(b), smart_forward(c), result);
176  }
177  else if
178  constexpr_select(impl == pool_impl::CUDNN) {
179  inc_counter("impl:cudnn");
181  }
182  else {
183  cpp_unreachable("Invalid pool implementation");
184  }
185  }
186  }
187 
192  template <typename L>
193  void assign_add_to(L&& lhs) const {
194  std_add_evaluate(*this, lhs);
195  }
196 
201  template <typename L>
202  void assign_sub_to(L&& lhs) const {
203  std_sub_evaluate(*this, lhs);
204  }
205 
210  template <typename L>
211  void assign_mul_to(L&& lhs) const {
212  std_mul_evaluate(*this, lhs);
213  }
214 
219  template <typename L>
220  void assign_div_to(L&& lhs) const {
221  std_div_evaluate(*this, lhs);
222  }
223 
228  template <typename L>
229  void assign_mod_to(L&& lhs) const {
230  std_mod_evaluate(*this, lhs);
231  }
232 
239  friend std::ostream& operator<<(std::ostream& os, const pool_upsample_3d_expr& expr) {
240  return os << "max_pool_upsample3(" << expr._a << ", " << expr._b << ", " << expr._c << ")";
241  }
242 };
243 
248 template <typename A, typename B, typename C, size_t C1, size_t C2, size_t C3, bool Max>
249 struct etl_traits<etl::pool_upsample_3d_expr<A, B, C, C1, C2, C3, Max>> {
251  using sub_expr_t = std::decay_t<A>;
254 
255  static constexpr bool is_etl = true;
256  static constexpr bool is_transformer = false;
257  static constexpr bool is_view = false;
258  static constexpr bool is_magic_view = false;
259  static constexpr bool is_fast = sub_traits::is_fast;
260  static constexpr bool is_linear = false;
261  static constexpr bool is_thread_safe = true;
262  static constexpr bool is_value = false;
263  static constexpr bool is_direct = true;
264  static constexpr bool is_generator = false;
265  static constexpr bool is_padded = false;
266  static constexpr bool is_aligned = true;
267  static constexpr bool is_temporary = true;
268  static constexpr order storage_order = sub_traits::storage_order;
269  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
270 
276  template <vector_mode_t V>
277  static constexpr bool vectorizable = true;
278 
283  template <size_t DD>
284  static constexpr size_t dim() {
285  return decay_traits<A>::template dim<DD>();
286  }
287 
294  static size_t dim(const expr_t& e, size_t d) {
295  return etl::dim(e.a(), d);
296  }
297 
303  static size_t size(const expr_t& e) {
304  return etl::size(e.a());
305  }
306 
311  static constexpr size_t size() {
312  return decay_traits<A>::size();
313  }
314 
319  static constexpr size_t dimensions() {
320  return sub_traits::dimensions();
321  }
322 
327  static constexpr int complexity() noexcept {
328  return -1;
329  }
330 };
331 
341 template <size_t C1, size_t C2, size_t C3, typename A, typename B, typename C>
343  B&& output,
344  C&& errors) {
345  return {input, output, errors};
346 }
347 
357 template <size_t C1, size_t C2, size_t C3, typename A, typename B, typename C>
358 pool_upsample_3d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, C1, C2, C3, false> avg_pool_upsample_3d(A&& input,
359  B&& output,
360  C&& errors) {
361  return {input, output, errors};
362 }
363 
364 } //end of namespace etl
static constexpr size_t size()
Returns the size of the expression.
Definition: pool_upsample_3d_expr.hpp:311
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: pool_upsample_3d_expr.hpp:220
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:702
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: pool_upsample_3d_expr.hpp:38
dyn_pool_upsample_3d_expr< detail::build_type< A >, detail::build_type< B >, detail::build_type< C >, true > max_pool_upsample_3d(A &&input, B &&output, C &&errors, size_t c1, size_t c2, size_t c3)
Derivative of the 3D Max Pooling of the given matrix expression and upsampling.
Definition: dyn_pool_upsample_3d_expr.hpp:319
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 size_t dim()
Returns the DDth dimension of the expression.
Definition: pool_upsample_3d_expr.hpp:284
friend std::ostream & operator<<(std::ostream &os, const pool_upsample_3d_expr &expr)
Print a representation of the expression on the given stream.
Definition: pool_upsample_3d_expr.hpp:239
D D
The number of dimensions.
Definition: dyn_matrix_view.hpp:24
order
Storage order of a matrix.
Definition: order.hpp:15
constexpr bool cuda_enabled
Indicates if CUDA is available.
Definition: config.hpp:94
A derivative of the max pooling (combine derivative and upsampling for performance) ...
Definition: pool_upsample_3d_expr.hpp:26
A _a
The first sub expression reference.
Definition: base_temporary_expr.hpp:638
value_t< A > value_type
The type of value of the expression.
Definition: pool_upsample_3d_expr.hpp:27
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 c3)
Apply the functor on sub and store the result in m.
Definition: pooling_upsample.hpp:290
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
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
context & local_context()
Return the configuration context of the current thread.
Definition: context.hpp:50
static constexpr size_t dimensions()
Return the number of dimensions of the expression.
Definition: traits_base.hpp:31
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
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
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: pool_upsample_3d_expr.hpp:303
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
value_t< A > value_type
The value type of the expression.
Definition: pool_upsample_3d_expr.hpp:253
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
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: pool_upsample_3d_expr.hpp:202
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
std::decay_t< A > sub_expr_t
The sub expression type.
Definition: pool_upsample_3d_expr.hpp:251
decltype(auto) smart_forward_gpu(E &expr)
Smart forwarding for a temporary expression that will be computed in GPU.
Definition: helpers.hpp:343
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
dyn_pool_upsample_3d_expr< detail::build_type< A >, detail::build_type< B >, detail::build_type< C >, false > avg_pool_upsample_3d(A &&input, B &&output, C &&errors, size_t c1, size_t c2, size_t c3)
Derivative of the 3D Average Pooling of the given matrix expression and upsampling.
Definition: dyn_pool_upsample_3d_expr.hpp:334
void assign_to(R &&result) const
Assign to a matrix of the same storage order.
Definition: pool_upsample_3d_expr.hpp:146
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
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 c3)
Apply the functor on sub and store the result in m.
Definition: pooling_upsample.hpp:240
pool_upsample_3d_expr(A a, B b, C c)
Construct a new expression.
Definition: pool_upsample_3d_expr.hpp:46
static size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: pool_upsample_3d_expr.hpp:294
static constexpr bool is_fast
Indicates if T is a fast structure.
Definition: traits_base.hpp:25
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
static constexpr auto storage_order
The sub storage order.
Definition: pool_upsample_3d_expr.hpp:32
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: pool_upsample_3d_expr.hpp:193
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
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: pool_upsample_3d_expr.hpp:229
C _c
The third sub expression reference.
Definition: base_temporary_expr.hpp:640
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
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: pool_upsample_3d_expr.hpp:327
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_3d_expr.hpp:89
std::add_lvalue_reference_t< C > c()
Returns the sub expression.
Definition: base_temporary_expr.hpp:718
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_3d_expr.hpp:56
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: pool_upsample_3d_expr.hpp:211
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195
static constexpr etl::pool_impl select_impl()
Select the pool implementation for an expression of type ABC->R.
Definition: pool_upsample_3d_expr.hpp:135
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: pool_upsample_3d_expr.hpp:319