Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
Anton Moberg | 4e5e056 | 2021-03-31 11:11:36 +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 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 19 | #include "autogen/tflite_schema.hpp" |
| 20 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 21 | #include <ethosu.hpp> |
| 22 | #include <uapi/ethosu.h> |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <exception> |
| 26 | #include <iostream> |
| 27 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 28 | #include <fcntl.h> |
| 29 | #include <poll.h> |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 30 | #include <signal.h> |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 31 | #include <sys/ioctl.h> |
| 32 | #include <sys/mman.h> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | using namespace std; |
| 36 | |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 37 | namespace EthosU { |
| 38 | __attribute__((weak)) int eioctl(int fd, unsigned long cmd, void *data = nullptr) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 39 | int ret = ::ioctl(fd, cmd, data); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 40 | if (ret < 0) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 41 | throw EthosU::Exception("IOCTL failed"); |
| 42 | } |
| 43 | |
| 44 | return ret; |
| 45 | } |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 46 | |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 47 | __attribute__((weak)) int eopen(const char *pathname, int flags) { |
| 48 | int fd = ::open(pathname, flags); |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 49 | if (fd < 0) { |
| 50 | throw Exception("Failed to open device"); |
| 51 | } |
| 52 | |
| 53 | return fd; |
| 54 | } |
| 55 | |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 56 | __attribute__((weak)) int |
| 57 | eppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, const sigset_t *sigmask) { |
| 58 | int result = ::ppoll(fds, nfds, tmo_p, sigmask); |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 59 | if (result < 0) { |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 60 | throw Exception("Failed to wait for ppoll event or signal"); |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return result; |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | __attribute__((weak)) int eclose(int fd) { |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 67 | int result = ::close(fd); |
| 68 | if (result < 0) { |
| 69 | throw Exception("Failed to close file"); |
| 70 | } |
| 71 | |
| 72 | return result; |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 73 | } |
| 74 | __attribute((weak)) void *emmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 75 | void *ptr = ::mmap(addr, length, prot, flags, fd, offset); |
| 76 | if (ptr == MAP_FAILED) { |
| 77 | throw Exception("Failed to mmap file"); |
| 78 | } |
| 79 | |
| 80 | return ptr; |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | __attribute__((weak)) int emunmap(void *addr, size_t length) { |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 84 | int result = ::munmap(addr, length); |
| 85 | if (result < 0) { |
| 86 | throw Exception("Failed to munmap file"); |
| 87 | } |
| 88 | |
| 89 | return result; |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } // namespace EthosU |
| 93 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 94 | /**************************************************************************** |
| 95 | * TFL micro helpers |
| 96 | ****************************************************************************/ |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 97 | namespace { |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 98 | size_t getShapeSize(const flatbuffers::Vector<int32_t> *shape) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 99 | size_t size = 1; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 100 | |
Jonny Svärd | 2c01713 | 2021-04-12 15:56:44 +0200 | [diff] [blame] | 101 | if (shape == nullptr) { |
| 102 | throw EthosU::Exception("getShapeSize(): nullptr arg"); |
| 103 | } |
| 104 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 105 | for (auto it = shape->begin(); it != shape->end(); ++it) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 106 | size *= *it; |
| 107 | } |
| 108 | |
| 109 | return size; |
| 110 | } |
| 111 | |
Kristofer Jonsson | 5e632c2 | 2020-11-25 10:58:38 +0100 | [diff] [blame] | 112 | size_t getTensorTypeSize(const enum tflite::TensorType type) { |
| 113 | switch (type) { |
| 114 | case tflite::TensorType::TensorType_UINT8: |
| 115 | case tflite::TensorType::TensorType_INT8: |
| 116 | return 1; |
| 117 | case tflite::TensorType::TensorType_INT16: |
| 118 | return 2; |
Henrik Hoglind | 3db0ffa | 2021-11-23 11:55:48 +0100 | [diff] [blame] | 119 | case tflite::TensorType::TensorType_INT32: |
Henrik Hoglind | 031f4c1 | 2021-03-25 08:32:23 +0100 | [diff] [blame] | 120 | case tflite::TensorType::TensorType_FLOAT32: |
| 121 | return 4; |
Kristofer Jonsson | 5e632c2 | 2020-11-25 10:58:38 +0100 | [diff] [blame] | 122 | default: |
| 123 | throw EthosU::Exception("Unsupported tensor type"); |
| 124 | } |
| 125 | } |
| 126 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 127 | vector<size_t> getSubGraphDims(const tflite::SubGraph *subgraph, const flatbuffers::Vector<int32_t> *tensorMap) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 128 | vector<size_t> dims; |
| 129 | |
Jonny Svärd | 2c01713 | 2021-04-12 15:56:44 +0200 | [diff] [blame] | 130 | if (subgraph == nullptr || tensorMap == nullptr) { |
| 131 | throw EthosU::Exception("getSubGraphDims(): nullptr arg(s)"); |
| 132 | } |
| 133 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 134 | for (auto index = tensorMap->begin(); index != tensorMap->end(); ++index) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 135 | auto tensor = subgraph->tensors()->Get(*index); |
| 136 | size_t size = getShapeSize(tensor->shape()); |
Kristofer Jonsson | 5e632c2 | 2020-11-25 10:58:38 +0100 | [diff] [blame] | 137 | size *= getTensorTypeSize(tensor->type()); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 138 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 139 | if (size > 0) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 140 | dims.push_back(size); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return dims; |
| 145 | } |
| 146 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 147 | } // namespace |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 148 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 149 | namespace EthosU { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 150 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 151 | /**************************************************************************** |
| 152 | * Exception |
| 153 | ****************************************************************************/ |
| 154 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 155 | Exception::Exception(const char *msg) : msg(msg) {} |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 156 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 157 | Exception::~Exception() throw() {} |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 158 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 159 | const char *Exception::what() const throw() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 160 | return msg.c_str(); |
| 161 | } |
| 162 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 163 | /**************************************************************************** |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 164 | * Semantic Version |
| 165 | ****************************************************************************/ |
| 166 | |
| 167 | bool SemanticVersion::operator==(const SemanticVersion &other) { |
| 168 | return other.major == major && other.minor == minor && other.patch == patch; |
| 169 | } |
| 170 | |
| 171 | bool SemanticVersion::operator<(const SemanticVersion &other) { |
| 172 | if (other.major > major) |
| 173 | return true; |
| 174 | if (other.minor > minor) |
| 175 | return true; |
| 176 | return other.patch > patch; |
| 177 | } |
| 178 | |
| 179 | bool SemanticVersion::operator<=(const SemanticVersion &other) { |
| 180 | return *this < other || *this == other; |
| 181 | } |
| 182 | |
| 183 | bool SemanticVersion::operator!=(const SemanticVersion &other) { |
| 184 | return !(*this == other); |
| 185 | } |
| 186 | |
| 187 | bool SemanticVersion::operator>(const SemanticVersion &other) { |
| 188 | return !(*this <= other); |
| 189 | } |
| 190 | |
| 191 | bool SemanticVersion::operator>=(const SemanticVersion &other) { |
| 192 | return !(*this < other); |
| 193 | } |
| 194 | |
| 195 | ostream &operator<<(ostream &out, const SemanticVersion &v) { |
| 196 | return out << "{ major=" << unsigned(v.major) << ", minor=" << unsigned(v.minor) << ", patch=" << unsigned(v.patch) |
| 197 | << " }"; |
| 198 | } |
| 199 | |
| 200 | /**************************************************************************** |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 201 | * Device |
| 202 | ****************************************************************************/ |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 203 | Device::Device(const char *device) { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 204 | fd = eopen(device, O_RDWR | O_NONBLOCK); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 207 | Device::~Device() { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 208 | eclose(fd); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 211 | int Device::ioctl(unsigned long cmd, void *data) const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 212 | return eioctl(fd, cmd, data); |
| 213 | } |
| 214 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 215 | Capabilities Device::capabilities() const { |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 216 | ethosu_uapi_device_capabilities uapi; |
| 217 | (void)eioctl(fd, ETHOSU_IOCTL_CAPABILITIES_REQ, static_cast<void *>(&uapi)); |
| 218 | |
| 219 | Capabilities capabilities( |
| 220 | HardwareId(uapi.hw_id.version_status, |
| 221 | SemanticVersion(uapi.hw_id.version_major, uapi.hw_id.version_minor), |
| 222 | SemanticVersion(uapi.hw_id.product_major), |
| 223 | SemanticVersion(uapi.hw_id.arch_major_rev, uapi.hw_id.arch_minor_rev, uapi.hw_id.arch_patch_rev)), |
Davide Grohmann | 40e23d1 | 2021-06-17 09:59:40 +0200 | [diff] [blame] | 224 | HardwareConfiguration(uapi.hw_cfg.macs_per_cc, uapi.hw_cfg.cmd_stream_version, bool(uapi.hw_cfg.custom_dma)), |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 225 | SemanticVersion(uapi.driver_major_rev, uapi.driver_minor_rev, uapi.driver_patch_rev)); |
| 226 | return capabilities; |
| 227 | } |
| 228 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 229 | /**************************************************************************** |
| 230 | * Buffer |
| 231 | ****************************************************************************/ |
| 232 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 233 | Buffer::Buffer(const Device &device, const size_t capacity) : fd(-1), dataPtr(nullptr), dataCapacity(capacity) { |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 234 | ethosu_uapi_buffer_create uapi = {static_cast<uint32_t>(dataCapacity)}; |
| 235 | fd = device.ioctl(ETHOSU_IOCTL_BUFFER_CREATE, static_cast<void *>(&uapi)); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 236 | |
Davide Grohmann | 2ba3c1d | 2021-11-08 15:08:48 +0100 | [diff] [blame] | 237 | void *d; |
| 238 | try { |
| 239 | d = emmap(nullptr, dataCapacity, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 240 | } catch (std::exception &e) { |
| 241 | try { |
| 242 | eclose(fd); |
| 243 | } catch (...) { std::throw_with_nested(e); } |
| 244 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 245 | dataPtr = reinterpret_cast<char *>(d); |
| 246 | } |
| 247 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 248 | Buffer::~Buffer() { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 249 | emunmap(dataPtr, dataCapacity); |
| 250 | eclose(fd); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 253 | size_t Buffer::capacity() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 254 | return dataCapacity; |
| 255 | } |
| 256 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 257 | void Buffer::clear() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 258 | resize(0, 0); |
| 259 | } |
| 260 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 261 | char *Buffer::data() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 262 | return dataPtr + offset(); |
| 263 | } |
| 264 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 265 | void Buffer::resize(size_t size, size_t offset) const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 266 | ethosu_uapi_buffer uapi; |
| 267 | uapi.offset = offset; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 268 | uapi.size = size; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 269 | eioctl(fd, ETHOSU_IOCTL_BUFFER_SET, static_cast<void *>(&uapi)); |
| 270 | } |
| 271 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 272 | size_t Buffer::offset() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 273 | ethosu_uapi_buffer uapi; |
| 274 | eioctl(fd, ETHOSU_IOCTL_BUFFER_GET, static_cast<void *>(&uapi)); |
| 275 | return uapi.offset; |
| 276 | } |
| 277 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 278 | size_t Buffer::size() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 279 | ethosu_uapi_buffer uapi; |
| 280 | eioctl(fd, ETHOSU_IOCTL_BUFFER_GET, static_cast<void *>(&uapi)); |
| 281 | return uapi.size; |
| 282 | } |
| 283 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 284 | int Buffer::getFd() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 285 | return fd; |
| 286 | } |
| 287 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 288 | /**************************************************************************** |
| 289 | * Network |
| 290 | ****************************************************************************/ |
| 291 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 292 | Network::Network(const Device &device, shared_ptr<Buffer> &buffer) : fd(-1), buffer(buffer) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 293 | // Create buffer handle |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 294 | ethosu_uapi_network_create uapi; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 295 | uapi.fd = buffer->getFd(); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 296 | fd = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, static_cast<void *>(&uapi)); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 297 | |
| 298 | // Create model handle |
| 299 | const tflite::Model *model = tflite::GetModel(reinterpret_cast<void *>(buffer->data())); |
| 300 | |
Jonny Svärd | 2c01713 | 2021-04-12 15:56:44 +0200 | [diff] [blame] | 301 | if (model->subgraphs() == nullptr) { |
Davide Grohmann | 2ba3c1d | 2021-11-08 15:08:48 +0100 | [diff] [blame] | 302 | try { |
| 303 | eclose(fd); |
| 304 | } catch (...) { std::throw_with_nested(EthosU::Exception("Failed to get subgraphs: nullptr")); } |
Jonny Svärd | 2c01713 | 2021-04-12 15:56:44 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 307 | // Get input dimensions for first subgraph |
| 308 | auto *subgraph = *model->subgraphs()->begin(); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 309 | ifmDims = getSubGraphDims(subgraph, subgraph->inputs()); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 310 | |
| 311 | // Get output dimensions for last subgraph |
| 312 | subgraph = *model->subgraphs()->rbegin(); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 313 | ofmDims = getSubGraphDims(subgraph, subgraph->outputs()); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 316 | Network::~Network() { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 317 | eclose(fd); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 320 | int Network::ioctl(unsigned long cmd, void *data) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 321 | return eioctl(fd, cmd, data); |
| 322 | } |
| 323 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 324 | shared_ptr<Buffer> Network::getBuffer() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 325 | return buffer; |
| 326 | } |
| 327 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 328 | const std::vector<size_t> &Network::getIfmDims() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 329 | return ifmDims; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 332 | size_t Network::getIfmSize() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 333 | size_t size = 0; |
| 334 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 335 | for (auto s : ifmDims) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 336 | size += s; |
| 337 | } |
| 338 | |
| 339 | return size; |
| 340 | } |
| 341 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 342 | const std::vector<size_t> &Network::getOfmDims() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 343 | return ofmDims; |
| 344 | } |
| 345 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 346 | size_t Network::getOfmSize() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 347 | size_t size = 0; |
| 348 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 349 | for (auto s : ofmDims) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 350 | size += s; |
| 351 | } |
| 352 | |
| 353 | return size; |
| 354 | } |
| 355 | |
| 356 | /**************************************************************************** |
| 357 | * Inference |
| 358 | ****************************************************************************/ |
| 359 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 360 | Inference::~Inference() { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 361 | eclose(fd); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 364 | void Inference::create(std::vector<uint32_t> &counterConfigs, bool cycleCounterEnable = false) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 365 | ethosu_uapi_inference_create uapi; |
| 366 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 367 | if (ifmBuffers.size() > ETHOSU_FD_MAX) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 368 | throw Exception("IFM buffer overflow"); |
| 369 | } |
| 370 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 371 | if (ofmBuffers.size() > ETHOSU_FD_MAX) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 372 | throw Exception("OFM buffer overflow"); |
| 373 | } |
| 374 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 375 | if (counterConfigs.size() != ETHOSU_PMU_EVENT_MAX) { |
| 376 | throw Exception("Wrong size of counter configurations"); |
| 377 | } |
| 378 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 379 | uapi.ifm_count = 0; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 380 | for (auto it : ifmBuffers) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 381 | uapi.ifm_fd[uapi.ifm_count++] = it->getFd(); |
| 382 | } |
| 383 | |
| 384 | uapi.ofm_count = 0; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 385 | for (auto it : ofmBuffers) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 386 | uapi.ofm_fd[uapi.ofm_count++] = it->getFd(); |
| 387 | } |
| 388 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 389 | for (int i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 390 | uapi.pmu_config.events[i] = counterConfigs[i]; |
| 391 | } |
| 392 | |
| 393 | uapi.pmu_config.cycle_count = cycleCounterEnable; |
| 394 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 395 | fd = network->ioctl(ETHOSU_IOCTL_INFERENCE_CREATE, static_cast<void *>(&uapi)); |
| 396 | } |
| 397 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 398 | std::vector<uint32_t> Inference::initializeCounterConfig() { |
| 399 | return std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0); |
| 400 | } |
| 401 | |
| 402 | uint32_t Inference::getMaxPmuEventCounters() { |
| 403 | return ETHOSU_PMU_EVENT_MAX; |
| 404 | } |
| 405 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 406 | int Inference::wait(int64_t timeoutNanos) const { |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 407 | struct pollfd pfd; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 408 | pfd.fd = fd; |
| 409 | pfd.events = POLLIN | POLLERR; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 410 | pfd.revents = 0; |
| 411 | |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 412 | // if timeout negative wait forever |
| 413 | if (timeoutNanos < 0) { |
| 414 | return eppoll(&pfd, 1, NULL, NULL); |
| 415 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 416 | |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 417 | struct timespec tmo_p; |
| 418 | int64_t nanosec = 1000000000; |
| 419 | tmo_p.tv_sec = timeoutNanos / nanosec; |
| 420 | tmo_p.tv_nsec = timeoutNanos % nanosec; |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 421 | |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 422 | return eppoll(&pfd, 1, &tmo_p, NULL); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 425 | bool Inference::failed() const { |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 426 | ethosu_uapi_result_status uapi; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 427 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 428 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 429 | |
| 430 | return uapi.status != ETHOSU_UAPI_STATUS_OK; |
| 431 | } |
| 432 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 433 | const std::vector<uint32_t> Inference::getPmuCounters() const { |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 434 | ethosu_uapi_result_status uapi; |
| 435 | std::vector<uint32_t> counterValues = std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0); |
| 436 | |
| 437 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 438 | |
| 439 | for (int i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 440 | if (uapi.pmu_config.events[i]) { |
| 441 | counterValues.at(i) = uapi.pmu_count.events[i]; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return counterValues; |
| 446 | } |
| 447 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 448 | uint64_t Inference::getCycleCounter() const { |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 449 | ethosu_uapi_result_status uapi; |
| 450 | |
| 451 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 452 | |
| 453 | return uapi.pmu_count.cycle_count; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 456 | int Inference::getFd() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 457 | return fd; |
| 458 | } |
| 459 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 460 | const shared_ptr<Network> Inference::getNetwork() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 461 | return network; |
| 462 | } |
| 463 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 464 | vector<shared_ptr<Buffer>> &Inference::getIfmBuffers() { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 465 | return ifmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 468 | vector<shared_ptr<Buffer>> &Inference::getOfmBuffers() { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 469 | return ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 472 | } // namespace EthosU |