blob: 20c740e3065c014c8fc1ecb372c990656616cb69 [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"
Xusong Wangbcaa7822019-08-23 16:10:54 -070022#include "GeneratedTestHarness.h"
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010023#include "TestHarness.h"
24#include "Utils.h"
25#include "VtsHalNeuralnetworks.h"
26
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010027namespace android {
28namespace hardware {
29namespace neuralnetworks {
30namespace V1_2 {
31namespace vts {
32namespace functional {
33
Xusong Wang1a06e772018-10-31 08:43:12 -070034using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010035
36///////////////////////// UTILITY FUNCTIONS /////////////////////////
37
David Gross55a3d322019-01-23 14:01:52 -080038static bool badTiming(Timing timing) {
39 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
40}
41
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010042// 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);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010049
David Gross55a3d322019-01-23 14:01:52 -080050 // 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 Butler29471a82019-01-15 11:02:55 -080060 // asynchronous
David Gross4592ed12018-12-21 11:20:26 -080061 {
62 SCOPED_TRACE(message + " [execute_1_2]");
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010063
David Gross4592ed12018-12-21 11:20:26 -080064 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
65 ASSERT_NE(nullptr, executionCallback.get());
66 Return<ErrorStatus> executeLaunchStatus =
David Gross55a3d322019-01-23 14:01:52 -080067 preparedModel->execute_1_2(request, measure, executionCallback);
David Gross4592ed12018-12-21 11:20:26 -080068 ASSERT_TRUE(executeLaunchStatus.isOk());
69 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
70
71 executionCallback->wait();
72 ErrorStatus executionReturnStatus = executionCallback->getStatus();
Xusong Wangb50bc312018-11-07 09:33:59 -080073 const auto& outputShapes = executionCallback->getOutputShapes();
David Gross55a3d322019-01-23 14:01:52 -080074 Timing timing = executionCallback->getTiming();
David Gross4592ed12018-12-21 11:20:26 -080075 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
Xusong Wangb50bc312018-11-07 09:33:59 -080076 ASSERT_EQ(outputShapes.size(), 0);
David Gross55a3d322019-01-23 14:01:52 -080077 ASSERT_TRUE(badTiming(timing));
David Gross4592ed12018-12-21 11:20:26 -080078 }
79
Michael Butler29471a82019-01-15 11:02:55 -080080 // synchronous
David Gross4592ed12018-12-21 11:20:26 -080081 {
82 SCOPED_TRACE(message + " [executeSynchronously]");
83
Xusong Wangb50bc312018-11-07 09:33:59 -080084 Return<void> executeStatus = preparedModel->executeSynchronously(
David Gross55a3d322019-01-23 14:01:52 -080085 request, measure,
86 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
87 const Timing& timing) {
88 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
89 EXPECT_EQ(outputShapes.size(), 0);
90 EXPECT_TRUE(badTiming(timing));
91 });
David Gross4592ed12018-12-21 11:20:26 -080092 ASSERT_TRUE(executeStatus.isOk());
David Gross4592ed12018-12-21 11:20:26 -080093 }
Michael Butler29471a82019-01-15 11:02:55 -080094
95 // burst
96 {
97 SCOPED_TRACE(message + " [burst]");
98
99 // create burst
Michael Butler51c72182019-03-27 10:59:25 -0700100 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler60a22532019-03-28 13:41:14 -0700101 ::android::nn::ExecutionBurstController::create(preparedModel, /*blocking=*/true);
Michael Butler29471a82019-01-15 11:02:55 -0800102 ASSERT_NE(nullptr, burst.get());
103
104 // create memory keys
105 std::vector<intptr_t> keys(request.pools.size());
106 for (size_t i = 0; i < keys.size(); ++i) {
107 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
108 }
109
110 // execute and verify
111 ErrorStatus error;
112 std::vector<OutputShape> outputShapes;
113 Timing timing;
114 std::tie(error, outputShapes, timing) = burst->compute(request, measure, keys);
115 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
116 EXPECT_EQ(outputShapes.size(), 0);
117 EXPECT_TRUE(badTiming(timing));
118
119 // additional burst testing
120 if (request.pools.size() > 0) {
121 // valid free
122 burst->freeMemory(keys.front());
123
124 // negative test: invalid free of unknown (blank) memory
125 burst->freeMemory(intptr_t{});
126
127 // negative test: double free of memory
128 burst->freeMemory(keys.front());
129 }
130 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100131}
132
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100133///////////////////////// REMOVE INPUT ////////////////////////////////////
134
135static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
136 for (size_t input = 0; input < request.inputs.size(); ++input) {
137 const std::string message = "removeInput: removed input " + std::to_string(input);
138 validate(preparedModel, message, request,
139 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
140 }
141}
142
143///////////////////////// REMOVE OUTPUT ////////////////////////////////////
144
145static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
146 for (size_t output = 0; output < request.outputs.size(); ++output) {
147 const std::string message = "removeOutput: removed Output " + std::to_string(output);
148 validate(preparedModel, message, request,
149 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
150 }
151}
152
153///////////////////////////// ENTRY POINT //////////////////////////////////
154
Xusong Wangead950d2019-08-09 16:45:24 -0700155void ValidationTest::validateRequest(const sp<IPreparedModel>& preparedModel,
156 const Request& request) {
157 removeInputTest(preparedModel, request);
158 removeOutputTest(preparedModel, request);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100159}
160
Slava Shklyaev95a59782019-05-10 16:08:30 +0100161void ValidationTest::validateRequestFailure(const sp<IPreparedModel>& preparedModel,
Xusong Wangead950d2019-08-09 16:45:24 -0700162 const Request& request) {
163 SCOPED_TRACE("Expecting request to fail [executeSynchronously]");
164 Return<void> executeStatus = preparedModel->executeSynchronously(
165 request, MeasureTiming::NO,
166 [](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());
Slava Shklyaev95a59782019-05-10 16:08:30 +0100172}
173
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100174} // namespace functional
175} // namespace vts
176} // namespace V1_2
177} // namespace neuralnetworks
178} // namespace hardware
179} // namespace android