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