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