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)); |
| 99 | create(); |
| 100 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 101 | virtual ~Inference(); |
| 102 | |
| 103 | void wait(int timeoutSec = -1); |
| 104 | bool failed(); |
| 105 | int getFd(); |
| 106 | std::shared_ptr<Network> getNetwork(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 107 | std::vector<std::shared_ptr<Buffer>> &getIfmBuffers(); |
| 108 | std::vector<std::shared_ptr<Buffer>> &getOfmBuffers(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 109 | |
| 110 | private: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 111 | void create(); |
| 112 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 113 | int fd; |
| 114 | std::shared_ptr<Network> network; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 115 | std::vector<std::shared_ptr<Buffer>> ifmBuffers; |
| 116 | std::vector<std::shared_ptr<Buffer>> ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 119 | } // namespace EthosU |