blob: 96dc589d50ac1a66854382677c0cc2cf2ed50b71 [file] [log] [blame]
Lev Proleev13fdfcd2019-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 Butler648ada52019-07-25 17:22:11 -070019#include <chrono>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010020#include "1.0/Utils.h"
21#include "1.2/Callbacks.h"
22#include "ExecutionBurstController.h"
23#include "GeneratedTestHarness.h"
24#include "TestHarness.h"
25#include "Utils.h"
26#include "VtsHalNeuralnetworks.h"
27
Lev Proleev26d1bc82019-08-30 11:57:18 +010028namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010029
Lev Proleev13fdfcd2019-08-30 11:35:34 +010030using V1_0::ErrorStatus;
Lev Proleev26d1bc82019-08-30 11:57:18 +010031using V1_2::MeasureTiming;
32using V1_2::OutputShape;
33using V1_2::Timing;
34using V1_2::implementation::ExecutionCallback;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010035
36///////////////////////// UTILITY FUNCTIONS /////////////////////////
37
38static bool badTiming(Timing timing) {
39 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
40}
41
42// Primary validation function. This function will take a valid request, apply a
43// mutation to it to invalidate the request, then pass it to interface calls
44// that use the request. Note that the request here is passed by value, and any
45// mutation to the request does not leave this function.
46static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
47 Request request, const std::function<void(Request*)>& mutation) {
48 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
60 // asynchronous
61 {
Xusong Wang1b3f4262019-10-25 12:07:17 -070062 SCOPED_TRACE(message + " [execute_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010063
64 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
65 Return<ErrorStatus> executeLaunchStatus =
Xusong Wang1b3f4262019-10-25 12:07:17 -070066 preparedModel->execute_1_3(request, measure, executionCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010067 ASSERT_TRUE(executeLaunchStatus.isOk());
68 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
69
70 executionCallback->wait();
71 ErrorStatus executionReturnStatus = executionCallback->getStatus();
72 const auto& outputShapes = executionCallback->getOutputShapes();
73 Timing timing = executionCallback->getTiming();
74 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
75 ASSERT_EQ(outputShapes.size(), 0);
76 ASSERT_TRUE(badTiming(timing));
77 }
78
79 // synchronous
80 {
Xusong Wangd4a060b2019-10-28 11:11:19 -070081 SCOPED_TRACE(message + " [executeSynchronously_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010082
Xusong Wangd4a060b2019-10-28 11:11:19 -070083 Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
Lev Proleev13fdfcd2019-08-30 11:35:34 +010084 request, measure,
85 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
86 const Timing& timing) {
87 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
88 EXPECT_EQ(outputShapes.size(), 0);
89 EXPECT_TRUE(badTiming(timing));
90 });
91 ASSERT_TRUE(executeStatus.isOk());
92 }
93
94 // burst
Xusong Wangb345a462019-11-27 12:46:48 -080095 // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains V1_2.
Lev Proleev13fdfcd2019-08-30 11:35:34 +010096 {
97 SCOPED_TRACE(message + " [burst]");
98
Xusong Wangb345a462019-11-27 12:46:48 -080099 ASSERT_TRUE(nn::compliantWithV1_0(request));
100 V1_0::Request request10 = nn::convertToV1_0(request);
101
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100102 // create burst
103 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler648ada52019-07-25 17:22:11 -0700104 android::nn::ExecutionBurstController::create(preparedModel,
105 std::chrono::microseconds{0});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100106 ASSERT_NE(nullptr, burst.get());
107
108 // create memory keys
Xusong Wangb345a462019-11-27 12:46:48 -0800109 std::vector<intptr_t> keys(request10.pools.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100110 for (size_t i = 0; i < keys.size(); ++i) {
Xusong Wangb345a462019-11-27 12:46:48 -0800111 keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100112 }
113
114 // execute and verify
Xusong Wangb345a462019-11-27 12:46:48 -0800115 const auto [n, outputShapes, timing, fallback] = burst->compute(request10, measure, keys);
Michael Butler648ada52019-07-25 17:22:11 -0700116 const ErrorStatus status = nn::convertResultCodeToErrorStatus(n);
117 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100118 EXPECT_EQ(outputShapes.size(), 0);
119 EXPECT_TRUE(badTiming(timing));
Michael Butler648ada52019-07-25 17:22:11 -0700120 EXPECT_FALSE(fallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100121
122 // additional burst testing
Xusong Wangb345a462019-11-27 12:46:48 -0800123 if (request10.pools.size() > 0) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100124 // valid free
125 burst->freeMemory(keys.front());
126
127 // negative test: invalid free of unknown (blank) memory
128 burst->freeMemory(intptr_t{});
129
130 // negative test: double free of memory
131 burst->freeMemory(keys.front());
132 }
133 }
134}
135
136///////////////////////// REMOVE INPUT ////////////////////////////////////
137
138static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
139 for (size_t input = 0; input < request.inputs.size(); ++input) {
140 const std::string message = "removeInput: removed input " + std::to_string(input);
141 validate(preparedModel, message, request,
142 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
143 }
144}
145
146///////////////////////// REMOVE OUTPUT ////////////////////////////////////
147
148static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
149 for (size_t output = 0; output < request.outputs.size(); ++output) {
150 const std::string message = "removeOutput: removed Output " + std::to_string(output);
151 validate(preparedModel, message, request,
152 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
153 }
154}
155
156///////////////////////////// ENTRY POINT //////////////////////////////////
157
158void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
159 removeInputTest(preparedModel, request);
160 removeOutputTest(preparedModel, request);
161}
162
163void validateRequestFailure(const sp<IPreparedModel>& preparedModel, const Request& request) {
Xusong Wangd4a060b2019-10-28 11:11:19 -0700164 SCOPED_TRACE("Expecting request to fail [executeSynchronously_1_3]");
165 Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100166 request, MeasureTiming::NO,
167 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
168 ASSERT_NE(ErrorStatus::NONE, error);
169 EXPECT_EQ(outputShapes.size(), 0);
170 EXPECT_TRUE(badTiming(timing));
171 });
172 ASSERT_TRUE(executeStatus.isOk());
173}
174
Lev Proleev26d1bc82019-08-30 11:57:18 +0100175} // namespace android::hardware::neuralnetworks::V1_3::vts::functional