Expression Templates Library (ETL)
bias_batch_mean.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 #ifdef ETL_CUDNN_MODE
16 
17 #include "etl/impl/cublas/cuda.hpp"
18 #include "etl/impl/cudnn/cudnn.hpp"
19 
20 #endif
21 
22 namespace etl::impl::cudnn {
23 
24 #ifdef ETL_CUDNN_MODE
25 
31 template <typename X, typename Y>
32 void bias_batch_mean_4d(X&& x, Y&& y) {
33  using type = value_t<X>;
34 
35  type alpha[] = {1.0f};
36  type beta[] = {0.0f};
37 
38  decltype(auto) handle = start_cudnn();
39 
40  // Prepare the tensors
41  auto x_tensor = create_tensor_wrapper(x);
42  auto y_tensor = create_tensor_wrapper(y);
43 
44  // Allocate GPU memory, if necessary
45 
48 
49  // Perform the convolution
50 
51  cudnn_check(cudnnConvolutionBackwardBias(handle.get(), alpha, *x_tensor, x.gpu_memory(), beta, *y_tensor, y.gpu_memory()));
52 
53  y.validate_gpu();
54  y.invalidate_cpu();
55 }
56 
62 template <typename X, typename Y>
63 void bias_batch_mean_2d(X&& x, Y&& y) {
64  using type = value_t<X>;
65 
66  type alpha[] = {1.0f};
67  type beta[] = {0.0f};
68 
69  decltype(auto) handle = start_cudnn();
70 
71  // Prepare the tensors
72  auto x_tensor = create_tensor_front_wrapper(x);
73  auto y_tensor = create_tensor_wrapper(y);
74 
75  // Allocate GPU memory, if necessary
76 
77  x.ensure_gpu_up_to_date();
78  y.ensure_gpu_allocated();
79 
80  // Perform the convolution
81 
82  cudnn_check(cudnnConvolutionBackwardBias(handle.get(), alpha, *x_tensor, x.gpu_memory(), beta, *y_tensor, y.gpu_memory()));
83 
84  y.validate_gpu();
85  y.invalidate_cpu();
86 }
87 
88 #else
89 
90 //COVERAGE_EXCLUDE_BEGIN
91 
97 template <typename X, typename Y>
98 void bias_batch_mean_4d([[maybe_unused]] X&& x, [[maybe_unused]] Y&& y) {}
99 
105 template <typename X, typename Y>
106 void bias_batch_mean_2d([[maybe_unused]] X&& x, [[maybe_unused]] Y&& y) {}
107 
108 //COVERAGE_EXCLUDE_END
109 
110 #endif
111 
112 } //end of namespace etl::impl::cudnn
Definition: bias_add.hpp:24
void ensure_gpu_allocated() const
Ensures that the GPU memory is allocated and that the GPU memory is up to date (to undefined value)...
Definition: sub_view.hpp:717
void invalidate_cpu() const noexcept
Invalidates the CPU memory.
Definition: sub_view.hpp:688
void ensure_gpu_up_to_date() const
Copy back from the GPU to the expression memory if necessary.
Definition: dyn_matrix_view.hpp:280
void validate_gpu() const noexcept
Validates the GPU memory.
Definition: sub_view.hpp:709
Utility functions for cudnn.
bias_batch_mean_2d_expr< detail::build_type< E >, true > bias_batch_mean_2d(const E &value)
Returns the transpose of the given expression.
Definition: bias_batch_mean_2d_expr.hpp:441
bias_batch_mean_4d_expr< detail::build_type< E >, true > bias_batch_mean_4d(const E &value)
Returns the transpose of the given expression.
Definition: bias_batch_mean_4d_expr.hpp:503
value_type * gpu_memory() const noexcept
Return GPU memory of this expression, if any.
Definition: sub_view.hpp:674