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