Expression Templates Library (ETL)
state_inverted_dropout_mask.hpp
Go to the documentation of this file.
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 
13 #pragma once
14 
15 #include <chrono> //for std::time
16 
18 
19 namespace etl {
20 
24 template <typename T = double>
26  using value_type = T;
27 
28  const T probability;
29  std::shared_ptr<void*> states;
32 
36  static constexpr bool gpu_computable =
37  impl::egblas::has_dropout_prepare && impl::egblas::has_dropout_release
38  && ((is_single_precision_t<T> && impl::egblas::has_sdropout_states) || (is_double_precision_t<T> && impl::egblas::has_ddropout_states));
39 
43  state_inverted_dropout_mask_generator_op(T probability) : probability(probability), rand_engine(std::time(nullptr)), distribution(T(0), T(1)) {
44  if constexpr (impl::egblas::has_dropout_prepare) {
45  states = std::make_shared<void*>();
46  *states = impl::egblas::dropout_prepare();
47  }
48  }
49 
55  if (distribution(rand_engine) < probability) {
56  return T(0);
57  } else {
58  return T(1) / (T(1) - probability);
59  }
60  }
61 
69  template <typename Y>
70  auto gpu_compute_hint(Y& y) noexcept {
71  decltype(auto) t1 = force_temporary_gpu_dim_only(y);
72 
73  impl::egblas::inv_dropout_states(etl::size(y), probability, T(1), t1.gpu_memory(), 1, *states);
74 
75  return t1;
76  }
83  template <typename Y>
84  Y& gpu_compute(Y& y) noexcept {
85  impl::egblas::inv_dropout_states(etl::size(y), probability, T(1), y.gpu_memory(), 1, *states);
86 
87  y.validate_gpu();
88  y.invalidate_cpu();
89 
90  return y;
91  }
92 
99  friend std::ostream& operator<<(std::ostream& os, const state_inverted_dropout_mask_generator_op& s) {
100  return os << "dropout(p=" << s.probability << ")";
101  }
102 };
103 
107 template <typename G, typename T = double>
109  using value_type = T;
110 
111  const T probability;
113  std::shared_ptr<void*> states;
115 
119  static constexpr bool gpu_computable =
120  impl::egblas::has_dropout_prepare_seed && impl::egblas::has_dropout_release
121  && ((is_single_precision_t<T> && impl::egblas::has_sdropout_states) || (is_double_precision_t<T> && impl::egblas::has_ddropout_states));
122 
128  state_inverted_dropout_mask_generator_g_op(G& g, T probability) : probability(probability), rand_engine(g), distribution(T(0), T(1)) {
129  if (impl::egblas::has_dropout_prepare) {
130  states = std::make_shared<void*>();
131  std::uniform_int_distribution<long> seed_dist;
132  *states = impl::egblas::dropout_prepare_seed(seed_dist(rand_engine));
133  }
134  }
135 
141  if (distribution(rand_engine) < probability) {
142  return T(0);
143  } else {
144  return T(1) / (T(1) - probability);
145  }
146  }
147 
155  template <typename Y>
156  auto gpu_compute_hint(Y& y) noexcept {
157  decltype(auto) t1 = force_temporary_gpu_dim_only(y);
158 
159  impl::egblas::inv_dropout_states(etl::size(y), probability, T(1), t1.gpu_memory(), 1, *states);
160 
161  return t1;
162  }
169  template <typename Y>
170  Y& gpu_compute(Y& y) noexcept {
171  impl::egblas::inv_dropout_states(etl::size(y), probability, T(1), y.gpu_memory(), 1, *states);
172 
173  y.validate_gpu();
174  y.invalidate_cpu();
175 
176  return y;
177  }
178 
185  friend std::ostream& operator<<(std::ostream& os, const state_inverted_dropout_mask_generator_g_op& s) {
186  return os << "inverted_dropout(p=" << s.probability << ")";
187  }
188 };
189 
190 } //end of namespace etl
state_inverted_dropout_mask_generator_g_op(G &g, T probability)
Construct a new generator with the given start and end of the range.
Definition: state_inverted_dropout_mask.hpp:128
auto s(T &&value)
Force the evaluation of the given expression.
Definition: stop.hpp:18
G & rand_engine
The random engine.
Definition: state_inverted_dropout_mask.hpp:112
Generator from an uniform distribution.
Definition: state_inverted_dropout_mask.hpp:25
EGBLAS wrappers for the dropout operation.
value_type operator()()
Generate a new value.
Definition: state_inverted_dropout_mask.hpp:140
Generator from an uniform distribution using a custom random engine.
Definition: state_inverted_dropout_mask.hpp:108
Y & gpu_compute(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: state_inverted_dropout_mask.hpp:170
const T probability
The dropout probability.
Definition: state_inverted_dropout_mask.hpp:111
std::conditional_t< std::is_floating_point_v< T >, std::uniform_real_distribution< T >, std::uniform_int_distribution< T > > dropout_distribution
Selector helper to get an dropout_distribution based on the type (real or int)
Definition: dropout_mask.hpp:26
auto gpu_compute_hint(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: state_inverted_dropout_mask.hpp:70
random_engine rand_engine
The random engine.
Definition: state_inverted_dropout_mask.hpp:30
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: state_inverted_dropout_mask.hpp:36
Y & gpu_compute(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: state_inverted_dropout_mask.hpp:84
T value_type
The value type.
Definition: state_inverted_dropout_mask.hpp:26
value_type operator()()
Generate a new value.
Definition: state_inverted_dropout_mask.hpp:54
state_inverted_dropout_mask_generator_op(T probability)
Construct a new generator with the given start and end of the range.
Definition: state_inverted_dropout_mask.hpp:43
decltype(auto) force_temporary_gpu_dim_only(E &&expr)
Force a temporary out of the expression, without copying its content.
Definition: temporary.hpp:223
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
T value_type
The value type.
Definition: state_inverted_dropout_mask.hpp:109
std::mt19937_64 random_engine
The random engine used by the library.
Definition: random.hpp:22
dropout_distribution< value_type > distribution
The used distribution.
Definition: state_inverted_dropout_mask.hpp:114
friend std::ostream & operator<<(std::ostream &os, const state_inverted_dropout_mask_generator_op &s)
Outputs the given generator to the given stream.
Definition: state_inverted_dropout_mask.hpp:99
const T probability
The dropout probability.
Definition: state_inverted_dropout_mask.hpp:28
friend std::ostream & operator<<(std::ostream &os, const state_inverted_dropout_mask_generator_g_op &s)
Outputs the given generator to the given stream.
Definition: state_inverted_dropout_mask.hpp:185
dropout_distribution< value_type > distribution
The used distribution.
Definition: state_inverted_dropout_mask.hpp:31
auto gpu_compute_hint(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: state_inverted_dropout_mask.hpp:156