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