blob: 13d45e4a1af5a95c082e23533a6edb2cf5be5975 [file] [log] [blame]
Slava Shklyaevfeb87a92018-09-12 14:52:02 +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
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010019#include "1.0/Utils.h"
20#include "1.2/Callbacks.h"
21#include "ExecutionBurstController.h"
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010022#include "TestHarness.h"
23#include "Utils.h"
24#include "VtsHalNeuralnetworks.h"
25
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010026namespace android {
27namespace hardware {
28namespace neuralnetworks {
29namespace V1_2 {
30namespace vts {
31namespace functional {
32
Xusong Wang1a06e772018-10-31 08:43:12 -070033using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010034
35///////////////////////// UTILITY FUNCTIONS /////////////////////////
36
David Gross55a3d322019-01-23 14:01:52 -080037static bool badTiming(Timing timing) {
38 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
39}
40
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010041// 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);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010048
David Gross55a3d322019-01-23 14:01:52 -080049 // 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
Michael Butler29471a82019-01-15 11:02:55 -080059 // asynchronous
David Gross4592ed12018-12-21 11:20:26 -080060 {
61 SCOPED_TRACE(message + " [execute_1_2]");
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010062
David Gross4592ed12018-12-21 11:20:26 -080063 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
64 ASSERT_NE(nullptr, executionCallback.get());
65 Return<ErrorStatus> executeLaunchStatus =
David Gross55a3d322019-01-23 14:01:52 -080066 preparedModel->execute_1_2(request, measure, executionCallback);
David Gross4592ed12018-12-21 11:20:26 -080067 ASSERT_TRUE(executeLaunchStatus.isOk());
68 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
69
70 executionCallback->wait();
71 ErrorStatus executionReturnStatus = executionCallback->getStatus();
Xusong Wangb50bc312018-11-07 09:33:59 -080072 const auto& outputShapes = executionCallback->getOutputShapes();
David Gross55a3d322019-01-23 14:01:52 -080073 Timing timing = executionCallback->getTiming();
David Gross4592ed12018-12-21 11:20:26 -080074 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
Xusong Wangb50bc312018-11-07 09:33:59 -080075 ASSERT_EQ(outputShapes.size(), 0);
David Gross55a3d322019-01-23 14:01:52 -080076 ASSERT_TRUE(badTiming(timing));
David Gross4592ed12018-12-21 11:20:26 -080077 }
78
Michael Butler29471a82019-01-15 11:02:55 -080079 // synchronous
David Gross4592ed12018-12-21 11:20:26 -080080 {
81 SCOPED_TRACE(message + " [executeSynchronously]");
82
Xusong Wangb50bc312018-11-07 09:33:59 -080083 Return<void> executeStatus = preparedModel->executeSynchronously(
David Gross55a3d322019-01-23 14:01:52 -080084 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 });
David Gross4592ed12018-12-21 11:20:26 -080091 ASSERT_TRUE(executeStatus.isOk());
David Gross4592ed12018-12-21 11:20:26 -080092 }
Michael Butler29471a82019-01-15 11:02:55 -080093
94 // burst
95 {
96 SCOPED_TRACE(message + " [burst]");
97
98 // create burst
Michael Butler51c72182019-03-27 10:59:25 -070099 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler60a22532019-03-28 13:41:14 -0700100 ::android::nn::ExecutionBurstController::create(preparedModel, /*blocking=*/true);
Michael Butler29471a82019-01-15 11:02:55 -0800101 ASSERT_NE(nullptr, burst.get());
102
103 // create memory keys
104 std::vector<intptr_t> keys(request.pools.size());
105 for (size_t i = 0; i < keys.size(); ++i) {
106 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
107 }
108
109 // execute and verify
110 ErrorStatus error;
111 std::vector<OutputShape> outputShapes;
112 Timing timing;
113 std::tie(error, outputShapes, timing) = burst->compute(request, measure, keys);
114 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
115 EXPECT_EQ(outputShapes.size(), 0);
116 EXPECT_TRUE(badTiming(timing));
117
118 // additional burst testing
119 if (request.pools.size() > 0) {
120 // valid free
121 burst->freeMemory(keys.front());
122
123 // negative test: invalid free of unknown (blank) memory
124 burst->freeMemory(intptr_t{});
125
126 // negative test: double free of memory
127 burst->freeMemory(keys.front());
128 }
129 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100130}
131
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100132///////////////////////// REMOVE INPUT ////////////////////////////////////
133
134static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
135 for (size_t input = 0; input < request.inputs.size(); ++input) {
136 const std::string message = "removeInput: removed input " + std::to_string(input);
137 validate(preparedModel, message, request,
138 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
139 }
140}
141
142///////////////////////// REMOVE OUTPUT ////////////////////////////////////
143
144static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
145 for (size_t output = 0; output < request.outputs.size(); ++output) {
146 const std::string message = "removeOutput: removed Output " + std::to_string(output);
147 validate(preparedModel, message, request,
148 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
149 }
150}
151
152///////////////////////////// ENTRY POINT //////////////////////////////////
153
Xusong Wangead950d2019-08-09 16:45:24 -0700154void ValidationTest::validateRequest(const sp<IPreparedModel>& preparedModel,
155 const Request& request) {
156 removeInputTest(preparedModel, request);
157 removeOutputTest(preparedModel, request);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100158}
159
Slava Shklyaev95a59782019-05-10 16:08:30 +0100160void ValidationTest::validateRequestFailure(const sp<IPreparedModel>& preparedModel,
Xusong Wangead950d2019-08-09 16:45:24 -0700161 const Request& request) {
162 SCOPED_TRACE("Expecting request to fail [executeSynchronously]");
163 Return<void> executeStatus = preparedModel->executeSynchronously(
164 request, MeasureTiming::NO,
165 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
166 ASSERT_NE(ErrorStatus::NONE, error);
167 EXPECT_EQ(outputShapes.size(), 0);
168 EXPECT_TRUE(badTiming(timing));
169 });
170 ASSERT_TRUE(executeStatus.isOk());
Slava Shklyaev95a59782019-05-10 16:08:30 +0100171}
172
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100173} // namespace functional
174} // namespace vts
175} // namespace V1_2
176} // namespace neuralnetworks
177} // namespace hardware
178} // namespace android