blob: 2d83b8186c54f8cca29899df01b413525c448fa7 [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
34///////////////////////// UTILITY FUNCTIONS /////////////////////////
35
David Grosse3013492019-01-23 14:01:52 -080036static bool badTiming(Timing timing) {
37 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
38}
39
Slava Shklyaev871be942018-09-12 14:52:02 +010040// Primary validation function. This function will take a valid request, apply a
41// mutation to it to invalidate the request, then pass it to interface calls
42// that use the request. Note that the request here is passed by value, and any
43// mutation to the request does not leave this function.
44static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
45 Request request, const std::function<void(Request*)>& mutation) {
46 mutation(&request);
Slava Shklyaev871be942018-09-12 14:52:02 +010047
David Grosse3013492019-01-23 14:01:52 -080048 // We'd like to test both with timing requested and without timing
49 // requested. Rather than running each test both ways, we'll decide whether
50 // to request timing by hashing the message. We do not use std::hash because
51 // it is not guaranteed stable across executions.
52 char hash = 0;
53 for (auto c : message) {
54 hash ^= c;
55 };
56 MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO;
57
Michael Butler814d8372019-01-15 11:02:55 -080058 // asynchronous
David Gross49e41672018-12-21 11:20:26 -080059 {
60 SCOPED_TRACE(message + " [execute_1_2]");
Slava Shklyaev871be942018-09-12 14:52:02 +010061
David Gross49e41672018-12-21 11:20:26 -080062 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
David Gross49e41672018-12-21 11:20:26 -080063 Return<ErrorStatus> executeLaunchStatus =
David Grosse3013492019-01-23 14:01:52 -080064 preparedModel->execute_1_2(request, measure, executionCallback);
David Gross49e41672018-12-21 11:20:26 -080065 ASSERT_TRUE(executeLaunchStatus.isOk());
66 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
67
68 executionCallback->wait();
69 ErrorStatus executionReturnStatus = executionCallback->getStatus();
Xusong Wang187c5972018-11-07 09:33:59 -080070 const auto& outputShapes = executionCallback->getOutputShapes();
David Grosse3013492019-01-23 14:01:52 -080071 Timing timing = executionCallback->getTiming();
David Gross49e41672018-12-21 11:20:26 -080072 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
Xusong Wang187c5972018-11-07 09:33:59 -080073 ASSERT_EQ(outputShapes.size(), 0);
David Grosse3013492019-01-23 14:01:52 -080074 ASSERT_TRUE(badTiming(timing));
David Gross49e41672018-12-21 11:20:26 -080075 }
76
Michael Butler814d8372019-01-15 11:02:55 -080077 // synchronous
David Gross49e41672018-12-21 11:20:26 -080078 {
79 SCOPED_TRACE(message + " [executeSynchronously]");
80
Xusong Wang187c5972018-11-07 09:33:59 -080081 Return<void> executeStatus = preparedModel->executeSynchronously(
David Grosse3013492019-01-23 14:01:52 -080082 request, measure,
83 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
84 const Timing& timing) {
85 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
86 EXPECT_EQ(outputShapes.size(), 0);
87 EXPECT_TRUE(badTiming(timing));
88 });
David Gross49e41672018-12-21 11:20:26 -080089 ASSERT_TRUE(executeStatus.isOk());
David Gross49e41672018-12-21 11:20:26 -080090 }
Michael Butler814d8372019-01-15 11:02:55 -080091
92 // burst
93 {
94 SCOPED_TRACE(message + " [burst]");
95
96 // create burst
Michael Butler102e0442019-03-27 10:59:25 -070097 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler57568872019-07-25 17:22:11 -070098 android::nn::ExecutionBurstController::create(preparedModel,
99 std::chrono::microseconds{0});
Michael Butler814d8372019-01-15 11:02:55 -0800100 ASSERT_NE(nullptr, burst.get());
101
102 // create memory keys
103 std::vector<intptr_t> keys(request.pools.size());
104 for (size_t i = 0; i < keys.size(); ++i) {
105 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
106 }
107
108 // execute and verify
Michael Butler57568872019-07-25 17:22:11 -0700109 const auto [n, outputShapes, timing, fallback] = burst->compute(request, measure, keys);
110 const ErrorStatus status = nn::convertResultCodeToErrorStatus(n);
111 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
Michael Butler814d8372019-01-15 11:02:55 -0800112 EXPECT_EQ(outputShapes.size(), 0);
113 EXPECT_TRUE(badTiming(timing));
Michael Butler57568872019-07-25 17:22:11 -0700114 EXPECT_FALSE(fallback);
Michael Butler814d8372019-01-15 11:02:55 -0800115
116 // additional burst testing
117 if (request.pools.size() > 0) {
118 // valid free
119 burst->freeMemory(keys.front());
120
121 // negative test: invalid free of unknown (blank) memory
122 burst->freeMemory(intptr_t{});
123
124 // negative test: double free of memory
125 burst->freeMemory(keys.front());
126 }
127 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100128}
129
Slava Shklyaev871be942018-09-12 14:52:02 +0100130///////////////////////// REMOVE INPUT ////////////////////////////////////
131
132static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
133 for (size_t input = 0; input < request.inputs.size(); ++input) {
134 const std::string message = "removeInput: removed input " + std::to_string(input);
135 validate(preparedModel, message, request,
136 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
137 }
138}
139
140///////////////////////// REMOVE OUTPUT ////////////////////////////////////
141
142static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
143 for (size_t output = 0; output < request.outputs.size(); ++output) {
144 const std::string message = "removeOutput: removed Output " + std::to_string(output);
145 validate(preparedModel, message, request,
146 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
147 }
148}
149
150///////////////////////////// ENTRY POINT //////////////////////////////////
151
Michael Butlere16af0a2019-08-29 22:17:24 -0700152void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
Xusong Wang491b0a82019-08-09 16:45:24 -0700153 removeInputTest(preparedModel, request);
154 removeOutputTest(preparedModel, request);
Slava Shklyaev871be942018-09-12 14:52:02 +0100155}
156
Michael Butlere16af0a2019-08-29 22:17:24 -0700157void validateRequestFailure(const sp<IPreparedModel>& preparedModel, const Request& request) {
Xusong Wang491b0a82019-08-09 16:45:24 -0700158 SCOPED_TRACE("Expecting request to fail [executeSynchronously]");
159 Return<void> executeStatus = preparedModel->executeSynchronously(
160 request, MeasureTiming::NO,
161 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
162 ASSERT_NE(ErrorStatus::NONE, error);
163 EXPECT_EQ(outputShapes.size(), 0);
164 EXPECT_TRUE(badTiming(timing));
165 });
166 ASSERT_TRUE(executeStatus.isOk());
Slava Shklyaev1a18c082019-05-10 16:08:30 +0100167}
168
Michael Butlerbbe5dad2019-08-26 23:55:47 -0700169} // namespace android::hardware::neuralnetworks::V1_2::vts::functional