Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 1 | /* |
Mikael Olsson | 0754515 | 2023-10-17 13:05:38 +0200 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +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" |
| 33 | #include "test_assertions.hpp" |
| 34 | |
| 35 | using namespace EthosU; |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | int64_t defaultTimeout = 60000000000; |
| 40 | |
| 41 | void testCancelInference(const Device &device) { |
| 42 | try { |
| 43 | auto networkBuffer = std::make_shared<Buffer>(device, sizeof(networkModelData)); |
Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 44 | std::memcpy(networkBuffer->data(), networkModelData, sizeof(networkModelData)); |
| 45 | auto network = std::make_shared<Network>(device, networkBuffer); |
| 46 | |
| 47 | std::vector<std::shared_ptr<Buffer>> inputBuffers; |
| 48 | std::vector<std::shared_ptr<Buffer>> outputBuffers; |
| 49 | |
| 50 | auto inputBuffer = std::make_shared<Buffer>(device, sizeof(inputData)); |
Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 51 | std::memcpy(inputBuffer->data(), inputData, sizeof(inputData)); |
| 52 | |
| 53 | inputBuffers.push_back(inputBuffer); |
| 54 | outputBuffers.push_back(std::make_shared<Buffer>(device, sizeof(expectedOutputData))); |
| 55 | std::vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters()); |
| 56 | |
| 57 | auto inference = std::make_shared<Inference>(network, |
| 58 | inputBuffers.begin(), |
| 59 | inputBuffers.end(), |
| 60 | outputBuffers.begin(), |
| 61 | outputBuffers.end(), |
| 62 | enabledCounters, |
| 63 | false); |
| 64 | |
| 65 | InferenceStatus status = inference->status(); |
| 66 | TEST_ASSERT(status == InferenceStatus::RUNNING); |
| 67 | |
| 68 | bool success = inference->cancel(); |
| 69 | TEST_ASSERT(success); |
| 70 | |
| 71 | status = inference->status(); |
| 72 | TEST_ASSERT(status == InferenceStatus::ABORTED); |
| 73 | |
| 74 | bool timedout = inference->wait(defaultTimeout); |
| 75 | TEST_ASSERT(!timedout); |
| 76 | |
| 77 | } catch (std::exception &e) { throw TestFailureException("Inference run test: ", e.what()); } |
| 78 | } |
| 79 | |
| 80 | void testRejectInference(const Device &device) { |
| 81 | try { |
| 82 | auto networkBuffer = std::make_shared<Buffer>(device, sizeof(networkModelData)); |
Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 83 | std::memcpy(networkBuffer->data(), networkModelData, sizeof(networkModelData)); |
| 84 | auto network = std::make_shared<Network>(device, networkBuffer); |
| 85 | |
| 86 | std::vector<std::shared_ptr<Buffer>> inputBuffers; |
| 87 | std::vector<std::shared_ptr<Buffer>> outputBuffers; |
| 88 | |
| 89 | auto inputBuffer = std::make_shared<Buffer>(device, sizeof(inputData)); |
Davide Grohmann | 6d2e5b7 | 2022-08-24 17:01:40 +0200 | [diff] [blame] | 90 | std::memcpy(inputBuffer->data(), inputData, sizeof(inputData)); |
| 91 | |
| 92 | inputBuffers.push_back(inputBuffer); |
| 93 | outputBuffers.push_back(std::make_shared<Buffer>(device, sizeof(expectedOutputData))); |
| 94 | std::vector<uint8_t> enabledCounters(Inference::getMaxPmuEventCounters()); |
| 95 | |
| 96 | std::shared_ptr<Inference> inferences[5]; |
| 97 | |
| 98 | for (int i = 0; i < 5; i++) { |
| 99 | inferences[i] = std::make_shared<Inference>(network, |
| 100 | inputBuffers.begin(), |
| 101 | inputBuffers.end(), |
| 102 | outputBuffers.begin(), |
| 103 | outputBuffers.end(), |
| 104 | enabledCounters, |
| 105 | false); |
| 106 | |
| 107 | InferenceStatus status = inferences[i]->status(); |
| 108 | TEST_ASSERT(status == InferenceStatus::RUNNING); |
| 109 | } |
| 110 | |
| 111 | auto inference = std::make_shared<Inference>(network, |
| 112 | inputBuffers.begin(), |
| 113 | inputBuffers.end(), |
| 114 | outputBuffers.begin(), |
| 115 | outputBuffers.end(), |
| 116 | enabledCounters, |
| 117 | false); |
| 118 | |
| 119 | bool timedout = inference->wait(defaultTimeout); |
| 120 | TEST_ASSERT(!timedout); |
| 121 | |
| 122 | InferenceStatus status = inference->status(); |
| 123 | TEST_ASSERT(status == InferenceStatus::REJECTED); |
| 124 | |
| 125 | for (int i = 0; i < 5; i++) { |
| 126 | |
| 127 | bool success = inferences[i]->cancel(); |
| 128 | TEST_ASSERT(success); |
| 129 | |
| 130 | InferenceStatus status = inferences[i]->status(); |
| 131 | TEST_ASSERT(status == InferenceStatus::ABORTED); |
| 132 | |
| 133 | bool timedout = inference->wait(defaultTimeout); |
| 134 | TEST_ASSERT(!timedout); |
| 135 | } |
| 136 | } catch (std::exception &e) { throw TestFailureException("Inference run test: ", e.what()); } |
| 137 | } |
| 138 | |
| 139 | } // namespace |
| 140 | |
| 141 | int main() { |
| 142 | Device device; |
| 143 | |
| 144 | try { |
| 145 | testCancelInference(device); |
| 146 | testRejectInference(device); |
| 147 | } catch (TestFailureException &e) { |
| 148 | std::cerr << "Test failure: " << e.what() << std::endl; |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |