Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Arm Limited. |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 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 | #include "arm_compute/core/Types.h" |
| 25 | #include "arm_compute/runtime/NEON/NEFunctions.h" |
| 26 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 27 | |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 28 | #include "utils/Utils.h" |
| 29 | |
| 30 | #include <cstdlib> |
| 31 | |
| 32 | using namespace arm_compute; |
| 33 | using namespace utils; |
| 34 | |
| 35 | class NESGEMMExample : public Example |
| 36 | { |
| 37 | public: |
| 38 | bool do_setup(int argc, char **argv) override |
| 39 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 40 | NPYLoader npy0; |
| 41 | NPYLoader npy1; |
| 42 | NPYLoader npy2; |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 43 | alpha = 1.0f; |
| 44 | beta = 0.0f; |
| 45 | |
| 46 | std::ifstream stream; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | if (argc > 1) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 48 | { |
| 49 | stream.open(argv[1], std::fstream::in); |
| 50 | } |
| 51 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | if (argc < 3 || (argc < 4 && stream.bad())) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 53 | { |
| 54 | // Print help |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | std::cout << "Usage: 1) ./build/neon_sgemm input_matrix_1.npy input_matrix_2.npy [input_matrix_3.npy] " |
| 56 | "[alpha = 1] [beta = 0]\n"; |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 57 | std::cout << " 2) ./build/neon_sgemm M N K [alpha = 1.0f] [beta = 0.0f]\n\n"; |
| 58 | std::cout << "Too few or no input_matrices provided. Using M=7, N=3, K=5, alpha=1.0f and beta=0.0f\n\n"; |
| 59 | |
| 60 | src0.allocator()->init(TensorInfo(TensorShape(5U, 7U), 1, DataType::F32)); |
| 61 | src1.allocator()->init(TensorInfo(TensorShape(3U, 5U), 1, DataType::F32)); |
| 62 | src2.allocator()->init(TensorInfo(TensorShape(3U, 7U), 1, DataType::F32)); |
| 63 | } |
| 64 | else |
| 65 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 66 | if (stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1.0f] [beta = 0.0f] */ |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 67 | { |
| 68 | npy0.open(argv[1]); |
| 69 | npy0.init_tensor(src0, DataType::F32); |
| 70 | npy1.open(argv[2]); |
| 71 | npy1.init_tensor(src1, DataType::F32); |
| 72 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | if (argc > 3) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 74 | { |
| 75 | stream.close(); |
| 76 | stream.clear(); |
| 77 | stream.open(argv[3], std::fstream::in); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | if (stream.good()) /* case with third file */ |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 79 | { |
| 80 | npy2.open(argv[3]); |
| 81 | npy2.init_tensor(src2, DataType::F32); |
| 82 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 83 | if (argc > 4) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 84 | { |
| 85 | // Convert string to float |
| 86 | alpha = strtof(argv[4], nullptr); |
| 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | if (argc > 5) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 89 | { |
| 90 | // Convert string to float |
| 91 | beta = strtof(argv[5], nullptr); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | else /* case without third file */ |
| 96 | { |
| 97 | alpha = strtof(argv[3], nullptr); |
| 98 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 99 | if (argc > 4) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 100 | { |
| 101 | beta = strtof(argv[4], nullptr); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | else /* case M N K [alpha = 1.0f] [beta = 0.0f] */ |
| 107 | { |
| 108 | size_t M = strtol(argv[1], nullptr, 10); |
| 109 | size_t N = strtol(argv[2], nullptr, 10); |
| 110 | size_t K = strtol(argv[3], nullptr, 10); |
| 111 | |
| 112 | src0.allocator()->init(TensorInfo(TensorShape(K, M), 1, DataType::F32)); |
| 113 | src1.allocator()->init(TensorInfo(TensorShape(N, K), 1, DataType::F32)); |
| 114 | src2.allocator()->init(TensorInfo(TensorShape(N, M), 1, DataType::F32)); |
| 115 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 116 | if (argc > 4) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 117 | { |
| 118 | alpha = strtof(argv[4], nullptr); |
| 119 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 120 | if (argc > 5) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 121 | { |
| 122 | beta = strtof(argv[5], nullptr); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | init_sgemm_output(dst, src0, src1, DataType::F32); |
| 129 | |
| 130 | // Configure function |
| 131 | sgemm.configure(&src0, &src1, nullptr, &dst, alpha, beta); |
| 132 | |
| 133 | // Allocate all the images |
| 134 | src0.allocator()->allocate(); |
| 135 | src1.allocator()->allocate(); |
| 136 | dst.allocator()->allocate(); |
| 137 | |
| 138 | // Fill the input images with either the data provided or random data |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 139 | if (npy0.is_open()) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 140 | { |
| 141 | npy0.fill_tensor(src0); |
| 142 | npy1.fill_tensor(src1); |
| 143 | |
| 144 | output_filename = "sgemm_out.npy"; |
| 145 | is_fortran = npy0.is_fortran(); |
| 146 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 147 | if (npy2.is_open()) |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 148 | { |
| 149 | src2.allocator()->allocate(); |
| 150 | npy2.fill_tensor(src2); |
| 151 | } |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | src2.allocator()->allocate(); |
| 156 | |
| 157 | fill_random_tensor(src0, -1.f, 1.f); |
| 158 | fill_random_tensor(src1, -1.f, 1.f); |
| 159 | fill_random_tensor(src2, -1.f, 1.f); |
| 160 | } |
| 161 | |
| 162 | // Dummy run for CLTuner |
| 163 | sgemm.run(); |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | void do_run() override |
| 168 | { |
| 169 | // Execute the function |
| 170 | sgemm.run(); |
| 171 | } |
| 172 | void do_teardown() override |
| 173 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 174 | if (!output_filename.empty()) /* Save to .npy file */ |
Georgios Pinitas | 421405b | 2018-10-26 19:05:32 +0100 | [diff] [blame] | 175 | { |
| 176 | save_to_npy(dst, output_filename, is_fortran); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | Tensor src0{}, src1{}, src2{}, dst{}; |
| 182 | NEGEMM sgemm{}; |
| 183 | float alpha{}, beta{}; |
| 184 | bool is_fortran{}; |
| 185 | std::string output_filename{}; |
| 186 | }; |
| 187 | |
| 188 | /** Main program for sgemm test |
| 189 | * |
| 190 | * @param[in] argc Number of arguments |
| 191 | * @param[in] argv Arguments ( [optional] Matrix A, [optional] Matrix B, [optional] Matrix C, [optional] alpha, [optional] beta ) |
| 192 | */ |
| 193 | int main(int argc, char **argv) |
| 194 | { |
| 195 | return utils::run_example<NESGEMMExample>(argc, argv); |
| 196 | } |