Expression Templates Library (ETL)
dyn_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, bool Max>
26 struct dyn_pool_upsample_3d_expr : base_temporary_expr_tern<dyn_pool_upsample_3d_expr<A, B, C, 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 private:
41  const size_t c1;
42  const size_t c2;
43  const size_t c3;
44 
46 
47 public:
52  dyn_pool_upsample_3d_expr(A a, B b, C c, size_t c1, size_t c2, size_t c3) : base_type(a, b, c), c1(c1), c2(c2), c3(c3) {
53  //Nothing else to init
54  }
55 
61  template <same_dimensions<A> R>
62  void check([[maybe_unused]] const A& a, [[maybe_unused]] const B& b, [[maybe_unused]] const C& c, [[maybe_unused]] const R& result) const {
63  static constexpr size_t D = etl::decay_traits<A>::dimensions();
64 
65  cpp_assert(etl::size(result) == etl::size(a), "max_pool_upsample_3d:A and R must have the same size");
66  cpp_assert(etl::size(b) == etl::size(c), "max_pool_upsample_3d:B and C must have the same size");
67 
68  cpp_assert(etl::dim<D - 3>(a) == c1 * etl::dim<D - 3>(b), "Invalid pooling dimensions for max_pool_upsample_2d");
69  cpp_assert(etl::dim<D - 2>(a) == c2 * etl::dim<D - 2>(b), "Invalid pooling dimensions for max_pool_upsample_2d");
70  cpp_assert(etl::dim<D - 1>(a) == c3 * etl::dim<D - 1>(b), "Invalid pooling dimensions for max_pool_upsample_2d");
71  }
72 
73  // Assignment functions
74 
82  template <typename R>
83  static constexpr etl::pool_impl select_default_impl(bool no_gpu) {
84  if (cudnn_enabled && all_floating<A, B, C, R> && !no_gpu) {
85  return etl::pool_impl::CUDNN;
86  }
87 
88  return etl::pool_impl::STD;
89  }
90 
91 #ifdef ETL_MANUAL_SELECT
92 
97  template <typename R>
98  static etl::pool_impl select_impl() {
99  if (local_context().pool_selector.forced) {
100  auto forced = local_context().pool_selector.impl;
101 
102  switch (forced) {
103  // CUDNN cannot always be used
104  case pool_impl::CUDNN:
105  if (!cudnn_enabled || !all_floating<A, B, C, R> || local_context().cpu) { //COVERAGE_EXCLUDE_LINE
106  std::cerr << "Forced selection to CUDNN pool implementation, but not possible for this expression" << std::endl; //COVERAGE_EXCLUDE_LINE
107  return select_default_impl<R>(local_context().cpu); //COVERAGE_EXCLUDE_LINE
108  } //COVERAGE_EXCLUDE_LINE
109 
110  return forced;
111 
112  //In other cases, simply use the forced impl
113  default:
114  return forced;
115  }
116  }
117 
118  return select_default_impl<R>(local_context().cpu);
119  }
120 
121 #else
122 
128  template <typename R>
129  static constexpr etl::pool_impl select_impl() {
130  return select_default_impl<R>(false);
131  }
132 
133 #endif
134 
139  template <etl_expr R>
140  void assign_to(R&& result) const {
141  inc_counter("temp:assign");
142 
143  auto& a = this->a();
144  auto& b = this->b();
145  auto& c = this->c();
146 
147  check(a, b, c, result);
148 
149  constexpr_select auto impl = select_impl<R>();
150 
151  if constexpr (Max) {
152  if
153  constexpr_select(impl == pool_impl::STD) {
154  inc_counter("inc:std");
156  }
157  else if
158  constexpr_select(impl == pool_impl::CUDNN) {
159  inc_counter("inc:cudnn");
161  }
162  else {
163  cpp_unreachable("Invalid pool implementation");
164  }
165  } else {
166  if
167  constexpr_select(impl == pool_impl::STD) {
168  inc_counter("inc:std");
170  }
171  else if
172  constexpr_select(impl == pool_impl::CUDNN) {
173  inc_counter("inc:cudnn");
175  }
176  else {
177  cpp_unreachable("Invalid pool implementation");
178  }
179  }
180  }
181 
186  template <typename L>
187  void assign_add_to(L&& lhs) const {
188  std_add_evaluate(*this, lhs);
189  }
190 
195  template <typename L>
196  void assign_sub_to(L&& lhs) const {
197  std_sub_evaluate(*this, lhs);
198  }
199 
204  template <typename L>
205  void assign_mul_to(L&& lhs) const {
206  std_mul_evaluate(*this, lhs);
207  }
208 
213  template <typename L>
214  void assign_div_to(L&& lhs) const {
215  std_div_evaluate(*this, lhs);
216  }
217 
222  template <typename L>
223  void assign_mod_to(L&& lhs) const {
224  std_mod_evaluate(*this, lhs);
225  }
226 
233  friend std::ostream& operator<<(std::ostream& os, const dyn_pool_upsample_3d_expr& expr) {
234  return os << "max_pool_upsample3(" << expr._a << ", " << expr._b << ", " << expr._c << ")";
235  }
236 };
237 
242 template <typename A, typename B, typename C, bool Max>
243 struct etl_traits<etl::dyn_pool_upsample_3d_expr<A, B, C, Max>> {
245  using sub_expr_t = std::decay_t<A>;
248 
249  static constexpr bool is_etl = true;
250  static constexpr bool is_transformer = false;
251  static constexpr bool is_view = false;
252  static constexpr bool is_magic_view = false;
253  static constexpr bool is_fast = false;
254  static constexpr bool is_linear = false;
255  static constexpr bool is_thread_safe = true;
256  static constexpr bool is_value = false;
257  static constexpr bool is_direct = true;
258  static constexpr bool is_generator = false;
259  static constexpr bool is_padded = false;
260  static constexpr bool is_aligned = true;
261  static constexpr bool is_temporary = true;
262  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
263  static constexpr order storage_order = sub_traits::storage_order;
264 
270  template <vector_mode_t V>
271  static constexpr bool vectorizable = true;
272 
279  static size_t dim(const expr_t& e, size_t d) {
280  return etl::dim(e.a(), d);
281  }
282 
288  static size_t size(const expr_t& e) {
289  return etl::size(e.a());
290  }
291 
296  static constexpr size_t dimensions() {
297  return sub_traits::dimensions();
298  }
299 
304  static constexpr int complexity() noexcept {
305  return -1;
306  }
307 };
308 
318 template <typename A, typename B, typename C>
320  A&& input, B&& output, C&& errors, size_t c1, size_t c2, size_t c3) {
321  return {input, output, errors, c1, c2, c3};
322 }
323 
333 template <typename A, typename B, typename C>
334 dyn_pool_upsample_3d_expr<detail::build_type<A>, detail::build_type<B>, detail::build_type<C>, false> avg_pool_upsample_3d(
335  A&& input, B&& output, C&& errors, size_t c1, size_t c2, size_t c3) {
336  return {input, output, errors, c1, c2, c3};
337 }
338 
339 } //end of namespace etl
dyn_pool_upsample_3d_expr(A a, B b, C c, size_t c1, size_t c2, size_t c3)
Construct a new expression.
Definition: dyn_pool_upsample_3d_expr.hpp:52
static void apply([[maybe_unused]] A &&in, [[maybe_unused]] B &&out, C &&errors, M &&m)
Apply the functor on sub and store the result in m.
Definition: avg_pooling_upsample.hpp:619
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:702
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
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: dyn_pool_upsample_3d_expr.hpp:296
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: dyn_pool_upsample_3d_expr.hpp:214
pool_impl
Enumeration describing the different implementations of pooling.
Definition: pool_impl.hpp:21
Standard implementation.
static void apply(A &&in, B &&out, C &&errors, M &&m)
Apply the functor on sub and store the result in m.
Definition: max_pooling_upsample.hpp:666
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
D D
The number of dimensions.
Definition: dyn_matrix_view.hpp:24
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: dyn_pool_upsample_3d_expr.hpp:38
static constexpr etl::pool_impl select_impl()
Select the pool implementation for an expression of type ABC->R.
Definition: dyn_pool_upsample_3d_expr.hpp:129
order
Storage order of a matrix.
Definition: order.hpp:15
void assign_to(R &&result) const
Assign to a matrix of the same storage order.
Definition: dyn_pool_upsample_3d_expr.hpp:140
constexpr bool cuda_enabled
Indicates if CUDA is available.
Definition: config.hpp:94
static size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: dyn_pool_upsample_3d_expr.hpp:279
A _a
The first sub expression reference.
Definition: base_temporary_expr.hpp:638
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: dyn_pool_upsample_3d_expr.hpp:187
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
value_t< A > value_type
The value type of the expression.
Definition: dyn_pool_upsample_3d_expr.hpp:247
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: dyn_pool_upsample_3d_expr.hpp:288
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
static constexpr etl::pool_impl select_default_impl(bool no_gpu)
Select the pool implementation for an expression of type ABC->R.
Definition: dyn_pool_upsample_3d_expr.hpp:83
bool cpu
Force CPU evaluation.
Definition: context.hpp:29
GPU implementation.
std::decay_t< A > sub_expr_t
The sub expression type.
Definition: dyn_pool_upsample_3d_expr.hpp:245
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: dyn_pool_upsample_3d_expr.hpp:304
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
static constexpr auto storage_order
The sub storage order.
Definition: dyn_pool_upsample_3d_expr.hpp:32
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
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
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
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
friend std::ostream & operator<<(std::ostream &os, const dyn_pool_upsample_3d_expr &expr)
Print a representation of the expression on the given stream.
Definition: dyn_pool_upsample_3d_expr.hpp:233
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: dyn_pool_upsample_3d_expr.hpp:205
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
void check([[maybe_unused]] const A &a, [[maybe_unused]] const B &b, [[maybe_unused]] const C &c, [[maybe_unused]] const R &result) const
Validate the transposition dimensions.
Definition: dyn_pool_upsample_3d_expr.hpp:62
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_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: dyn_pool_upsample_3d_expr.hpp:196
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
value_t< A > value_type
The type of value of the expression.
Definition: dyn_pool_upsample_3d_expr.hpp:27
void inc_counter([[maybe_unused]] const char *name)
Increase the given counter.
Definition: counters.hpp:25
A derivative of the 3D max pooling (combine derivative and upsampling for performance) ...
Definition: dyn_pool_upsample_3d_expr.hpp:26
std::add_lvalue_reference_t< C > c()
Returns the sub expression.
Definition: base_temporary_expr.hpp:718
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: dyn_pool_upsample_3d_expr.hpp:223
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195