blob: 34fb07c5733e86a69ed56e4750783c0f61ab0328 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Mikael Olssondc18cea2024-02-16 11:04:46 +01002 * SPDX-FileCopyrightText: Copyright 2020-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Kristofer Jonsson116a6352020-08-20 17:25:23 +02003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the License); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020018#include <ethosu.hpp>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020019#include <uapi/ethosu.h>
20
Kristofer Jonsson116a6352020-08-20 17:25:23 +020021#include <fstream>
22#include <iomanip>
23#include <iostream>
24#include <list>
Davide Grohmann35ce6c82021-06-01 15:03:51 +020025#include <stdio.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020026#include <string>
Kristofer Jonsson79689c52020-10-16 14:42:19 +020027#include <unistd.h>
Mikael Olssonc081e592023-10-30 11:10:56 +010028#include <utility>
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
Mikael Olssona025eb52024-02-19 10:34:06 +010036void 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
Mikael Olssona025eb52024-02-19 10:34:06 +010053void rangeCheck(const int i, const int argc, const string &arg) {
Kristofer Jonsson79689c52020-10-16 14:42:19 +020054 if (i >= argc) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020055 cerr << "Error: Missing argument to '" << arg << "'" << endl;
56 exit(1);
57 }
58}
59
Mikael Olssona025eb52024-02-19 10:34:06 +010060pair<unique_ptr<unsigned char[]>, size_t> getNetworkData(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
Mikael Olssonc081e592023-10-30 11:10:56 +010071 unique_ptr<unsigned char[]> data = std::make_unique<unsigned char[]>(size);
72 stream.read(reinterpret_cast<char *>(data.get()), size);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020073
Mikael Olssonc081e592023-10-30 11:10:56 +010074 return make_pair(std::move(data), size);
Kristofer Jonsson116a6352020-08-20 17:25:23 +020075}
76
Per Åstrand716546a2020-10-24 20:17:32 +020077shared_ptr<Inference> createInference(Device &device,
78 shared_ptr<Network> &network,
79 const string &filename,
80 const std::vector<uint8_t> &counters,
81 bool enableCycleCounter) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020082 // Open IFM file
83 ifstream stream(filename, ios::binary);
Kristofer Jonsson79689c52020-10-16 14:42:19 +020084 if (!stream.is_open()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020085 cerr << "Error: Failed to open '" << filename << "'" << endl;
86 exit(1);
87 }
88
89 // Get IFM file size
90 stream.seekg(0, ios_base::end);
91 size_t size = stream.tellg();
92 stream.seekg(0, ios_base::beg);
93
Kristofer Jonsson79689c52020-10-16 14:42:19 +020094 if (size != network->getIfmSize()) {
95 cerr << "Error: IFM size does not match network size. filename=" << filename << ", size=" << size
96 << ", network=" << network->getIfmSize() << endl;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020097 exit(1);
98 }
99
100 // Create IFM buffers
101 vector<shared_ptr<Buffer>> ifm;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200102 for (auto size : network->getIfmDims()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200103 shared_ptr<Buffer> buffer = make_shared<Buffer>(device, size);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200104 stream.read(buffer->data(), size);
105
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200106 if (!stream) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200107 cerr << "Error: Failed to read IFM" << endl;
108 exit(1);
109 }
110
111 ifm.push_back(buffer);
112 }
113
114 // Create OFM buffers
115 vector<shared_ptr<Buffer>> ofm;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200116 for (auto size : network->getOfmDims()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200117 ofm.push_back(make_shared<Buffer>(device, size));
118 }
119
Per Åstrand716546a2020-10-24 20:17:32 +0200120 return make_shared<Inference>(
121 network, ifm.begin(), ifm.end(), ofm.begin(), ofm.end(), counters, enableCycleCounter);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200122}
123
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200124ostream &operator<<(ostream &os, Buffer &buf) {
125 char *c = buf.data();
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200126 const char *end = c + buf.size();
127
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200128 while (c < end) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200129 os << hex << setw(2) << static_cast<int>(*c++) << " " << dec;
130 }
131
132 return os;
133}
134
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200135} // namespace
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200136
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200137int main(int argc, char *argv[]) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200138 const string exe = argv[0];
139 string networkArg;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100140 int networkIndex = -1;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200141 list<string> ifmArg;
Per Åstrand716546a2020-10-24 20:17:32 +0200142 vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters());
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200143 string ofmArg;
Davide Grohmanne446b422021-10-19 15:33:23 +0200144 int64_t timeout = defaultTimeout;
Per Åstrand716546a2020-10-24 20:17:32 +0200145 bool print = false;
146 bool enableCycleCounter = false;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200147
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200148 for (int i = 1; i < argc; ++i) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200149 const string arg(argv[i]);
150
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200151 if (arg == "-h" || arg == "--help") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200152 help(exe);
153 exit(1);
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200154 } else if (arg == "--network" || arg == "-n") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200155 rangeCheck(++i, argc, arg);
156 networkArg = argv[i];
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100157 } else if (arg == "--index") {
158 rangeCheck(++i, argc, arg);
159 networkIndex = stoi(argv[i]);
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200160 } else if (arg == "--ifm" || arg == "-i") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200161 rangeCheck(++i, argc, arg);
162 ifmArg.push_back(argv[i]);
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200163 } else if (arg == "--ofm" || arg == "-o") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200164 rangeCheck(++i, argc, arg);
165 ofmArg = argv[i];
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200166 } else if (arg == "--timeout" || arg == "-t") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200167 rangeCheck(++i, argc, arg);
Davide Grohmanne446b422021-10-19 15:33:23 +0200168 timeout = stoll(argv[i]);
Per Åstrand716546a2020-10-24 20:17:32 +0200169 } else if (arg == "--pmu" || arg == "-P") {
170 unsigned pmu = 0, event = 0;
171 rangeCheck(++i, argc, arg);
172 pmu = stoi(argv[i]);
173
174 rangeCheck(++i, argc, arg);
175 event = stoi(argv[i]);
176
177 if (pmu >= enabledCounters.size()) {
178 cerr << "PMU out of bounds!" << endl;
179 help(exe);
180 exit(1);
181 }
182 cout << argv[i] << " -> Enabling " << pmu << " with event " << event << endl;
183 enabledCounters[pmu] = event;
184 } else if (arg == "--cycles" || arg == "-C") {
185 enableCycleCounter = true;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200186 } else if (arg == "-p") {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200187 print = true;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200188 } else {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200189 cerr << "Error: Invalid argument '" << arg << "'" << endl;
190 help(exe);
191 exit(1);
192 }
193 }
194
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200195 if (networkArg.empty()) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200196 cerr << "Error: Missing 'network' argument" << endl;
197 exit(1);
198 }
199
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200200 if (ifmArg.empty()) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200201 cerr << "Error: Missing 'ifm' argument" << endl;
202 exit(1);
203 }
204
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200205 if (ofmArg.empty()) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200206 cerr << "Error: Missing 'ofm' argument" << endl;
207 exit(1);
208 }
209
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200210 try {
Mikael Olssone9c3f072023-06-12 15:58:10 +0200211 cout << "Driver library version:" << getLibraryVersion() << endl;
212
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200213 Device device;
Mikael Olssone9c3f072023-06-12 15:58:10 +0200214 cout << "Kernel 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
Mikael Olssondc18cea2024-02-16 11:04:46 +0100230 << "\ttype:" << capabilities.hwCfg.type << endl
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200231 << "\tcustom_dma:" << std::boolalpha << capabilities.hwCfg.customDma << endl;
232
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200233 /* Create network */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200234 cout << "Create network" << endl;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100235
236 shared_ptr<Network> network;
237
238 if (networkIndex < 0) {
Mikael Olssonc081e592023-10-30 11:10:56 +0100239 auto networkData = getNetworkData(networkArg);
240 network = make_shared<Network>(device, networkData.first.get(), networkData.second);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100241 } else {
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100242 network = make_shared<Network>(device, networkIndex);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100243 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200244
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200245 /* Create one inference per IFM */
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200246 list<shared_ptr<Inference>> inferences;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200247 for (auto &filename : ifmArg) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200248 cout << "Create inference" << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200249 inferences.push_back(createInference(device, network, filename, enabledCounters, enableCycleCounter));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200250 }
251
252 cout << "Wait for inferences" << endl;
253
254 int ofmIndex = 0;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200255 for (auto &inference : inferences) {
Davide Grohmann82d22582022-04-25 12:52:38 +0200256 cout << "Inference status: " << inference->status() << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200257
258 /* make sure the wait completes ok */
Davide Grohmanne446b422021-10-19 15:33:23 +0200259 try {
Davide Grohmann82d22582022-04-25 12:52:38 +0200260 cout << "Wait for inference" << endl;
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100261 bool timedout = inference->wait(timeout);
262 if (timedout) {
263 cout << "Inference timed out, cancelling it" << endl;
264 bool aborted = inference->cancel();
265 if (!aborted || inference->status() != InferenceStatus::ABORTED) {
266 cout << "Inference cancellation failed" << endl;
267 }
268 }
Davide Grohmanne446b422021-10-19 15:33:23 +0200269 } catch (std::exception &e) {
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100270 cout << "Failed to wait for or to cancel inference: " << e.what() << endl;
Per Åstrand716546a2020-10-24 20:17:32 +0200271 exit(1);
272 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200273
Davide Grohmann82d22582022-04-25 12:52:38 +0200274 cout << "Inference status: " << inference->status() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200275
Davide Grohmann82d22582022-04-25 12:52:38 +0200276 if (inference->status() == InferenceStatus::OK) {
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100277 string ofmFilename = ofmArg + "." + to_string(ofmIndex);
278 ofstream ofmStream(ofmFilename, ios::binary);
279 if (!ofmStream.is_open()) {
280 cerr << "Error: Failed to open '" << ofmFilename << "'" << endl;
281 exit(1);
282 }
283
Per Åstrand716546a2020-10-24 20:17:32 +0200284 /* The inference completed and has ok status */
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200285 for (auto &ofmBuffer : inference->getOfmBuffers()) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200286 cout << "OFM size: " << ofmBuffer->size() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200287
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200288 if (print) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200289 cout << "OFM data: " << *ofmBuffer << endl;
290 }
291
292 ofmStream.write(ofmBuffer->data(), ofmBuffer->size());
293 }
Per Åstrand716546a2020-10-24 20:17:32 +0200294
Davide Grohmann7e8f5082022-03-23 12:48:45 +0100295 ofmStream.flush();
296
Per Åstrand716546a2020-10-24 20:17:32 +0200297 /* Read out PMU counters if configured */
298 if (std::count(enabledCounters.begin(), enabledCounters.end(), 0) <
299 Inference::getMaxPmuEventCounters()) {
300
Mikael Olssone87446c2023-12-15 17:17:06 +0100301 const std::vector<uint64_t> pmus = inference->getPmuCounters();
Per Åstrand716546a2020-10-24 20:17:32 +0200302 cout << "PMUs : [";
303 for (auto p : pmus) {
304 cout << " " << p;
305 }
306 cout << " ]" << endl;
307 }
308 if (enableCycleCounter)
309 cout << "Cycle counter: " << inference->getCycleCounter() << endl;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200310 }
311
312 ofmIndex++;
313 }
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200314 } catch (Exception &e) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200315 cerr << "Error: " << e.what() << endl;
316 return 1;
317 }
318
319 return 0;
320}