Expression Templates Library (ETL)
batch_softmax_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 
19 template <typename A, bool Stable>
20 struct batch_softmax_expr : base_temporary_expr_un<batch_softmax_expr<A, Stable>, A> {
25 
26  static constexpr auto storage_order = sub_traits::storage_order;
27 
32  explicit batch_softmax_expr(A a) : base_type(a) {
33  //Nothing else to init
34  }
35 
41  template <same_dimensions_and_order<A> C>
42  static void check([[maybe_unused]] const A& a, [[maybe_unused]] const C& c) {
43  if constexpr (all_fast<A, C>) {
44  static_assert(decay_traits<A>::size() == decay_traits<C>::size(), "Invalid size");
45  } else {
46  cpp_assert(etl::size(a) == etl::size(c), "Invalid size");
47  }
48  }
49 
50  // Assignment functions
51 
57  template <typename C>
58  constexpr static batch_softmax_impl select_default_impl(bool no_gpu) {
59  if (cudnn_enabled && all_homogeneous<A, C> && all_floating<A, C> && !no_gpu) {
61  }
62 
64  }
65 
66 #ifdef ETL_MANUAL_SELECT
67 
71  template <typename C>
73  return select_default_impl<C>(local_context().cpu);
74  }
75 
76 #else
77 
81  template <typename C>
82  constexpr static batch_softmax_impl select_impl() {
83  return select_default_impl<C>(false);
84  }
85 
86 #endif
87 
92  template <etl_expr C>
93  void assign_to(C&& c) const {
94  if constexpr (decay_traits<C>::storage_order == storage_order) {
95  inc_counter("temp:assign");
96 
97  auto& a = this->a();
98 
99  standard_evaluator::pre_assign_rhs(a);
100 
101  check(a, c);
102 
103  constexpr_select auto impl = select_impl<C>();
104 
105  if constexpr_select (impl == batch_softmax_impl::CUDNN) {
106  inc_counter("impl:cudnn");
107 
108  decltype(auto) a_gpu = smart_forward_gpu(a);
109 
110  if constexpr (Stable) {
111  impl::cudnn::stable_softmax(a_gpu, c);
112  } else {
113  impl::cudnn::softmax(a_gpu, c);
114  }
115  } else if constexpr_select (impl == batch_softmax_impl::STD) {
116  inc_counter("impl:std");
117 
118  if constexpr (Stable) {
119  for (size_t i = 0; i < etl::dim<0>(c); ++i) {
120  auto m = max(a(i));
121  c(i) = exp(a(i) - m) / sum(exp(a(i) - m));
122  }
123  } else {
124  for (size_t i = 0; i < etl::dim<0>(c); ++i) {
125  c(i) = exp(a(i)) / sum(exp(a(i)));
126  }
127  }
128  } else {
129  cpp_unreachable("Invalid selection for batch_softmax");
130  }
131  } else {
132  std_assign_evaluate(*this, c);
133  }
134  }
135 
140  template <typename L>
141  void assign_add_to(L&& lhs) const {
142  std_add_evaluate(*this, std::forward<L>(lhs));
143  }
144 
149  template <typename L>
150  void assign_sub_to(L&& lhs) const {
151  std_sub_evaluate(*this, std::forward<L>(lhs));
152  }
153 
158  template <typename L>
159  void assign_mul_to(L&& lhs) const {
160  std_mul_evaluate(*this, std::forward<L>(lhs));
161  }
162 
167  template <typename L>
168  void assign_div_to(L&& lhs) const {
169  std_div_evaluate(*this, std::forward<L>(lhs));
170  }
171 
176  template <typename L>
177  void assign_mod_to(L&& lhs) const {
178  std_mod_evaluate(*this, std::forward<L>(lhs));
179  }
180 
187  friend std::ostream& operator<<(std::ostream& os, const batch_softmax_expr& expr) {
188  return os << "batch_softmax(" << expr._a << ")";
189  }
190 };
191 
196 template <typename A, bool Stable>
197 struct etl_traits<etl::batch_softmax_expr<A, Stable>> {
199  using sub_expr_t = std::decay_t<A>;
202 
203  static constexpr bool is_etl = true;
204  static constexpr bool is_transformer = false;
205  static constexpr bool is_view = false;
206  static constexpr bool is_magic_view = false;
207  static constexpr bool is_fast = sub_traits::is_fast;
208  static constexpr bool is_linear = true;
209  static constexpr bool is_thread_safe = true;
210  static constexpr bool is_value = false;
211  static constexpr bool is_direct = true;
212  static constexpr bool is_generator = false;
213  static constexpr bool is_padded = false;
214  static constexpr bool is_aligned = true;
215  static constexpr bool is_temporary = true;
216  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
217  static constexpr order storage_order = sub_traits::storage_order;
218 
224  template <vector_mode_t V>
225  using vectorizable = std::true_type;
226 
231  template <size_t DD>
232  static constexpr size_t dim() {
233  return decay_traits<A>::template dim<DD>();
234  }
235 
242  static size_t dim(const expr_t& e, size_t d) {
243  return etl::dim(e._a, d);
244  }
245 
251  static size_t size(const expr_t& e) {
252  return etl::size(e._a);
253  }
254 
259  static constexpr size_t size() {
260  return decay_traits<A>::size();
261  }
262 
267  static constexpr size_t dimensions() {
269  }
270 
275  static constexpr int complexity() noexcept {
276  return -1;
277  }
278 };
279 
280 } //end of namespace etl
value_t< A > value_type
The value type of the expression.
Definition: batch_softmax_expr.hpp:201
static constexpr batch_softmax_impl select_impl()
Select the best possible implementation for the batch softmax operation.
Definition: batch_softmax_expr.hpp:82
batch_softmax_impl
Enumeration describing the different implementations of CCE.
Definition: batch_softmax_impl.hpp:20
std::add_lvalue_reference_t< A > a()
Returns the sub expression.
Definition: base_temporary_expr.hpp:489
auto max(L &&lhs, R &&rhs)
Create an expression with the max value of lhs or rhs.
Definition: expression_builder.hpp:65
void std_assign_evaluate(Expr &&expr, Result &&result)
Evaluation of the expr into result.
Definition: evaluator.hpp:1176
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: batch_softmax_expr.hpp:159
Standard implementation.
static size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: batch_softmax_expr.hpp:242
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
auto softmax(E &&e)
Return the softmax function of the given ETL expression.
Definition: function_expression_builder.hpp:253
static constexpr auto storage_order
The sub storage order.
Definition: batch_softmax_expr.hpp:26
static constexpr batch_softmax_impl select_default_impl(bool no_gpu)
Select the best possible implementation for the batch softmax operation.
Definition: batch_softmax_expr.hpp:58
order
Storage order of a matrix.
Definition: order.hpp:15
static void check([[maybe_unused]] const A &a, [[maybe_unused]] const C &c)
Validate the function dimensions.
Definition: batch_softmax_expr.hpp:42
constexpr bool cuda_enabled
Indicates if CUDA is available.
Definition: config.hpp:94
static constexpr size_t dim()
Returns the DDth dimension of the expression.
Definition: batch_softmax_expr.hpp:232
static constexpr size_t size()
Returns the size of the expression.
Definition: batch_softmax_expr.hpp:259
A _a
The sub expression reference.
Definition: base_temporary_expr.hpp:447
constexpr bool is_fast
Traits to test if the given ETL expresion type is fast (sizes known at compile-time) ...
Definition: traits.hpp:588
std::true_type vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: batch_softmax_expr.hpp:225
Traits to get information about ETL types.
Definition: tmp.hpp:68
Root namespace for the ETL library.
Definition: adapter.hpp:15
context & local_context()
Return the configuration context of the current thread.
Definition: context.hpp:50
static constexpr size_t dimensions()
Return the number of dimensions of the expression.
Definition: traits_base.hpp:31
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
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: batch_softmax_expr.hpp:267
constexpr bool cudnn_enabled
Indicates if the NVIDIA CUDNN library is available for ETL.
Definition: config.hpp:114
bool cpu
Force CPU evaluation.
Definition: context.hpp:29
GPU implementation.
friend std::ostream & operator<<(std::ostream &os, const batch_softmax_expr &expr)
Print a representation of the expression on the given stream.
Definition: batch_softmax_expr.hpp:187
value_t< A > value_type
The type of value of the expression.
Definition: batch_softmax_expr.hpp:21
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
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< E > sum(E &&values)
Returns the sum of all the values contained in the given expression.
Definition: expression_builder.hpp:624
decltype(auto) smart_forward_gpu(E &expr)
Smart forwarding for a temporary expression that will be computed in GPU.
Definition: helpers.hpp:343
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: batch_softmax_expr.hpp:177
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
auto exp(E &&value) -> detail::unary_helper< E, exp_unary_op >
Apply exponential on each value of the given expression.
Definition: function_expression_builder.hpp:154
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: batch_softmax_expr.hpp:275
static constexpr bool is_fast
Indicates if T is a fast structure.
Definition: traits_base.hpp:25
void assign_to(C &&c) const
Assign to a matrix of the same storage order.
Definition: batch_softmax_expr.hpp:93
auto stable_softmax(E &&e)
Returns the softmax function of the given ETL expression. This version is implemented so that numeric...
Definition: function_expression_builder.hpp:268
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: batch_softmax_expr.hpp:251
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
Abstract base class for temporary unary expression.
Definition: base_temporary_expr.hpp:443
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
std::decay_t< A > sub_expr_t
The sub expression type.
Definition: batch_softmax_expr.hpp:199
A batch softmax function expression.
Definition: batch_softmax_expr.hpp:20
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: batch_softmax_expr.hpp:168
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 std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: batch_softmax_expr.hpp:141
batch_softmax_expr(A a)
Construct a new expression.
Definition: batch_softmax_expr.hpp:32
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: batch_softmax_expr.hpp:150