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