Expression Templates Library (ETL)
batch_embedding_gradients_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 namespace etl {
13 
18 template <etl_2d A, etl_3d B, typename C>
19 struct batch_embedding_gradients_expr : base_temporary_expr_tern<batch_embedding_gradients_expr<A, B, C>, A, B, C> {
24 
25  static constexpr auto storage_order = sub_traits::storage_order;
26 
31  static constexpr bool gpu_computable = false;
32 
37  explicit batch_embedding_gradients_expr(A a, B b, C c) : base_type(a, b, c) {
38  //Nothing else to init
39  }
40 
46  template <etl_2d L>
47  static void check([[maybe_unused]] const A& a, [[maybe_unused]] const B& b, [[maybe_unused]] const C& c, [[maybe_unused]] const L& lhs) {
48  if constexpr (all_fast<A, B, C, L>) {
49  static_assert(etl::dim<0, A>() == etl::dim<0, B>(), "Invalid dimensions for batch_embedding_gradients");
50  static_assert(etl::dim<1, A>() == etl::dim<1, B>(), "Invalid dimensions for batch_embedding_gradients");
51  static_assert(etl::dim<2, B>() == etl::dim<1, L>(), "Invalid dimensions for batch_embedding_gradients");
52  } else {
53  cpp_assert(etl::dim<0>(a) == etl::dim<0>(b), "Invalid dimensions for batch_embedding_gradients");
54  cpp_assert(etl::dim<1>(a) == etl::dim<1>(b), "Invalid dimensions for batch_embedding_gradients");
55  cpp_assert(etl::dim<2>(b) == etl::dim<1>(lhs), "Invalid dimensions for batch_embedding_gradients");
56  }
57  }
58 
59  // Assignment functions
60 
65  template <etl_expr L>
66  void assign_to(L&& lhs) const {
67  inc_counter("temp:assign");
68 
69  auto& a = this->a();
70  auto& b = this->b();
71  auto& c = this->c();
72 
73  check(a, b, c, lhs);
74 
75  const auto BB = etl::dim<0>(a);
76  const auto I = etl::dim<1>(a);
77 
78  standard_evaluator::pre_assign_rhs(a);
79  standard_evaluator::pre_assign_rhs(b);
80 
81  lhs = 0;
82 
83  for (size_t bb = 0; bb < BB; ++bb) {
84  for (size_t i = 0; i < I; ++i) {
85  lhs(a(bb, i)) += b(bb)(i);
86  }
87  }
88  }
89 
94  template <etl_expr L>
95  void assign_add_to(L&& lhs) const {
96  std_add_evaluate(*this, lhs);
97  }
98 
103  template <etl_expr L>
104  void assign_sub_to(L&& lhs) const {
105  std_sub_evaluate(*this, lhs);
106  }
107 
112  template <etl_expr L>
113  void assign_mul_to(L&& lhs) const {
114  std_mul_evaluate(*this, lhs);
115  }
116 
121  template <etl_expr L>
122  void assign_div_to(L&& lhs) const {
123  std_div_evaluate(*this, lhs);
124  }
125 
130  template <etl_expr L>
131  void assign_mod_to(L&& lhs) const {
132  std_mod_evaluate(*this, lhs);
133  }
134 
141  friend std::ostream& operator<<(std::ostream& os, const batch_embedding_gradients_expr& expr) {
142  return os << "embedding_gradients(" << expr._a << ", " << expr._b << ")";
143  }
144 };
145 
150 template <typename A, typename B, typename C>
153  using sub_expr_t = std::decay_t<A>;
156 
157  static constexpr bool is_etl = true;
158  static constexpr bool is_transformer = false;
159  static constexpr bool is_view = false;
160  static constexpr bool is_magic_view = false;
161  static constexpr bool is_fast = sub_traits::is_fast;
162  static constexpr bool is_linear = false;
163  static constexpr bool is_thread_safe = true;
164  static constexpr bool is_value = false;
165  static constexpr bool is_direct = true;
166  static constexpr bool is_generator = false;
167  static constexpr bool is_padded = false;
168  static constexpr bool is_aligned = true;
169  static constexpr bool is_temporary = true;
170  static constexpr order storage_order = sub_traits::storage_order;
171  static constexpr bool gpu_computable = false;
172 
178  template <vector_mode_t V>
179  static constexpr bool vectorizable = true;
180 
185  template <size_t DD>
186  static constexpr size_t dim() requires(DD < 2) {
187  return DD == 0 ? decay_traits<C>::template dim<0>() : decay_traits<B>::template dim<2>();
188  }
189 
196  static size_t dim(const expr_t& e, [[maybe_unused]] size_t d) {
197  cpp_assert(d < 2, "Invalid dimensions access");
198 
199  return d == 0 ? etl::dim<0>(e._c) : etl::dim<2>(e._b);
200  }
201 
207  static size_t size(const expr_t& e) {
208  return etl::dim<0>(e._c) * etl::dim<2>(e._b);
209  }
210 
215  static constexpr size_t size() {
216  return decay_traits<C>::template dim<0>() * decay_traits<B>::template dim<2>();
217  }
218 
223  static constexpr size_t dimensions() {
224  return 2;
225  }
226 
231  static constexpr int complexity() noexcept {
232  return -1;
233  }
234 };
235 
242 template <etl_2d I, etl_3d E, etl_expr W>
244  const E& errors,
245  const W& vocab) {
246  return batch_embedding_gradients_expr<detail::build_type<I>, detail::build_type<E>, detail::build_type<W>>{value, errors, vocab};
247 }
248 
249 } //end of namespace etl
static size_t dim(const expr_t &e, [[maybe_unused]] size_t d)
Returns the dth dimension of the expression.
Definition: batch_embedding_gradients_expr.hpp:196
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:702
friend std::ostream & operator<<(std::ostream &os, const batch_embedding_gradients_expr &expr)
Print a representation of the expression on the given stream.
Definition: batch_embedding_gradients_expr.hpp:141
batch_embedding_gradients_expr(A a, B b, C c)
Construct a new expression.
Definition: batch_embedding_gradients_expr.hpp:37
value_t< A > value_type
The value type of the expression.
Definition: batch_embedding_gradients_expr.hpp:155
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
static void check([[maybe_unused]] const A &a, [[maybe_unused]] const B &b, [[maybe_unused]] const C &c, [[maybe_unused]] const L &lhs)
Validate the transposition dimensions.
Definition: batch_embedding_gradients_expr.hpp:47
order
Storage order of a matrix.
Definition: order.hpp:15
A _a
The first sub expression reference.
Definition: base_temporary_expr.hpp:638
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: batch_embedding_gradients_expr.hpp:131
static constexpr auto storage_order
The sub storage order.
Definition: batch_embedding_gradients_expr.hpp:25
A transposition expression.
Definition: batch_embedding_gradients_expr.hpp:19
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: batch_embedding_gradients_expr.hpp:104
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
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: batch_embedding_gradients_expr.hpp:95
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
batch_embedding_gradients_expr< detail::build_type< I >, detail::build_type< E >, detail::build_type< W > > batch_embedding_gradients(const I &value, const E &errors, const W &vocab)
Returns the embeddings for the given sequence.
Definition: batch_embedding_gradients_expr.hpp:243
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 constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: batch_embedding_gradients_expr.hpp:231
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
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
value_t< A > value_type
The type of value of the expression.
Definition: batch_embedding_gradients_expr.hpp:20
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: batch_embedding_gradients_expr.hpp:223
requires(D > 0) struct dyn_base
Matrix with run-time fixed dimensions.
Definition: dyn_base.hpp:113
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: batch_embedding_gradients_expr.hpp:31
static constexpr bool is_fast
Indicates if T is a fast structure.
Definition: traits_base.hpp:25
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: batch_embedding_gradients_expr.hpp:113
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: batch_embedding_gradients_expr.hpp:207
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: batch_embedding_gradients_expr.hpp:122
static constexpr size_t dim() requires(DD< 2)
Returns the DDth dimension of the expression.
Definition: batch_embedding_gradients_expr.hpp:186
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
C _c
The third sub expression reference.
Definition: base_temporary_expr.hpp:640
void assign_to(L &&lhs) const
Assign to a matrix of the same storage order.
Definition: batch_embedding_gradients_expr.hpp:66
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 size_t size()
Returns the size of the expression.
Definition: batch_embedding_gradients_expr.hpp:215
std::add_lvalue_reference_t< C > c()
Returns the sub expression.
Definition: base_temporary_expr.hpp:718
std::decay_t< A > sub_expr_t
The sub expression type.
Definition: batch_embedding_gradients_expr.hpp:153
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195