Expression Templates Library (ETL)
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 
25 template <typename T>
26 using dropout_distribution = std::conditional_t<std::is_floating_point_v<T>, std::uniform_real_distribution<T>, std::uniform_int_distribution<T>>;
27 
31 template <typename T = double>
33  using value_type = T;
34 
35  const T probability;
38 
42  static constexpr bool gpu_computable =
43  (is_single_precision_t<T> && impl::egblas::has_sdropout_seed) || (is_double_precision_t<T> && impl::egblas::has_ddropout_seed);
44 
48  dropout_mask_generator_op(T probability) : probability(probability), rand_engine(std::time(nullptr)), distribution(T(0), T(1)) {}
49 
55  if (distribution(rand_engine) < probability) {
56  return T(0);
57  } else {
58  return T(1);
59  }
60  }
61 
69  template <typename Y>
70  auto gpu_compute_hint(Y& y) noexcept {
71  std::uniform_int_distribution<long> seed_dist;
72 
73  decltype(auto) t1 = force_temporary_gpu_dim_only(y);
74 
75  T alpha(1.0);
76  impl::egblas::dropout_seed(etl::size(y), probability, alpha, t1.gpu_memory(), 1, seed_dist(rand_engine));
77 
78  return t1;
79  }
86  template <typename Y>
87  Y& gpu_compute(Y& y) noexcept {
88  std::uniform_int_distribution<long> seed_dist;
89 
90  T alpha(1.0);
91  impl::egblas::dropout_seed(etl::size(y), probability, alpha, y.gpu_memory(), 1, seed_dist(rand_engine));
92 
93  y.validate_gpu();
94  y.invalidate_cpu();
95 
96  return y;
97  }
98 
105  friend std::ostream& operator<<(std::ostream& os, const dropout_mask_generator_op& s) {
106  return os << "dropout(p=" << s.probability << ")";
107  }
108 };
109 
113 template <typename G, typename T = double>
115  using value_type = T;
116 
117  const T probability;
120 
124  static constexpr bool gpu_computable =
125  (is_single_precision_t<T> && impl::egblas::has_sdropout_seed) || (is_double_precision_t<T> && impl::egblas::has_ddropout_seed);
126 
132  dropout_mask_generator_g_op(G& g, T probability) : probability(probability), rand_engine(g), distribution(T(0), T(1)) {}
133 
139  if (distribution(rand_engine) < probability) {
140  return T(0);
141  } else {
142  return T(1);
143  }
144  }
145 
153  template <typename Y>
154  auto gpu_compute_hint(Y& y) noexcept {
155  std::uniform_int_distribution<long> seed_dist;
156 
157  decltype(auto) t1 = force_temporary_gpu_dim_only(y);
158 
159  T alpha(1.0);
160  impl::egblas::dropout_seed(etl::size(y), probability, alpha, t1.gpu_memory(), 1, seed_dist(rand_engine));
161 
162  return t1;
163  }
170  template <typename Y>
171  Y& gpu_compute(Y& y) noexcept {
172  std::uniform_int_distribution<long> seed_dist;
173 
174  T alpha(1.0);
175  impl::egblas::dropout_seed(etl::size(y), probability, alpha, y.gpu_memory(), 1, seed_dist(rand_engine));
176 
177  y.validate_gpu();
178  y.invalidate_cpu();
179 
180  return y;
181  }
182 
189  friend std::ostream& operator<<(std::ostream& os, const dropout_mask_generator_g_op& s) {
190  return os << "dropout(p=" << s.probability << ")";
191  }
192 };
193 
194 } //end of namespace etl
auto s(T &&value)
Force the evaluation of the given expression.
Definition: stop.hpp:18
G & rand_engine
The random engine.
Definition: dropout_mask.hpp:118
EGBLAS wrappers for the dropout operation.
friend std::ostream & operator<<(std::ostream &os, const dropout_mask_generator_op &s)
Outputs the given generator to the given stream.
Definition: dropout_mask.hpp:105
Y & gpu_compute(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: dropout_mask.hpp:171
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: dropout_mask.hpp:42
friend std::ostream & operator<<(std::ostream &os, const dropout_mask_generator_g_op &s)
Outputs the given generator to the given stream.
Definition: dropout_mask.hpp:189
dropout_mask_generator_g_op(G &g, T probability)
Construct a new generator with the given start and end of the range.
Definition: dropout_mask.hpp:132
value_type operator()()
Generate a new value.
Definition: dropout_mask.hpp:54
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
Root namespace for the ETL library.
Definition: adapter.hpp:15
T value_type
The value type.
Definition: dropout_mask.hpp:115
dropout_distribution< value_type > distribution
The used distribution.
Definition: dropout_mask.hpp:37
auto gpu_compute_hint(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: dropout_mask.hpp:70
decltype(auto) force_temporary_gpu_dim_only(E &&expr)
Force a temporary out of the expression, without copying its content.
Definition: temporary.hpp:223
Generator from an uniform distribution using a custom random engine.
Definition: dropout_mask.hpp:114
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
random_engine rand_engine
The random engine.
Definition: dropout_mask.hpp:36
const T probability
The dropout probability.
Definition: dropout_mask.hpp:117
auto gpu_compute_hint(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: dropout_mask.hpp:154
value_type operator()()
Generate a new value.
Definition: dropout_mask.hpp:138
std::mt19937_64 random_engine
The random engine used by the library.
Definition: random.hpp:22
const T probability
The dropout probability.
Definition: dropout_mask.hpp:35
Y & gpu_compute(Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: dropout_mask.hpp:87
Generator from an uniform distribution.
Definition: dropout_mask.hpp:32
dropout_mask_generator_op(T probability)
Construct a new generator with the given start and end of the range.
Definition: dropout_mask.hpp:48
dropout_distribution< value_type > distribution
The used distribution.
Definition: dropout_mask.hpp:119
T value_type
The value type.
Definition: dropout_mask.hpp:33