Expression Templates Library (ETL)
relu_derivative.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 
11 
12 namespace etl {
13 
18 template <typename T>
20  static constexpr bool linear = true;
21  static constexpr bool thread_safe = true;
22 
28  template <vector_mode_t V>
29  static constexpr bool vectorizable = !is_complex_t<T>;
30 
34  template <typename E>
35  static constexpr bool gpu_computable = is_floating_t<T>&& impl::egblas::has_srelu_der_out&& impl::egblas::has_drelu_der_out;
36 
41  static constexpr int complexity() {
42  return 1;
43  }
44 
48  template <typename V = default_vec>
49  using vec_type = typename V::template vec_type<T>;
50 
56  static T apply(const T& x) {
57  return x > T(0) ? T(1) : T(0);
58  }
59 
66  template <typename V = default_vec>
67  static vec_type<V> load(const vec_type<V>& x) noexcept {
68  return V::round_up(V::min(V::set(T(1)), x));
69  }
70 
78  template <typename X, typename Y>
79  static auto gpu_compute_hint(const X& x, Y& y) noexcept {
80  decltype(auto) t1 = smart_gpu_compute_hint(x, y);
81 
82  auto t2 = force_temporary_gpu_dim_only(t1);
83 
84  T alpha(1.0);
85  impl::egblas::relu_der_out(etl::size(y), alpha, t1.gpu_memory(), 1, t2.gpu_memory(), 1);
86 
87  return t2;
88  }
95  template <typename X, typename Y>
96  static Y& gpu_compute(const X& x, Y& y) noexcept {
97  decltype(auto) t1 = select_smart_gpu_compute(x, y);
98 
99  T alpha(1.0);
100  impl::egblas::relu_der_out(etl::size(y), alpha, t1.gpu_memory(), 1, y.gpu_memory(), 1);
101 
102  y.validate_gpu();
103  y.invalidate_cpu();
104 
105  return y;
106  }
107 
112  static std::string desc() noexcept {
113  return "relu_derivative";
114  }
115 };
116 
117 } //end of namespace etl
static T apply(const T &x)
Apply the unary operator on x.
Definition: relu_derivative.hpp:56
static constexpr bool gpu_computable
Indicates if the operator can be computed on GPU.
Definition: relu_derivative.hpp:35
decltype(auto) select_smart_gpu_compute(X &x, Y &y)
Compute the expression into a representation that is GPU up to date and possibly store this represent...
Definition: helpers.hpp:434
static constexpr bool linear
Indicates if the operator is linear.
Definition: relu_derivative.hpp:20
static auto gpu_compute_hint(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: relu_derivative.hpp:79
static Y & gpu_compute(const X &x, Y &y) noexcept
Compute the result of the operation using the GPU.
Definition: relu_derivative.hpp:96
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr bool thread_safe
Indicates if the operator is thread safe or not.
Definition: relu_derivative.hpp:21
Unary operation computing the derivate of the RELU operation.
Definition: relu_derivative.hpp:19
static constexpr int complexity()
Estimate the complexity of operator.
Definition: relu_derivative.hpp:41
typename V::template vec_type< T > vec_type
Definition: relu_derivative.hpp:49
auto min(L &&lhs, R &&rhs)
Create an expression with the min value of lhs or rhs.
Definition: expression_builder.hpp:77
decltype(auto) force_temporary_gpu_dim_only(E &&expr)
Force a temporary out of the expression, without copying its content.
Definition: temporary.hpp:223
static constexpr bool vectorizable
Indicates if the expression is vectorizable using the given vector mode.
Definition: relu_derivative.hpp:29
constexpr size_t size(const E &expr) noexcept
Returns the size of the given ETL expression.
Definition: helpers.hpp:108
static std::string desc() noexcept
Returns a textual representation of the operator.
Definition: relu_derivative.hpp:112
decltype(auto) smart_gpu_compute_hint(E &expr, Y &y)
Compute the expression into a representation that is GPU up to date.
Definition: helpers.hpp:368
static vec_type< V > load(const vec_type< V > &x) noexcept
Compute several applications of the operator at a time.
Definition: relu_derivative.hpp:67
EGBLAS wrappers for the relu_der_out operation.