xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/assembly/arm_gemm_compute_iface.hpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2020-2021 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 #include "arm_compute/core/Dimensions.h"
27 #include "arm_compute/core/Window.h"
28 
29 #include "ndrange.hpp"
30 
31 #include <cassert>
32 
33 /* This file contains mapping between integral types used in arm_compute and arm_gemm
34  * These two codebases both require a degree of separation for the sake of modularity
35  * so maintain their own types which represent similar information.
36  */
37 
38 namespace arm_gemm
39 {
40 //we want to unify the maximum number of dimensions used beween arm_gemm and arm compute library
41 constexpr std::size_t ndrange_max =
42     arm_compute::Dimensions<unsigned int>::num_max_dimensions;
43 
44 using ndrange_t = NDRange<ndrange_max>;
45 using ndcoord_t = NDCoordinate<ndrange_max>;
46 
47 /* Converts an `arm_gemm::ndrange_t` to a `arm_compute::Window`
48  *
49  * As `NDRange<T>` does not not encode start positions, we specify
50  * the start to be zero in the produced `arm_compute::Window`
51  *
52  * @param [ndr] the `arm_gemm::ndrange_t` we wish to convert into a `arm_compute::Window`
53  * @returns an `arm_compute::Window` representing the same dimensional ranges as `ndr`
54  */
to_window(const ndrange_t & ndr)55 inline arm_compute::Window to_window(const ndrange_t &ndr)
56 {
57     arm_compute::Window win;
58 
59     for(unsigned int i = 0; i != ndrange_max; ++i)
60     {
61         //populate the window with the dimensions of the NDRange
62         win.set(i, arm_compute::Window::Dimension(0, ndr.get_size(i)));
63     }
64 
65     return win;
66 }
67 
68 /*
69  * Converts an `arm_gemm::ndcoord_t` to a `arm_compute::Window`
70  *
71  * @param [ndc] the `arm_gemm::ndcoord_t` we wish to convert into a `arm_compute::Window`
72  * @returns an `arm_compute::Window` representing the same dimensional ranges as `ndc`
73  */
to_window(const ndcoord_t & ndc)74 inline arm_compute::Window to_window(const ndcoord_t &ndc)
75 {
76     arm_compute::Window win;
77 
78     for(unsigned int i = 0; i != ndrange_max; ++i)
79     {
80         const auto start = ndc.get_position(i);
81         const auto size  = ndc.get_size(i);
82         const auto stop  = start + size;
83 
84         //populate the window with the dimensions of the NDRange
85         win.set(i, arm_compute::Window::Dimension(start, stop));
86     }
87 
88     return win;
89 }
90 
91 /** Convert an `arm_compute::Window` to an `arm_gemm::NDRange` of the same max dimensions
92  *
93  * It should be noted that `arm_compute::Window` specifies a `start()` and an `end()`
94  * where as `arm_gemm::ndrange_t` only has a size, as a result we store the delta between the range
95  *
96  * @param [win] the `arm_compute::Window` we want to convert to `arm_gemm::ndrange_t`
97  * @return the resultant ndrange_t
98  */
to_ndrange(const arm_compute::Window & win)99 inline ndrange_t to_ndrange(const arm_compute::Window &win)
100 {
101     return
102     {
103         static_cast<unsigned int>(win[0].end() - win[0].start()),
104         static_cast<unsigned int>(win[1].end() - win[1].start()),
105         static_cast<unsigned int>(win[2].end() - win[2].start()),
106         static_cast<unsigned int>(win[3].end() - win[3].start()),
107         static_cast<unsigned int>(win[4].end() - win[4].start()),
108         static_cast<unsigned int>(win[5].end() - win[5].start())
109     };
110 }
111 
112 /** Convert an `arm_compute::Window` to an `arm_gemm::NDCoord` of the same max dimensions
113  *
114  * @param [win] the `arm_compute::Window` we want to convert to `arm_gemm::ndcoord_t`
115  * @return the resultant ndcoord_t
116  */
to_ndcoord(const arm_compute::Window & win)117 inline ndcoord_t to_ndcoord(const arm_compute::Window &win)
118 {
119     return
120     {
121         { static_cast<unsigned int>(win[0].start()), static_cast<unsigned int>(win[0].end() - win[0].start()) },
122         { static_cast<unsigned int>(win[1].start()), static_cast<unsigned int>(win[1].end() - win[1].start()) },
123         { static_cast<unsigned int>(win[2].start()), static_cast<unsigned int>(win[2].end() - win[2].start()) },
124         { static_cast<unsigned int>(win[3].start()), static_cast<unsigned int>(win[3].end() - win[3].start()) },
125         { static_cast<unsigned int>(win[4].start()), static_cast<unsigned int>(win[4].end() - win[4].start()) },
126         { static_cast<unsigned int>(win[5].start()), static_cast<unsigned int>(win[5].end() - win[5].start()) }
127     };
128 }
129 
130 } //namespace arm_gemm
131