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 | |
| 21 | #include <uapi/ethosu.h> |
| 22 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 23 | #include <algorithm> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 24 | #include <memory> |
| 25 | #include <string> |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 26 | #include <vector> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 27 | |
| 28 | namespace EthosU |
| 29 | { |
| 30 | |
| 31 | class Exception : |
| 32 | public std::exception |
| 33 | { |
| 34 | public: |
| 35 | Exception(const char *msg); |
| 36 | virtual ~Exception() throw(); |
| 37 | virtual const char *what() const throw(); |
| 38 | |
| 39 | private: |
| 40 | std::string msg; |
| 41 | }; |
| 42 | |
| 43 | class Device |
| 44 | { |
| 45 | public: |
| 46 | Device(const char *device = "/dev/ethosu0"); |
| 47 | virtual ~Device(); |
| 48 | |
| 49 | int ioctl(unsigned long cmd, void *data = nullptr); |
| 50 | |
| 51 | private: |
| 52 | int fd; |
| 53 | }; |
| 54 | |
| 55 | class Buffer |
| 56 | { |
| 57 | public: |
| 58 | Buffer(Device &device, const size_t capacity); |
| 59 | virtual ~Buffer(); |
| 60 | |
| 61 | size_t capacity() const; |
| 62 | void clear(); |
| 63 | char *data(); |
| 64 | void resize(size_t size, size_t offset = 0); |
| 65 | size_t offset() const; |
| 66 | size_t size() const; |
| 67 | |
| 68 | int getFd() const; |
| 69 | |
| 70 | private: |
| 71 | int fd; |
| 72 | char *dataPtr; |
| 73 | const size_t dataCapacity; |
| 74 | }; |
| 75 | |
| 76 | class Network |
| 77 | { |
| 78 | public: |
| 79 | Network(Device &device, std::shared_ptr<Buffer> &buffer); |
| 80 | virtual ~Network(); |
| 81 | |
| 82 | int ioctl(unsigned long cmd, void *data = nullptr); |
| 83 | std::shared_ptr<Buffer> getBuffer(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 84 | const std::vector<size_t> &getIfmDims() const; |
| 85 | size_t getIfmSize() const; |
| 86 | const std::vector<size_t> &getOfmDims() const; |
| 87 | size_t getOfmSize() const; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | int fd; |
| 91 | std::shared_ptr<Buffer> buffer; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 92 | std::vector<size_t> ifmDims; |
| 93 | std::vector<size_t> ofmDims; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | class Inference |
| 97 | { |
| 98 | public: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 99 | template <typename T> |
| 100 | Inference(std::shared_ptr<Network> &network, const T &ifmBegin, const T &ifmEnd, const T &ofmBegin, const T &ofmEnd) : |
| 101 | network(network) |
| 102 | { |
| 103 | std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers)); |
| 104 | std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers)); |
| 105 | create(); |
| 106 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 107 | virtual ~Inference(); |
| 108 | |
| 109 | void wait(int timeoutSec = -1); |
| 110 | bool failed(); |
| 111 | int getFd(); |
| 112 | std::shared_ptr<Network> getNetwork(); |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 113 | std::vector<std::shared_ptr<Buffer>> &getIfmBuffers(); |
| 114 | std::vector<std::shared_ptr<Buffer>> &getOfmBuffers(); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 115 | |
| 116 | private: |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 117 | void create(); |
| 118 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 119 | int fd; |
| 120 | std::shared_ptr<Network> network; |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 121 | std::vector<std::shared_ptr<Buffer>> ifmBuffers; |
| 122 | std::vector<std::shared_ptr<Buffer>> ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } |