Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 1 | /* |
Mikael Olsson | dc18cea | 2024-02-16 11:04:46 +0100 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022-2024 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> |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 22 | #include <errno.h> |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 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 Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 34 | #include "test_assertions.hpp" |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 35 | |
| 36 | using namespace EthosU; |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | int64_t defaultTimeout = 60000000000; |
| 41 | |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 42 | void 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 Olsson | 8f918c6 | 2023-10-12 15:30:32 +0200 | [diff] [blame] | 51 | void 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 Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 64 | void 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()); |
Mikael Olsson | dc18cea | 2024-02-16 11:04:46 +0100 | [diff] [blame] | 71 | TEST_ASSERT(capabilities.hwCfg.type == HardwareConfiguration::DeviceType::SUBSYSTEM); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 74 | void testBufferSeek(const Device &device) { |
| 75 | try { |
| 76 | Buffer buf{device, 1024}; |
| 77 | |
| 78 | // SEEK_END should return the size of the buffer |
| 79 | TEST_ASSERT(lseek(buf.getFd(), 0, SEEK_END) == 1024); |
| 80 | |
| 81 | // SEEK_SET is supported when moving the file pointer to the start |
| 82 | TEST_ASSERT(lseek(buf.getFd(), 0, SEEK_SET) == 0); |
| 83 | |
| 84 | // SEEK_CUR is not supported |
| 85 | errno = 0; |
| 86 | TEST_ASSERT(lseek(buf.getFd(), 0, SEEK_CUR) == -1); |
| 87 | TEST_ASSERT(errno == EINVAL); |
| 88 | |
| 89 | // Non-zero offset is not supported |
| 90 | errno = 0; |
| 91 | TEST_ASSERT(lseek(buf.getFd(), 1, SEEK_CUR) == -1); |
| 92 | TEST_ASSERT(errno == EINVAL); |
| 93 | |
| 94 | errno = 0; |
| 95 | TEST_ASSERT(lseek(buf.getFd(), 2, SEEK_END) == -1); |
| 96 | TEST_ASSERT(errno == EINVAL); |
| 97 | |
| 98 | errno = 0; |
| 99 | TEST_ASSERT(lseek(buf.getFd(), 3, SEEK_SET) == -1); |
| 100 | TEST_ASSERT(errno == EINVAL); |
| 101 | } catch (std::exception &e) { throw TestFailureException("Buffer seek test: ", e.what()); } |
| 102 | } |
| 103 | |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 104 | void testNetworkInfoNotExistentIndex(const Device &device) { |
| 105 | try { |
| 106 | Network(device, 0); |
| 107 | FAIL(); |
| 108 | } catch (Exception &e) { |
| 109 | // good it should have thrown |
| 110 | } catch (std::exception &e) { throw TestFailureException("NetworkInfo no index test: ", e.what()); } |
| 111 | } |
| 112 | |
| 113 | void testNetworkInfoBuffer(const Device &device) { |
| 114 | try { |
Mikael Olsson | c081e59 | 2023-10-30 11:10:56 +0100 | [diff] [blame] | 115 | Network network(device, networkModelData, sizeof(networkModelData)); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 116 | TEST_ASSERT(network.getIfmDims().size() == 1); |
| 117 | TEST_ASSERT(network.getOfmDims().size() == 1); |
| 118 | } catch (std::exception &e) { throw TestFailureException("NetworkInfo buffer test: ", e.what()); } |
| 119 | } |
| 120 | |
| 121 | void testNetworkInfoUnparsableBuffer(const Device &device) { |
| 122 | try { |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 123 | try { |
Mikael Olsson | c081e59 | 2023-10-30 11:10:56 +0100 | [diff] [blame] | 124 | Network network(device, networkModelData + sizeof(networkModelData) / 4, sizeof(networkModelData) / 4); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 125 | FAIL(); |
| 126 | } catch (Exception) { |
| 127 | // good, it should have thrown! |
| 128 | } |
| 129 | } catch (std::exception &e) { throw TestFailureException("NetworkInfo unparsable buffer test: ", e.what()); } |
| 130 | } |
| 131 | |
Mikael Olsson | 45d4799 | 2023-10-12 15:32:56 +0200 | [diff] [blame] | 132 | void testNetworkInvalidType(const Device &device) { |
| 133 | const std::string expected_error = |
| 134 | std::string("IOCTL cmd=") + std::to_string(ETHOSU_IOCTL_NETWORK_CREATE) + " failed: " + std::strerror(EINVAL); |
| 135 | struct ethosu_uapi_network_create net_req = {}; |
| 136 | net_req.type = ETHOSU_UAPI_NETWORK_INDEX + 1; |
| 137 | try { |
| 138 | int r = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, &net_req); |
| 139 | FAIL(); |
| 140 | } catch (Exception &e) { |
| 141 | // The call is expected to throw |
| 142 | TEST_ASSERT(expected_error.compare(e.what()) == 0); |
| 143 | } catch (std::exception &e) { throw TestFailureException("NetworkCreate invalid type test: ", e.what()); } |
| 144 | } |
| 145 | |
Mikael Olsson | c081e59 | 2023-10-30 11:10:56 +0100 | [diff] [blame] | 146 | void testNetworkInvalidDataPtr(const Device &device) { |
| 147 | const std::string expected_error = |
| 148 | std::string("IOCTL cmd=") + std::to_string(ETHOSU_IOCTL_NETWORK_CREATE) + " failed: " + std::strerror(EINVAL); |
| 149 | struct ethosu_uapi_network_create net_req = {}; |
| 150 | net_req.type = ETHOSU_UAPI_NETWORK_USER_BUFFER; |
| 151 | net_req.network.data_ptr = 0U; |
| 152 | net_req.network.size = 128U; |
| 153 | try { |
| 154 | int r = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, &net_req); |
| 155 | FAIL(); |
| 156 | } catch (Exception &e) { |
| 157 | // The call is expected to throw |
| 158 | TEST_ASSERT(expected_error.compare(e.what()) == 0); |
| 159 | } catch (std::exception &e) { throw TestFailureException("NetworkCreate invalid data ptr: ", e.what()); } |
| 160 | } |
| 161 | |
| 162 | void testNetworkInvalidDataSize(const Device &device) { |
| 163 | const std::string expected_error = |
| 164 | std::string("IOCTL cmd=") + std::to_string(ETHOSU_IOCTL_NETWORK_CREATE) + " failed: " + std::strerror(EINVAL); |
| 165 | struct ethosu_uapi_network_create net_req = {}; |
| 166 | net_req.type = ETHOSU_UAPI_NETWORK_USER_BUFFER; |
| 167 | net_req.network.data_ptr = reinterpret_cast<uintptr_t>(networkModelData); |
| 168 | net_req.network.size = 0U; |
| 169 | try { |
| 170 | int r = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, &net_req); |
| 171 | FAIL(); |
| 172 | } catch (Exception &e) { |
| 173 | // The call is expected to throw |
| 174 | TEST_ASSERT(expected_error.compare(e.what()) == 0); |
| 175 | } catch (std::exception &e) { throw TestFailureException("NetworkCreate invalid data size: ", e.what()); } |
| 176 | } |
| 177 | |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 178 | void testRunInferenceBuffer(const Device &device) { |
| 179 | try { |
Mikael Olsson | c081e59 | 2023-10-30 11:10:56 +0100 | [diff] [blame] | 180 | auto network = std::make_shared<Network>(device, networkModelData, sizeof(networkModelData)); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 181 | |
| 182 | std::vector<std::shared_ptr<Buffer>> inputBuffers; |
| 183 | std::vector<std::shared_ptr<Buffer>> outputBuffers; |
| 184 | |
| 185 | auto inputBuffer = std::make_shared<Buffer>(device, sizeof(inputData)); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 186 | std::memcpy(inputBuffer->data(), inputData, sizeof(inputData)); |
| 187 | |
| 188 | inputBuffers.push_back(inputBuffer); |
| 189 | outputBuffers.push_back(std::make_shared<Buffer>(device, sizeof(expectedOutputData))); |
| 190 | std::vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters()); |
| 191 | |
| 192 | auto inference = std::make_shared<Inference>(network, |
| 193 | inputBuffers.begin(), |
| 194 | inputBuffers.end(), |
| 195 | outputBuffers.begin(), |
| 196 | outputBuffers.end(), |
| 197 | enabledCounters, |
| 198 | false); |
| 199 | |
| 200 | bool timedout = inference->wait(defaultTimeout); |
| 201 | TEST_ASSERT(!timedout); |
| 202 | |
Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 203 | InferenceStatus status = inference->status(); |
| 204 | TEST_ASSERT(status == InferenceStatus::OK); |
| 205 | |
| 206 | bool success = inference->cancel(); |
| 207 | TEST_ASSERT(!success); |
| 208 | |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 209 | TEST_ASSERT(std::memcmp(expectedOutputData, outputBuffers[0]->data(), sizeof(expectedOutputData)) == 0); |
| 210 | |
| 211 | } catch (std::exception &e) { throw TestFailureException("Inference run test: ", e.what()); } |
| 212 | } |
| 213 | |
| 214 | } // namespace |
| 215 | |
| 216 | int main() { |
| 217 | Device device; |
| 218 | |
| 219 | try { |
| 220 | testPing(device); |
Mikael Olsson | 8f918c6 | 2023-10-12 15:30:32 +0200 | [diff] [blame] | 221 | testDriverVersion(device); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 222 | testCapabilties(device); |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 223 | testBufferSeek(device); |
Mikael Olsson | 45d4799 | 2023-10-12 15:32:56 +0200 | [diff] [blame] | 224 | testNetworkInvalidType(device); |
Mikael Olsson | c081e59 | 2023-10-30 11:10:56 +0100 | [diff] [blame] | 225 | testNetworkInvalidDataPtr(device); |
| 226 | testNetworkInvalidDataSize(device); |
Davide Grohmann | f036423 | 2022-06-16 17:42:58 +0200 | [diff] [blame] | 227 | testNetworkInfoNotExistentIndex(device); |
| 228 | testNetworkInfoBuffer(device); |
| 229 | testNetworkInfoUnparsableBuffer(device); |
| 230 | testRunInferenceBuffer(device); |
| 231 | } catch (TestFailureException &e) { |
| 232 | std::cerr << "Test failure: " << e.what() << std::endl; |
| 233 | return 1; |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |