blob: 4e45485c3e5570b231b17587fdc626daf4a559f2 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
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 Jonssonb74492c2020-09-10 13:26:01 +020023#include <algorithm>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020024#include <memory>
25#include <string>
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020026#include <vector>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020027
Kristofer Jonsson79689c52020-10-16 14:42:19 +020028namespace EthosU {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020029
Kristofer Jonsson79689c52020-10-16 14:42:19 +020030class Exception : public std::exception {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020031public:
32 Exception(const char *msg);
33 virtual ~Exception() throw();
34 virtual const char *what() const throw();
35
36private:
37 std::string msg;
38};
39
Kristofer Jonsson79689c52020-10-16 14:42:19 +020040class Device {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020041public:
42 Device(const char *device = "/dev/ethosu0");
43 virtual ~Device();
44
45 int ioctl(unsigned long cmd, void *data = nullptr);
46
47private:
48 int fd;
49};
50
Kristofer Jonsson79689c52020-10-16 14:42:19 +020051class Buffer {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020052public:
53 Buffer(Device &device, const size_t capacity);
54 virtual ~Buffer();
55
56 size_t capacity() const;
57 void clear();
58 char *data();
59 void resize(size_t size, size_t offset = 0);
60 size_t offset() const;
61 size_t size() const;
62
63 int getFd() const;
64
65private:
66 int fd;
67 char *dataPtr;
68 const size_t dataCapacity;
69};
70
Kristofer Jonsson79689c52020-10-16 14:42:19 +020071class Network {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020072public:
73 Network(Device &device, std::shared_ptr<Buffer> &buffer);
74 virtual ~Network();
75
76 int ioctl(unsigned long cmd, void *data = nullptr);
77 std::shared_ptr<Buffer> getBuffer();
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020078 const std::vector<size_t> &getIfmDims() const;
79 size_t getIfmSize() const;
80 const std::vector<size_t> &getOfmDims() const;
81 size_t getOfmSize() const;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020082
83private:
84 int fd;
85 std::shared_ptr<Buffer> buffer;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020086 std::vector<size_t> ifmDims;
87 std::vector<size_t> ofmDims;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020088};
89
Kristofer Jonsson79689c52020-10-16 14:42:19 +020090class Inference {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020091public:
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020092 template <typename T>
Kristofer Jonsson79689c52020-10-16 14:42:19 +020093 Inference(std::shared_ptr<Network> &network,
94 const T &ifmBegin,
95 const T &ifmEnd,
96 const T &ofmBegin,
97 const T &ofmEnd) :
98 network(network) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020099 std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers));
100 std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers));
101 create();
102 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200103 virtual ~Inference();
104
105 void wait(int timeoutSec = -1);
106 bool failed();
107 int getFd();
108 std::shared_ptr<Network> getNetwork();
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200109 std::vector<std::shared_ptr<Buffer>> &getIfmBuffers();
110 std::vector<std::shared_ptr<Buffer>> &getOfmBuffers();
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200111
112private:
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200113 void create();
114
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200115 int fd;
116 std::shared_ptr<Network> network;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200117 std::vector<std::shared_ptr<Buffer>> ifmBuffers;
118 std::vector<std::shared_ptr<Buffer>> ofmBuffers;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200119};
120
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200121} // namespace EthosU