blob: be4112ac2d67de799d3c4adbe4e062a793a512f5 [file] [log] [blame]
Lev Proleev3b13b552019-08-30 11:35:34 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "neuralnetworks_hidl_hal_test"
18
Michael Butler57568872019-07-25 17:22:11 -070019#include <chrono>
Lev Proleev3b13b552019-08-30 11:35:34 +010020#include "1.0/Utils.h"
Michael Butler9449a282019-12-11 19:08:08 -080021#include "1.3/Callbacks.h"
Lev Proleev3b13b552019-08-30 11:35:34 +010022#include "ExecutionBurstController.h"
23#include "GeneratedTestHarness.h"
24#include "TestHarness.h"
25#include "Utils.h"
26#include "VtsHalNeuralnetworks.h"
27
Lev Proleevb49dadf2019-08-30 11:57:18 +010028namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev3b13b552019-08-30 11:35:34 +010029
Michael Butler9449a282019-12-11 19:08:08 -080030using implementation::ExecutionCallback;
Lev Proleevb49dadf2019-08-30 11:57:18 +010031using V1_2::MeasureTiming;
32using V1_2::OutputShape;
33using V1_2::Timing;
Lev Proleev3b13b552019-08-30 11:35:34 +010034
35///////////////////////// UTILITY FUNCTIONS /////////////////////////
36
37static bool badTiming(Timing timing) {
38 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
39}
40
41// Primary validation function. This function will take a valid request, apply a
42// mutation to it to invalidate the request, then pass it to interface calls
43// that use the request. Note that the request here is passed by value, and any
44// mutation to the request does not leave this function.
45static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
Michael Butler616701d2020-01-07 14:52:44 -080046 Request request, const std::function<void(Request*)>& mutation,
47 bool testDeadline = false) {
Lev Proleev3b13b552019-08-30 11:35:34 +010048 mutation(&request);
49
50 // We'd like to test both with timing requested and without timing
51 // requested. Rather than running each test both ways, we'll decide whether
52 // to request timing by hashing the message. We do not use std::hash because
53 // it is not guaranteed stable across executions.
54 char hash = 0;
55 for (auto c : message) {
56 hash ^= c;
57 };
58 MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO;
59
Michael Butler616701d2020-01-07 14:52:44 -080060 OptionalTimePoint deadline;
61 if (testDeadline) {
62 deadline.nanoseconds(std::numeric_limits<uint64_t>::max());
63 }
64
Lev Proleev3b13b552019-08-30 11:35:34 +010065 // asynchronous
66 {
Xusong Wang62a760c2019-10-25 12:07:17 -070067 SCOPED_TRACE(message + " [execute_1_3]");
Lev Proleev3b13b552019-08-30 11:35:34 +010068
69 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
70 Return<ErrorStatus> executeLaunchStatus =
Michael Butler616701d2020-01-07 14:52:44 -080071 preparedModel->execute_1_3(request, measure, deadline, executionCallback);
Lev Proleev3b13b552019-08-30 11:35:34 +010072 ASSERT_TRUE(executeLaunchStatus.isOk());
73 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
74
75 executionCallback->wait();
76 ErrorStatus executionReturnStatus = executionCallback->getStatus();
77 const auto& outputShapes = executionCallback->getOutputShapes();
78 Timing timing = executionCallback->getTiming();
79 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
80 ASSERT_EQ(outputShapes.size(), 0);
81 ASSERT_TRUE(badTiming(timing));
82 }
83
84 // synchronous
85 {
Xusong Wangebd88ba2019-10-28 11:11:19 -070086 SCOPED_TRACE(message + " [executeSynchronously_1_3]");
Lev Proleev3b13b552019-08-30 11:35:34 +010087
Xusong Wangebd88ba2019-10-28 11:11:19 -070088 Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
Michael Butler616701d2020-01-07 14:52:44 -080089 request, measure, deadline,
Lev Proleev3b13b552019-08-30 11:35:34 +010090 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
91 const Timing& timing) {
92 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
93 EXPECT_EQ(outputShapes.size(), 0);
94 EXPECT_TRUE(badTiming(timing));
95 });
96 ASSERT_TRUE(executeStatus.isOk());
97 }
98
99 // burst
Xusong Wang931d5a12019-11-27 12:46:48 -0800100 // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains V1_2.
Michael Butler616701d2020-01-07 14:52:44 -0800101 if (!testDeadline) {
Lev Proleev3b13b552019-08-30 11:35:34 +0100102 SCOPED_TRACE(message + " [burst]");
103
Xusong Wang931d5a12019-11-27 12:46:48 -0800104 ASSERT_TRUE(nn::compliantWithV1_0(request));
105 V1_0::Request request10 = nn::convertToV1_0(request);
106
Lev Proleev3b13b552019-08-30 11:35:34 +0100107 // create burst
108 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler57568872019-07-25 17:22:11 -0700109 android::nn::ExecutionBurstController::create(preparedModel,
110 std::chrono::microseconds{0});
Lev Proleev3b13b552019-08-30 11:35:34 +0100111 ASSERT_NE(nullptr, burst.get());
112
113 // create memory keys
Xusong Wang931d5a12019-11-27 12:46:48 -0800114 std::vector<intptr_t> keys(request10.pools.size());
Lev Proleev3b13b552019-08-30 11:35:34 +0100115 for (size_t i = 0; i < keys.size(); ++i) {
Xusong Wang931d5a12019-11-27 12:46:48 -0800116 keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
Lev Proleev3b13b552019-08-30 11:35:34 +0100117 }
118
119 // execute and verify
Xusong Wang931d5a12019-11-27 12:46:48 -0800120 const auto [n, outputShapes, timing, fallback] = burst->compute(request10, measure, keys);
Michael Butler57568872019-07-25 17:22:11 -0700121 const ErrorStatus status = nn::convertResultCodeToErrorStatus(n);
122 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
Lev Proleev3b13b552019-08-30 11:35:34 +0100123 EXPECT_EQ(outputShapes.size(), 0);
124 EXPECT_TRUE(badTiming(timing));
Michael Butler57568872019-07-25 17:22:11 -0700125 EXPECT_FALSE(fallback);
Lev Proleev3b13b552019-08-30 11:35:34 +0100126
127 // additional burst testing
Xusong Wang931d5a12019-11-27 12:46:48 -0800128 if (request10.pools.size() > 0) {
Lev Proleev3b13b552019-08-30 11:35:34 +0100129 // valid free
130 burst->freeMemory(keys.front());
131
132 // negative test: invalid free of unknown (blank) memory
133 burst->freeMemory(intptr_t{});
134
135 // negative test: double free of memory
136 burst->freeMemory(keys.front());
137 }
138 }
139}
140
141///////////////////////// REMOVE INPUT ////////////////////////////////////
142
143static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
144 for (size_t input = 0; input < request.inputs.size(); ++input) {
145 const std::string message = "removeInput: removed input " + std::to_string(input);
146 validate(preparedModel, message, request,
147 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
148 }
149}
150
151///////////////////////// REMOVE OUTPUT ////////////////////////////////////
152
153static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
154 for (size_t output = 0; output < request.outputs.size(); ++output) {
155 const std::string message = "removeOutput: removed Output " + std::to_string(output);
156 validate(preparedModel, message, request,
157 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
158 }
159}
160
Michael Butler616701d2020-01-07 14:52:44 -0800161///////////////////////// DEADLINE ////////////////////////////////////
162
163static void deadlineTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
164 const std::string message = "deadlineTest: deadline not supported";
165 const auto noop = [](Request*) {};
166 validate(preparedModel, message, request, noop, /*testDeadline=*/true);
167}
168
Lev Proleev3b13b552019-08-30 11:35:34 +0100169///////////////////////////// ENTRY POINT //////////////////////////////////
170
Michael Butler616701d2020-01-07 14:52:44 -0800171void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request,
172 bool executionDeadlineSupported) {
Lev Proleev3b13b552019-08-30 11:35:34 +0100173 removeInputTest(preparedModel, request);
174 removeOutputTest(preparedModel, request);
Michael Butler616701d2020-01-07 14:52:44 -0800175 if (!executionDeadlineSupported) {
176 deadlineTest(preparedModel, request);
177 }
Lev Proleev3b13b552019-08-30 11:35:34 +0100178}
179
180void validateRequestFailure(const sp<IPreparedModel>& preparedModel, const Request& request) {
Xusong Wangebd88ba2019-10-28 11:11:19 -0700181 SCOPED_TRACE("Expecting request to fail [executeSynchronously_1_3]");
182 Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
Michael Butler9449a282019-12-11 19:08:08 -0800183 request, MeasureTiming::NO, {},
Lev Proleev3b13b552019-08-30 11:35:34 +0100184 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
185 ASSERT_NE(ErrorStatus::NONE, error);
186 EXPECT_EQ(outputShapes.size(), 0);
187 EXPECT_TRUE(badTiming(timing));
188 });
189 ASSERT_TRUE(executeStatus.isOk());
190}
191
Lev Proleevb49dadf2019-08-30 11:57:18 +0100192} // namespace android::hardware::neuralnetworks::V1_3::vts::functional