Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
Mikael Olsson | 308e7f1 | 2023-06-12 15:00:55 +0200 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2020-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 3 | * 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 | |
| 18 | #pragma once |
| 19 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 20 | #include <algorithm> |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 21 | #include <iostream> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <string> |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 24 | #include <vector> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 25 | |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 26 | /* |
| 27 | *The following undef are necessary to avoid clash with macros in GNU C Library |
| 28 | * if removed the following warning/error are produced: |
| 29 | * |
| 30 | * In the GNU C Library, "major" ("minor") is defined |
| 31 | * by <sys/sysmacros.h>. For historical compatibility, it is |
| 32 | * currently defined by <sys/types.h> as well, but we plan to |
| 33 | * remove this soon. To use "major" ("minor"), include <sys/sysmacros.h> |
| 34 | * directly. If you did not intend to use a system-defined macro |
| 35 | * "major" ("minor"), you should undefine it after including <sys/types.h>. |
| 36 | */ |
| 37 | #undef major |
| 38 | #undef minor |
| 39 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 40 | namespace EthosU { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 41 | |
Mikael Olsson | c081e59 | 2023-10-30 11:10:56 +0100 | [diff] [blame] | 42 | constexpr uint32_t DRIVER_LIBRARY_VERSION_MAJOR = 3; |
Mikael Olsson | e9c3f07 | 2023-06-12 15:58:10 +0200 | [diff] [blame] | 43 | constexpr uint32_t DRIVER_LIBRARY_VERSION_MINOR = 0; |
| 44 | constexpr uint32_t DRIVER_LIBRARY_VERSION_PATCH = 0; |
| 45 | |
Mikael Olsson | c081e59 | 2023-10-30 11:10:56 +0100 | [diff] [blame] | 46 | constexpr uint32_t MAX_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION = 3; |
| 47 | constexpr uint32_t MIN_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION = 3; |
Mikael Olsson | 308e7f1 | 2023-06-12 15:00:55 +0200 | [diff] [blame] | 48 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 49 | class Exception : public std::exception { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 50 | public: |
| 51 | Exception(const char *msg); |
| 52 | virtual ~Exception() throw(); |
| 53 | virtual const char *what() const throw(); |
| 54 | |
| 55 | private: |
| 56 | std::string msg; |
| 57 | }; |
| 58 | |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 59 | /** |
| 60 | * Sematic Version : major.minor.patch |
| 61 | */ |
| 62 | class SemanticVersion { |
| 63 | public: |
| 64 | SemanticVersion(uint32_t _major = 0, uint32_t _minor = 0, uint32_t _patch = 0) : |
| 65 | major(_major), minor(_minor), patch(_patch){}; |
| 66 | |
| 67 | bool operator==(const SemanticVersion &other); |
| 68 | bool operator<(const SemanticVersion &other); |
| 69 | bool operator<=(const SemanticVersion &other); |
| 70 | bool operator!=(const SemanticVersion &other); |
| 71 | bool operator>(const SemanticVersion &other); |
| 72 | bool operator>=(const SemanticVersion &other); |
| 73 | |
| 74 | uint32_t major; |
| 75 | uint32_t minor; |
| 76 | uint32_t patch; |
| 77 | }; |
| 78 | |
| 79 | std::ostream &operator<<(std::ostream &out, const SemanticVersion &v); |
| 80 | |
Mikael Olsson | e9c3f07 | 2023-06-12 15:58:10 +0200 | [diff] [blame] | 81 | const SemanticVersion getLibraryVersion(); |
| 82 | |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 83 | /* |
| 84 | * Hardware Identifier |
| 85 | * @versionStatus: Version status |
| 86 | * @version: Version revision |
| 87 | * @product: Product revision |
| 88 | * @architecture: Architecture revison |
| 89 | */ |
| 90 | struct HardwareId { |
| 91 | public: |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 92 | HardwareId(uint32_t _versionStatus = 0, |
| 93 | const SemanticVersion &_version = SemanticVersion(), |
| 94 | const SemanticVersion &_product = SemanticVersion(), |
| 95 | const SemanticVersion &_architecture = SemanticVersion()) : |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 96 | versionStatus(_versionStatus), |
| 97 | version(_version), product(_product), architecture(_architecture) {} |
| 98 | |
| 99 | uint32_t versionStatus; |
| 100 | SemanticVersion version; |
| 101 | SemanticVersion product; |
| 102 | SemanticVersion architecture; |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * Hardware Configuration |
| 107 | * @macsPerClockCycle: MACs per clock cycle |
| 108 | * @cmdStreamVersion: NPU command stream version |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 109 | * @customDma: Custom DMA enabled |
| 110 | */ |
| 111 | struct HardwareConfiguration { |
| 112 | public: |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 113 | HardwareConfiguration(uint32_t _macsPerClockCycle = 0, uint32_t _cmdStreamVersion = 0, bool _customDma = false) : |
Davide Grohmann | 40e23d1 | 2021-06-17 09:59:40 +0200 | [diff] [blame] | 114 | macsPerClockCycle(_macsPerClockCycle), cmdStreamVersion(_cmdStreamVersion), customDma(_customDma) {} |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 115 | |
| 116 | uint32_t macsPerClockCycle; |
| 117 | uint32_t cmdStreamVersion; |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 118 | bool customDma; |
| 119 | }; |
| 120 | |
| 121 | /** |
| 122 | * Device capabilities |
| 123 | * @hwId: Hardware |
| 124 | * @driver: Driver revision |
| 125 | * @hwCfg Hardware configuration |
| 126 | */ |
| 127 | class Capabilities { |
| 128 | public: |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 129 | Capabilities() {} |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 130 | Capabilities(const HardwareId &_hwId, const HardwareConfiguration &_hwCfg, const SemanticVersion &_driver) : |
| 131 | hwId(_hwId), hwCfg(_hwCfg), driver(_driver) {} |
| 132 | |
| 133 | HardwareId hwId; |
| 134 | HardwareConfiguration hwCfg; |
| 135 | SemanticVersion driver; |
| 136 | }; |
| 137 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 138 | class Device { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 139 | public: |
| 140 | Device(const char *device = "/dev/ethosu0"); |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 141 | virtual ~Device() noexcept(false); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 142 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 143 | int ioctl(unsigned long cmd, void *data = nullptr) const; |
| 144 | Capabilities capabilities() const; |
Mikael Olsson | 308e7f1 | 2023-06-12 15:00:55 +0200 | [diff] [blame] | 145 | const SemanticVersion &getDriverVersion() const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 146 | |
| 147 | private: |
| 148 | int fd; |
Mikael Olsson | 308e7f1 | 2023-06-12 15:00:55 +0200 | [diff] [blame] | 149 | SemanticVersion driverVersion; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 152 | class Buffer { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 153 | public: |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 154 | Buffer(const Device &device, const size_t size); |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 155 | virtual ~Buffer() noexcept(false); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 156 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 157 | void clear() const; |
| 158 | char *data() const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 159 | size_t size() const; |
| 160 | |
| 161 | int getFd() const; |
| 162 | |
| 163 | private: |
| 164 | int fd; |
| 165 | char *dataPtr; |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 166 | const size_t dataSize; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 167 | }; |
| 168 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 169 | class Network { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 170 | public: |
Mikael Olsson | c081e59 | 2023-10-30 11:10:56 +0100 | [diff] [blame] | 171 | Network(const Device &device, const unsigned char *networkData, size_t networkSize); |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 172 | Network(const Device &device, const unsigned index); |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 173 | virtual ~Network() noexcept(false); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 174 | |
| 175 | int ioctl(unsigned long cmd, void *data = nullptr); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 176 | const std::vector<size_t> &getIfmDims() const; |
| 177 | size_t getIfmSize() const; |
| 178 | const std::vector<size_t> &getOfmDims() const; |
| 179 | size_t getOfmSize() const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 180 | |
| 181 | private: |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 182 | void collectNetworkInfo(); |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 183 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 184 | int fd; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 185 | std::vector<size_t> ifmDims; |
| 186 | std::vector<size_t> ofmDims; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 187 | }; |
| 188 | |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 189 | enum class InferenceStatus { |
| 190 | OK, |
| 191 | ERROR, |
| 192 | RUNNING, |
| 193 | REJECTED, |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 194 | ABORTED, |
| 195 | ABORTING, |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | std::ostream &operator<<(std::ostream &out, const InferenceStatus &v); |
| 199 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 200 | class Inference { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 201 | public: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 202 | template <typename T> |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 203 | Inference(const std::shared_ptr<Network> &network, |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 204 | const T &ifmBegin, |
| 205 | const T &ifmEnd, |
| 206 | const T &ofmBegin, |
| 207 | const T &ofmEnd) : |
| 208 | network(network) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 209 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 210 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 211 | std::vector<uint32_t> counterConfigs = initializeCounterConfig(); |
| 212 | |
| 213 | create(counterConfigs, false); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 214 | } |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 215 | template <typename T, typename U> |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 216 | Inference(const std::shared_ptr<Network> &network, |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 217 | const T &ifmBegin, |
| 218 | const T &ifmEnd, |
| 219 | const T &ofmBegin, |
| 220 | const T &ofmEnd, |
| 221 | const U &counters, |
| 222 | bool enableCycleCounter) : |
| 223 | network(network) { |
| 224 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 225 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
| 226 | std::vector<uint32_t> counterConfigs = initializeCounterConfig(); |
| 227 | |
| 228 | if (counters.size() > counterConfigs.size()) |
| 229 | throw EthosU::Exception("PMU Counters argument to large."); |
| 230 | |
| 231 | std::copy(counters.begin(), counters.end(), counterConfigs.begin()); |
| 232 | create(counterConfigs, enableCycleCounter); |
| 233 | } |
| 234 | |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 235 | virtual ~Inference() noexcept(false); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 236 | |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 237 | bool wait(int64_t timeoutNanos = -1) const; |
Mikael Olsson | e87446c | 2023-12-15 17:17:06 +0100 | [diff] [blame^] | 238 | const std::vector<uint64_t> getPmuCounters() const; |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 239 | uint64_t getCycleCounter() const; |
Davide Grohmann | 7e8f508 | 2022-03-23 12:48:45 +0100 | [diff] [blame] | 240 | bool cancel() const; |
Davide Grohmann | 82d2258 | 2022-04-25 12:52:38 +0200 | [diff] [blame] | 241 | InferenceStatus status() const; |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 242 | int getFd() const; |
| 243 | const std::shared_ptr<Network> getNetwork() const; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 244 | std::vector<std::shared_ptr<Buffer>> &getIfmBuffers(); |
| 245 | std::vector<std::shared_ptr<Buffer>> &getOfmBuffers(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 246 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 247 | static uint32_t getMaxPmuEventCounters(); |
| 248 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 249 | private: |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 250 | void create(std::vector<uint32_t> &counterConfigs, bool enableCycleCounter); |
| 251 | std::vector<uint32_t> initializeCounterConfig(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 252 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 253 | int fd; |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 254 | const std::shared_ptr<Network> network; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 255 | std::vector<std::shared_ptr<Buffer>> ifmBuffers; |
| 256 | std::vector<std::shared_ptr<Buffer>> ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 257 | }; |
| 258 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 259 | } // namespace EthosU |