blob: 94bb4992725d4a530784547e988b411c340dc533 [file] [log] [blame]
Davide Grohmannf0364232022-06-16 17:42:58 +02001/*
2 * Copyright (c) 2022 Arm Limited.
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#include <ethosu.hpp>
20#include <uapi/ethosu.h>
21
22#include <cstring>
23#include <iostream>
24#include <list>
25#include <memory>
26#include <sstream>
27#include <stdio.h>
28#include <string>
29#include <unistd.h>
30
31#include "input.h"
32#include "model.h"
33#include "output.h"
Davide Grohmann6d2e5b72022-08-24 17:01:40 +020034#include "test_assertions.hpp"
Davide Grohmannf0364232022-06-16 17:42:58 +020035
36using namespace EthosU;
37
38namespace {
39
40int64_t defaultTimeout = 60000000000;
41
Davide Grohmannf0364232022-06-16 17:42:58 +020042void testPing(const Device &device) {
43 int r;
44 try {
45 r = device.ioctl(ETHOSU_IOCTL_PING);
46 } catch (std::exception &e) { throw TestFailureException("Ping test: ", e.what()); }
47
48 TEST_ASSERT(r == 0);
49}
50
51void testVersion(const Device &device) {
52 int r;
53 try {
54 r = device.ioctl(ETHOSU_IOCTL_VERSION_REQ);
55 } catch (std::exception &e) { throw TestFailureException("Version test: ", e.what()); }
56
57 TEST_ASSERT(r == 0);
58}
59
60void testCapabilties(const Device &device) {
61 Capabilities capabilities;
62 try {
63 capabilities = device.capabilities();
64 } catch (std::exception &e) { throw TestFailureException("Capabilities test: ", e.what()); }
65
66 TEST_ASSERT(capabilities.hwId.architecture > SemanticVersion());
67}
68
69void testNetworkInfoNotExistentIndex(const Device &device) {
70 try {
71 Network(device, 0);
72 FAIL();
73 } catch (Exception &e) {
74 // good it should have thrown
75 } catch (std::exception &e) { throw TestFailureException("NetworkInfo no index test: ", e.what()); }
76}
77
78void testNetworkInfoBuffer(const Device &device) {
79 try {
80 std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>(device, sizeof(networkModelData));
81 buffer->resize(sizeof(networkModelData));
82 std::memcpy(buffer->data(), networkModelData, sizeof(networkModelData));
83 Network network(device, buffer);
84
85 TEST_ASSERT(network.getIfmDims().size() == 1);
86 TEST_ASSERT(network.getOfmDims().size() == 1);
87 } catch (std::exception &e) { throw TestFailureException("NetworkInfo buffer test: ", e.what()); }
88}
89
90void testNetworkInfoUnparsableBuffer(const Device &device) {
91 try {
92 auto buffer = std::make_shared<Buffer>(device, sizeof(networkModelData) / 4);
93 buffer->resize(sizeof(networkModelData) / 4);
94 std::memcpy(buffer->data(), networkModelData + sizeof(networkModelData) / 4, sizeof(networkModelData) / 4);
95
96 try {
97 Network network(device, buffer);
98 FAIL();
99 } catch (Exception) {
100 // good, it should have thrown!
101 }
102 } catch (std::exception &e) { throw TestFailureException("NetworkInfo unparsable buffer test: ", e.what()); }
103}
104
105void testRunInferenceBuffer(const Device &device) {
106 try {
107 auto networkBuffer = std::make_shared<Buffer>(device, sizeof(networkModelData));
108 networkBuffer->resize(sizeof(networkModelData));
109 std::memcpy(networkBuffer->data(), networkModelData, sizeof(networkModelData));
110 auto network = std::make_shared<Network>(device, networkBuffer);
111
112 std::vector<std::shared_ptr<Buffer>> inputBuffers;
113 std::vector<std::shared_ptr<Buffer>> outputBuffers;
114
115 auto inputBuffer = std::make_shared<Buffer>(device, sizeof(inputData));
116 inputBuffer->resize(sizeof(inputData));
117 std::memcpy(inputBuffer->data(), inputData, sizeof(inputData));
118
119 inputBuffers.push_back(inputBuffer);
120 outputBuffers.push_back(std::make_shared<Buffer>(device, sizeof(expectedOutputData)));
121 std::vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters());
122
123 auto inference = std::make_shared<Inference>(network,
124 inputBuffers.begin(),
125 inputBuffers.end(),
126 outputBuffers.begin(),
127 outputBuffers.end(),
128 enabledCounters,
129 false);
130
131 bool timedout = inference->wait(defaultTimeout);
132 TEST_ASSERT(!timedout);
133
Davide Grohmann6d2e5b72022-08-24 17:01:40 +0200134 InferenceStatus status = inference->status();
135 TEST_ASSERT(status == InferenceStatus::OK);
136
137 bool success = inference->cancel();
138 TEST_ASSERT(!success);
139
Davide Grohmannf0364232022-06-16 17:42:58 +0200140 TEST_ASSERT(std::memcmp(expectedOutputData, outputBuffers[0]->data(), sizeof(expectedOutputData)) == 0);
141
142 } catch (std::exception &e) { throw TestFailureException("Inference run test: ", e.what()); }
143}
144
145} // namespace
146
147int main() {
148 Device device;
149
150 try {
151 testPing(device);
152 testVersion(device);
153 testCapabilties(device);
154 testNetworkInfoNotExistentIndex(device);
155 testNetworkInfoBuffer(device);
156 testNetworkInfoUnparsableBuffer(device);
157 testRunInferenceBuffer(device);
158 } catch (TestFailureException &e) {
159 std::cerr << "Test failure: " << e.what() << std::endl;
160 return 1;
161 }
162
163 return 0;
164}