blob: d70ec950072b7bdd085d260be98fc877979e85b9 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Davide Grohmann35ce6c82021-06-01 15:03:51 +02002 * Copyright (c) 2020-2021 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
19#pragma once
20
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020021#include <algorithm>
Davide Grohmann35ce6c82021-06-01 15:03:51 +020022#include <iostream>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020023#include <memory>
24#include <string>
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020025#include <vector>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020026
Davide Grohmann35ce6c82021-06-01 15:03:51 +020027/*
28 *The following undef are necessary to avoid clash with macros in GNU C Library
29 * if removed the following warning/error are produced:
30 *
31 * In the GNU C Library, "major" ("minor") is defined
32 * by <sys/sysmacros.h>. For historical compatibility, it is
33 * currently defined by <sys/types.h> as well, but we plan to
34 * remove this soon. To use "major" ("minor"), include <sys/sysmacros.h>
35 * directly. If you did not intend to use a system-defined macro
36 * "major" ("minor"), you should undefine it after including <sys/types.h>.
37 */
38#undef major
39#undef minor
40
Kristofer Jonsson79689c52020-10-16 14:42:19 +020041namespace EthosU {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020042
Kristofer Jonsson79689c52020-10-16 14:42:19 +020043class Exception : public std::exception {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020044public:
45 Exception(const char *msg);
46 virtual ~Exception() throw();
47 virtual const char *what() const throw();
48
49private:
50 std::string msg;
51};
52
Davide Grohmann35ce6c82021-06-01 15:03:51 +020053/**
54 * Sematic Version : major.minor.patch
55 */
56class SemanticVersion {
57public:
58 SemanticVersion(uint32_t _major = 0, uint32_t _minor = 0, uint32_t _patch = 0) :
59 major(_major), minor(_minor), patch(_patch){};
60
61 bool operator==(const SemanticVersion &other);
62 bool operator<(const SemanticVersion &other);
63 bool operator<=(const SemanticVersion &other);
64 bool operator!=(const SemanticVersion &other);
65 bool operator>(const SemanticVersion &other);
66 bool operator>=(const SemanticVersion &other);
67
68 uint32_t major;
69 uint32_t minor;
70 uint32_t patch;
71};
72
73std::ostream &operator<<(std::ostream &out, const SemanticVersion &v);
74
75/*
76 * Hardware Identifier
77 * @versionStatus: Version status
78 * @version: Version revision
79 * @product: Product revision
80 * @architecture: Architecture revison
81 */
82struct HardwareId {
83public:
84 HardwareId(uint32_t _versionStatus,
85 const SemanticVersion &_version,
86 const SemanticVersion &_product,
87 const SemanticVersion &_architecture) :
88 versionStatus(_versionStatus),
89 version(_version), product(_product), architecture(_architecture) {}
90
91 uint32_t versionStatus;
92 SemanticVersion version;
93 SemanticVersion product;
94 SemanticVersion architecture;
95};
96
97/*
98 * Hardware Configuration
99 * @macsPerClockCycle: MACs per clock cycle
100 * @cmdStreamVersion: NPU command stream version
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200101 * @customDma: Custom DMA enabled
102 */
103struct HardwareConfiguration {
104public:
Davide Grohmann40e23d12021-06-17 09:59:40 +0200105 HardwareConfiguration(uint32_t _macsPerClockCycle, uint32_t _cmdStreamVersion, bool _customDma) :
106 macsPerClockCycle(_macsPerClockCycle), cmdStreamVersion(_cmdStreamVersion), customDma(_customDma) {}
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200107
108 uint32_t macsPerClockCycle;
109 uint32_t cmdStreamVersion;
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200110 bool customDma;
111};
112
113/**
114 * Device capabilities
115 * @hwId: Hardware
116 * @driver: Driver revision
117 * @hwCfg Hardware configuration
118 */
119class Capabilities {
120public:
121 Capabilities(const HardwareId &_hwId, const HardwareConfiguration &_hwCfg, const SemanticVersion &_driver) :
122 hwId(_hwId), hwCfg(_hwCfg), driver(_driver) {}
123
124 HardwareId hwId;
125 HardwareConfiguration hwCfg;
126 SemanticVersion driver;
127};
128
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200129class Device {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200130public:
131 Device(const char *device = "/dev/ethosu0");
132 virtual ~Device();
133
134 int ioctl(unsigned long cmd, void *data = nullptr);
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200135 Capabilities capabilities();
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200136
137private:
138 int fd;
139};
140
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200141class Buffer {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200142public:
143 Buffer(Device &device, const size_t capacity);
144 virtual ~Buffer();
145
146 size_t capacity() const;
147 void clear();
148 char *data();
149 void resize(size_t size, size_t offset = 0);
150 size_t offset() const;
151 size_t size() const;
152
153 int getFd() const;
154
155private:
156 int fd;
157 char *dataPtr;
158 const size_t dataCapacity;
159};
160
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200161class Network {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200162public:
163 Network(Device &device, std::shared_ptr<Buffer> &buffer);
164 virtual ~Network();
165
166 int ioctl(unsigned long cmd, void *data = nullptr);
167 std::shared_ptr<Buffer> getBuffer();
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200168 const std::vector<size_t> &getIfmDims() const;
169 size_t getIfmSize() const;
170 const std::vector<size_t> &getOfmDims() const;
171 size_t getOfmSize() const;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200172
173private:
174 int fd;
175 std::shared_ptr<Buffer> buffer;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200176 std::vector<size_t> ifmDims;
177 std::vector<size_t> ofmDims;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200178};
179
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200180class Inference {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200181public:
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200182 template <typename T>
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200183 Inference(std::shared_ptr<Network> &network,
184 const T &ifmBegin,
185 const T &ifmEnd,
186 const T &ofmBegin,
187 const T &ofmEnd) :
188 network(network) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200189 std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers));
190 std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers));
Per Åstrand2354d3e2020-10-24 20:17:10 +0200191 std::vector<uint32_t> counterConfigs = initializeCounterConfig();
192
193 create(counterConfigs, false);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200194 }
Per Åstrand2354d3e2020-10-24 20:17:10 +0200195 template <typename T, typename U>
196 Inference(std::shared_ptr<Network> &network,
197 const T &ifmBegin,
198 const T &ifmEnd,
199 const T &ofmBegin,
200 const T &ofmEnd,
201 const U &counters,
202 bool enableCycleCounter) :
203 network(network) {
204 std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers));
205 std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers));
206 std::vector<uint32_t> counterConfigs = initializeCounterConfig();
207
208 if (counters.size() > counterConfigs.size())
209 throw EthosU::Exception("PMU Counters argument to large.");
210
211 std::copy(counters.begin(), counters.end(), counterConfigs.begin());
212 create(counterConfigs, enableCycleCounter);
213 }
214
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200215 virtual ~Inference();
216
Per Åstrand2354d3e2020-10-24 20:17:10 +0200217 int wait(int timeoutSec = -1);
218 const std::vector<uint32_t> getPmuCounters();
219 uint64_t getCycleCounter();
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200220 bool failed();
221 int getFd();
222 std::shared_ptr<Network> getNetwork();
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200223 std::vector<std::shared_ptr<Buffer>> &getIfmBuffers();
224 std::vector<std::shared_ptr<Buffer>> &getOfmBuffers();
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200225
Per Åstrand2354d3e2020-10-24 20:17:10 +0200226 static uint32_t getMaxPmuEventCounters();
227
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200228private:
Per Åstrand2354d3e2020-10-24 20:17:10 +0200229 void create(std::vector<uint32_t> &counterConfigs, bool enableCycleCounter);
230 std::vector<uint32_t> initializeCounterConfig();
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200231
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200232 int fd;
233 std::shared_ptr<Network> network;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200234 std::vector<std::shared_ptr<Buffer>> ifmBuffers;
235 std::vector<std::shared_ptr<Buffer>> ofmBuffers;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200236};
237
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200238} // namespace EthosU