blob: 6b17ac2f3acf1a75dc39ec10d9bceb84eb308276 [file] [log] [blame]
Davide Grohmannf0364232022-06-16 17:42:58 +02001/*
Mikael Olssonc4ae2182023-10-12 15:29:30 +02002 * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Davide Grohmannf0364232022-06-16 17:42:58 +02003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the License); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <ethosu.hpp>
19#include <uapi/ethosu.h>
20
21#include <cstring>
Mikael Olsson07545152023-10-17 13:05:38 +020022#include <errno.h>
Davide Grohmannf0364232022-06-16 17:42:58 +020023#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
Mikael Olsson8f918c62023-10-12 15:30:32 +020051void testDriverVersion(const Device &device) {
52 int r;
53 struct ethosu_uapi_kernel_driver_version version = {};
54 try {
55 r = device.ioctl(ETHOSU_IOCTL_DRIVER_VERSION_GET, &version);
56 } catch (std::exception &e) { throw TestFailureException("Driver version test: ", e.what()); }
57
58 TEST_ASSERT(r == 0);
59 TEST_ASSERT(version.major == ETHOSU_KERNEL_DRIVER_VERSION_MAJOR);
60 TEST_ASSERT(version.minor == ETHOSU_KERNEL_DRIVER_VERSION_MINOR);
61 TEST_ASSERT(version.patch == ETHOSU_KERNEL_DRIVER_VERSION_PATCH);
62}
63
Davide Grohmannf0364232022-06-16 17:42:58 +020064void testCapabilties(const Device &device) {
65 Capabilities capabilities;
66 try {
67 capabilities = device.capabilities();
68 } catch (std::exception &e) { throw TestFailureException("Capabilities test: ", e.what()); }
69
70 TEST_ASSERT(capabilities.hwId.architecture > SemanticVersion());
71}
72
Mikael Olsson07545152023-10-17 13:05:38 +020073void testBufferSeek(const Device &device) {
74 try {
75 Buffer buf{device, 1024};
76
77 // SEEK_END should return the size of the buffer
78 TEST_ASSERT(lseek(buf.getFd(), 0, SEEK_END) == 1024);
79
80 // SEEK_SET is supported when moving the file pointer to the start
81 TEST_ASSERT(lseek(buf.getFd(), 0, SEEK_SET) == 0);
82
83 // SEEK_CUR is not supported
84 errno = 0;
85 TEST_ASSERT(lseek(buf.getFd(), 0, SEEK_CUR) == -1);
86 TEST_ASSERT(errno == EINVAL);
87
88 // Non-zero offset is not supported
89 errno = 0;
90 TEST_ASSERT(lseek(buf.getFd(), 1, SEEK_CUR) == -1);
91 TEST_ASSERT(errno == EINVAL);
92
93 errno = 0;
94 TEST_ASSERT(lseek(buf.getFd(), 2, SEEK_END) == -1);
95 TEST_ASSERT(errno == EINVAL);
96
97 errno = 0;
98 TEST_ASSERT(lseek(buf.getFd(), 3, SEEK_SET) == -1);
99 TEST_ASSERT(errno == EINVAL);
100 } catch (std::exception &e) { throw TestFailureException("Buffer seek test: ", e.what()); }
101}
102
Davide Grohmannf0364232022-06-16 17:42:58 +0200103void testNetworkInfoNotExistentIndex(const Device &device) {
104 try {
105 Network(device, 0);
106 FAIL();
107 } catch (Exception &e) {
108 // good it should have thrown
109 } catch (std::exception &e) { throw TestFailureException("NetworkInfo no index test: ", e.what()); }
110}
111
112void testNetworkInfoBuffer(const Device &device) {
113 try {
114 std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>(device, sizeof(networkModelData));
Davide Grohmannf0364232022-06-16 17:42:58 +0200115 std::memcpy(buffer->data(), networkModelData, sizeof(networkModelData));
116 Network network(device, buffer);
117
118 TEST_ASSERT(network.getIfmDims().size() == 1);
119 TEST_ASSERT(network.getOfmDims().size() == 1);
120 } catch (std::exception &e) { throw TestFailureException("NetworkInfo buffer test: ", e.what()); }
121}
122
123void testNetworkInfoUnparsableBuffer(const Device &device) {
124 try {
125 auto buffer = std::make_shared<Buffer>(device, sizeof(networkModelData) / 4);
Davide Grohmannf0364232022-06-16 17:42:58 +0200126 std::memcpy(buffer->data(), networkModelData + sizeof(networkModelData) / 4, sizeof(networkModelData) / 4);
127
128 try {
129 Network network(device, buffer);
130 FAIL();
131 } catch (Exception) {
132 // good, it should have thrown!
133 }
134 } catch (std::exception &e) { throw TestFailureException("NetworkInfo unparsable buffer test: ", e.what()); }
135}
136
Mikael Olsson45d47992023-10-12 15:32:56 +0200137void testNetworkInvalidType(const Device &device) {
138 const std::string expected_error =
139 std::string("IOCTL cmd=") + std::to_string(ETHOSU_IOCTL_NETWORK_CREATE) + " failed: " + std::strerror(EINVAL);
140 struct ethosu_uapi_network_create net_req = {};
141 net_req.type = ETHOSU_UAPI_NETWORK_INDEX + 1;
142 try {
143 int r = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, &net_req);
144 FAIL();
145 } catch (Exception &e) {
146 // The call is expected to throw
147 TEST_ASSERT(expected_error.compare(e.what()) == 0);
148 } catch (std::exception &e) { throw TestFailureException("NetworkCreate invalid type test: ", e.what()); }
149}
150
Davide Grohmannf0364232022-06-16 17:42:58 +0200151void testRunInferenceBuffer(const Device &device) {
152 try {
153 auto networkBuffer = std::make_shared<Buffer>(device, sizeof(networkModelData));
Davide Grohmannf0364232022-06-16 17:42:58 +0200154 std::memcpy(networkBuffer->data(), networkModelData, sizeof(networkModelData));
155 auto network = std::make_shared<Network>(device, networkBuffer);
156
157 std::vector<std::shared_ptr<Buffer>> inputBuffers;
158 std::vector<std::shared_ptr<Buffer>> outputBuffers;
159
160 auto inputBuffer = std::make_shared<Buffer>(device, sizeof(inputData));
Davide Grohmannf0364232022-06-16 17:42:58 +0200161 std::memcpy(inputBuffer->data(), inputData, sizeof(inputData));
162
163 inputBuffers.push_back(inputBuffer);
164 outputBuffers.push_back(std::make_shared<Buffer>(device, sizeof(expectedOutputData)));
165 std::vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters());
166
167 auto inference = std::make_shared<Inference>(network,
168 inputBuffers.begin(),
169 inputBuffers.end(),
170 outputBuffers.begin(),
171 outputBuffers.end(),
172 enabledCounters,
173 false);
174
175 bool timedout = inference->wait(defaultTimeout);
176 TEST_ASSERT(!timedout);
177
Davide Grohmann6d2e5b72022-08-24 17:01:40 +0200178 InferenceStatus status = inference->status();
179 TEST_ASSERT(status == InferenceStatus::OK);
180
181 bool success = inference->cancel();
182 TEST_ASSERT(!success);
183
Davide Grohmannf0364232022-06-16 17:42:58 +0200184 TEST_ASSERT(std::memcmp(expectedOutputData, outputBuffers[0]->data(), sizeof(expectedOutputData)) == 0);
185
186 } catch (std::exception &e) { throw TestFailureException("Inference run test: ", e.what()); }
187}
188
189} // namespace
190
191int main() {
192 Device device;
193
194 try {
195 testPing(device);
Mikael Olsson8f918c62023-10-12 15:30:32 +0200196 testDriverVersion(device);
Davide Grohmannf0364232022-06-16 17:42:58 +0200197 testCapabilties(device);
Mikael Olsson07545152023-10-17 13:05:38 +0200198 testBufferSeek(device);
Mikael Olsson45d47992023-10-12 15:32:56 +0200199 testNetworkInvalidType(device);
Davide Grohmannf0364232022-06-16 17:42:58 +0200200 testNetworkInfoNotExistentIndex(device);
201 testNetworkInfoBuffer(device);
202 testNetworkInfoUnparsableBuffer(device);
203 testRunInferenceBuffer(device);
204 } catch (TestFailureException &e) {
205 std::cerr << "Test failure: " << e.what() << std::endl;
206 return 1;
207 }
208
209 return 0;
210}