blob: 569fda6b14399ad8451eea768abb39dd751c32c8 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Mikael Olssonfab31eb2023-08-03 12:41:01 +02002 * Copyright (c) 2020-2023 Arm Limited. All rights reserved.
Kristofer Jonsson116a6352020-08-20 17:25:23 +02003 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020019#include <ethosu.hpp>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020020#include <uapi/ethosu.h>
21
Kristofer Jonsson116a6352020-08-20 17:25:23 +020022#include <fstream>
23#include <iomanip>
24#include <iostream>
25#include <list>
Davide Grohmann35ce6c82021-06-01 15:03:51 +020026#include <stdio.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020027#include <string>
Kristofer Jonsson79689c52020-10-16 14:42:19 +020028#include <unistd.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020029
30using namespace std;
31using namespace EthosU;
32
Kristofer Jonsson79689c52020-10-16 14:42:19 +020033namespace {
Davide Grohmanne446b422021-10-19 15:33:23 +020034int64_t defaultTimeout = 60000000000;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020035
Kristofer Jonsson79689c52020-10-16 14:42:19 +020036void help(const string exe) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020037 cerr << "Usage: " << exe << " [ARGS]\n";
38 cerr << "\n";
39 cerr << "Arguments:\n";
40 cerr << " -h --help Print this help message.\n";
41 cerr << " -n --network File to read network from.\n";
Kristofer Jonsson35de9e62022-03-08 13:25:45 +010042 cerr << " --index Network model index, stored in firmware binary.\n";
Kristofer Jonsson116a6352020-08-20 17:25:23 +020043 cerr << " -i --ifm File to read IFM from.\n";
44 cerr << " -o --ofm File to write IFM to.\n";
Per Åstrand716546a2020-10-24 20:17:32 +020045 cerr << " -P --pmu [0.." << Inference::getMaxPmuEventCounters() << "] eventid.\n";
46 cerr << " PMU counter to enable followed by eventid, can be passed multiple times.\n";
47 cerr << " -C --cycles Enable cycle counter for inference.\n";
Davide Grohmanne446b422021-10-19 15:33:23 +020048 cerr << " -t --timeout Timeout in nanoseconds (default " << defaultTimeout << ").\n";
Kristofer Jonsson116a6352020-08-20 17:25:23 +020049 cerr << " -p Print OFM.\n";
50 cerr << endl;
51}
52
Kristofer Jonsson79689c52020-10-16 14:42:19 +020053void rangeCheck(const int i, const int argc, const string arg) {
54 if (i >= argc) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020055 cerr << "Error: Missing argument to '" << arg << "'" << endl;
56 exit(1);
57 }
58}
59
Kristofer Jonsson79689c52020-10-16 14:42:19 +020060shared_ptr<Buffer> allocAndFill(Device &device, const string filename) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020061 ifstream stream(filename, ios::binary);
Kristofer Jonsson79689c52020-10-16 14:42:19 +020062 if (!stream.is_open()) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020063 cerr << "Error: Failed to open '" << filename << "'" << endl;
64 exit(1);
65 }
66
67 stream.seekg(0, ios_base::end);
68 size_t size = stream.tellg();
69 stream.seekg(0, ios_base::beg);
70
71 shared_ptr<Buffer> buffer = make_shared<Buffer>(device, size);
72 buffer->resize(size);
73 stream.read(buffer->data(), size);
74
75 return buffer;
76}
77
Per Åstrand716546a2020-10-24 20:17:32 +020078shared_ptr<Inference> createInference(Device &device,
79 shared_ptr<Network> &network,
80 const string &filename,
81 const std::vector<uint8_t> &counters,
82 bool enableCycleCounter) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020083 // Open IFM file
84 ifstream stream(filename, ios::binary);
Kristofer Jonsson79689c52020-10-16 14:42:19 +020085 if (!stream.is_open()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020086 cerr << "Error: Failed to open '" << filename << "'" << endl;
87 exit(1);
88 }
89
90 // Get IFM file size
91 stream.seekg(0, ios_base::end);
92 size_t size = stream.tellg();
93 stream.seekg(0, ios_base::beg);
94
Kristofer Jonsson79689c52020-10-16 14:42:19 +020095 if (size != network->getIfmSize()) {
96 cerr << "Error: IFM size does not match network size. filename=" << filename << ", size=" << size
97 << ", network=" << network->getIfmSize() << endl;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020098 exit(1);
99 }
100
101 // Create IFM buffers
102 vector<shared_ptr<Buffer>> ifm;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200103 for (auto size : network->getIfmDims()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200104 shared_ptr<Buffer> buffer = make_shared<Buffer>(device, size);
105 buffer->resize(size);
106 stream.read(buffer->data(), size);
107
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200108 if (!stream) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200109 cerr << "Error: Failed to read IFM" << endl;
110 exit(1);
111 }
112
113 ifm.push_back(buffer);
114 }
115
116 // Create OFM buffers
117 vector<shared_ptr<Buffer>> ofm;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200118 for (auto size : network->getOfmDims()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200119 ofm.push_back(make_shared<Buffer>(device, size));
120 }
121
Per Åstrand716546a2020-10-24 20:17:32 +0200122 return make_shared<Inference>(
123 network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end(), counters, enableCycleCounter);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200124}
125
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200126ostream &operator<<(ostream &os, Buffer &buf) {
127 char *c = buf.data();
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200128 const char *end = c + buf.size();
129
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200130 while (c < end) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200131 os << hex << setw(2) << static_cast<int>(*c++) << " " << dec;
132 }
133
134 return os;
135}
136
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200137} // namespace
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200138
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200139int main(int argc, char *argv[]) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200140 const string exe = argv[0];
141 string networkArg;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100142 int networkIndex = -1;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200143 list<string> ifmArg;
Per Åstrand716546a2020-10-24 20:17:32 +0200144 vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters());
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200145 string ofmArg;
Davide Grohmanne446b422021-10-19 15:33:23 +0200146 int64_t timeout = defaultTimeout;
Per Åstrand716546a2020-10-24 20:17:32 +0200147 bool print = false;
148 bool enableCycleCounter = false;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200149
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200150 for (int i = 1; i < argc; ++i) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200151 const string arg(argv[i]);
152
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200153 if (arg == "-h" || arg == "--help") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200154 help(exe);
155 exit(1);
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200156 } else if (arg == "--network" || arg == "-n") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200157 rangeCheck(++i, argc, arg);
158 networkArg = argv[i];
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100159 } else if (arg == "--index") {
160 rangeCheck(++i, argc, arg);
161 networkIndex = stoi(argv[i]);
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200162 } else if (arg == "--ifm" || arg == "-i") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200163 rangeCheck(++i, argc, arg);
164 ifmArg.push_back(argv[i]);
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200165 } else if (arg == "--ofm" || arg == "-o") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200166 rangeCheck(++i, argc, arg);
167 ofmArg = argv[i];
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200168 } else if (arg == "--timeout" || arg == "-t") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200169 rangeCheck(++i, argc, arg);
Davide Grohmanne446b422021-10-19 15:33:23 +0200170 timeout = stoll(argv[i]);
Per Åstrand716546a2020-10-24 20:17:32 +0200171 } else if (arg == "--pmu" || arg == "-P") {
172 unsigned pmu = 0, event = 0;
173 rangeCheck(++i, argc, arg);
174 pmu = stoi(argv[i]);
175
176 rangeCheck(++i, argc, arg);
177 event = stoi(argv[i]);
178
179 if (pmu >= enabledCounters.size()) {
180 cerr << "PMU out of bounds!" << endl;
181 help(exe);
182 exit(1);
183 }
184 cout << argv[i] << " -> Enabling " << pmu << " with event " << event << endl;
185 enabledCounters[pmu] = event;
186 } else if (arg == "--cycles" || arg == "-C") {
187 enableCycleCounter = true;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200188 } else if (arg == "-p") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200189 print = true;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200190 } else {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200191 cerr << "Error: Invalid argument '" << arg << "'" << endl;
192 help(exe);
193 exit(1);
194 }
195 }
196
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200197 if (networkArg.empty()) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200198 cerr << "Error: Missing 'network' argument" << endl;
199 exit(1);
200 }
201
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200202 if (ifmArg.empty()) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200203 cerr << "Error: Missing 'ifm' argument" << endl;
204 exit(1);
205 }
206
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200207 if (ofmArg.empty()) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200208 cerr << "Error: Missing 'ofm' argument" << endl;
209 exit(1);
210 }
211
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200212 try {
Mikael Olssone9c3f072023-06-12 15:58:10 +0200213 cout << "Driver library version:" << getLibraryVersion() << endl;
214
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200215 Device device;
Mikael Olssone9c3f072023-06-12 15:58:10 +0200216 cout << "Kernel driver version:" << device.getDriverVersion() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200217
Davide Grohmann1c26baa2021-06-15 13:21:15 +0200218 cout << "Send Ping" << endl;
219 device.ioctl(ETHOSU_IOCTL_PING);
220
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200221 cout << "Send capabilities request" << endl;
222 Capabilities capabilities = device.capabilities();
223
224 cout << "Capabilities:" << endl
225 << "\tversion_status:" << unsigned(capabilities.hwId.versionStatus) << endl
226 << "\tversion:" << capabilities.hwId.version << endl
227 << "\tproduct:" << capabilities.hwId.product << endl
228 << "\tarchitecture:" << capabilities.hwId.architecture << endl
229 << "\tdriver:" << capabilities.driver << endl
230 << "\tmacs_per_cc:" << unsigned(capabilities.hwCfg.macsPerClockCycle) << endl
231 << "\tcmd_stream_version:" << unsigned(capabilities.hwCfg.cmdStreamVersion) << endl
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200232 << "\tcustom_dma:" << std::boolalpha << capabilities.hwCfg.customDma << endl;
233
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200234 /* Create network */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200235 cout << "Create network" << endl;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100236
237 shared_ptr<Network> network;
238
239 if (networkIndex < 0) {
240 shared_ptr<Buffer> networkBuffer = allocAndFill(device, networkArg);
241 network = make_shared<Network>(device, networkBuffer);
242 } else {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100243 network = make_shared<Network>(device, networkIndex);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100244 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200245
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200246 /* Create one inference per IFM */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200247 list<shared_ptr<Inference>> inferences;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200248 for (auto &filename : ifmArg) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200249 cout << "Create inference" << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200250 inferences.push_back(createInference(device, network, filename, enabledCounters, enableCycleCounter));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200251 }
252
253 cout << "Wait for inferences" << endl;
254
255 int ofmIndex = 0;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200256 for (auto &inference : inferences) {
Davide Grohmann82d22582022-04-25 12:52:38 +0200257 cout << "Inference status: " << inference->status() << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200258
259 /* make sure the wait completes ok */
Davide Grohmanne446b422021-10-19 15:33:23 +0200260 try {
Davide Grohmann82d22582022-04-25 12:52:38 +0200261 cout << "Wait for inference" << endl;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100262 bool timedout = inference->wait(timeout);
263 if (timedout) {
264 cout << "Inference timed out, cancelling it" << endl;
265 bool aborted = inference->cancel();
266 if (!aborted || inference->status() != InferenceStatus::ABORTED) {
267 cout << "Inference cancellation failed" << endl;
268 }
269 }
Davide Grohmanne446b422021-10-19 15:33:23 +0200270 } catch (std::exception &e) {
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100271 cout << "Failed to wait for or to cancel inference: " << e.what() << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200272 exit(1);
273 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200274
Davide Grohmann82d22582022-04-25 12:52:38 +0200275 cout << "Inference status: " << inference->status() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200276
Davide Grohmann82d22582022-04-25 12:52:38 +0200277 if (inference->status() == InferenceStatus::OK) {
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100278 string ofmFilename = ofmArg + "." + to_string(ofmIndex);
279 ofstream ofmStream(ofmFilename, ios::binary);
280 if (!ofmStream.is_open()) {
281 cerr << "Error: Failed to open '" << ofmFilename << "'" << endl;
282 exit(1);
283 }
284
Per Åstrand716546a2020-10-24 20:17:32 +0200285 /* The inference completed and has ok status */
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200286 for (auto &ofmBuffer : inference->getOfmBuffers()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200287 cout << "OFM size: " << ofmBuffer->size() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200288
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200289 if (print) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200290 cout << "OFM data: " << *ofmBuffer << endl;
291 }
292
293 ofmStream.write(ofmBuffer->data(), ofmBuffer->size());
294 }
Per Åstrand716546a2020-10-24 20:17:32 +0200295
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100296 ofmStream.flush();
297
Per Åstrand716546a2020-10-24 20:17:32 +0200298 /* Read out PMU counters if configured */
299 if (std::count(enabledCounters.begin(), enabledCounters.end(), 0) <
300 Inference::getMaxPmuEventCounters()) {
301
302 const std::vector<uint32_t> pmus = inference->getPmuCounters();
303 cout << "PMUs : [";
304 for (auto p : pmus) {
305 cout << " " << p;
306 }
307 cout << " ]" << endl;
308 }
309 if (enableCycleCounter)
310 cout << "Cycle counter: " << inference->getCycleCounter() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200311 }
312
313 ofmIndex++;
314 }
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200315 } catch (Exception &e) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200316 cerr << "Error: " << e.what() << endl;
317 return 1;
318 }
319
320 return 0;
321}