Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame^] | 2 | * Copyright (c) 2020-2021 Arm Limited. All rights reserved. |
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: |
| 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 |
| 101 | * @shramSize: SHRAM size |
| 102 | * @customDma: Custom DMA enabled |
| 103 | */ |
| 104 | struct HardwareConfiguration { |
| 105 | public: |
| 106 | HardwareConfiguration(uint32_t _macsPerClockCycle, |
| 107 | uint32_t _cmdStreamVersion, |
| 108 | uint32_t _shramSize, |
| 109 | bool _customDma) : |
| 110 | macsPerClockCycle(_macsPerClockCycle), |
| 111 | cmdStreamVersion(_cmdStreamVersion), shramSize(_shramSize), customDma(_customDma) {} |
| 112 | |
| 113 | uint32_t macsPerClockCycle; |
| 114 | uint32_t cmdStreamVersion; |
| 115 | uint32_t shramSize; |
| 116 | bool customDma; |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * Device capabilities |
| 121 | * @hwId: Hardware |
| 122 | * @driver: Driver revision |
| 123 | * @hwCfg Hardware configuration |
| 124 | */ |
| 125 | class Capabilities { |
| 126 | public: |
| 127 | Capabilities(const HardwareId &_hwId, const HardwareConfiguration &_hwCfg, const SemanticVersion &_driver) : |
| 128 | hwId(_hwId), hwCfg(_hwCfg), driver(_driver) {} |
| 129 | |
| 130 | HardwareId hwId; |
| 131 | HardwareConfiguration hwCfg; |
| 132 | SemanticVersion driver; |
| 133 | }; |
| 134 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 135 | class Device { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 136 | public: |
| 137 | Device(const char *device = "/dev/ethosu0"); |
| 138 | virtual ~Device(); |
| 139 | |
| 140 | int ioctl(unsigned long cmd, void *data = nullptr); |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame^] | 141 | Capabilities capabilities(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 142 | |
| 143 | private: |
| 144 | int fd; |
| 145 | }; |
| 146 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 147 | class Buffer { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 148 | public: |
| 149 | Buffer(Device &device, const size_t capacity); |
| 150 | virtual ~Buffer(); |
| 151 | |
| 152 | size_t capacity() const; |
| 153 | void clear(); |
| 154 | char *data(); |
| 155 | void resize(size_t size, size_t offset = 0); |
| 156 | size_t offset() const; |
| 157 | size_t size() const; |
| 158 | |
| 159 | int getFd() const; |
| 160 | |
| 161 | private: |
| 162 | int fd; |
| 163 | char *dataPtr; |
| 164 | const size_t dataCapacity; |
| 165 | }; |
| 166 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 167 | class Network { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 168 | public: |
| 169 | Network(Device &device, std::shared_ptr<Buffer> &buffer); |
| 170 | virtual ~Network(); |
| 171 | |
| 172 | int ioctl(unsigned long cmd, void *data = nullptr); |
| 173 | std::shared_ptr<Buffer> getBuffer(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 174 | const std::vector<size_t> &getIfmDims() const; |
| 175 | size_t getIfmSize() const; |
| 176 | const std::vector<size_t> &getOfmDims() const; |
| 177 | size_t getOfmSize() const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 178 | |
| 179 | private: |
| 180 | int fd; |
| 181 | std::shared_ptr<Buffer> buffer; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 182 | std::vector<size_t> ifmDims; |
| 183 | std::vector<size_t> ofmDims; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 186 | class Inference { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 187 | public: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 188 | template <typename T> |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 189 | Inference(std::shared_ptr<Network> &network, |
| 190 | const T &ifmBegin, |
| 191 | const T &ifmEnd, |
| 192 | const T &ofmBegin, |
| 193 | const T &ofmEnd) : |
| 194 | network(network) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 195 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 196 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 197 | std::vector<uint32_t> counterConfigs = initializeCounterConfig(); |
| 198 | |
| 199 | create(counterConfigs, false); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 200 | } |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 201 | template <typename T, typename U> |
| 202 | Inference(std::shared_ptr<Network> &network, |
| 203 | const T &ifmBegin, |
| 204 | const T &ifmEnd, |
| 205 | const T &ofmBegin, |
| 206 | const T &ofmEnd, |
| 207 | const U &counters, |
| 208 | bool enableCycleCounter) : |
| 209 | network(network) { |
| 210 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 211 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
| 212 | std::vector<uint32_t> counterConfigs = initializeCounterConfig(); |
| 213 | |
| 214 | if (counters.size() > counterConfigs.size()) |
| 215 | throw EthosU::Exception("PMU Counters argument to large."); |
| 216 | |
| 217 | std::copy(counters.begin(), counters.end(), counterConfigs.begin()); |
| 218 | create(counterConfigs, enableCycleCounter); |
| 219 | } |
| 220 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 221 | virtual ~Inference(); |
| 222 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 223 | int wait(int timeoutSec = -1); |
| 224 | const std::vector<uint32_t> getPmuCounters(); |
| 225 | uint64_t getCycleCounter(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 226 | bool failed(); |
| 227 | int getFd(); |
| 228 | std::shared_ptr<Network> getNetwork(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 229 | std::vector<std::shared_ptr<Buffer>> &getIfmBuffers(); |
| 230 | std::vector<std::shared_ptr<Buffer>> &getOfmBuffers(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 231 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 232 | static uint32_t getMaxPmuEventCounters(); |
| 233 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 234 | private: |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 235 | void create(std::vector<uint32_t> &counterConfigs, bool enableCycleCounter); |
| 236 | std::vector<uint32_t> initializeCounterConfig(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 237 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 238 | int fd; |
| 239 | std::shared_ptr<Network> network; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 240 | std::vector<std::shared_ptr<Buffer>> ifmBuffers; |
| 241 | std::vector<std::shared_ptr<Buffer>> ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 242 | }; |
| 243 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 244 | } // namespace EthosU |