Expression Templates Library (ETL)
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_1d A, etl_2d B>
19 struct embedding_lookup_expr : base_temporary_expr_bin<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 embedding_lookup_expr(A a, B b) : base_type(a, b) {
38  //Nothing else to init
39  }
40 
46  template <etl_2d 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 embedding_lookup");
50  static_assert(etl::dim<1, B>() == etl::dim<1, C>(), "Invalid dimensions for embedding_lookup");
51  } else {
52  cpp_assert(etl::dim<0>(a) == etl::dim<0>(c), "Invalid dimensions for embedding_lookup");
53  cpp_assert(etl::dim<1>(b) == etl::dim<1>(c), "Invalid dimensions for embedding_lookup");
54  }
55  }
56 
57  // Assignment functions
58 
63  template <etl_expr L>
64  void assign_to(L&& lhs) const {
65  inc_counter("temp:assign");
66 
67  auto& a = this->a();
68  auto& b = this->b();
69 
70  check(a, b, lhs);
71 
72  const auto I = etl::dim<0>(a);
73 
74  standard_evaluator::pre_assign_rhs(a);
75  standard_evaluator::pre_assign_rhs(b);
76 
77  for (size_t i = 0; i < I; ++i) {
78  lhs(i) = b(a(i));
79  }
80  }
81 
86  template <etl_expr L>
87  void assign_add_to(L&& lhs) const {
88  auto& a = this->a();
89  auto& b = this->b();
90 
91  check(a, b, lhs);
92 
93  const auto I = etl::dim<0>(a);
94 
95  standard_evaluator::pre_assign_rhs(a);
96  standard_evaluator::pre_assign_rhs(b);
97 
98  for (size_t i = 0; i < I; ++i) {
99  lhs(i) += b(a(i));
100  }
101  }
102 
107  template <etl_expr L>
108  void assign_sub_to(L&& lhs) const {
109  auto& a = this->a();
110  auto& b = this->b();
111 
112  check(a, b, lhs);
113 
114  const auto I = etl::dim<0>(a);
115 
116  standard_evaluator::pre_assign_rhs(a);
117  standard_evaluator::pre_assign_rhs(b);
118 
119  for (size_t i = 0; i < I; ++i) {
120  lhs(i) -= b(a(i));
121  }
122  }
123 
128  template <etl_expr L>
129  void assign_mul_to(L&& lhs) const {
130  auto& a = this->a();
131  auto& b = this->b();
132 
133  check(a, b, lhs);
134 
135  const auto I = etl::dim<0>(a);
136 
137  standard_evaluator::pre_assign_rhs(a);
138  standard_evaluator::pre_assign_rhs(b);
139 
140  for (size_t i = 0; i < I; ++i) {
141  lhs(i) *= b(a(i));
142  }
143  }
144 
149  template <etl_expr L>
150  void assign_div_to(L&& lhs) const {
151  auto& a = this->a();
152  auto& b = this->b();
153 
154  check(a, b, lhs);
155 
156  const auto I = etl::dim<0>(a);
157 
158  standard_evaluator::pre_assign_rhs(a);
159  standard_evaluator::pre_assign_rhs(b);
160 
161  for (size_t i = 0; i < I; ++i) {
162  lhs(i) /= b(a(i));
163  }
164  }
165 
170  template <etl_expr L>
171  void assign_mod_to(L&& lhs) const {
172  auto& a = this->a();
173  auto& b = this->b();
174 
175  check(a, b, lhs);
176 
177  const auto I = etl::dim<0>(a);
178 
179  standard_evaluator::pre_assign_rhs(a);
180  standard_evaluator::pre_assign_rhs(b);
181 
182  for (size_t i = 0; i < I; ++i) {
183  lhs(i) %= b(a(i));
184  }
185  }
186 
193  friend std::ostream& operator<<(std::ostream& os, const embedding_lookup_expr& expr) {
194  return os << "embedding_lookup(" << expr._a << ", " << expr._b << ")";
195  }
196 };
197 
202 template <typename A, typename B>
205  using sub_expr_t = std::decay_t<A>;
208 
209  static constexpr bool is_etl = true;
210  static constexpr bool is_transformer = false;
211  static constexpr bool is_view = false;
212  static constexpr bool is_magic_view = false;
213  static constexpr bool is_fast = sub_traits::is_fast;
214  static constexpr bool is_linear = false;
215  static constexpr bool is_thread_safe = true;
216  static constexpr bool is_value = false;
217  static constexpr bool is_direct = true;
218  static constexpr bool is_generator = false;
219  static constexpr bool is_padded = false;
220  static constexpr bool is_aligned = true;
221  static constexpr bool is_temporary = true;
222  static constexpr order storage_order = sub_traits::storage_order;
223  static constexpr bool gpu_computable = false;
224 
230  template <vector_mode_t V>
231  static constexpr bool vectorizable = true;
232 
237  template <size_t DD>
238  static constexpr size_t dim() requires(DD < 2) {
239  return DD == 0 ? decay_traits<A>::template dim<0>() : decay_traits<B>::template dim<1>();
240  }
241 
248  static size_t dim(const expr_t& e, [[maybe_unused]] size_t d) {
249  cpp_assert(d < 2, "Invalid dimensions access");
250 
251  return d == 0 ? etl::dim<0>(e._a) : etl::dim<1>(e._b);
252  }
253 
259  static size_t size(const expr_t& e) {
260  return etl::dim<0>(e._a) * etl::dim<1>(e._b);
261  }
262 
267  static constexpr size_t size() {
268  return decay_traits<A>::template dim<0>() * decay_traits<B>::template dim<1>();
269  }
270 
275  static constexpr size_t dimensions() {
276  return 2;
277  }
278 
283  static constexpr int complexity() noexcept {
284  return -1;
285  }
286 };
287 
294 template <etl_1d I, etl_2d V>
297 }
298 
299 } //end of namespace etl
void assign_to(L &&lhs) const
Assign to a matrix of the same storage order.
Definition: embedding_lookup_expr.hpp:64
static size_t dim(const expr_t &e, [[maybe_unused]] size_t d)
Returns the dth dimension of the expression.
Definition: embedding_lookup_expr.hpp:248
B _b
The sub expression reference.
Definition: base_temporary_expr.hpp:534
embedding_lookup_expr< detail::build_type< I >, detail::build_type< V > > embedding_lookup(const I &value, const V &vocab)
Returns the embeddings for the given sequence.
Definition: embedding_lookup_expr.hpp:295
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: embedding_lookup_expr.hpp:87
static void check([[maybe_unused]] const A &a, [[maybe_unused]] const B &b, [[maybe_unused]] const C &c)
Validate the transposition dimensions.
Definition: embedding_lookup_expr.hpp:47
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: embedding_lookup_expr.hpp:283
A _a
The sub expression reference.
Definition: base_temporary_expr.hpp:533
A transposition expression.
Definition: embedding_lookup_expr.hpp:19
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: embedding_lookup_expr.hpp:129
order
Storage order of a matrix.
Definition: order.hpp:15
Abstract base class for temporary binary expression.
Definition: base_temporary_expr.hpp:529
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: embedding_lookup_expr.hpp:108
embedding_lookup_expr(A a, B b)
Construct a new expression.
Definition: embedding_lookup_expr.hpp:37
std::add_lvalue_reference_t< B > b()
Returns the sub expression.
Definition: base_temporary_expr.hpp:593
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
value_t< A > value_type
The value type of the expression.
Definition: embedding_lookup_expr.hpp:207
static constexpr size_t dim() requires(DD< 2)
Returns the DDth dimension of the expression.
Definition: embedding_lookup_expr.hpp:238
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 size_t dimensions()
Returns the number of dimensions of the expression.
Definition: embedding_lookup_expr.hpp:275
value_t< A > value_type
The type of value of the expression.
Definition: embedding_lookup_expr.hpp:20
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: embedding_lookup_expr.hpp:259
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
std::decay_t< A > sub_expr_t
The sub expression type.
Definition: embedding_lookup_expr.hpp:205
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 is_fast
Indicates if T is a fast structure.
Definition: traits_base.hpp:25
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: embedding_lookup_expr.hpp:150
static constexpr size_t size()
Returns the size of the expression.
Definition: embedding_lookup_expr.hpp:267
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: embedding_lookup_expr.hpp:171
static constexpr auto storage_order
The sub storage order.
Definition: embedding_lookup_expr.hpp:25
friend std::ostream & operator<<(std::ostream &os, const embedding_lookup_expr &expr)
Print a representation of the expression on the given stream.
Definition: embedding_lookup_expr.hpp:193
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
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: embedding_lookup_expr.hpp:31