blob: 3b441fb6b0341c8e6be89e58651c27c2afa0c6b5 [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
Miao Wang2c4e0232019-12-26 18:03:56 -080019#include <android/hardware/neuralnetworks/1.3/IFencedExecutionCallback.h>
Michael Butler57568872019-07-25 17:22:11 -070020#include <chrono>
Miao Wang2c4e0232019-12-26 18:03:56 -080021
Lev Proleev3b13b552019-08-30 11:35:34 +010022#include "1.0/Utils.h"
Michael Butler9449a282019-12-11 19:08:08 -080023#include "1.3/Callbacks.h"
Lev Proleev3b13b552019-08-30 11:35:34 +010024#include "ExecutionBurstController.h"
25#include "GeneratedTestHarness.h"
26#include "TestHarness.h"
27#include "Utils.h"
28#include "VtsHalNeuralnetworks.h"
29
Lev Proleevb49dadf2019-08-30 11:57:18 +010030namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev3b13b552019-08-30 11:35:34 +010031
Michael Butler9449a282019-12-11 19:08:08 -080032using implementation::ExecutionCallback;
Lev Proleevb49dadf2019-08-30 11:57:18 +010033using V1_2::MeasureTiming;
34using V1_2::OutputShape;
35using V1_2::Timing;
Lev Proleev3b13b552019-08-30 11:35:34 +010036
Michael Butlerda1a6922020-03-11 18:45:45 -070037using ExecutionMutation = std::function<void(Request*)>;
38
Lev Proleev3b13b552019-08-30 11:35:34 +010039///////////////////////// UTILITY FUNCTIONS /////////////////////////
40
41static bool badTiming(Timing timing) {
42 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
43}
44
45// Primary validation function. This function will take a valid request, apply a
46// mutation to it to invalidate the request, then pass it to interface calls
Michael Butlerda1a6922020-03-11 18:45:45 -070047// that use the request.
Lev Proleev3b13b552019-08-30 11:35:34 +010048static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
Michael Butlerda1a6922020-03-11 18:45:45 -070049 const Request& originalRequest, const ExecutionMutation& mutate) {
50 Request request = originalRequest;
51 mutate(&request);
Lev Proleev3b13b552019-08-30 11:35:34 +010052
53 // We'd like to test both with timing requested and without timing
54 // requested. Rather than running each test both ways, we'll decide whether
55 // to request timing by hashing the message. We do not use std::hash because
56 // it is not guaranteed stable across executions.
57 char hash = 0;
58 for (auto c : message) {
59 hash ^= c;
60 };
61 MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO;
62
63 // asynchronous
64 {
Xusong Wang62a760c2019-10-25 12:07:17 -070065 SCOPED_TRACE(message + " [execute_1_3]");
Lev Proleev3b13b552019-08-30 11:35:34 +010066
67 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
68 Return<ErrorStatus> executeLaunchStatus =
Michael Butler9b9a8042020-02-13 16:37:22 -080069 preparedModel->execute_1_3(request, measure, {}, {}, executionCallback);
Lev Proleev3b13b552019-08-30 11:35:34 +010070 ASSERT_TRUE(executeLaunchStatus.isOk());
71 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
72
73 executionCallback->wait();
74 ErrorStatus executionReturnStatus = executionCallback->getStatus();
75 const auto& outputShapes = executionCallback->getOutputShapes();
76 Timing timing = executionCallback->getTiming();
77 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
78 ASSERT_EQ(outputShapes.size(), 0);
79 ASSERT_TRUE(badTiming(timing));
80 }
81
82 // synchronous
83 {
Xusong Wangebd88ba2019-10-28 11:11:19 -070084 SCOPED_TRACE(message + " [executeSynchronously_1_3]");
Lev Proleev3b13b552019-08-30 11:35:34 +010085
Xusong Wangebd88ba2019-10-28 11:11:19 -070086 Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
Michael Butler9b9a8042020-02-13 16:37:22 -080087 request, measure, {}, {},
Lev Proleev3b13b552019-08-30 11:35:34 +010088 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
89 const Timing& timing) {
90 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
91 EXPECT_EQ(outputShapes.size(), 0);
92 EXPECT_TRUE(badTiming(timing));
93 });
94 ASSERT_TRUE(executeStatus.isOk());
95 }
96
97 // burst
Xusong Wang931d5a12019-11-27 12:46:48 -080098 // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains V1_2.
Michael Butler9b9a8042020-02-13 16:37:22 -080099 {
Lev Proleev3b13b552019-08-30 11:35:34 +0100100 SCOPED_TRACE(message + " [burst]");
101
Xusong Wang931d5a12019-11-27 12:46:48 -0800102 ASSERT_TRUE(nn::compliantWithV1_0(request));
103 V1_0::Request request10 = nn::convertToV1_0(request);
104
Lev Proleev3b13b552019-08-30 11:35:34 +0100105 // create burst
106 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler57568872019-07-25 17:22:11 -0700107 android::nn::ExecutionBurstController::create(preparedModel,
108 std::chrono::microseconds{0});
Lev Proleev3b13b552019-08-30 11:35:34 +0100109 ASSERT_NE(nullptr, burst.get());
110
111 // create memory keys
Xusong Wang931d5a12019-11-27 12:46:48 -0800112 std::vector<intptr_t> keys(request10.pools.size());
Lev Proleev3b13b552019-08-30 11:35:34 +0100113 for (size_t i = 0; i < keys.size(); ++i) {
Xusong Wang931d5a12019-11-27 12:46:48 -0800114 keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
Lev Proleev3b13b552019-08-30 11:35:34 +0100115 }
116
117 // execute and verify
Xusong Wang931d5a12019-11-27 12:46:48 -0800118 const auto [n, outputShapes, timing, fallback] = burst->compute(request10, measure, keys);
Slava Shklyaev8230f8c2020-08-20 11:55:52 +0100119 const ErrorStatus status = nn::convertToV1_3(nn::convertResultCodeToErrorStatus(n));
Michael Butler57568872019-07-25 17:22:11 -0700120 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
Lev Proleev3b13b552019-08-30 11:35:34 +0100121 EXPECT_EQ(outputShapes.size(), 0);
122 EXPECT_TRUE(badTiming(timing));
Michael Butler57568872019-07-25 17:22:11 -0700123 EXPECT_FALSE(fallback);
Lev Proleev3b13b552019-08-30 11:35:34 +0100124
125 // additional burst testing
Xusong Wang931d5a12019-11-27 12:46:48 -0800126 if (request10.pools.size() > 0) {
Lev Proleev3b13b552019-08-30 11:35:34 +0100127 // valid free
128 burst->freeMemory(keys.front());
129
130 // negative test: invalid free of unknown (blank) memory
131 burst->freeMemory(intptr_t{});
132
133 // negative test: double free of memory
134 burst->freeMemory(keys.front());
135 }
136 }
Miao Wang2c4e0232019-12-26 18:03:56 -0800137
138 // dispatch
139 {
140 SCOPED_TRACE(message + " [executeFenced]");
Miao Wang15a25f62020-02-06 15:36:41 -0800141 Return<void> ret =
Michael Butler9b9a8042020-02-13 16:37:22 -0800142 preparedModel->executeFenced(request, {}, MeasureTiming::NO, {}, {}, {},
Miao Wang15a25f62020-02-06 15:36:41 -0800143 [](ErrorStatus error, const hidl_handle& handle,
144 const sp<IFencedExecutionCallback>& callback) {
145 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
146 ASSERT_EQ(handle.getNativeHandle(), nullptr);
147 ASSERT_EQ(callback, nullptr);
148 });
Miao Wang2c4e0232019-12-26 18:03:56 -0800149 ASSERT_TRUE(ret.isOk());
150 }
Lev Proleev3b13b552019-08-30 11:35:34 +0100151}
152
153///////////////////////// REMOVE INPUT ////////////////////////////////////
154
155static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
156 for (size_t input = 0; input < request.inputs.size(); ++input) {
157 const std::string message = "removeInput: removed input " + std::to_string(input);
158 validate(preparedModel, message, request,
159 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
160 }
161}
162
163///////////////////////// REMOVE OUTPUT ////////////////////////////////////
164
165static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
166 for (size_t output = 0; output < request.outputs.size(); ++output) {
167 const std::string message = "removeOutput: removed Output " + std::to_string(output);
168 validate(preparedModel, message, request,
169 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
170 }
171}
172
173///////////////////////////// ENTRY POINT //////////////////////////////////
174
Michael Butler9b9a8042020-02-13 16:37:22 -0800175void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
Lev Proleev3b13b552019-08-30 11:35:34 +0100176 removeInputTest(preparedModel, request);
177 removeOutputTest(preparedModel, request);
178}
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(
Slava Shklyaevf034bf92020-02-11 14:27:02 +0000183 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