Expression Templates Library (ETL)
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 
17 namespace etl {
18 
22 template <typename T = double>
24  using value_type = T;
25 
26  const T probability;
29 
33  static constexpr bool gpu_computable =
34  (is_single_precision_t<T> && impl::egblas::has_sinv_dropout_seed) || (is_double_precision_t<T> && impl::egblas::has_dinv_dropout_seed);
35 
39  inverted_dropout_mask_generator_op(T probability) : probability(probability), rand_engine(std::time(nullptr)), distribution(T(0), T(1)) {}
40 
46  if (distribution(rand_engine) < probability) {
47  return T(0);
48  } else {
49  return T(1) / (T(1) - probability);
50  }
51  }
52 
60  template <typename Y>
61  auto gpu_compute_hint(Y& y) noexcept {
62  std::uniform_int_distribution<long> seed_dist;
63 
64  decltype(auto) t1 = force_temporary_gpu_dim_only(y);
65 
66  T alpha(1.0);
67  impl::egblas::inv_dropout_seed(etl::size(y), probability, alpha, t1.gpu_memory(), 1, seed_dist(rand_engine));
68 
69  return t1;
70  }
71 
78  template <typename Y>
79  Y& gpu_compute(Y& y) noexcept {
80  std::uniform_int_distribution<long> seed_dist;
81 
82  T alpha(1.0);
83  impl::egblas::inv_dropout_seed(etl::size(y), probability, alpha, y.gpu_memory(), 1, seed_dist(rand_engine));
84 
85  y.validate_gpu();
86  y.invalidate_cpu();
87 
88  return y;
89  }
90 
97  friend std::ostream& operator<<(std::ostream& os, const inverted_dropout_mask_generator_op& s) {
98  return os << "inverted_dropout(p=" << s.probability << ")";
99  }
100 };
101 
105 template <typename G, typename T = double>
107  using value_type = T;
108 
109  const T probability;
112 
116  static constexpr bool gpu_computable =
117  (is_single_precision_t<T> && impl::egblas::has_sinv_dropout_seed) || (is_double_precision_t<T> && impl::egblas::has_dinv_dropout_seed);
118 
124  inverted_dropout_mask_generator_g_op(G& g, T probability) : probability(probability), rand_engine(g), distribution(T(0), T(1)) {}
125 
131  if (distribution(rand_engine) < probability) {
132  return T(0);
133  } else {
134  return T(1) / (T(1) - probability);
135  }
136  }
137 
145  template <typename Y>
146  auto gpu_compute_hint(Y& y) noexcept {
147  std::uniform_int_distribution<long> seed_dist;
148 
149  decltype(auto) t1 = force_temporary_gpu_dim_only(y);
150 
151  T alpha(1.0);
152  impl::egblas::inv_dropout_seed(etl::size(y), probability, alpha, t1.gpu_memory(), 1, seed_dist(rand_engine));
153 
154  return t1;
155  }
162  template <typename Y>
163  Y& gpu_compute(Y& y) noexcept {
164  std::uniform_int_distribution<long> seed_dist;
165 
166  T alpha(1.0);
167  impl::egblas::inv_dropout_seed(etl::size(y), probability, alpha, y.gpu_memory(), 1, seed_dist(rand_engine));
168 
169  y.validate_gpu();
170  y.invalidate_cpu();
171 
172  return y;
173  }
174 
181  friend std::ostream& operator<<(std::ostream& os, const inverted_dropout_mask_generator_g_op& s) {
182  return os << "inverted_dropout(p=" << s.probability << ")";
183  }
184 };
185 
186 } //end of namespace etl
G & rand_engine
The random engine.
Definition: inverted_dropout_mask.hpp:110
auto s(T &&value)
Force the evaluation of the given expression.
Definition: stop.hpp:18
value_type operator()()
Generate a new value.
Definition: inverted_dropout_mask.hpp:130
Generator from an uniform distribution using a custom random engine.
Definition: inverted_dropout_mask.hpp:106
auto gpu_compute_hint(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: inverted_dropout_mask.hpp:61
const T probability
The dropout probability.
Definition: inverted_dropout_mask.hpp:26
T value_type
The value type.
Definition: inverted_dropout_mask.hpp:24
Generator from an uniform distribution.
Definition: inverted_dropout_mask.hpp:23
friend std::ostream & operator<<(std::ostream &os, const inverted_dropout_mask_generator_g_op &s)
Outputs the given generator to the given stream.
Definition: inverted_dropout_mask.hpp:181
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: inverted_dropout_mask.hpp:33
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
dropout_distribution< value_type > distribution
The used distribution.
Definition: inverted_dropout_mask.hpp:28
Root namespace for the ETL library.
Definition: adapter.hpp:15
inverted_dropout_mask_generator_op(T probability)
Construct a new generator with the given start and end of the range.
Definition: inverted_dropout_mask.hpp:39
friend std::ostream & operator<<(std::ostream &os, const inverted_dropout_mask_generator_op &s)
Outputs the given generator to the given stream.
Definition: inverted_dropout_mask.hpp:97
decltype(auto) force_temporary_gpu_dim_only(E &&expr)
Force a temporary out of the expression, without copying its content.
Definition: temporary.hpp:223
auto gpu_compute_hint(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: inverted_dropout_mask.hpp:146
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
inverted_dropout_mask_generator_g_op(G &g, T probability)
Construct a new generator with the given start and end of the range.
Definition: inverted_dropout_mask.hpp:124
T value_type
The value type.
Definition: inverted_dropout_mask.hpp:107
const T probability
The dropout probability.
Definition: inverted_dropout_mask.hpp:109
value_type operator()()
Generate a new value.
Definition: inverted_dropout_mask.hpp:45
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: inverted_dropout_mask.hpp:111
Y & gpu_compute(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: inverted_dropout_mask.hpp:163
Y & gpu_compute(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: inverted_dropout_mask.hpp:79
random_engine rand_engine
The random engine.
Definition: inverted_dropout_mask.hpp:27