Compute Library
 22.08
bias_adder.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #pragma once
25 
26 namespace arm_gemm
27 {
28 
29 // Fallback routine to add bias to a block
30 template<typename T>
31 inline void bias_adder(T *out, unsigned int stride, const T *bias, unsigned int rows, unsigned int cols) {
32  for (unsigned int row=0; row<rows; row++) {
33  for (unsigned int col=0; col<cols; col++) {
34  out[row * stride + col] += bias[col];
35  }
36  }
37 }
38 
39 template<bool DoBias, typename T>
40 inline void activator(T *out, unsigned int stride, const T *bias, Activation act, unsigned int rows, unsigned int cols) {
41  if (act.type == Activation::Type::None) {
42  if (DoBias) {
43  bias_adder(out, stride, bias, rows, cols);
44  }
45  return;
46  }
47 
48  if (act.type == Activation::Type::ReLU) {
49  for (unsigned int row=0; row<rows; row++) {
50  for (unsigned int col=0; col<cols; col++) {
51  T &v = out[row * stride + col];
52  if (DoBias) {
53  v += bias[col];
54  }
55  v = std::max(static_cast<T>(0), v);
56  }
57  }
58  }
59 
61  const T max = static_cast<T>(act.param1);
62 
63  for (unsigned int row=0; row<rows; row++) {
64  for (unsigned int col=0; col<cols; col++) {
65  T &v = out[row * stride + col];
66  if (DoBias) {
67  v += bias[col];
68  }
69  v = std::max(static_cast<T>(0), std::min(v, max));
70  }
71  }
72  }
73 }
74 
75 } // namespace arm_gemm
void activator(T *out, unsigned int stride, const T *bias, Activation act, unsigned int rows, unsigned int cols)
Definition: bias_adder.hpp:40
void bias_adder(T *out, unsigned int stride, const T *bias, unsigned int rows, unsigned int cols)
Definition: bias_adder.hpp:31
const int32_t * bias