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