blob: ef2266eadec35b91519bdd48fc702a9080e003e0 [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
Davide Grohmannf0364232022-06-16 17:42:58 +020050void testCapabilties(const Device &device) {
51 Capabilities capabilities;
52 try {
53 capabilities = device.capabilities();
54 } catch (std::exception &e) { throw TestFailureException("Capabilities test: ", e.what()); }
55
56 TEST_ASSERT(capabilities.hwId.architecture > SemanticVersion());
57}
58
59void testNetworkInfoNotExistentIndex(const Device &device) {
60 try {
61 Network(device, 0);
62 FAIL();
63 } catch (Exception &e) {
64 // good it should have thrown
65 } catch (std::exception &e) { throw TestFailureException("NetworkInfo no index test: ", e.what()); }
66}
67
68void testNetworkInfoBuffer(const Device &device) {
69 try {
70 std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>(device, sizeof(networkModelData));
71 buffer->resize(sizeof(networkModelData));
72 std::memcpy(buffer->data(), networkModelData, sizeof(networkModelData));
73 Network network(device, buffer);
74
75 TEST_ASSERT(network.getIfmDims().size() == 1);
76 TEST_ASSERT(network.getOfmDims().size() == 1);
77 } catch (std::exception &e) { throw TestFailureException("NetworkInfo buffer test: ", e.what()); }
78}
79
80void testNetworkInfoUnparsableBuffer(const Device &device) {
81 try {
82 auto buffer = std::make_shared<Buffer>(device, sizeof(networkModelData) / 4);
83 buffer->resize(sizeof(networkModelData) / 4);
84 std::memcpy(buffer->data(), networkModelData + sizeof(networkModelData) / 4, sizeof(networkModelData) / 4);
85
86 try {
87 Network network(device, buffer);
88 FAIL();
89 } catch (Exception) {
90 // good, it should have thrown!
91 }
92 } catch (std::exception &e) { throw TestFailureException("NetworkInfo unparsable buffer test: ", e.what()); }
93}
94
95void testRunInferenceBuffer(const Device &device) {
96 try {
97 auto networkBuffer = std::make_shared<Buffer>(device, sizeof(networkModelData));
98 networkBuffer->resize(sizeof(networkModelData));
99 std::memcpy(networkBuffer->data(), networkModelData, sizeof(networkModelData));
100 auto network = std::make_shared<Network>(device, networkBuffer);
101
102 std::vector<std::shared_ptr<Buffer>> inputBuffers;
103 std::vector<std::shared_ptr<Buffer>> outputBuffers;
104
105 auto inputBuffer = std::make_shared<Buffer>(device, sizeof(inputData));
106 inputBuffer->resize(sizeof(inputData));
107 std::memcpy(inputBuffer->data(), inputData, sizeof(inputData));
108
109 inputBuffers.push_back(inputBuffer);
110 outputBuffers.push_back(std::make_shared<Buffer>(device, sizeof(expectedOutputData)));
111 std::vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters());
112
113 auto inference = std::make_shared<Inference>(network,
114 inputBuffers.begin(),
115 inputBuffers.end(),
116 outputBuffers.begin(),
117 outputBuffers.end(),
118 enabledCounters,
119 false);
120
121 bool timedout = inference->wait(defaultTimeout);
122 TEST_ASSERT(!timedout);
123
Davide Grohmann6d2e5b72022-08-24 17:01:40 +0200124 InferenceStatus status = inference->status();
125 TEST_ASSERT(status == InferenceStatus::OK);
126
127 bool success = inference->cancel();
128 TEST_ASSERT(!success);
129
Davide Grohmannf0364232022-06-16 17:42:58 +0200130 TEST_ASSERT(std::memcmp(expectedOutputData, outputBuffers[0]->data(), sizeof(expectedOutputData)) == 0);
131
132 } catch (std::exception &e) { throw TestFailureException("Inference run test: ", e.what()); }
133}
134
135} // namespace
136
137int main() {
138 Device device;
139
140 try {
141 testPing(device);
Davide Grohmannf0364232022-06-16 17:42:58 +0200142 testCapabilties(device);
143 testNetworkInfoNotExistentIndex(device);
144 testNetworkInfoBuffer(device);
145 testNetworkInfoUnparsableBuffer(device);
146 testRunInferenceBuffer(device);
147 } catch (TestFailureException &e) {
148 std::cerr << "Test failure: " << e.what() << std::endl;
149 return 1;
150 }
151
152 return 0;
153}