blob: d53ab8c130539b69231b9eba6171d4cc94fe3bcb [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 {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200213 Device device;
Mikael Olsson308e7f12023-06-12 15:00:55 +0200214 cout << "Driver version:" << device.getDriverVersion() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200215
Davide Grohmann1c26baa2021-06-15 13:21:15 +0200216 cout << "Send Ping" << endl;
217 device.ioctl(ETHOSU_IOCTL_PING);
218
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200219 cout << "Send capabilities request" << endl;
220 Capabilities capabilities = device.capabilities();
221
222 cout << "Capabilities:" << endl
223 << "\tversion_status:" << unsigned(capabilities.hwId.versionStatus) << endl
224 << "\tversion:" << capabilities.hwId.version << endl
225 << "\tproduct:" << capabilities.hwId.product << endl
226 << "\tarchitecture:" << capabilities.hwId.architecture << endl
227 << "\tdriver:" << capabilities.driver << endl
228 << "\tmacs_per_cc:" << unsigned(capabilities.hwCfg.macsPerClockCycle) << endl
229 << "\tcmd_stream_version:" << unsigned(capabilities.hwCfg.cmdStreamVersion) << endl
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200230 << "\tcustom_dma:" << std::boolalpha << capabilities.hwCfg.customDma << endl;
231
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200232 /* Create network */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200233 cout << "Create network" << endl;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100234
235 shared_ptr<Network> network;
236
237 if (networkIndex < 0) {
238 shared_ptr<Buffer> networkBuffer = allocAndFill(device, networkArg);
239 network = make_shared<Network>(device, networkBuffer);
240 } else {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100241 network = make_shared<Network>(device, networkIndex);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100242 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200243
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200244 /* Create one inference per IFM */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200245 list<shared_ptr<Inference>> inferences;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200246 for (auto &filename : ifmArg) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200247 cout << "Create inference" << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200248 inferences.push_back(createInference(device, network, filename, enabledCounters, enableCycleCounter));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200249 }
250
251 cout << "Wait for inferences" << endl;
252
253 int ofmIndex = 0;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200254 for (auto &inference : inferences) {
Davide Grohmann82d22582022-04-25 12:52:38 +0200255 cout << "Inference status: " << inference->status() << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200256
257 /* make sure the wait completes ok */
Davide Grohmanne446b422021-10-19 15:33:23 +0200258 try {
Davide Grohmann82d22582022-04-25 12:52:38 +0200259 cout << "Wait for inference" << endl;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100260 bool timedout = inference->wait(timeout);
261 if (timedout) {
262 cout << "Inference timed out, cancelling it" << endl;
263 bool aborted = inference->cancel();
264 if (!aborted || inference->status() != InferenceStatus::ABORTED) {
265 cout << "Inference cancellation failed" << endl;
266 }
267 }
Davide Grohmanne446b422021-10-19 15:33:23 +0200268 } catch (std::exception &e) {
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100269 cout << "Failed to wait for or to cancel inference: " << e.what() << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200270 exit(1);
271 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200272
Davide Grohmann82d22582022-04-25 12:52:38 +0200273 cout << "Inference status: " << inference->status() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200274
Davide Grohmann82d22582022-04-25 12:52:38 +0200275 if (inference->status() == InferenceStatus::OK) {
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100276 string ofmFilename = ofmArg + "." + to_string(ofmIndex);
277 ofstream ofmStream(ofmFilename, ios::binary);
278 if (!ofmStream.is_open()) {
279 cerr << "Error: Failed to open '" << ofmFilename << "'" << endl;
280 exit(1);
281 }
282
Per Åstrand716546a2020-10-24 20:17:32 +0200283 /* The inference completed and has ok status */
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200284 for (auto &ofmBuffer : inference->getOfmBuffers()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200285 cout << "OFM size: " << ofmBuffer->size() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200286
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200287 if (print) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200288 cout << "OFM data: " << *ofmBuffer << endl;
289 }
290
291 ofmStream.write(ofmBuffer->data(), ofmBuffer->size());
292 }
Per Åstrand716546a2020-10-24 20:17:32 +0200293
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100294 ofmStream.flush();
295
Per Åstrand716546a2020-10-24 20:17:32 +0200296 /* Read out PMU counters if configured */
297 if (std::count(enabledCounters.begin(), enabledCounters.end(), 0) <
298 Inference::getMaxPmuEventCounters()) {
299
300 const std::vector<uint32_t> pmus = inference->getPmuCounters();
301 cout << "PMUs : [";
302 for (auto p : pmus) {
303 cout << " " << p;
304 }
305 cout << " ]" << endl;
306 }
307 if (enableCycleCounter)
308 cout << "Cycle counter: " << inference->getCycleCounter() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200309 }
310
311 ofmIndex++;
312 }
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200313 } catch (Exception &e) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200314 cerr << "Error: " << e.what() << endl;
315 return 1;
316 }
317
318 return 0;
319}