• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2018 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 
25 /* As some of the merges need these headers, but are all included in the
26  * arm_gemm namespace, put these headers here.  */
27 #include <algorithm>
28 
29 #include <arm_neon.h>
30 
31 #include "arm_gemm.hpp"
32 #include "asmlib.hpp"
33 #include "utils.hpp"
34 
35 namespace arm_gemm {
36 
37 template<unsigned int twidth, unsigned int height, bool sve=false, typename Tin, typename Tout>
MergeResults(Tout * out,const Tin * in,int ldc,int y0,int ymax,int x0,int xmax,const Tout * bias,Activation act,bool append)38 void MergeResults(Tout * out, const Tin * in, int ldc, int y0, int ymax, int x0, int xmax, const Tout *bias, Activation act, bool append) {
39     // For SVE cases, multiply the width up by the vector length.
40     // Use the *input* type to determine this, since this will be what the kernel operated on.
41     const int width = twidth * (sve ? get_vector_length<Tin>() : 1);
42 
43     const int full_y_blocks = (ymax - y0) / height;
44     const int y_remainder = (ymax - y0) % height;
45     const int y_blocks = full_y_blocks + (y_remainder ? 1 : 0);
46 
47     const int full_x_blocks = (xmax - x0) / width;
48     const int x_remainder = (xmax - x0) % width;
49     const int x_blocks = full_x_blocks + (x_remainder ? 1 : 0);
50 
51     for (int y_block = 0; y_block < y_blocks; y_block++) {
52         int ybase = y0 + (y_block * height);
53 
54         int fill_rows = (y_block < full_y_blocks) ? height : y_remainder;
55 
56         for (int x_block = 0; x_block < x_blocks; x_block++) {
57             int xbase = x0 + (x_block * width);
58 
59             int fill_cols = (x_block < full_x_blocks) ? width : x_remainder;
60 
61             for (int row=0; row < fill_rows; row++) {
62                 for (int col=0; col < fill_cols; col++) {
63                     Tout &r = out[(ybase + row) * ldc + xbase + col];
64                     Tout v = in[row * width + col];
65 
66                     if (append) {
67                         v += r;
68                     }
69 
70                     if (bias) {
71                         v += bias[xbase + col];
72                     }
73 
74                     switch(act.type) {
75                         default:
76                         case Activation::Type::None:
77                             break;
78 
79                         case Activation::Type::ReLU:
80                             v = std::max(v, static_cast<Tout>(0));
81                             break;
82 
83                         case Activation::Type::BoundedReLU:
84                             v = std::max(std::min(v, static_cast<Tout>(act.param1)), static_cast<Tout>(0));
85                             break;
86                     }
87 
88                     r = v;
89                 }
90             }
91 
92             in += (width * height);
93         }
94     }
95 }
96 
97 #include "merges/list.hpp"
98 
99 #if defined(__aarch64__) && defined(__ARM_FP16_ARGS)
100 template void MergeResults<12u, 8u, false, float, __fp16>(__fp16*, float const*, int, int, int, int, int, __fp16 const*, Activation, bool);
101 #endif
102 
103 #if defined(__arm__) && defined(__ARM_FP16_ARGS)
104 template void MergeResults<8u, 6u, false, float, __fp16>(__fp16*, float const*, int, int, int, int, int, __fp16 const*, Activation, bool);
105 #endif
106 
107 } // namespace arm_gemm
108