blob: 9fb4c6e55b504dbd33560e5c0620b205012fec47 [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"
Michael Butler79a41d72019-12-11 19:08:08 -080021#include "1.3/Callbacks.h"
Lev Proleev13fdfcd2019-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 Proleev26d1bc82019-08-30 11:57:18 +010028namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010029
Michael Butler79a41d72019-12-11 19:08:08 -080030using implementation::ExecutionCallback;
Lev Proleev26d1bc82019-08-30 11:57:18 +010031using V1_2::MeasureTiming;
32using V1_2::OutputShape;
33using V1_2::Timing;
Lev Proleev13fdfcd2019-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,
46 Request request, const std::function<void(Request*)>& mutation) {
47 mutation(&request);
48
49 // We'd like to test both with timing requested and without timing
50 // requested. Rather than running each test both ways, we'll decide whether
51 // to request timing by hashing the message. We do not use std::hash because
52 // it is not guaranteed stable across executions.
53 char hash = 0;
54 for (auto c : message) {
55 hash ^= c;
56 };
57 MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO;
58
59 // asynchronous
60 {
Xusong Wang1b3f4262019-10-25 12:07:17 -070061 SCOPED_TRACE(message + " [execute_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010062
63 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
64 Return<ErrorStatus> executeLaunchStatus =
Michael Butler79a41d72019-12-11 19:08:08 -080065 preparedModel->execute_1_3(request, measure, {}, executionCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +010066 ASSERT_TRUE(executeLaunchStatus.isOk());
67 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
68
69 executionCallback->wait();
70 ErrorStatus executionReturnStatus = executionCallback->getStatus();
71 const auto& outputShapes = executionCallback->getOutputShapes();
72 Timing timing = executionCallback->getTiming();
73 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
74 ASSERT_EQ(outputShapes.size(), 0);
75 ASSERT_TRUE(badTiming(timing));
76 }
77
78 // synchronous
79 {
Xusong Wangd4a060b2019-10-28 11:11:19 -070080 SCOPED_TRACE(message + " [executeSynchronously_1_3]");
Lev Proleev13fdfcd2019-08-30 11:35:34 +010081
Xusong Wangd4a060b2019-10-28 11:11:19 -070082 Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
Michael Butler79a41d72019-12-11 19:08:08 -080083 request, measure, {},
Lev Proleev13fdfcd2019-08-30 11:35:34 +010084 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
85 const Timing& timing) {
86 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
87 EXPECT_EQ(outputShapes.size(), 0);
88 EXPECT_TRUE(badTiming(timing));
89 });
90 ASSERT_TRUE(executeStatus.isOk());
91 }
92
93 // burst
Xusong Wangb345a462019-11-27 12:46:48 -080094 // 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 +010095 {
96 SCOPED_TRACE(message + " [burst]");
97
Xusong Wangb345a462019-11-27 12:46:48 -080098 ASSERT_TRUE(nn::compliantWithV1_0(request));
99 V1_0::Request request10 = nn::convertToV1_0(request);
100
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100101 // create burst
102 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler648ada52019-07-25 17:22:11 -0700103 android::nn::ExecutionBurstController::create(preparedModel,
104 std::chrono::microseconds{0});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100105 ASSERT_NE(nullptr, burst.get());
106
107 // create memory keys
Xusong Wangb345a462019-11-27 12:46:48 -0800108 std::vector<intptr_t> keys(request10.pools.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100109 for (size_t i = 0; i < keys.size(); ++i) {
Xusong Wangb345a462019-11-27 12:46:48 -0800110 keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100111 }
112
113 // execute and verify
Xusong Wangb345a462019-11-27 12:46:48 -0800114 const auto [n, outputShapes, timing, fallback] = burst->compute(request10, measure, keys);
Michael Butler648ada52019-07-25 17:22:11 -0700115 const ErrorStatus status = nn::convertResultCodeToErrorStatus(n);
116 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100117 EXPECT_EQ(outputShapes.size(), 0);
118 EXPECT_TRUE(badTiming(timing));
Michael Butler648ada52019-07-25 17:22:11 -0700119 EXPECT_FALSE(fallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100120
121 // additional burst testing
Xusong Wangb345a462019-11-27 12:46:48 -0800122 if (request10.pools.size() > 0) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100123 // valid free
124 burst->freeMemory(keys.front());
125
126 // negative test: invalid free of unknown (blank) memory
127 burst->freeMemory(intptr_t{});
128
129 // negative test: double free of memory
130 burst->freeMemory(keys.front());
131 }
132 }
133}
134
135///////////////////////// REMOVE INPUT ////////////////////////////////////
136
137static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
138 for (size_t input = 0; input < request.inputs.size(); ++input) {
139 const std::string message = "removeInput: removed input " + std::to_string(input);
140 validate(preparedModel, message, request,
141 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
142 }
143}
144
145///////////////////////// REMOVE OUTPUT ////////////////////////////////////
146
147static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
148 for (size_t output = 0; output < request.outputs.size(); ++output) {
149 const std::string message = "removeOutput: removed Output " + std::to_string(output);
150 validate(preparedModel, message, request,
151 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
152 }
153}
154
155///////////////////////////// ENTRY POINT //////////////////////////////////
156
157void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
158 removeInputTest(preparedModel, request);
159 removeOutputTest(preparedModel, request);
160}
161
162void validateRequestFailure(const sp<IPreparedModel>& preparedModel, const Request& request) {
Xusong Wangd4a060b2019-10-28 11:11:19 -0700163 SCOPED_TRACE("Expecting request to fail [executeSynchronously_1_3]");
164 Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
Michael Butler79a41d72019-12-11 19:08:08 -0800165 request, MeasureTiming::NO, {},
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100166 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
167 ASSERT_NE(ErrorStatus::NONE, error);
168 EXPECT_EQ(outputShapes.size(), 0);
169 EXPECT_TRUE(badTiming(timing));
170 });
171 ASSERT_TRUE(executeStatus.isOk());
172}
173
Lev Proleev26d1bc82019-08-30 11:57:18 +0100174} // namespace android::hardware::neuralnetworks::V1_3::vts::functional