Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 1 | /* |
Mikael Olsson | c4ae218 | 2023-10-12 15:29:30 +0200 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 3 | * 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 Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 33 | #include "test_assertions.hpp" |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 34 | |
| 35 | using namespace EthosU; |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | int64_t defaultTimeout = 60000000000; |
| 40 | |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 41 | void 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 Olsson | 8f918c6 | 2023-10-12 15:30:32 +0200 | [diff] [blame] | 50 | void 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 Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 63 | void 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 | |
| 72 | void 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 | |
| 81 | void 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 | |
| 93 | void 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 | |
Mikael Olsson | 45d4799 | 2023-10-12 15:32:56 +0200 | [diff] [blame^] | 108 | void testNetworkInvalidType(const Device &device) { |
| 109 | const std::string expected_error = |
| 110 | std::string("IOCTL cmd=") + std::to_string(ETHOSU_IOCTL_NETWORK_CREATE) + " failed: " + std::strerror(EINVAL); |
| 111 | struct ethosu_uapi_network_create net_req = {}; |
| 112 | net_req.type = ETHOSU_UAPI_NETWORK_INDEX + 1; |
| 113 | try { |
| 114 | int r = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, &net_req); |
| 115 | FAIL(); |
| 116 | } catch (Exception &e) { |
| 117 | // The call is expected to throw |
| 118 | TEST_ASSERT(expected_error.compare(e.what()) == 0); |
| 119 | } catch (std::exception &e) { throw TestFailureException("NetworkCreate invalid type test: ", e.what()); } |
| 120 | } |
| 121 | |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 122 | void testRunInferenceBuffer(const Device &device) { |
| 123 | try { |
| 124 | auto networkBuffer = std::make_shared<Buffer>(device, sizeof(networkModelData)); |
| 125 | networkBuffer->resize(sizeof(networkModelData)); |
| 126 | std::memcpy(networkBuffer->data(), networkModelData, sizeof(networkModelData)); |
| 127 | auto network = std::make_shared<Network>(device, networkBuffer); |
| 128 | |
| 129 | std::vector<std::shared_ptr<Buffer>> inputBuffers; |
| 130 | std::vector<std::shared_ptr<Buffer>> outputBuffers; |
| 131 | |
| 132 | auto inputBuffer = std::make_shared<Buffer>(device, sizeof(inputData)); |
| 133 | inputBuffer->resize(sizeof(inputData)); |
| 134 | std::memcpy(inputBuffer->data(), inputData, sizeof(inputData)); |
| 135 | |
| 136 | inputBuffers.push_back(inputBuffer); |
| 137 | outputBuffers.push_back(std::make_shared<Buffer>(device, sizeof(expectedOutputData))); |
| 138 | std::vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters()); |
| 139 | |
| 140 | auto inference = std::make_shared<Inference>(network, |
| 141 | inputBuffers.begin(), |
| 142 | inputBuffers.end(), |
| 143 | outputBuffers.begin(), |
| 144 | outputBuffers.end(), |
| 145 | enabledCounters, |
| 146 | false); |
| 147 | |
| 148 | bool timedout = inference->wait(defaultTimeout); |
| 149 | TEST_ASSERT(!timedout); |
| 150 | |
Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 151 | InferenceStatus status = inference->status(); |
| 152 | TEST_ASSERT(status == InferenceStatus::OK); |
| 153 | |
| 154 | bool success = inference->cancel(); |
| 155 | TEST_ASSERT(!success); |
| 156 | |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 157 | TEST_ASSERT(std::memcmp(expectedOutputData, outputBuffers[0]->data(), sizeof(expectedOutputData)) == 0); |
| 158 | |
| 159 | } catch (std::exception &e) { throw TestFailureException("Inference run test: ", e.what()); } |
| 160 | } |
| 161 | |
| 162 | } // namespace |
| 163 | |
| 164 | int main() { |
| 165 | Device device; |
| 166 | |
| 167 | try { |
| 168 | testPing(device); |
Mikael Olsson | 8f918c6 | 2023-10-12 15:30:32 +0200 | [diff] [blame] | 169 | testDriverVersion(device); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 170 | testCapabilties(device); |
Mikael Olsson | 45d4799 | 2023-10-12 15:32:56 +0200 | [diff] [blame^] | 171 | testNetworkInvalidType(device); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 172 | testNetworkInfoNotExistentIndex(device); |
| 173 | testNetworkInfoBuffer(device); |
| 174 | testNetworkInfoUnparsableBuffer(device); |
| 175 | testRunInferenceBuffer(device); |
| 176 | } catch (TestFailureException &e) { |
| 177 | std::cerr << "Test failure: " << e.what() << std::endl; |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |