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 "VtsHalNeuralnetworks.h"
20 #include <android-base/logging.h>
21 #include <hidl/ServiceManagement.h>
22 #include <string>
23 #include <utility>
24 #include "1.0/Callbacks.h"
25 #include "1.0/Utils.h"
26 #include "GeneratedTestHarness.h"
27 #include "TestHarness.h"
28 
29 namespace android::hardware::neuralnetworks::V1_2::vts::functional {
30 
31 using implementation::PreparedModelCallback;
32 using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
33 using V1_0::ErrorStatus;
34 using V1_0::Request;
35 using V1_1::ExecutionPreference;
36 
37 // internal helper function
createPreparedModel(const sp<IDevice> & device,const Model & model,sp<IPreparedModel> * preparedModel)38 void createPreparedModel(const sp<IDevice>& device, const Model& model,
39                          sp<IPreparedModel>* preparedModel) {
40     ASSERT_NE(nullptr, preparedModel);
41     *preparedModel = nullptr;
42 
43     // see if service can handle model
44     bool fullySupportsModel = false;
45     const Return<void> supportedCall = device->getSupportedOperations_1_2(
46             model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
47                 ASSERT_EQ(ErrorStatus::NONE, status);
48                 ASSERT_NE(0ul, supported.size());
49                 fullySupportsModel = std::all_of(supported.begin(), supported.end(),
50                                                  [](bool valid) { return valid; });
51             });
52     ASSERT_TRUE(supportedCall.isOk());
53 
54     // launch prepare model
55     const sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
56     const Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
57             model, ExecutionPreference::FAST_SINGLE_ANSWER, hidl_vec<hidl_handle>(),
58             hidl_vec<hidl_handle>(), HidlToken(), preparedModelCallback);
59     ASSERT_TRUE(prepareLaunchStatus.isOk());
60     ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
61 
62     // retrieve prepared model
63     preparedModelCallback->wait();
64     const ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
65     *preparedModel = getPreparedModel_1_2(preparedModelCallback);
66 
67     // The getSupportedOperations_1_2 call returns a list of operations that are
68     // guaranteed not to fail if prepareModel_1_2 is called, and
69     // 'fullySupportsModel' is true i.f.f. the entire model is guaranteed.
70     // If a driver has any doubt that it can prepare an operation, it must
71     // return false. So here, if a driver isn't sure if it can support an
72     // operation, but reports that it successfully prepared the model, the test
73     // can continue.
74     if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
75         ASSERT_EQ(nullptr, preparedModel->get());
76         LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot prepare "
77                      "model that it does not support.";
78         std::cout << "[          ]   Early termination of test because vendor service cannot "
79                      "prepare model that it does not support."
80                   << std::endl;
81         GTEST_SKIP();
82     }
83     ASSERT_EQ(ErrorStatus::NONE, prepareReturnStatus);
84     ASSERT_NE(nullptr, preparedModel->get());
85 }
86 
SetUp()87 void NeuralnetworksHidlTest::SetUp() {
88     testing::TestWithParam<NeuralnetworksHidlTestParam>::SetUp();
89     ASSERT_NE(kDevice, nullptr);
90     const bool deviceIsResponsive = kDevice->ping().isOk();
91     ASSERT_TRUE(deviceIsResponsive);
92 }
93 
makeNamedDevice(const std::string & name)94 static NamedDevice makeNamedDevice(const std::string& name) {
95     return {name, IDevice::getService(name)};
96 }
97 
getNamedDevicesImpl()98 static std::vector<NamedDevice> getNamedDevicesImpl() {
99     // Retrieves the name of all service instances that implement IDevice,
100     // including any Lazy HAL instances.
101     const std::vector<std::string> names = hardware::getAllHalInstanceNames(IDevice::descriptor);
102 
103     // Get a handle to each device and pair it with its name.
104     std::vector<NamedDevice> namedDevices;
105     namedDevices.reserve(names.size());
106     std::transform(names.begin(), names.end(), std::back_inserter(namedDevices), makeNamedDevice);
107     return namedDevices;
108 }
109 
getNamedDevices()110 const std::vector<NamedDevice>& getNamedDevices() {
111     const static std::vector<NamedDevice> devices = getNamedDevicesImpl();
112     return devices;
113 }
114 
printNeuralnetworksHidlTest(const testing::TestParamInfo<NeuralnetworksHidlTestParam> & info)115 std::string printNeuralnetworksHidlTest(
116         const testing::TestParamInfo<NeuralnetworksHidlTestParam>& info) {
117     return gtestCompliantName(getName(info.param));
118 }
119 
120 INSTANTIATE_DEVICE_TEST(NeuralnetworksHidlTest);
121 
122 // Forward declaration from ValidateModel.cpp
123 void validateModel(const sp<IDevice>& device, const Model& model);
124 // Forward declaration from ValidateRequest.cpp
125 void validateRequest(const sp<IPreparedModel>& preparedModel, const V1_0::Request& request);
126 // Forward declaration from ValidateRequest.cpp
127 void validateRequestFailure(const sp<IPreparedModel>& preparedModel, const V1_0::Request& request);
128 // Forward declaration from ValidateBurst.cpp
129 void validateBurst(const sp<IPreparedModel>& preparedModel, const V1_0::Request& request);
130 
validateEverything(const sp<IDevice> & device,const Model & model,const Request & request)131 void validateEverything(const sp<IDevice>& device, const Model& model, const Request& request) {
132     validateModel(device, model);
133 
134     // Create IPreparedModel.
135     sp<IPreparedModel> preparedModel;
136     createPreparedModel(device, model, &preparedModel);
137     if (preparedModel == nullptr) return;
138 
139     validateRequest(preparedModel, request);
140     validateBurst(preparedModel, request);
141 }
142 
validateFailure(const sp<IDevice> & device,const Model & model,const Request & request)143 void validateFailure(const sp<IDevice>& device, const Model& model, const Request& request) {
144     // TODO: Should this always succeed?
145     //       What if the invalid input is part of the model (i.e., a parameter).
146     validateModel(device, model);
147 
148     // Create IPreparedModel.
149     sp<IPreparedModel> preparedModel;
150     createPreparedModel(device, model, &preparedModel);
151     if (preparedModel == nullptr) return;
152 
153     validateRequestFailure(preparedModel, request);
154 }
155 
TEST_P(ValidationTest,Test)156 TEST_P(ValidationTest, Test) {
157     const Model model = createModel(kTestModel);
158     ExecutionContext context;
159     const Request request = context.createRequest(kTestModel);
160     if (kTestModel.expectFailure) {
161         validateFailure(kDevice, model, request);
162     } else {
163         validateEverything(kDevice, model, request);
164     }
165 }
166 
__anoneeeb8a370302(const std::string& testName) 167 INSTANTIATE_GENERATED_TEST(ValidationTest, [](const std::string& testName) {
168     // Skip validation for the "inputs_as_internal" and "all_tensors_as_inputs"
169     // generated tests.
170     return testName.find("inputs_as_internal") == std::string::npos &&
171            testName.find("all_tensors_as_inputs") == std::string::npos;
172 });
173 
getPreparedModel_1_2(const sp<implementation::PreparedModelCallback> & callback)174 sp<IPreparedModel> getPreparedModel_1_2(const sp<implementation::PreparedModelCallback>& callback) {
175     sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel();
176     return IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr);
177 }
178 
179 }  // namespace android::hardware::neuralnetworks::V1_2::vts::functional
180