xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/kernels/arm_gemm/misc.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2018, 2022 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 #ifndef NO_MULTI_THREADING
26 #include <mutex>
27 #endif
28 #include <cstdint>
29 
30 #include "arm_gemm.hpp"
31 #include "kernel_weight_format.hpp"
32 #include "utils.hpp"
33 
34 namespace arm_gemm {
35 
36 #ifndef NO_MULTI_THREADING
37 std::mutex report_mutex;
38 #endif
39 
get_weight_format(const KernelWeightFormat kwf,size_t element_size)40 WeightFormat get_weight_format(const KernelWeightFormat kwf, size_t element_size) {
41     if (kwf==KernelWeightFormat::NON_FIXED) {
42         return WeightFormat::UNSPECIFIED;
43     }
44 
45     uint32_t kwf_i = static_cast<uint32_t>(kwf);
46     uint32_t wf_i = 0;
47 
48     const auto block_bytes = (kwf_i >> 8) & 0xf;
49     const auto vector_count = (kwf_i >> 12) & 0xf;
50 
51     uint32_t vector_bytes;
52 
53     // For fast mode BF16 kernels set the appropriate bit and override element size to 2.
54     if (kwf_i & 0x10) {
55         element_size = 2;
56         wf_i |= 0x10;
57     }
58 
59     // Get total bytes in vector output
60     if (kwf_i & 0x1) {
61         vector_bytes = vector_count * get_vector_length<uint8_t>();
62     } else {
63         vector_bytes = vector_count * 16;
64     }
65 
66     auto input_blocking = block_bytes / element_size;
67     auto output_blocking = vector_bytes / block_bytes;
68 
69     wf_i |= (input_blocking << 20);
70     wf_i |= (output_blocking << 8);
71 
72     return static_cast<WeightFormat>(wf_i);
73 }
74 
75 } // namespace arm_gemm
76