blob: 7a23c13fe06dd6ea2f9f452b87bafc5ac0a09ccc [file] [log] [blame]
Slava Shklyaev871be942018-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
Michael Butler57568872019-07-25 17:22:11 -070019#include <chrono>
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010020#include "1.0/Utils.h"
21#include "1.2/Callbacks.h"
22#include "ExecutionBurstController.h"
Xusong Wang9e2b97b2019-08-23 16:10:54 -070023#include "GeneratedTestHarness.h"
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010024#include "TestHarness.h"
25#include "Utils.h"
26#include "VtsHalNeuralnetworks.h"
27
Michael Butlerbbe5dad2019-08-26 23:55:47 -070028namespace android::hardware::neuralnetworks::V1_2::vts::functional {
Slava Shklyaev871be942018-09-12 14:52:02 +010029
Michael Butlerbbe5dad2019-08-26 23:55:47 -070030using implementation::ExecutionCallback;
31using V1_0::ErrorStatus;
32using V1_0::Request;
Slava Shklyaev871be942018-09-12 14:52:02 +010033
Michael Butlerda1a6922020-03-11 18:45:45 -070034using ExecutionMutation = std::function<void(Request*)>;
35
Slava Shklyaev871be942018-09-12 14:52:02 +010036///////////////////////// UTILITY FUNCTIONS /////////////////////////
37
David Grosse3013492019-01-23 14:01:52 -080038static bool badTiming(Timing timing) {
39 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
40}
41
Slava Shklyaev871be942018-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
Michael Butlerda1a6922020-03-11 18:45:45 -070044// that use the request.
Slava Shklyaev871be942018-09-12 14:52:02 +010045static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
Michael Butlerda1a6922020-03-11 18:45:45 -070046 const Request& originalRequest, const ExecutionMutation& mutate) {
47 Request request = originalRequest;
48 mutate(&request);
Slava Shklyaev871be942018-09-12 14:52:02 +010049
David Grosse3013492019-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 Butler814d8372019-01-15 11:02:55 -080060 // asynchronous
David Gross49e41672018-12-21 11:20:26 -080061 {
62 SCOPED_TRACE(message + " [execute_1_2]");
Slava Shklyaev871be942018-09-12 14:52:02 +010063
David Gross49e41672018-12-21 11:20:26 -080064 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
David Gross49e41672018-12-21 11:20:26 -080065 Return<ErrorStatus> executeLaunchStatus =
David Grosse3013492019-01-23 14:01:52 -080066 preparedModel->execute_1_2(request, measure, executionCallback);
David Gross49e41672018-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 Wang187c5972018-11-07 09:33:59 -080072 const auto& outputShapes = executionCallback->getOutputShapes();
David Grosse3013492019-01-23 14:01:52 -080073 Timing timing = executionCallback->getTiming();
David Gross49e41672018-12-21 11:20:26 -080074 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
Xusong Wang187c5972018-11-07 09:33:59 -080075 ASSERT_EQ(outputShapes.size(), 0);
David Grosse3013492019-01-23 14:01:52 -080076 ASSERT_TRUE(badTiming(timing));
David Gross49e41672018-12-21 11:20:26 -080077 }
78
Michael Butler814d8372019-01-15 11:02:55 -080079 // synchronous
David Gross49e41672018-12-21 11:20:26 -080080 {
81 SCOPED_TRACE(message + " [executeSynchronously]");
82
Xusong Wang187c5972018-11-07 09:33:59 -080083 Return<void> executeStatus = preparedModel->executeSynchronously(
David Grosse3013492019-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 Gross49e41672018-12-21 11:20:26 -080091 ASSERT_TRUE(executeStatus.isOk());
David Gross49e41672018-12-21 11:20:26 -080092 }
Michael Butler814d8372019-01-15 11:02:55 -080093
94 // burst
95 {
96 SCOPED_TRACE(message + " [burst]");
97
98 // create burst
Michael Butler102e0442019-03-27 10:59:25 -070099 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler57568872019-07-25 17:22:11 -0700100 android::nn::ExecutionBurstController::create(preparedModel,
101 std::chrono::microseconds{0});
Michael Butler814d8372019-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
Michael Butler57568872019-07-25 17:22:11 -0700111 const auto [n, outputShapes, timing, fallback] = burst->compute(request, measure, keys);
Michael Butler9449a282019-12-11 19:08:08 -0800112 const ErrorStatus status = nn::convertToV1_0(nn::convertResultCodeToErrorStatus(n));
Michael Butler57568872019-07-25 17:22:11 -0700113 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
Michael Butler814d8372019-01-15 11:02:55 -0800114 EXPECT_EQ(outputShapes.size(), 0);
115 EXPECT_TRUE(badTiming(timing));
Michael Butler57568872019-07-25 17:22:11 -0700116 EXPECT_FALSE(fallback);
Michael Butler814d8372019-01-15 11:02:55 -0800117
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 Shklyaev871be942018-09-12 14:52:02 +0100130}
131
Slava Shklyaev871be942018-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
Michael Butlere16af0a2019-08-29 22:17:24 -0700154void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
Xusong Wang491b0a82019-08-09 16:45:24 -0700155 removeInputTest(preparedModel, request);
156 removeOutputTest(preparedModel, request);
Slava Shklyaev871be942018-09-12 14:52:02 +0100157}
158
Michael Butlere16af0a2019-08-29 22:17:24 -0700159void validateRequestFailure(const sp<IPreparedModel>& preparedModel, const Request& request) {
Xusong Wang491b0a82019-08-09 16:45:24 -0700160 SCOPED_TRACE("Expecting request to fail [executeSynchronously]");
161 Return<void> executeStatus = preparedModel->executeSynchronously(
162 request, MeasureTiming::NO,
163 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
164 ASSERT_NE(ErrorStatus::NONE, error);
165 EXPECT_EQ(outputShapes.size(), 0);
166 EXPECT_TRUE(badTiming(timing));
167 });
168 ASSERT_TRUE(executeStatus.isOk());
Slava Shklyaev1a18c082019-05-10 16:08:30 +0100169}
170
Michael Butlerbbe5dad2019-08-26 23:55:47 -0700171} // namespace android::hardware::neuralnetworks::V1_2::vts::functional