blob: 70d07017fad16bf522cff40d4788e65a55e8c04c [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
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020021#include <algorithm>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020022#include <memory>
23#include <string>
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020024#include <vector>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020025
Kristofer Jonsson79689c52020-10-16 14:42:19 +020026namespace EthosU {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020027
Kristofer Jonsson79689c52020-10-16 14:42:19 +020028class Exception : public std::exception {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020029public:
30 Exception(const char *msg);
31 virtual ~Exception() throw();
32 virtual const char *what() const throw();
33
34private:
35 std::string msg;
36};
37
Kristofer Jonsson79689c52020-10-16 14:42:19 +020038class Device {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020039public:
40 Device(const char *device = "/dev/ethosu0");
41 virtual ~Device();
42
43 int ioctl(unsigned long cmd, void *data = nullptr);
44
45private:
46 int fd;
47};
48
Kristofer Jonsson79689c52020-10-16 14:42:19 +020049class Buffer {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020050public:
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
63private:
64 int fd;
65 char *dataPtr;
66 const size_t dataCapacity;
67};
68
Kristofer Jonsson79689c52020-10-16 14:42:19 +020069class Network {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020070public:
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 Jonssonb74492c2020-09-10 13:26:01 +020076 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 Jonsson116a6352020-08-20 17:25:23 +020080
81private:
82 int fd;
83 std::shared_ptr<Buffer> buffer;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020084 std::vector<size_t> ifmDims;
85 std::vector<size_t> ofmDims;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020086};
87
Kristofer Jonsson79689c52020-10-16 14:42:19 +020088class Inference {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020089public:
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020090 template <typename T>
Kristofer Jonsson79689c52020-10-16 14:42:19 +020091 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 Jonssonb74492c2020-09-10 13:26:01 +020097 std::copy(ifmBegin, ifmEnd, std::back_inserter(ifmBuffers));
98 std::copy(ofmBegin, ofmEnd, std::back_inserter(ofmBuffers));
Per Åstrand2354d3e2020-10-24 20:17:10 +020099 std::vector<uint32_t> counterConfigs = initializeCounterConfig();
100
101 create(counterConfigs, false);
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200102 }
Per Åstrand2354d3e2020-10-24 20:17:10 +0200103 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 Jonsson116a6352020-08-20 17:25:23 +0200123 virtual ~Inference();
124
Per Åstrand2354d3e2020-10-24 20:17:10 +0200125 int wait(int timeoutSec = -1);
126 const std::vector<uint32_t> getPmuCounters();
127 uint64_t getCycleCounter();
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200128 bool failed();
129 int getFd();
130 std::shared_ptr<Network> getNetwork();
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200131 std::vector<std::shared_ptr<Buffer>> &getIfmBuffers();
132 std::vector<std::shared_ptr<Buffer>> &getOfmBuffers();
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200133
Per Åstrand2354d3e2020-10-24 20:17:10 +0200134 static uint32_t getMaxPmuEventCounters();
135
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200136private:
Per Åstrand2354d3e2020-10-24 20:17:10 +0200137 void create(std::vector<uint32_t> &counterConfigs, bool enableCycleCounter);
138 std::vector<uint32_t> initializeCounterConfig();
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200139
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200140 int fd;
141 std::shared_ptr<Network> network;
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200142 std::vector<std::shared_ptr<Buffer>> ifmBuffers;
143 std::vector<std::shared_ptr<Buffer>> ofmBuffers;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200144};
145
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200146} // namespace EthosU