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 const bool deviceIsResponsive = kDevice->ping().isOk();
85 ASSERT_TRUE(deviceIsResponsive);
86 }
87
makeNamedDevice(const std::string & name)88 static NamedDevice makeNamedDevice(const std::string& name) {
89 return {name, IDevice::getService(name)};
90 }
91
getNamedDevicesImpl()92 static std::vector<NamedDevice> getNamedDevicesImpl() {
93 // Retrieves the name of all service instances that implement IDevice,
94 // including any Lazy HAL instances.
95 const std::vector<std::string> names = hardware::getAllHalInstanceNames(IDevice::descriptor);
96
97 // Get a handle to each device and pair it with its name.
98 std::vector<NamedDevice> namedDevices;
99 namedDevices.reserve(names.size());
100 std::transform(names.begin(), names.end(), std::back_inserter(namedDevices), makeNamedDevice);
101 return namedDevices;
102 }
103
getNamedDevices()104 const std::vector<NamedDevice>& getNamedDevices() {
105 const static std::vector<NamedDevice> devices = getNamedDevicesImpl();
106 return devices;
107 }
108
printNeuralnetworksHidlTest(const testing::TestParamInfo<NeuralnetworksHidlTestParam> & info)109 std::string printNeuralnetworksHidlTest(
110 const testing::TestParamInfo<NeuralnetworksHidlTestParam>& info) {
111 return gtestCompliantName(getName(info.param));
112 }
113
114 INSTANTIATE_DEVICE_TEST(NeuralnetworksHidlTest);
115
116 // Forward declaration from ValidateModel.cpp
117 void validateModel(const sp<IDevice>& device, const Model& model);
118 // Forward declaration from ValidateRequest.cpp
119 void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request);
120
validateEverything(const sp<IDevice> & device,const Model & model,const Request & request)121 void validateEverything(const sp<IDevice>& device, const Model& model, const Request& request) {
122 validateModel(device, model);
123
124 // Create IPreparedModel.
125 sp<IPreparedModel> preparedModel;
126 createPreparedModel(device, model, &preparedModel);
127 if (preparedModel == nullptr) return;
128
129 validateRequest(preparedModel, request);
130 }
131
TEST_P(ValidationTest,Test)132 TEST_P(ValidationTest, Test) {
133 const Model model = createModel(kTestModel);
134 ExecutionContext context;
135 const Request request = context.createRequest(kTestModel);
136 ASSERT_FALSE(kTestModel.expectFailure);
137 validateEverything(kDevice, model, request);
138 }
139
__anon5dcf0f210302(const std::string& testName) 140 INSTANTIATE_GENERATED_TEST(ValidationTest, [](const std::string& testName) {
141 // Skip validation for the "inputs_as_internal" and "all_tensors_as_inputs"
142 // generated tests.
143 return testName.find("inputs_as_internal") == std::string::npos &&
144 testName.find("all_tensors_as_inputs") == std::string::npos;
145 });
146
147 } // namespace android::hardware::neuralnetworks::V1_0::vts::functional
148