Expression Templates Library (ETL)
dyn_conv_4d_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_4d A, etl_4d B, bool Flipped>
40 struct dyn_conv_4d_backward_expr : base_temporary_expr_bin<dyn_conv_4d_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_4d_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_4d I, etl_4d K, etl_4d 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) == etl::dim(input, 0), "Invalid dimensions for conv4_backward");
75  cpp_assert(etl::dim(conv, 1) == etl::dim(kernel, 1), "Invalid dimensions for conv4_backward");
76  cpp_assert(etl::dim(input, 1) == etl::dim(kernel, 0), "Invalid dimensions for conv4_backward");
77 
78  cpp_assert(etl::dim(conv, 2) == s1 * (etl::dim(input, 2) - 1) + etl::dim(kernel, 2) - 2 * p1, "Invalid dimensions for conv2_backward");
79  cpp_assert(etl::dim(conv, 3) == s2 * (etl::dim(input, 3) - 1) + etl::dim(kernel, 3) - 2 * p2, "Invalid dimensions for conv2_backward");
80  }
81 
86  template <etl_expr 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<2>(kernel);
97  const size_t k2 = etl::dim<3>(kernel);
98 
99  if constexpr (Flipped) {
100  // The GPU implementation needs the real forward parameters, not the
101  // converted backward parameters
102  if constexpr (cudnn_enabled && all_floating<A, B, C>) {
103  impl::cudnn::conv4_backward_data_flipped(smart_forward_gpu(input), smart_forward_gpu(kernel), conv, s1, s2, p1, p2);
104  return;
105  } else {
106  // 1. Handle unit strides
107  if (s1 == 1 && s2 == 1) {
108  if (p1 == 0 && p2 == 0) {
109  // Unit strides, non-zero padding -> Full convolution
110  detail::conv4_full_flipped_impl::apply(input, kernel, conv);
111  } else {
112  // Unit strides, zero padding -> Valid convolution with the correct padding
113  detail::dyn_conv4_valid_back_flipped_impl::apply(input, kernel, conv, 1, 1, k1 - p1 - 1, k2 - p2 - 1);
114  }
115  }
116  // 2. Handle non_unit strides
117  else {
118  // Fractionally-strided convolution needs inner padding of the input
119  auto strided_input = impl::common::inner_pad(input, s1, s2);
120 
121  if (p1 == 0 && p2 == 0) {
122  // Non-unit strides, non-zero padding -> Fractionally-strided full convolution
123  detail::conv4_full_flipped_impl::apply(strided_input, kernel, conv);
124  } else {
125  // Non-unit strides, zero padding -> Fractionally-strided Valid convolution with the correct padding
126  detail::dyn_conv4_valid_back_flipped_impl::apply(strided_input, kernel, conv, 1, 1, k1 - p1 - 1, k2 - p2 - 1);
127  }
128  }
129  }
130  } else {
131  // The GPU implementation needs the real forward parameters, not the
132  // converted backward parameters
133  if constexpr (cudnn_enabled && all_floating<A, B, C>) {
134  impl::cudnn::conv4_backward_data(smart_forward_gpu(input), smart_forward_gpu(kernel), conv, s1, s2, p1, p2);
135  return;
136  } else {
137  // 1. Handle unit strides
138  if (s1 == 1 && s2 == 1) {
139  if (p1 == 0 && p2 == 0) {
140  // Unit strides, non-zero padding -> Full convolution
141  detail::conv4_full_impl::apply(input, kernel, conv);
142  } else {
143  // Unit strides, zero padding -> Valid convolution with the correct padding
144  detail::dyn_conv4_valid_back_impl::apply(input, kernel, conv, 1, 1, k1 - p1 - 1, k2 - p2 - 1);
145  }
146  }
147  // 2. Handle non_unit strides
148  else {
149  // Fractionally-strided convolution needs inner padding of the input
150  auto strided_input = impl::common::inner_pad(input, s1, s2);
151 
152  if (p1 == 0 && p2 == 0) {
153  // Non-unit strides, non-zero padding -> Fractionally-strided full convolution
154  detail::conv4_full_impl::apply(strided_input, kernel, conv);
155  } else {
156  // Non-unit strides, zero padding -> Fractionally-strided Valid convolution with the correct padding
157  detail::dyn_conv4_valid_back_impl::apply(strided_input, kernel, conv, 1, 1, k1 - p1 - 1, k2 - p2 - 1);
158  }
159  }
160  }
161  }
162  }
163 
168  template <typename L>
169  void assign_add_to(L&& lhs) const {
170  std_add_evaluate(*this, lhs);
171  }
172 
177  template <typename L>
178  void assign_sub_to(L&& lhs) const {
179  std_sub_evaluate(*this, lhs);
180  }
181 
186  template <typename L>
187  void assign_mul_to(L&& lhs) const {
188  std_mul_evaluate(*this, lhs);
189  }
190 
195  template <typename L>
196  void assign_div_to(L&& lhs) const {
197  std_div_evaluate(*this, lhs);
198  }
199 
204  template <typename L>
205  void assign_mod_to(L&& lhs) const {
206  std_mod_evaluate(*this, lhs);
207  }
208 
215  friend std::ostream& operator<<(std::ostream& os, const dyn_conv_4d_backward_expr& expr) {
216  return os << "conv4_backward(" << expr._a << ", " << expr._b << ")";
217  }
218 };
219 
224 template <typename A, typename B, bool Flipped>
225 struct etl_traits<etl::dyn_conv_4d_backward_expr<A, B, Flipped>> {
227  using left_expr_t = std::decay_t<A>;
228  using right_expr_t = std::decay_t<B>;
232 
233  static constexpr bool is_etl = true;
234  static constexpr bool is_transformer = false;
235  static constexpr bool is_view = false;
236  static constexpr bool is_magic_view = false;
237  static constexpr bool is_fast = false;
238  static constexpr bool is_linear = false;
239  static constexpr bool is_thread_safe = true;
240  static constexpr bool is_value = false;
241  static constexpr bool is_direct = true;
242  static constexpr bool is_generator = false;
243  static constexpr bool is_padded = false;
244  static constexpr bool is_aligned = true;
245  static constexpr bool is_temporary = true;
246  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
247  static constexpr order storage_order = left_traits::storage_order;
248 
254  template <vector_mode_t V>
255  static constexpr bool vectorizable = true;
256 
263  static size_t dim(const expr_t& e, size_t d) {
264  if (d == 0) {
265  return etl::dim(e._a, 0);
266  } else if (d == 1) {
267  return etl::dim(e._b, 1);
268  } else if (d == 2) {
269  return e.s1 * (etl::dim(e._a, 2) - 1) + etl::dim(e._b, 2) - 2 * e.p1;
270  } else {
271  return e.s2 * (etl::dim(e._a, 3) - 1) + etl::dim(e._b, 3) - 2 * e.p2;
272  }
273  }
274 
280  static size_t size(const expr_t& e) {
281  return etl::dim(e._a, 0) * etl::dim(e._b, 1) * (e.s1 * (etl::dim(e._a, 2) - 1) + etl::dim(e._b, 2) - 2 * e.p1)
282  * (e.s2 * (etl::dim(e._a, 3) - 1) + etl::dim(e._b, 3) - 2 * e.p2);
283  }
284 
289  static constexpr size_t dimensions() {
290  return 4;
291  }
292 
297  static constexpr int complexity() noexcept {
298  return -1;
299  }
300 };
301 
313 template <etl_expr A, etl_expr B>
315  return dyn_conv_4d_backward_expr<detail::build_type<A>, detail::build_type<B>, false>{a, b, s1, s2, p1, p2};
316 }
317 
331 template <etl_expr A, etl_expr B, etl_expr C>
332 auto conv_4d_backward(A&& a, B&& b, C&& c, size_t s1, size_t s2, size_t p1, size_t p2) {
333  c = conv_4d_backward(a, b, s1, s2, p1, p2);
334 
335  return c;
336 }
337 
349 template <etl_expr A, etl_expr B>
351  A&& a, B&& b, size_t s1, size_t s2, size_t p1, size_t p2) {
352  return dyn_conv_4d_backward_expr<detail::build_type<A>, detail::build_type<B>, true>{a, b, s1, s2, p1, p2};
353 }
354 
368 template <etl_expr A, etl_expr B, etl_expr C>
369 auto conv_4d_backward_flipped(A&& a, B&& b, C&& c, size_t s1, size_t s2, size_t p1, size_t p2) {
370  c = conv_4d_backward_flipped(a, b, s1, s2, p1, p2);
371 
372  return c;
373 }
374 
375 } //end of namespace etl
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: dyn_conv_4d_backward_expr.hpp:205
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: dyn_conv_4d_backward_expr.hpp:297
B _b
The sub expression reference.
Definition: base_temporary_expr.hpp:534
value_t< A > value_type
The value type of the expression.
Definition: dyn_conv_4d_backward_expr.hpp:231
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
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_4d.hpp:380
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: dyn_conv_4d_backward_expr.hpp:280
value_t< A > value_type
The type of value of the expression.
Definition: dyn_conv_4d_backward_expr.hpp:41
static void apply(const I &input, const K &kernel, C &&conv)
Apply the convolution.
Definition: conv_4d.hpp:408
order
Storage order of a matrix.
Definition: order.hpp:15
constexpr bool cuda_enabled
Indicates if CUDA is available.
Definition: config.hpp:94
friend std::ostream & operator<<(std::ostream &os, const dyn_conv_4d_backward_expr &expr)
Print a representation of the expression on the given stream.
Definition: dyn_conv_4d_backward_expr.hpp:215
Abstract base class for temporary binary expression.
Definition: base_temporary_expr.hpp:529
std::decay_t< A > left_expr_t
The left sub expression type.
Definition: dyn_conv_4d_backward_expr.hpp:227
conv_4d_backward_expr< detail::build_type< A >, detail::build_type< B >, S1, S2, P1, P2, false > conv_4d_backward(A &&a, B &&b)
Creates an expression representing the transposed 2D convolution of a and b.
Definition: conv_4d_backward_expr.hpp:343
Expression representing a batch of transposed 2D convolution of an batch of image with a set of kerne...
Definition: dyn_conv_4d_backward_expr.hpp:40
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:593
const size_t s2
The stride of the second dimension.
Definition: dyn_conv_4d_backward_expr.hpp:55
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: dyn_conv_4d_backward_expr.hpp:178
const size_t p2
The padding of the second dimension.
Definition: dyn_conv_4d_backward_expr.hpp:57
constexpr bool is_fast
Traits to test if the given ETL expresion type is fast (sizes known at compile-time) ...
Definition: traits.hpp:588
dyn_conv_4d_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_4d_backward_expr.hpp:63
Traits to get information about ETL types.
Definition: tmp.hpp:68
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_4d_backward_expr.hpp:73
Root namespace for the ETL library.
Definition: adapter.hpp:15
std::decay_t< B > right_expr_t
The right sub expression type.
Definition: dyn_conv_4d_backward_expr.hpp:228
conv_4d_backward_expr< detail::build_type< A >, detail::build_type< B >, S1, S2, P1, P2, true > conv_4d_backward_flipped(A &&a, B &&b)
Creates an expression representing the transposed 2D convolution of a and flipped b...
Definition: conv_4d_backward_expr.hpp:379
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: dyn_conv_4d_backward_expr.hpp:196
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: dyn_conv_4d_backward_expr.hpp:52
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 size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: dyn_conv_4d_backward_expr.hpp:263
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: dyn_conv_4d_backward_expr.hpp:187
static constexpr auto storage_order
The sub storage order.
Definition: dyn_conv_4d_backward_expr.hpp:46
const size_t s1
The stride of the first dimension.
Definition: dyn_conv_4d_backward_expr.hpp:54
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
static void apply(const I &input, const K &kernel, C &&conv)
Apply the convolution.
Definition: conv_4d.hpp:448
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: dyn_conv_4d_backward_expr.hpp:289
void std_mul_evaluate(Expr &&expr, Result &&result)
Compound multiply evaluation of the expr into result.
Definition: evaluator.hpp:1233
const size_t p1
The padding of the first dimension.
Definition: dyn_conv_4d_backward_expr.hpp:56
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
Selector for the convolution implementations.
decltype(auto) smart_forward_gpu(E &expr)
Smart forwarding for a temporary expression that will be computed in GPU.
Definition: helpers.hpp:343
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: dyn_conv_4d_backward_expr.hpp:87
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
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_4d.hpp:352
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
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: dyn_conv_4d_backward_expr.hpp:169
std::add_lvalue_reference_t< A > a()
Returns the sub expression.
Definition: base_temporary_expr.hpp:577
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195