1 /*
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 
19 #include <android/hardware/neuralnetworks/1.3/IFencedExecutionCallback.h>
20 #include <chrono>
21 
22 #include "1.0/Utils.h"
23 #include "1.3/Callbacks.h"
24 #include "ExecutionBurstController.h"
25 #include "GeneratedTestHarness.h"
26 #include "TestHarness.h"
27 #include "Utils.h"
28 #include "VtsHalNeuralnetworks.h"
29 
30 namespace android::hardware::neuralnetworks::V1_3::vts::functional {
31 
32 using implementation::ExecutionCallback;
33 using V1_2::MeasureTiming;
34 using V1_2::OutputShape;
35 using V1_2::Timing;
36 
37 using ExecutionMutation = std::function<void(Request*)>;
38 
39 ///////////////////////// UTILITY FUNCTIONS /////////////////////////
40 
badTiming(Timing timing)41 static bool badTiming(Timing timing) {
42     return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
43 }
44 
45 // Primary validation function. This function will take a valid request, apply a
46 // mutation to it to invalidate the request, then pass it to interface calls
47 // that use the request.
validate(const sp<IPreparedModel> & preparedModel,const std::string & message,const Request & originalRequest,const ExecutionMutation & mutate)48 static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
49                      const Request& originalRequest, const ExecutionMutation& mutate) {
50     Request request = originalRequest;
51     mutate(&request);
52 
53     // We'd like to test both with timing requested and without timing
54     // requested. Rather than running each test both ways, we'll decide whether
55     // to request timing by hashing the message. We do not use std::hash because
56     // it is not guaranteed stable across executions.
57     char hash = 0;
58     for (auto c : message) {
59         hash ^= c;
60     };
61     MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO;
62 
63     // asynchronous
64     {
65         SCOPED_TRACE(message + " [execute_1_3]");
66 
67         sp<ExecutionCallback> executionCallback = new ExecutionCallback();
68         Return<ErrorStatus> executeLaunchStatus =
69                 preparedModel->execute_1_3(request, measure, {}, {}, executionCallback);
70         ASSERT_TRUE(executeLaunchStatus.isOk());
71         ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
72 
73         executionCallback->wait();
74         ErrorStatus executionReturnStatus = executionCallback->getStatus();
75         const auto& outputShapes = executionCallback->getOutputShapes();
76         Timing timing = executionCallback->getTiming();
77         ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
78         ASSERT_EQ(outputShapes.size(), 0);
79         ASSERT_TRUE(badTiming(timing));
80     }
81 
82     // synchronous
83     {
84         SCOPED_TRACE(message + " [executeSynchronously_1_3]");
85 
86         Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
87                 request, measure, {}, {},
88                 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
89                    const Timing& timing) {
90                     ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
91                     EXPECT_EQ(outputShapes.size(), 0);
92                     EXPECT_TRUE(badTiming(timing));
93                 });
94         ASSERT_TRUE(executeStatus.isOk());
95     }
96 
97     // burst
98     // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains V1_2.
99     {
100         SCOPED_TRACE(message + " [burst]");
101 
102         ASSERT_TRUE(nn::compliantWithV1_0(request));
103         V1_0::Request request10 = nn::convertToV1_0(request);
104 
105         // create burst
106         std::shared_ptr<::android::nn::ExecutionBurstController> burst =
107                 android::nn::ExecutionBurstController::create(preparedModel,
108                                                               std::chrono::microseconds{0});
109         ASSERT_NE(nullptr, burst.get());
110 
111         // create memory keys
112         std::vector<intptr_t> keys(request10.pools.size());
113         for (size_t i = 0; i < keys.size(); ++i) {
114             keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
115         }
116 
117         // execute and verify
118         const auto [n, outputShapes, timing, fallback] = burst->compute(request10, measure, keys);
119         const ErrorStatus status = nn::convertResultCodeToErrorStatus(n);
120         EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status);
121         EXPECT_EQ(outputShapes.size(), 0);
122         EXPECT_TRUE(badTiming(timing));
123         EXPECT_FALSE(fallback);
124 
125         // additional burst testing
126         if (request10.pools.size() > 0) {
127             // valid free
128             burst->freeMemory(keys.front());
129 
130             // negative test: invalid free of unknown (blank) memory
131             burst->freeMemory(intptr_t{});
132 
133             // negative test: double free of memory
134             burst->freeMemory(keys.front());
135         }
136     }
137 
138     // dispatch
139     {
140         SCOPED_TRACE(message + " [executeFenced]");
141         Return<void> ret =
142                 preparedModel->executeFenced(request, {}, MeasureTiming::NO, {}, {}, {},
143                                              [](ErrorStatus error, const hidl_handle& handle,
144                                                 const sp<IFencedExecutionCallback>& callback) {
145                                                  ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
146                                                  ASSERT_EQ(handle.getNativeHandle(), nullptr);
147                                                  ASSERT_EQ(callback, nullptr);
148                                              });
149         ASSERT_TRUE(ret.isOk());
150     }
151 }
152 
153 ///////////////////////// REMOVE INPUT ////////////////////////////////////
154 
removeInputTest(const sp<IPreparedModel> & preparedModel,const Request & request)155 static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
156     for (size_t input = 0; input < request.inputs.size(); ++input) {
157         const std::string message = "removeInput: removed input " + std::to_string(input);
158         validate(preparedModel, message, request,
159                  [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
160     }
161 }
162 
163 ///////////////////////// REMOVE OUTPUT ////////////////////////////////////
164 
removeOutputTest(const sp<IPreparedModel> & preparedModel,const Request & request)165 static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
166     for (size_t output = 0; output < request.outputs.size(); ++output) {
167         const std::string message = "removeOutput: removed Output " + std::to_string(output);
168         validate(preparedModel, message, request,
169                  [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
170     }
171 }
172 
173 ///////////////////////////// ENTRY POINT //////////////////////////////////
174 
validateRequest(const sp<IPreparedModel> & preparedModel,const Request & request)175 void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request) {
176     removeInputTest(preparedModel, request);
177     removeOutputTest(preparedModel, request);
178 }
179 
validateRequestFailure(const sp<IPreparedModel> & preparedModel,const Request & request)180 void validateRequestFailure(const sp<IPreparedModel>& preparedModel, const Request& request) {
181     SCOPED_TRACE("Expecting request to fail [executeSynchronously_1_3]");
182     Return<void> executeStatus = preparedModel->executeSynchronously_1_3(
183             request, MeasureTiming::NO, {}, {},
184             [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
185                 ASSERT_NE(ErrorStatus::NONE, error);
186                 EXPECT_EQ(outputShapes.size(), 0);
187                 EXPECT_TRUE(badTiming(timing));
188             });
189     ASSERT_TRUE(executeStatus.isOk());
190 }
191 
192 }  // namespace android::hardware::neuralnetworks::V1_3::vts::functional
193