Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 2 | * Copyright (c) 2020-2022 Arm Limited. |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 3 | * |
| 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 Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 21 | #include <algorithm> |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 22 | #include <iostream> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 23 | #include <memory> |
| 24 | #include <string> |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 25 | #include <vector> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 26 | |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 27 | /* |
| 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 Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 41 | namespace EthosU { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 42 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 43 | class Exception : public std::exception { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 44 | public: |
| 45 | Exception(const char *msg); |
| 46 | virtual ~Exception() throw(); |
| 47 | virtual const char *what() const throw(); |
| 48 | |
| 49 | private: |
| 50 | std::string msg; |
| 51 | }; |
| 52 | |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 53 | /** |
| 54 | * Sematic Version : major.minor.patch |
| 55 | */ |
| 56 | class SemanticVersion { |
| 57 | public: |
| 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 | |
| 73 | std::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 | */ |
| 82 | struct HardwareId { |
| 83 | public: |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame^] | 84 | HardwareId(uint32_t _versionStatus = 0, |
| 85 | const SemanticVersion &_version = SemanticVersion(), |
| 86 | const SemanticVersion &_product = SemanticVersion(), |
| 87 | const SemanticVersion &_architecture = SemanticVersion()) : |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 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 Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 101 | * @customDma: Custom DMA enabled |
| 102 | */ |
| 103 | struct HardwareConfiguration { |
| 104 | public: |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame^] | 105 | HardwareConfiguration(uint32_t _macsPerClockCycle = 0, uint32_t _cmdStreamVersion = 0, bool _customDma = false) : |
Davide Grohmann | 40e23d1 | 2021-06-17 09:59:40 +0200 | [diff] [blame] | 106 | macsPerClockCycle(_macsPerClockCycle), cmdStreamVersion(_cmdStreamVersion), customDma(_customDma) {} |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 107 | |
| 108 | uint32_t macsPerClockCycle; |
| 109 | uint32_t cmdStreamVersion; |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 110 | bool customDma; |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * Device capabilities |
| 115 | * @hwId: Hardware |
| 116 | * @driver: Driver revision |
| 117 | * @hwCfg Hardware configuration |
| 118 | */ |
| 119 | class Capabilities { |
| 120 | public: |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame^] | 121 | Capabilities() {} |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 122 | Capabilities(const HardwareId &_hwId, const HardwareConfiguration &_hwCfg, const SemanticVersion &_driver) : |
| 123 | hwId(_hwId), hwCfg(_hwCfg), driver(_driver) {} |
| 124 | |
| 125 | HardwareId hwId; |
| 126 | HardwareConfiguration hwCfg; |
| 127 | SemanticVersion driver; |
| 128 | }; |
| 129 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 130 | class Device { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 131 | public: |
| 132 | Device(const char *device = "/dev/ethosu0"); |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 133 | virtual ~Device() noexcept(false); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 134 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 135 | int ioctl(unsigned long cmd, void *data = nullptr) const; |
| 136 | Capabilities capabilities() const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 137 | |
| 138 | private: |
| 139 | int fd; |
| 140 | }; |
| 141 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 142 | class Buffer { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 143 | public: |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 144 | Buffer(const Device &device, const size_t capacity); |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 145 | virtual ~Buffer() noexcept(false); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 146 | |
| 147 | size_t capacity() const; |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 148 | void clear() const; |
| 149 | char *data() const; |
| 150 | void resize(size_t size, size_t offset = 0) const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 151 | size_t offset() const; |
| 152 | size_t size() const; |
| 153 | |
| 154 | int getFd() const; |
| 155 | |
| 156 | private: |
| 157 | int fd; |
| 158 | char *dataPtr; |
| 159 | const size_t dataCapacity; |
| 160 | }; |
| 161 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 162 | class Network { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 163 | public: |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 164 | Network(const Device &device, std::shared_ptr<Buffer> &buffer); |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 165 | Network(const Device &device, const unsigned index); |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 166 | virtual ~Network() noexcept(false); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 167 | |
| 168 | int ioctl(unsigned long cmd, void *data = nullptr); |
| 169 | std::shared_ptr<Buffer> getBuffer(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 170 | const std::vector<size_t> &getIfmDims() const; |
| 171 | size_t getIfmSize() const; |
| 172 | const std::vector<size_t> &getOfmDims() const; |
| 173 | size_t getOfmSize() const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 174 | |
| 175 | private: |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 176 | void collectNetworkInfo(); |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 177 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 178 | int fd; |
| 179 | std::shared_ptr<Buffer> buffer; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 180 | std::vector<size_t> ifmDims; |
| 181 | std::vector<size_t> ofmDims; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 182 | }; |
| 183 | |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 184 | enum class InferenceStatus { |
| 185 | OK, |
| 186 | ERROR, |
| 187 | RUNNING, |
| 188 | REJECTED, |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 189 | ABORTED, |
| 190 | ABORTING, |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | std::ostream &operator<<(std::ostream &out, const InferenceStatus &v); |
| 194 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 195 | class Inference { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 196 | public: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 197 | template <typename T> |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 198 | Inference(const std::shared_ptr<Network> &network, |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 199 | const T &ifmBegin, |
| 200 | const T &ifmEnd, |
| 201 | const T &ofmBegin, |
| 202 | const T &ofmEnd) : |
| 203 | network(network) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 204 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 205 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 206 | std::vector<uint32_t> counterConfigs = initializeCounterConfig(); |
| 207 | |
| 208 | create(counterConfigs, false); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 209 | } |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 210 | template <typename T, typename U> |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 211 | Inference(const std::shared_ptr<Network> &network, |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 212 | const T &ifmBegin, |
| 213 | const T &ifmEnd, |
| 214 | const T &ofmBegin, |
| 215 | const T &ofmEnd, |
| 216 | const U &counters, |
| 217 | bool enableCycleCounter) : |
| 218 | network(network) { |
| 219 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 220 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
| 221 | std::vector<uint32_t> counterConfigs = initializeCounterConfig(); |
| 222 | |
| 223 | if (counters.size() > counterConfigs.size()) |
| 224 | throw EthosU::Exception("PMU Counters argument to large."); |
| 225 | |
| 226 | std::copy(counters.begin(), counters.end(), counterConfigs.begin()); |
| 227 | create(counterConfigs, enableCycleCounter); |
| 228 | } |
| 229 | |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 230 | virtual ~Inference() noexcept(false); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 231 | |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 232 | bool wait(int64_t timeoutNanos = -1) const; |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 233 | const std::vector<uint32_t> getPmuCounters() const; |
| 234 | uint64_t getCycleCounter() const; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 235 | bool cancel() const; |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 236 | InferenceStatus status() const; |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 237 | int getFd() const; |
| 238 | const std::shared_ptr<Network> getNetwork() const; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 239 | std::vector<std::shared_ptr<Buffer>> &getIfmBuffers(); |
| 240 | std::vector<std::shared_ptr<Buffer>> &getOfmBuffers(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 241 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 242 | static uint32_t getMaxPmuEventCounters(); |
| 243 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 244 | private: |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 245 | void create(std::vector<uint32_t> &counterConfigs, bool enableCycleCounter); |
| 246 | std::vector<uint32_t> initializeCounterConfig(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 247 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 248 | int fd; |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 249 | const std::shared_ptr<Network> network; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 250 | std::vector<std::shared_ptr<Buffer>> ifmBuffers; |
| 251 | std::vector<std::shared_ptr<Buffer>> ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 252 | }; |
| 253 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 254 | } // namespace EthosU |