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 | |
| 19 | #pragma once |
| 20 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 21 | #include <algorithm> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <string> |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 24 | #include <vector> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 25 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 26 | namespace EthosU { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 27 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 28 | class Exception : public std::exception { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 29 | public: |
| 30 | Exception(const char *msg); |
| 31 | virtual ~Exception() throw(); |
| 32 | virtual const char *what() const throw(); |
| 33 | |
| 34 | private: |
| 35 | std::string msg; |
| 36 | }; |
| 37 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 38 | class Device { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 39 | public: |
| 40 | Device(const char *device = "/dev/ethosu0"); |
| 41 | virtual ~Device(); |
| 42 | |
| 43 | int ioctl(unsigned long cmd, void *data = nullptr); |
| 44 | |
| 45 | private: |
| 46 | int fd; |
| 47 | }; |
| 48 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 49 | class Buffer { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 50 | public: |
| 51 | Buffer(Device &device, const size_t capacity); |
| 52 | virtual ~Buffer(); |
| 53 | |
| 54 | size_t capacity() const; |
| 55 | void clear(); |
| 56 | char *data(); |
| 57 | void resize(size_t size, size_t offset = 0); |
| 58 | size_t offset() const; |
| 59 | size_t size() const; |
| 60 | |
| 61 | int getFd() const; |
| 62 | |
| 63 | private: |
| 64 | int fd; |
| 65 | char *dataPtr; |
| 66 | const size_t dataCapacity; |
| 67 | }; |
| 68 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 69 | class Network { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 70 | public: |
| 71 | Network(Device &device, std::shared_ptr<Buffer> &buffer); |
| 72 | virtual ~Network(); |
| 73 | |
| 74 | int ioctl(unsigned long cmd, void *data = nullptr); |
| 75 | std::shared_ptr<Buffer> getBuffer(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 76 | const std::vector<size_t> &getIfmDims() const; |
| 77 | size_t getIfmSize() const; |
| 78 | const std::vector<size_t> &getOfmDims() const; |
| 79 | size_t getOfmSize() const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | int fd; |
| 83 | std::shared_ptr<Buffer> buffer; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 84 | std::vector<size_t> ifmDims; |
| 85 | std::vector<size_t> ofmDims; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 88 | class Inference { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 89 | public: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 90 | template <typename T> |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 91 | Inference(std::shared_ptr<Network> &network, |
| 92 | const T &ifmBegin, |
| 93 | const T &ifmEnd, |
| 94 | const T &ofmBegin, |
| 95 | const T &ofmEnd) : |
| 96 | network(network) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 97 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 98 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame^] | 99 | std::vector<uint32_t> counterConfigs = initializeCounterConfig(); |
| 100 | |
| 101 | create(counterConfigs, false); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 102 | } |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame^] | 103 | template <typename T, typename U> |
| 104 | Inference(std::shared_ptr<Network> &network, |
| 105 | const T &ifmBegin, |
| 106 | const T &ifmEnd, |
| 107 | const T &ofmBegin, |
| 108 | const T &ofmEnd, |
| 109 | const U &counters, |
| 110 | bool enableCycleCounter) : |
| 111 | network(network) { |
| 112 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 113 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
| 114 | std::vector<uint32_t> counterConfigs = initializeCounterConfig(); |
| 115 | |
| 116 | if (counters.size() > counterConfigs.size()) |
| 117 | throw EthosU::Exception("PMU Counters argument to large."); |
| 118 | |
| 119 | std::copy(counters.begin(), counters.end(), counterConfigs.begin()); |
| 120 | create(counterConfigs, enableCycleCounter); |
| 121 | } |
| 122 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 123 | virtual ~Inference(); |
| 124 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame^] | 125 | int wait(int timeoutSec = -1); |
| 126 | const std::vector<uint32_t> getPmuCounters(); |
| 127 | uint64_t getCycleCounter(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 128 | bool failed(); |
| 129 | int getFd(); |
| 130 | std::shared_ptr<Network> getNetwork(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 131 | std::vector<std::shared_ptr<Buffer>> &getIfmBuffers(); |
| 132 | std::vector<std::shared_ptr<Buffer>> &getOfmBuffers(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 133 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame^] | 134 | static uint32_t getMaxPmuEventCounters(); |
| 135 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 136 | private: |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame^] | 137 | void create(std::vector<uint32_t> &counterConfigs, bool enableCycleCounter); |
| 138 | std::vector<uint32_t> initializeCounterConfig(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 139 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 140 | int fd; |
| 141 | std::shared_ptr<Network> network; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 142 | std::vector<std::shared_ptr<Buffer>> ifmBuffers; |
| 143 | std::vector<std::shared_ptr<Buffer>> ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 144 | }; |
| 145 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 146 | } // namespace EthosU |