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