blob: 8cda65a40094457257d61bf2fa0554c91fc0a600 [file] [log] [blame]
Georgios Pinitas421405b2018-10-26 19:05:32 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2019 Arm Limited.
Georgios Pinitas421405b2018-10-26 19:05:32 +01003 *
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010027
Georgios Pinitas421405b2018-10-26 19:05:32 +010028#include "utils/Utils.h"
29
30#include <cstdlib>
31
32using namespace arm_compute;
33using namespace utils;
34
35class NESGEMMExample : public Example
36{
37public:
38 bool do_setup(int argc, char **argv) override
39 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010040 NPYLoader npy0;
41 NPYLoader npy1;
42 NPYLoader npy2;
Georgios Pinitas421405b2018-10-26 19:05:32 +010043 alpha = 1.0f;
44 beta = 0.0f;
45
46 std::ifstream stream;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 if (argc > 1)
Georgios Pinitas421405b2018-10-26 19:05:32 +010048 {
49 stream.open(argv[1], std::fstream::in);
50 }
51
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 if (argc < 3 || (argc < 4 && stream.bad()))
Georgios Pinitas421405b2018-10-26 19:05:32 +010053 {
54 // Print help
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 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 Pinitas421405b2018-10-26 19:05:32 +010057 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010066 if (stream.good()) /* case file1.npy file2.npy [file3.npy] [alpha = 1.0f] [beta = 0.0f] */
Georgios Pinitas421405b2018-10-26 19:05:32 +010067 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 if (argc > 3)
Georgios Pinitas421405b2018-10-26 19:05:32 +010074 {
75 stream.close();
76 stream.clear();
77 stream.open(argv[3], std::fstream::in);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078 if (stream.good()) /* case with third file */
Georgios Pinitas421405b2018-10-26 19:05:32 +010079 {
80 npy2.open(argv[3]);
81 npy2.init_tensor(src2, DataType::F32);
82
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 if (argc > 4)
Georgios Pinitas421405b2018-10-26 19:05:32 +010084 {
85 // Convert string to float
86 alpha = strtof(argv[4], nullptr);
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 if (argc > 5)
Georgios Pinitas421405b2018-10-26 19:05:32 +010089 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 if (argc > 4)
Georgios Pinitas421405b2018-10-26 19:05:32 +0100100 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 if (argc > 4)
Georgios Pinitas421405b2018-10-26 19:05:32 +0100117 {
118 alpha = strtof(argv[4], nullptr);
119
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 if (argc > 5)
Georgios Pinitas421405b2018-10-26 19:05:32 +0100121 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100139 if (npy0.is_open())
Georgios Pinitas421405b2018-10-26 19:05:32 +0100140 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 if (npy2.is_open())
Georgios Pinitas421405b2018-10-26 19:05:32 +0100148 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100174 if (!output_filename.empty()) /* Save to .npy file */
Georgios Pinitas421405b2018-10-26 19:05:32 +0100175 {
176 save_to_npy(dst, output_filename, is_fortran);
177 }
178 }
179
180private:
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 */
193int main(int argc, char **argv)
194{
195 return utils::run_example<NESGEMMExample>(argc, argv);
196}