Expression Templates Library (ETL)
dyn_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 
39 template <etl_expr A, etl_expr B, bool Flipped>
40 struct dyn_conv_2d_backward_expr : base_temporary_expr_bin<dyn_conv_2d_backward_expr<A, B, Flipped>, A, B> {
45 
46  static constexpr auto storage_order = left_traits::storage_order;
47 
52  static constexpr bool gpu_computable = cudnn_enabled && impl::cudnn::conv_possible_<A, B>;
53 
54  const size_t s1;
55  const size_t s2;
56  const size_t p1;
57  const size_t p2;
58 
63  explicit dyn_conv_2d_backward_expr(A a, B b, size_t s1, size_t s2, size_t p1, size_t p2) : base_type(a, b), s1(s1), s2(s2), p1(p1), p2(p2) {
64  //Nothing else to init
65  }
66 
67  // Assignment functions
68 
72  template <etl_2d I, etl_2d K, etl_2d C>
73  void check([[maybe_unused]] const I& input, [[maybe_unused]] const K& kernel, [[maybe_unused]] const C& conv) const {
74  cpp_assert(etl::dim(conv, 0) == s1 * (etl::dim(input, 0) - 1) + etl::dim(kernel, 0) - 2 * p1, "Invalid dimensions for conv2_backward");
75  cpp_assert(etl::dim(conv, 1) == s2 * (etl::dim(input, 1) - 1) + etl::dim(kernel, 1) - 2 * p2, "Invalid dimensions for conv2_backward");
76  }
77 
82  template <etl_expr C>
83  void assign_to(C&& conv) const {
84  inc_counter("temp:assign");
85 
86  auto& input = this->a();
87  auto& kernel = this->b();
88 
89  check(input, kernel, conv);
90 
91  // Need K1 / K2 to compute transposed padding
92  const size_t k1 = etl::dim<0>(kernel);
93  const size_t k2 = etl::dim<1>(kernel);
94 
95  if constexpr (Flipped) {
96  // 1. Handle unit strides
97  if (s1 == 1 && s2 == 1) {
98  if (p1 == 0 && p2 == 0) {
99  // Unit strides, non-zero padding -> Full convolution
100  detail::conv2_full_flipped_impl::apply(input, kernel, conv);
101  } else {
102  // Unit strides, zero padding -> Valid convolution with the correct padding
103  detail::dyn_conv2_valid_flipped_impl::apply(input, kernel, conv, 1, 1, k1 - p1 - 1, k2 - p2 - 1);
104  }
105  }
106  // 2. Handle non_unit strides
107  else {
108  // Fractionally-strided convolution needs inner padding of the input
109  auto strided_input = impl::common::inner_pad(input, s1, s2);
110 
111  if (p1 == 0 && p2 == 0) {
112  // Non-unit strides, non-zero padding -> Fractionally-strided full convolution
113  detail::conv2_full_flipped_impl::apply(strided_input, kernel, conv);
114  } else {
115  // Non-unit strides, zero padding -> Fractionally-strided Valid convolution with the correct padding
116  detail::dyn_conv2_valid_flipped_impl::apply(strided_input, kernel, conv, 1, 1, k1 - p1 - 1, k2 - p2 - 1);
117  }
118  }
119  } else {
120  // 1. Handle unit strides
121  if (s1 == 1 && s2 == 1) {
122  if (p1 == 0 && p2 == 0) {
123  // Unit strides, non-zero padding -> Full convolution
124  detail::conv2_full_impl::apply(input, kernel, conv);
125  } else {
126  // Unit strides, zero padding -> Valid convolution with the correct padding
127  detail::dyn_conv2_valid_impl::apply(input, kernel, conv, 1, 1, k1 - p1 - 1, k2 - p2 - 1);
128  }
129  }
130  // 2. Handle non_unit strides
131  else {
132  // Fractionally-strided convolution needs inner padding of the input
133  auto strided_input = impl::common::inner_pad(input, s1, s2);
134 
135  if (p1 == 0 && p2 == 0) {
136  // Non-unit strides, non-zero padding -> Fractionally-strided full convolution
137  detail::conv2_full_impl::apply(strided_input, kernel, conv);
138  } else {
139  // Non-unit strides, zero padding -> Fractionally-strided Valid convolution with the correct padding
140  detail::dyn_conv2_valid_impl::apply(strided_input, kernel, conv, 1, 1, k1 - p1 - 1, k2 - p2 - 1);
141  }
142  }
143  }
144  }
145 
150  template <etl_expr L>
151  void assign_add_to(L&& lhs) const {
152  std_add_evaluate(*this, lhs);
153  }
154 
159  template <etl_expr L>
160  void assign_sub_to(L&& lhs) const {
161  std_sub_evaluate(*this, lhs);
162  }
163 
168  template <etl_expr L>
169  void assign_mul_to(L&& lhs) const {
170  std_mul_evaluate(*this, lhs);
171  }
172 
177  template <etl_expr L>
178  void assign_div_to(L&& lhs) const {
179  std_div_evaluate(*this, lhs);
180  }
181 
186  template <etl_expr L>
187  void assign_mod_to(L&& lhs) const {
188  std_mod_evaluate(*this, lhs);
189  }
190 
197  friend std::ostream& operator<<(std::ostream& os, const dyn_conv_2d_backward_expr& expr) {
198  return os << "conv2_backward(" << expr._a << ", " << expr._b << ")";
199  }
200 };
201 
206 template <etl_expr A, etl_expr B, bool Flipped>
207 struct etl_traits<etl::dyn_conv_2d_backward_expr<A, B, Flipped>> {
209  using left_expr_t = std::decay_t<A>;
210  using right_expr_t = std::decay_t<B>;
214 
215  static constexpr bool is_etl = true;
216  static constexpr bool is_transformer = false;
217  static constexpr bool is_view = false;
218  static constexpr bool is_magic_view = false;
219  static constexpr bool is_fast = false;
220  static constexpr bool is_linear = false;
221  static constexpr bool is_thread_safe = true;
222  static constexpr bool is_value = false;
223  static constexpr bool is_direct = true;
224  static constexpr bool is_generator = false;
225  static constexpr bool is_padded = false;
226  static constexpr bool is_aligned = true;
227  static constexpr bool is_temporary = true;
228  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
229  static constexpr order storage_order = left_traits::storage_order;
230 
236  template <vector_mode_t V>
237  static constexpr bool vectorizable = true;
238 
245  static size_t dim(const expr_t& e, size_t d) {
246  if (d == 0) {
247  return e.s1 * (etl::dim(e._a, 0) - 1) + etl::dim(e._b, 0) - 2 * e.p1;
248  } else {
249  return e.s2 * (etl::dim(e._a, 1) - 1) + etl::dim(e._b, 1) - 2 * e.p2;
250  }
251  }
252 
258  static size_t size(const expr_t& e) {
259  return (e.s1 * (etl::dim(e._a, 0) - 1) + etl::dim(e._b, 0) - 2 * e.p1) * (e.s2 * (etl::dim(e._a, 1) - 1) + etl::dim(e._b, 1) - 2 * e.p2);
260  }
261 
266  static constexpr size_t dimensions() {
267  return 2;
268  }
269 
274  static constexpr int complexity() noexcept {
275  return -1;
276  }
277 };
278 
295 template <etl_expr A, etl_expr B>
297  return dyn_conv_2d_backward_expr<detail::build_type<A>, detail::build_type<B>, false>{a, b, s1, s2, p1, p2};
298 }
299 
318 template <etl_expr A, etl_expr B, etl_expr C>
319 auto conv_2d_backward(A&& a, B&& b, C&& c, size_t s1, size_t s2, size_t p1, size_t p2) {
320  c = conv_2d_backward(a, b, s1, s2, p1, p2);
321 
322  return c;
323 }
324 
341 template <etl_expr A, etl_expr B>
343  A&& a, B&& b, size_t s1, size_t s2, size_t p1, size_t p2) {
344  return dyn_conv_2d_backward_expr<detail::build_type<A>, detail::build_type<B>, true>{a, b, s1, s2, p1, p2};
345 }
346 
365 template <etl_expr A, etl_expr B, etl_expr C>
366 auto conv_2d_backward_flipped(A&& a, B&& b, C&& c, size_t s1, size_t s2, size_t p1, size_t p2) {
367  c = conv_2d_backward_flipped(a, b, s1, s2, p1, p2);
368 
369  return c;
370 }
371 
372 } //end of namespace etl
B _b
The sub expression reference.
Definition: base_temporary_expr.hpp:534
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: dyn_conv_2d_backward_expr.hpp:169
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
A _a
The sub expression reference.
Definition: base_temporary_expr.hpp:533
value_t< A > value_type
The value type of the expression.
Definition: dyn_conv_2d_backward_expr.hpp:213
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: dyn_conv_2d_backward_expr.hpp:266
order
Storage order of a matrix.
Definition: order.hpp:15
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
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 size_t size(const expr_t &e)
Returns the size of the expression.
Definition: dyn_conv_2d_backward_expr.hpp:258
dyn_conv_2d_backward_expr(A a, B b, size_t s1, size_t s2, size_t p1, size_t p2)
Construct a new expression.
Definition: dyn_conv_2d_backward_expr.hpp:63
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
void check([[maybe_unused]] const I &input, [[maybe_unused]] const K &kernel, [[maybe_unused]] const C &conv) const
Assert that the convolution is done on correct dimensions.
Definition: dyn_conv_2d_backward_expr.hpp:73
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
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
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: dyn_conv_2d_backward_expr.hpp:187
value_t< A > value_type
The type of value of the expression.
Definition: dyn_conv_2d_backward_expr.hpp:41
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
static size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: dyn_conv_2d_backward_expr.hpp:245
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
friend std::ostream & operator<<(std::ostream &os, const dyn_conv_2d_backward_expr &expr)
Print a representation of the expression on the given stream.
Definition: dyn_conv_2d_backward_expr.hpp:197
const size_t s2
The stride of the second dimension.
Definition: dyn_conv_2d_backward_expr.hpp:55
void std_mul_evaluate(Expr &&expr, Result &&result)
Compound multiply evaluation of the expr into result.
Definition: evaluator.hpp:1233
std::decay_t< A > left_expr_t
The left sub expression type.
Definition: dyn_conv_2d_backward_expr.hpp:209
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: dyn_conv_2d_backward_expr.hpp:151
Selector for the convolution implementations.
void assign_to(C &&conv) const
Assign to a matrix.
Definition: dyn_conv_2d_backward_expr.hpp:83
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
static constexpr auto storage_order
The sub storage order.
Definition: dyn_conv_2d_backward_expr.hpp:46
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
std::decay_t< B > right_expr_t
The right sub expression type.
Definition: dyn_conv_2d_backward_expr.hpp:210
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: dyn_conv_2d_backward_expr.hpp:274
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
Expression representing the transposed 2D convolution of an image with a kernel.
Definition: dyn_conv_2d_backward_expr.hpp:40
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
std::add_lvalue_reference_t< A > a()
Returns the sub expression.
Definition: base_temporary_expr.hpp:577
const size_t p2
The padding of the second dimension.
Definition: dyn_conv_2d_backward_expr.hpp:57
const size_t p1
The padding of the first dimension.
Definition: dyn_conv_2d_backward_expr.hpp:56
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: dyn_conv_2d_backward_expr.hpp:160
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: dyn_conv_2d_backward_expr.hpp:178
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: dyn_conv_2d_backward_expr.hpp:52
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
const size_t s1
The stride of the first dimension.
Definition: dyn_conv_2d_backward_expr.hpp:54