Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Arm Limited. All rights reserved. |
| 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 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 36 | namespace { |
| 37 | 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 | |
| 46 | /**************************************************************************** |
| 47 | * TFL micro helpers |
| 48 | ****************************************************************************/ |
| 49 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 50 | size_t getShapeSize(const flatbuffers::Vector<int32_t> *shape) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 51 | size_t size = 1; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 52 | |
| 53 | for (auto it = shape->begin(); it != shape->end(); ++it) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 54 | size *= *it; |
| 55 | } |
| 56 | |
| 57 | return size; |
| 58 | } |
| 59 | |
Kristofer Jonsson | 5e632c2 | 2020-11-25 10:58:38 +0100 | [diff] [blame] | 60 | size_t getTensorTypeSize(const enum tflite::TensorType type) { |
| 61 | switch (type) { |
| 62 | case tflite::TensorType::TensorType_UINT8: |
| 63 | case tflite::TensorType::TensorType_INT8: |
| 64 | return 1; |
| 65 | case tflite::TensorType::TensorType_INT16: |
| 66 | return 2; |
Henrik Hoglind | 031f4c1 | 2021-03-25 08:32:23 +0100 | [diff] [blame^] | 67 | case tflite::TensorType::TensorType_FLOAT32: |
| 68 | return 4; |
Kristofer Jonsson | 5e632c2 | 2020-11-25 10:58:38 +0100 | [diff] [blame] | 69 | default: |
| 70 | throw EthosU::Exception("Unsupported tensor type"); |
| 71 | } |
| 72 | } |
| 73 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 74 | 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] | 75 | vector<size_t> dims; |
| 76 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 77 | for (auto index = tensorMap->begin(); index != tensorMap->end(); ++index) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 78 | auto tensor = subgraph->tensors()->Get(*index); |
| 79 | size_t size = getShapeSize(tensor->shape()); |
Kristofer Jonsson | 5e632c2 | 2020-11-25 10:58:38 +0100 | [diff] [blame] | 80 | size *= getTensorTypeSize(tensor->type()); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 81 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 82 | if (size > 0) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 83 | dims.push_back(size); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return dims; |
| 88 | } |
| 89 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 90 | } // namespace |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 91 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 92 | namespace EthosU { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 93 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 94 | /**************************************************************************** |
| 95 | * Exception |
| 96 | ****************************************************************************/ |
| 97 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 98 | Exception::Exception(const char *msg) : msg(msg) {} |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 99 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 100 | Exception::~Exception() throw() {} |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 101 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 102 | const char *Exception::what() const throw() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 103 | return msg.c_str(); |
| 104 | } |
| 105 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 106 | /**************************************************************************** |
| 107 | * Device |
| 108 | ****************************************************************************/ |
| 109 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 110 | Device::Device(const char *device) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 111 | fd = open(device, O_RDWR | O_NONBLOCK); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 112 | if (fd < 0) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 113 | throw Exception("Failed to open device"); |
| 114 | } |
| 115 | } |
| 116 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 117 | Device::~Device() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 118 | close(fd); |
| 119 | } |
| 120 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 121 | int Device::ioctl(unsigned long cmd, void *data) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 122 | return eioctl(fd, cmd, data); |
| 123 | } |
| 124 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 125 | /**************************************************************************** |
| 126 | * Buffer |
| 127 | ****************************************************************************/ |
| 128 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 129 | Buffer::Buffer(Device &device, const size_t capacity) : fd(-1), dataPtr(nullptr), dataCapacity(capacity) { |
| 130 | ethosu_uapi_buffer_create uapi = {static_cast<uint32_t>(dataCapacity)}; |
| 131 | fd = device.ioctl(ETHOSU_IOCTL_BUFFER_CREATE, static_cast<void *>(&uapi)); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 132 | |
| 133 | void *d = ::mmap(nullptr, dataCapacity, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 134 | if (d == MAP_FAILED) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 135 | throw Exception("MMap failed"); |
| 136 | } |
| 137 | |
| 138 | dataPtr = reinterpret_cast<char *>(d); |
| 139 | } |
| 140 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 141 | Buffer::~Buffer() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 142 | close(fd); |
| 143 | } |
| 144 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 145 | size_t Buffer::capacity() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 146 | return dataCapacity; |
| 147 | } |
| 148 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 149 | void Buffer::clear() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 150 | resize(0, 0); |
| 151 | } |
| 152 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 153 | char *Buffer::data() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 154 | return dataPtr + offset(); |
| 155 | } |
| 156 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 157 | void Buffer::resize(size_t size, size_t offset) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 158 | ethosu_uapi_buffer uapi; |
| 159 | uapi.offset = offset; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 160 | uapi.size = size; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 161 | eioctl(fd, ETHOSU_IOCTL_BUFFER_SET, static_cast<void *>(&uapi)); |
| 162 | } |
| 163 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 164 | size_t Buffer::offset() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 165 | ethosu_uapi_buffer uapi; |
| 166 | eioctl(fd, ETHOSU_IOCTL_BUFFER_GET, static_cast<void *>(&uapi)); |
| 167 | return uapi.offset; |
| 168 | } |
| 169 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 170 | size_t Buffer::size() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 171 | ethosu_uapi_buffer uapi; |
| 172 | eioctl(fd, ETHOSU_IOCTL_BUFFER_GET, static_cast<void *>(&uapi)); |
| 173 | return uapi.size; |
| 174 | } |
| 175 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 176 | int Buffer::getFd() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 177 | return fd; |
| 178 | } |
| 179 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 180 | /**************************************************************************** |
| 181 | * Network |
| 182 | ****************************************************************************/ |
| 183 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 184 | Network::Network(Device &device, shared_ptr<Buffer> &buffer) : fd(-1), buffer(buffer) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 185 | // Create buffer handle |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 186 | ethosu_uapi_network_create uapi; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 187 | uapi.fd = buffer->getFd(); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 188 | fd = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, static_cast<void *>(&uapi)); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 189 | |
| 190 | // Create model handle |
| 191 | const tflite::Model *model = tflite::GetModel(reinterpret_cast<void *>(buffer->data())); |
| 192 | |
| 193 | // Get input dimensions for first subgraph |
| 194 | auto *subgraph = *model->subgraphs()->begin(); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 195 | ifmDims = getSubGraphDims(subgraph, subgraph->inputs()); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 196 | |
| 197 | // Get output dimensions for last subgraph |
| 198 | subgraph = *model->subgraphs()->rbegin(); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 199 | ofmDims = getSubGraphDims(subgraph, subgraph->outputs()); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 202 | Network::~Network() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 203 | close(fd); |
| 204 | } |
| 205 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 206 | int Network::ioctl(unsigned long cmd, void *data) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 207 | return eioctl(fd, cmd, data); |
| 208 | } |
| 209 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 210 | shared_ptr<Buffer> Network::getBuffer() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 211 | return buffer; |
| 212 | } |
| 213 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 214 | const std::vector<size_t> &Network::getIfmDims() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 215 | return ifmDims; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 218 | size_t Network::getIfmSize() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 219 | size_t size = 0; |
| 220 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 221 | for (auto s : ifmDims) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 222 | size += s; |
| 223 | } |
| 224 | |
| 225 | return size; |
| 226 | } |
| 227 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 228 | const std::vector<size_t> &Network::getOfmDims() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 229 | return ofmDims; |
| 230 | } |
| 231 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 232 | size_t Network::getOfmSize() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 233 | size_t size = 0; |
| 234 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 235 | for (auto s : ofmDims) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 236 | size += s; |
| 237 | } |
| 238 | |
| 239 | return size; |
| 240 | } |
| 241 | |
| 242 | /**************************************************************************** |
| 243 | * Inference |
| 244 | ****************************************************************************/ |
| 245 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 246 | Inference::~Inference() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 247 | close(fd); |
| 248 | } |
| 249 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 250 | void Inference::create(std::vector<uint32_t> &counterConfigs, bool cycleCounterEnable = false) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 251 | ethosu_uapi_inference_create uapi; |
| 252 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 253 | if (ifmBuffers.size() > ETHOSU_FD_MAX) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 254 | throw Exception("IFM buffer overflow"); |
| 255 | } |
| 256 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 257 | if (ofmBuffers.size() > ETHOSU_FD_MAX) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 258 | throw Exception("OFM buffer overflow"); |
| 259 | } |
| 260 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 261 | if (counterConfigs.size() != ETHOSU_PMU_EVENT_MAX) { |
| 262 | throw Exception("Wrong size of counter configurations"); |
| 263 | } |
| 264 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 265 | uapi.ifm_count = 0; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 266 | for (auto it : ifmBuffers) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 267 | uapi.ifm_fd[uapi.ifm_count++] = it->getFd(); |
| 268 | } |
| 269 | |
| 270 | uapi.ofm_count = 0; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 271 | for (auto it : ofmBuffers) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 272 | uapi.ofm_fd[uapi.ofm_count++] = it->getFd(); |
| 273 | } |
| 274 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 275 | for (int i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 276 | uapi.pmu_config.events[i] = counterConfigs[i]; |
| 277 | } |
| 278 | |
| 279 | uapi.pmu_config.cycle_count = cycleCounterEnable; |
| 280 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 281 | fd = network->ioctl(ETHOSU_IOCTL_INFERENCE_CREATE, static_cast<void *>(&uapi)); |
| 282 | } |
| 283 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 284 | std::vector<uint32_t> Inference::initializeCounterConfig() { |
| 285 | return std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0); |
| 286 | } |
| 287 | |
| 288 | uint32_t Inference::getMaxPmuEventCounters() { |
| 289 | return ETHOSU_PMU_EVENT_MAX; |
| 290 | } |
| 291 | |
| 292 | int Inference::wait(int timeoutSec) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 293 | pollfd pfd; |
| 294 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 295 | pfd.fd = fd; |
| 296 | pfd.events = POLLIN | POLLERR; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 297 | pfd.revents = 0; |
| 298 | |
| 299 | int ret = ::poll(&pfd, 1, timeoutSec * 1000); |
| 300 | |
| 301 | cout << "Poll. ret=" << ret << ", revents=" << pfd.revents << endl; |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 302 | |
| 303 | return ret; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 306 | bool Inference::failed() { |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 307 | ethosu_uapi_result_status uapi; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 308 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 309 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 310 | |
| 311 | return uapi.status != ETHOSU_UAPI_STATUS_OK; |
| 312 | } |
| 313 | |
| 314 | const std::vector<uint32_t> Inference::getPmuCounters() { |
| 315 | ethosu_uapi_result_status uapi; |
| 316 | std::vector<uint32_t> counterValues = std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0); |
| 317 | |
| 318 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 319 | |
| 320 | for (int i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 321 | if (uapi.pmu_config.events[i]) { |
| 322 | counterValues.at(i) = uapi.pmu_count.events[i]; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return counterValues; |
| 327 | } |
| 328 | |
| 329 | uint64_t Inference::getCycleCounter() { |
| 330 | ethosu_uapi_result_status uapi; |
| 331 | |
| 332 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 333 | |
| 334 | return uapi.pmu_count.cycle_count; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 337 | int Inference::getFd() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 338 | return fd; |
| 339 | } |
| 340 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 341 | shared_ptr<Network> Inference::getNetwork() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 342 | return network; |
| 343 | } |
| 344 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 345 | vector<shared_ptr<Buffer>> &Inference::getIfmBuffers() { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 346 | return ifmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 349 | vector<shared_ptr<Buffer>> &Inference::getOfmBuffers() { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 350 | return ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 353 | } // namespace EthosU |