Expression Templates Library (ETL)
conv_2d_backward_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/conv.hpp"
14 
15 namespace etl {
16 
43 template <etl_2d A, etl_2d B, size_t S1, size_t S2, size_t P1, size_t P2, bool Flipped>
44 struct conv_2d_backward_expr : base_temporary_expr_bin<conv_2d_backward_expr<A, B, S1, S2, P1, P2, Flipped>, A, B> {
49 
50  static constexpr auto storage_order = left_traits::storage_order;
51 
56  static constexpr bool gpu_computable = cudnn_enabled && impl::cudnn::conv_possible_<A, B>;
57 
62  explicit conv_2d_backward_expr(A a, B b) : base_type(a, b) {
63  //Nothing else to init
64  }
65 
66  // Assignment functions
67 
71  template <etl_2d I, etl_2d K, etl_2d C>
72  static void check([[maybe_unused]] const I& input, [[maybe_unused]] const K& kernel, [[maybe_unused]] const C& conv) {
73  if constexpr (all_fast<A, B, C>) {
74  static_assert(etl::dim<0, C>() == S1 * (etl::dim<0, I>() - 1) + etl::dim<0, K>() - 2 * P1, "Invalid dimensions for conv2_backward");
75  static_assert(etl::dim<1, C>() == S2 * (etl::dim<1, I>() - 1) + etl::dim<1, K>() - 2 * P2, "Invalid dimensions for conv2_backward");
76  } else {
77  cpp_assert(etl::dim(conv, 0) == S1 * (etl::dim(input, 0) - 1) + etl::dim(kernel, 0) - 2 * P1, "Invalid dimensions for conv2_backward");
78  cpp_assert(etl::dim(conv, 1) == S2 * (etl::dim(input, 1) - 1) + etl::dim(kernel, 1) - 2 * P2, "Invalid dimensions for conv2_backward");
79  }
80  }
81 
86  template <etl_2d C>
87  void assign_to(C&& conv) const {
88  inc_counter("temp:assign");
89 
90  auto& input = this->a();
91  auto& kernel = this->b();
92 
93  check(input, kernel, conv);
94 
95  // Need K1 / K2 to compute transposed padding
96  const size_t K1 = etl::dim<0>(kernel);
97  const size_t K2 = etl::dim<1>(kernel);
98 
99  if constexpr (Flipped) {
100  // 1. Handle unit strides
101  if constexpr (S1 == 1 && S2 == 1) {
102  if constexpr (P1 == 0 && P2 == 0) {
103  // Unit strides, non-zero padding -> Full convolution
104  detail::conv2_full_flipped_impl::apply(input, kernel, conv);
105  } else {
106  // Unit strides, zero padding -> Valid convolution with the correct padding
107  detail::dyn_conv2_valid_flipped_impl::apply(input, kernel, conv, 1, 1, K1 - P1 - 1, K2 - P2 - 1);
108  }
109  }
110  // 2. Handle non_unit strides
111  else {
112  // Fractionally-strided convolution needs inner padding of the input
113  auto strided_input = impl::common::inner_pad(input, S1, S2);
114 
115  if constexpr (P1 == 0 && P2 == 0) {
116  // Non-unit strides, non-zero padding -> Fractionally-strided full convolution
117  detail::conv2_full_flipped_impl::apply(strided_input, kernel, conv);
118  } else {
119  // Non-unit strides, zero padding -> Fractionally-strided Valid convolution with the correct padding
120  detail::dyn_conv2_valid_flipped_impl::apply(strided_input, kernel, conv, 1, 1, K1 - P1 - 1, K2 - P2 - 1);
121  }
122  }
123  } else {
124  // 1. Handle unit strides
125  if constexpr (S1 == 1 && S2 == 1) {
126  if constexpr (P1 == 0 && P2 == 0) {
127  // Unit strides, non-zero padding -> Full convolution
128  detail::conv2_full_impl::apply(input, kernel, conv);
129  } else {
130  // Unit strides, zero padding -> Valid convolution with the correct padding
131  detail::dyn_conv2_valid_impl::apply(input, kernel, conv, 1, 1, K1 - P1 - 1, K2 - P2 - 1);
132  }
133  }
134  // 2. Handle non_unit strides
135  else {
136  // Fractionally-strided convolution needs inner padding of the input
137  auto strided_input = impl::common::inner_pad(input, S1, S2);
138 
139  if constexpr (P1 == 0 && P2 == 0) {
140  // Non-unit strides, non-zero padding -> Fractionally-strided full convolution
141  detail::conv2_full_impl::apply(strided_input, kernel, conv);
142  } else {
143  // Non-unit strides, zero padding -> Fractionally-strided Valid convolution with the correct padding
144  detail::dyn_conv2_valid_impl::apply(strided_input, kernel, conv, 1, 1, K1 - P1 - 1, K2 - P2 - 1);
145  }
146  }
147  }
148  }
149 
154  template <etl_2d L>
155  void assign_add_to(L&& lhs) const {
156  std_add_evaluate(*this, lhs);
157  }
158 
163  template <etl_2d L>
164  void assign_sub_to(L&& lhs) const {
165  std_sub_evaluate(*this, lhs);
166  }
167 
172  template <etl_2d L>
173  void assign_mul_to(L&& lhs) const {
174  std_mul_evaluate(*this, lhs);
175  }
176 
181  template <etl_2d L>
182  void assign_div_to(L&& lhs) const {
183  std_div_evaluate(*this, lhs);
184  }
185 
190  template <etl_2d L>
191  void assign_mod_to(L&& lhs) const {
192  std_mod_evaluate(*this, lhs);
193  }
194 
201  friend std::ostream& operator<<(std::ostream& os, const conv_2d_backward_expr& expr) {
202  return os << "conv2_backward(" << expr._a << ", " << expr._b << ")";
203  }
204 };
205 
210 template <etl_2d A, etl_2d B, size_t S1, size_t S2, size_t P1, size_t P2, bool Flipped>
211 struct etl_traits<etl::conv_2d_backward_expr<A, B, S1, S2, P1, P2, Flipped>> {
213  using left_expr_t = std::decay_t<A>;
214  using right_expr_t = std::decay_t<B>;
218 
219  static constexpr bool is_etl = true;
220  static constexpr bool is_transformer = false;
221  static constexpr bool is_view = false;
222  static constexpr bool is_magic_view = false;
223  static constexpr bool is_fast = all_fast<A, B>;
224  static constexpr bool is_linear = false;
225  static constexpr bool is_thread_safe = true;
226  static constexpr bool is_value = false;
227  static constexpr bool is_direct = true;
228  static constexpr bool is_generator = false;
229  static constexpr bool is_padded = false;
230  static constexpr bool is_aligned = true;
231  static constexpr bool is_temporary = true;
232  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
233  static constexpr order storage_order = left_traits::storage_order;
234 
240  template <vector_mode_t V>
241  static constexpr bool vectorizable = true;
242 
247  template <size_t DD>
248  static constexpr size_t dim() {
249  return DD == 0 ? (S1 * (etl::dim<0, A>() - 1) + etl::dim<0, B>() - 2 * P1) : (S2 * (etl::dim<1, A>() - 1) + etl::dim<1, B>() - 2 * P2);
250  }
251 
258  static size_t dim(const expr_t& e, size_t d) {
259  if (d == 0) {
260  return S1 * (etl::dim(e._a, 0) - 1) + etl::dim(e._b, 0) - 2 * P1;
261  } else {
262  return S2 * (etl::dim(e._a, 1) - 1) + etl::dim(e._b, 1) - 2 * P2;
263  }
264  }
265 
271  static size_t size(const expr_t& e) {
272  return (S1 * (etl::dim(e._a, 0) - 1) + etl::dim(e._b, 0) - 2 * P1) * (S2 * (etl::dim(e._a, 1) - 1) + etl::dim(e._b, 1) - 2 * P2);
273  }
274 
279  static constexpr size_t size() {
280  return (S1 * (etl::dim<0, A>() - 1) + etl::dim<0, B>() - 2 * P1) * (S2 * (etl::dim<1, A>() - 1) + etl::dim<1, B>() - 2 * P2);
281  }
282 
287  static constexpr size_t dimensions() {
288  return 2;
289  }
290 
295  static constexpr int complexity() noexcept {
296  return -1;
297  }
298 };
299 
316 template <size_t S1 = 1, size_t S2 = 1, size_t P1 = 0, size_t P2 = 0, etl_2d A, etl_2d B>
318  return conv_2d_backward_expr<detail::build_type<A>, detail::build_type<B>, S1, S2, P1, P2, false>{a, b};
319 }
320 
339 template <size_t S1 = 1, size_t S2 = 1, size_t P1 = 0, size_t P2 = 0, etl_2d A, etl_2d B, etl_2d C>
340 auto conv_2d_backward(A&& a, B&& b, C&& c) {
341  c = conv_2d_backward<S1, S2, P1, P2>(a, b);
342 
343  return c;
344 }
345 
362 template <size_t S1 = 1, size_t S2 = 1, size_t P1 = 0, size_t P2 = 0, etl_2d A, etl_2d B>
363 conv_2d_backward_expr<detail::build_type<A>, detail::build_type<B>, S1, S2, P1, P2, true> conv_2d_backward_flipped(A&& a, B&& b) {
364  return conv_2d_backward_expr<detail::build_type<A>, detail::build_type<B>, S1, S2, P1, P2, true>{a, b};
365 }
366 
385 template <size_t S1 = 1, size_t S2 = 1, size_t P1 = 0, size_t P2 = 0, etl_2d A, etl_2d B, etl_2d C>
386 auto conv_2d_backward_flipped(A&& a, B&& b, C&& c) {
387  c = conv_2d_backward_flipped<S1, S2, P1, P2>(a, b);
388 
389  return c;
390 }
391 
392 } //end of namespace etl
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: conv_2d_backward_expr.hpp:182
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: conv_2d_backward_expr.hpp:191
value_t< A > value_type
The type of value of the expression.
Definition: conv_2d_backward_expr.hpp:45
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: conv_2d_backward_expr.hpp:287
static constexpr auto storage_order
The sub storage order.
Definition: conv_2d_backward_expr.hpp:50
B _b
The sub expression reference.
Definition: base_temporary_expr.hpp:534
std::decay_t< B > right_expr_t
The right sub expression type.
Definition: conv_2d_backward_expr.hpp:214
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: conv_2d_backward_expr.hpp:164
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: conv_2d_backward_expr.hpp:295
A _a
The sub expression reference.
Definition: base_temporary_expr.hpp:533
order
Storage order of a matrix.
Definition: order.hpp:15
value_t< A > value_type
The value type of the expression.
Definition: conv_2d_backward_expr.hpp:217
constexpr bool cuda_enabled
Indicates if CUDA is available.
Definition: config.hpp:94
Abstract base class for temporary binary expression.
Definition: base_temporary_expr.hpp:529
static void apply(const I &input, const K &kernel, C &conv)
Apply the convolution.
Definition: conv_2d.hpp:28
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: conv_2d_backward_expr.hpp:56
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:593
conv_2d_backward_expr< detail::build_type< A >, detail::build_type< B >, S1, S2, P1, P2, false > conv_2d_backward(A &&a, B &&b)
Creates an expression representing the transposed 2D convolution of a and b.
Definition: conv_2d_backward_expr.hpp:317
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 void apply(const I &input, const K &kernel, C &conv)
Apply the convolution.
Definition: conv_2d.hpp:78
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
static size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: conv_2d_backward_expr.hpp:258
static void apply(const I &input, const K &kernel, C &conv, size_t s1, size_t s2, size_t p1, size_t p2)
Apply the convolution.
Definition: conv_2d.hpp:256
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: conv_2d_backward_expr.hpp:155
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
constexpr bool cudnn_enabled
Indicates if the NVIDIA CUDNN library is available for ETL.
Definition: config.hpp:114
conv_2d_backward_expr< detail::build_type< A >, detail::build_type< B >, S1, S2, P1, P2, true > conv_2d_backward_flipped(A &&a, B &&b)
Creates an expression representing the &#39;backward&#39; 1D convolution of a and flipped b...
Definition: conv_2d_backward_expr.hpp:363
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
conv_2d_backward_expr(A a, B b)
Construct a new expression.
Definition: conv_2d_backward_expr.hpp:62
static constexpr size_t size()
Returns the size of the expression.
Definition: conv_2d_backward_expr.hpp:279
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
Selector for the convolution implementations.
static constexpr size_t dim()
Returns the DDth dimension of the expression.
Definition: conv_2d_backward_expr.hpp:248
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
void assign_to(C &&conv) const
Assign to a matrix.
Definition: conv_2d_backward_expr.hpp:87
std::decay_t< A > left_expr_t
The left sub expression type.
Definition: conv_2d_backward_expr.hpp:213
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: conv_2d_backward_expr.hpp:271
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
friend std::ostream & operator<<(std::ostream &os, const conv_2d_backward_expr &expr)
Print a representation of the expression on the given stream.
Definition: conv_2d_backward_expr.hpp:201
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: conv_2d_backward_expr.hpp:173
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 void check([[maybe_unused]] const I &input, [[maybe_unused]] const K &kernel, [[maybe_unused]] const C &conv)
Assert that the convolution is done on correct dimensions.
Definition: conv_2d_backward_expr.hpp:72
std::add_lvalue_reference_t< A > a()
Returns the sub expression.
Definition: base_temporary_expr.hpp:577
Expression representing the transposed 2D convolution of an image with a kernel.
Definition: conv_2d_backward_expr.hpp:44
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195
static void apply(const I &input, const K &kernel, C &conv, size_t s1, size_t s2, size_t p1, size_t p2)
Apply the convolution.
Definition: conv_2d.hpp:289