Expression Templates Library (ETL)
batch_embedding_lookup_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_2d B>
19 struct batch_embedding_lookup_expr : base_temporary_expr_bin<batch_embedding_lookup_expr<A, B>, A, B> {
24 
25  static constexpr auto storage_order = sub_traits::storage_order;
26 
31  static constexpr bool gpu_computable = false;
32 
37  explicit batch_embedding_lookup_expr(A a, B b) : base_type(a, b) {
38  //Nothing else to init
39  }
40 
46  template <etl_3d C>
47  static void check([[maybe_unused]] const A& a, [[maybe_unused]] const B& b, [[maybe_unused]] const C& c) {
48  if constexpr (all_fast<A, B, C>) {
49  static_assert(etl::dim<0, A>() == etl::dim<0, C>(), "Invalid dimensions for batch_embedding_lookup");
50  static_assert(etl::dim<1, A>() == etl::dim<1, C>(), "Invalid dimensions for batch_embedding_lookup");
51  static_assert(etl::dim<1, B>() == etl::dim<2, C>(), "Invalid dimensions for batch_embedding_lookup");
52  } else {
53  cpp_assert(etl::dim<0>(a) == etl::dim<0>(c), "Invalid dimensions for batch_embedding_lookup");
54  cpp_assert(etl::dim<1>(a) == etl::dim<1>(c), "Invalid dimensions for batch_embedding_lookup");
55  cpp_assert(etl::dim<1>(b) == etl::dim<2>(c), "Invalid dimensions for batch_embedding_lookup");
56  }
57  }
58 
59  // Assignment functions
60 
65  template <etl_3d 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 
72  check(a, b, lhs);
73 
74  standard_evaluator::pre_assign_rhs(a);
75  standard_evaluator::pre_assign_rhs(b);
76 
77  const auto BB = etl::dim<0>(a);
78  const auto I = etl::dim<1>(a);
79 
80  for (size_t bb = 0; bb < BB; ++bb) {
81  for (size_t i = 0; i < I; ++i) {
82  lhs(bb)(i) = b(a(bb, i));
83  }
84  }
85  }
86 
91  template <etl_3d L>
92  void assign_add_to(L&& lhs) const {
93  auto& a = this->a();
94  auto& b = this->b();
95 
96  check(a, b, lhs);
97 
98  standard_evaluator::pre_assign_rhs(a);
99  standard_evaluator::pre_assign_rhs(b);
100 
101  const auto BB = etl::dim<0>(a);
102  const auto I = etl::dim<1>(a);
103 
104  for (size_t bb = 0; bb < BB; ++bb) {
105  for (size_t i = 0; i < I; ++i) {
106  lhs(bb)(i) += b(a(bb, i));
107  }
108  }
109  }
110 
115  template <etl_3d L>
116  void assign_sub_to(L&& lhs) const {
117  auto& a = this->a();
118  auto& b = this->b();
119 
120  check(a, b, lhs);
121 
122  standard_evaluator::pre_assign_rhs(a);
123  standard_evaluator::pre_assign_rhs(b);
124 
125  const auto BB = etl::dim<0>(a);
126  const auto I = etl::dim<1>(a);
127 
128  for (size_t bb = 0; bb < BB; ++bb) {
129  for (size_t i = 0; i < I; ++i) {
130  lhs(bb)(i) -= b(a(bb, i));
131  }
132  }
133  }
134 
139  template <etl_3d L>
140  void assign_mul_to(L&& lhs) const {
141  auto& a = this->a();
142  auto& b = this->b();
143 
144  check(a, b, lhs);
145 
146  standard_evaluator::pre_assign_rhs(a);
147  standard_evaluator::pre_assign_rhs(b);
148 
149  const auto BB = etl::dim<0>(a);
150  const auto I = etl::dim<1>(a);
151 
152  for (size_t bb = 0; bb < BB; ++bb) {
153  for (size_t i = 0; i < I; ++i) {
154  lhs(bb)(i) *= b(a(bb, i));
155  }
156  }
157  }
158 
163  template <etl_3d L>
164  void assign_div_to(L&& lhs) const {
165  auto& a = this->a();
166  auto& b = this->b();
167 
168  check(a, b, lhs);
169 
170  standard_evaluator::pre_assign_rhs(a);
171  standard_evaluator::pre_assign_rhs(b);
172 
173  const auto BB = etl::dim<0>(a);
174  const auto I = etl::dim<1>(a);
175 
176  for (size_t bb = 0; bb < BB; ++bb) {
177  for (size_t i = 0; i < I; ++i) {
178  lhs(bb)(i) /= bb(a(b, i));
179  }
180  }
181  }
182 
187  template <etl_3d L>
188  void assign_mod_to(L&& lhs) const {
189  auto& a = this->a();
190  auto& b = this->b();
191 
192  check(a, b, lhs);
193 
194  standard_evaluator::pre_assign_rhs(a);
195  standard_evaluator::pre_assign_rhs(b);
196 
197  const auto BB = etl::dim<0>(a);
198  const auto I = etl::dim<1>(a);
199 
200  for (size_t bb = 0; bb < BB; ++bb) {
201  for (size_t i = 0; i < I; ++i) {
202  lhs(bb)(i) %= b(a(bb, i));
203  }
204  }
205  }
206 
213  friend std::ostream& operator<<(std::ostream& os, const batch_embedding_lookup_expr& expr) {
214  return os << "embedding_lookup(" << expr._a << ", " << expr._b << ")";
215  }
216 };
217 
222 template <etl_expr A, etl_expr B>
225  using sub_expr_t = std::decay_t<A>;
228 
229  static constexpr bool is_etl = true;
230  static constexpr bool is_transformer = false;
231  static constexpr bool is_view = false;
232  static constexpr bool is_magic_view = false;
233  static constexpr bool is_fast = sub_traits::is_fast;
234  static constexpr bool is_linear = false;
235  static constexpr bool is_thread_safe = true;
236  static constexpr bool is_value = false;
237  static constexpr bool is_direct = true;
238  static constexpr bool is_generator = false;
239  static constexpr bool is_padded = false;
240  static constexpr bool is_aligned = true;
241  static constexpr bool is_temporary = true;
242  static constexpr order storage_order = sub_traits::storage_order;
243  static constexpr bool gpu_computable = false;
244 
250  template <vector_mode_t V>
251  static constexpr bool vectorizable = true;
252 
257  template <size_t DD>
258  static constexpr size_t dim() requires(DD < 3) {
259  if (DD == 0) {
260  return decay_traits<A>::template dim<0>();
261  } else if (DD == 1) {
262  return decay_traits<A>::template dim<1>();
263  } else {
264  return decay_traits<B>::template dim<1>();
265  }
266  }
267 
274  static size_t dim(const expr_t& e, [[maybe_unused]] size_t d) {
275  cpp_assert(d < 3, "Invalid dimensions access");
276 
277  if (d == 0) {
278  return etl::dim<0>(e._a);
279  } else if (d == 1) {
280  return etl::dim<1>(e._a);
281  } else {
282  return etl::dim<1>(e._b);
283  }
284  }
285 
291  static size_t size(const expr_t& e) {
292  return etl::dim<0>(e._a) * etl::dim<1>(e._a) * etl::dim<1>(e._b);
293  }
294 
299  static constexpr size_t size() {
301  }
302 
307  static constexpr size_t dimensions() {
308  return 3;
309  }
310 
315  static constexpr int complexity() noexcept {
316  return 4;
317  }
318 };
319 
326 template <etl_2d I, etl_2d V>
329 }
330 
331 } //end of namespace etl
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: batch_embedding_lookup_expr.hpp:164
std::decay_t< A > sub_expr_t
The sub expression type.
Definition: batch_embedding_lookup_expr.hpp:225
A transposition expression.
Definition: batch_embedding_lookup_expr.hpp:19
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: batch_embedding_lookup_expr.hpp:291
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: batch_embedding_lookup_expr.hpp:31
B _b
The sub expression reference.
Definition: base_temporary_expr.hpp:534
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: batch_embedding_lookup_expr.hpp:116
batch_embedding_lookup_expr(A a, B b)
Construct a new expression.
Definition: batch_embedding_lookup_expr.hpp:37
void assign_to(L &&lhs) const
Assign to a matrix of the same storage order.
Definition: batch_embedding_lookup_expr.hpp:66
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
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: batch_embedding_lookup_expr.hpp:188
order
Storage order of a matrix.
Definition: order.hpp:15
static constexpr size_t dim() requires(DD< 3)
Returns the DDth dimension of the expression.
Definition: batch_embedding_lookup_expr.hpp:258
Abstract base class for temporary binary expression.
Definition: base_temporary_expr.hpp:529
static constexpr auto storage_order
The sub storage order.
Definition: batch_embedding_lookup_expr.hpp:25
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:593
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: batch_embedding_lookup_expr.hpp:307
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
static void check([[maybe_unused]] const A &a, [[maybe_unused]] const B &b, [[maybe_unused]] const C &c)
Validate the transposition dimensions.
Definition: batch_embedding_lookup_expr.hpp:47
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
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: batch_embedding_lookup_expr.hpp:140
batch_embedding_lookup_expr< detail::build_type< I >, detail::build_type< V > > batch_embedding_lookup(const I &value, const V &vocab)
Returns the embeddings for the given sequence.
Definition: batch_embedding_lookup_expr.hpp:327
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
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
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: batch_embedding_lookup_expr.hpp:92
static constexpr bool is_fast
Indicates if T is a fast structure.
Definition: traits_base.hpp:25
value_t< A > value_type
The value type of the expression.
Definition: batch_embedding_lookup_expr.hpp:227
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: batch_embedding_lookup_expr.hpp:315
static constexpr size_t size()
Returns the size of the expression.
Definition: batch_embedding_lookup_expr.hpp:299
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
value_t< A > value_type
The type of value of the expression.
Definition: batch_embedding_lookup_expr.hpp:20
friend std::ostream & operator<<(std::ostream &os, const batch_embedding_lookup_expr &expr)
Print a representation of the expression on the given stream.
Definition: batch_embedding_lookup_expr.hpp:213
static size_t dim(const expr_t &e, [[maybe_unused]] size_t d)
Returns the dth dimension of the expression.
Definition: batch_embedding_lookup_expr.hpp:274
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
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