I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 17 | #include "Callbacks.h" |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 18 | #include "TestHarness.h" |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 19 | #include "Utils.h" |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 20 | |
| 21 | #include <android-base/logging.h> |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 22 | #include <android/hardware/neuralnetworks/1.0/IDevice.h> |
| 23 | #include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h> |
| 24 | #include <android/hardware/neuralnetworks/1.0/IPreparedModel.h> |
| 25 | #include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h> |
| 26 | #include <android/hardware/neuralnetworks/1.0/types.h> |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 27 | #include <android/hardware/neuralnetworks/1.1/IDevice.h> |
| 28 | #include <android/hardware/neuralnetworks/1.2/IDevice.h> |
| 29 | #include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h> |
| 30 | #include <android/hardware/neuralnetworks/1.2/IPreparedModel.h> |
| 31 | #include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h> |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 32 | #include <android/hidl/allocator/1.0/IAllocator.h> |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 33 | #include <android/hidl/memory/1.0/IMemory.h> |
| 34 | #include <hidlmemory/mapping.h> |
Michael Butler | 0897ab3 | 2017-10-04 02:38:42 -0700 | [diff] [blame] | 35 | #include <iostream> |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | namespace hardware { |
| 39 | namespace neuralnetworks { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 40 | |
| 41 | namespace generated_tests { |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 42 | using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback; |
| 43 | using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback; |
Slava Shklyaev | 9e3fad1 | 2018-11-30 17:55:12 +0000 | [diff] [blame] | 44 | using ::test_helper::bool8; |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 45 | using ::test_helper::compare; |
| 46 | using ::test_helper::expectMultinomialDistributionWithinTolerance; |
Mika Raento | de16694 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 47 | using ::test_helper::filter; |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 48 | using ::test_helper::Float32Operands; |
Mika Raento | de16694 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 49 | using ::test_helper::for_all; |
| 50 | using ::test_helper::for_each; |
Mika Raento | de16694 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 51 | using ::test_helper::Int32Operands; |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 52 | using ::test_helper::MixedTyped; |
| 53 | using ::test_helper::MixedTypedExample; |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 54 | using ::test_helper::MixedTypedIndex; |
Mika Raento | de16694 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 55 | using ::test_helper::Quant8Operands; |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 56 | using ::test_helper::resize_accordingly; |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 57 | |
I-Jui (Ray) Sung | 5bf4edf | 2017-10-06 13:22:39 -0700 | [diff] [blame] | 58 | template <typename T> |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 59 | void copy_back_(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) { |
| 60 | MixedTyped& test = *dst; |
I-Jui (Ray) Sung | 5bf4edf | 2017-10-06 13:22:39 -0700 | [diff] [blame] | 61 | for_each<T>(test, [&ra, src](int index, std::vector<T>& m) { |
| 62 | ASSERT_EQ(m.size(), ra[index].location.length / sizeof(T)); |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 63 | char* begin = src + ra[index].location.offset; |
| 64 | memcpy(m.data(), begin, ra[index].location.length); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) { |
| 69 | copy_back_<float>(dst, ra, src); |
| 70 | copy_back_<int32_t>(dst, ra, src); |
| 71 | copy_back_<uint8_t>(dst, ra, src); |
Lev Proleev | ca80ff0 | 2018-11-05 13:20:06 +0000 | [diff] [blame] | 72 | copy_back_<int16_t>(dst, ra, src); |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 73 | copy_back_<_Float16>(dst, ra, src); |
Slava Shklyaev | 9e3fad1 | 2018-11-30 17:55:12 +0000 | [diff] [blame] | 74 | copy_back_<bool8>(dst, ra, src); |
Przemyslaw Szczepaniak | 4290961 | 2018-12-12 13:13:32 +0000 | [diff] [blame^] | 75 | copy_back_<int8_t>(dst, ra, src); |
| 76 | static_assert(7 == std::tuple_size<MixedTyped>::value, |
Lev Proleev | 9b490f4 | 2018-11-02 12:44:11 +0000 | [diff] [blame] | 77 | "Number of types in MixedTyped changed, but copy_back function wasn't updated"); |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 78 | } |
| 79 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 80 | // Top level driver for models and examples generated by test_generator.py |
| 81 | // Test driver for those generated from ml/nn/runtime/test/spec |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 82 | static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>& preparedModel, |
| 83 | const Request& request, |
| 84 | sp<ExecutionCallback>& callback) { |
| 85 | return preparedModel->execute(request, callback); |
| 86 | } |
| 87 | static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel, |
| 88 | const Request& request, |
| 89 | sp<ExecutionCallback>& callback) { |
| 90 | return preparedModel->execute_1_2(request, callback); |
| 91 | } |
| 92 | template <typename T_IPreparedModel> |
| 93 | void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored, |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 94 | const std::vector<MixedTypedExample>& examples, |
| 95 | bool hasRelaxedFloat32Model = false, float fpAtol = 1e-5f, |
Xusong Wang | 10d77e4 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 96 | float fpRtol = 1e-5f) { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 97 | const uint32_t INPUT = 0; |
| 98 | const uint32_t OUTPUT = 1; |
| 99 | |
| 100 | int example_no = 1; |
| 101 | for (auto& example : examples) { |
| 102 | SCOPED_TRACE(example_no++); |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 103 | const MixedTyped& inputs = example.operands.first; |
| 104 | const MixedTyped& golden = example.operands.second; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 105 | |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 106 | const bool hasFloat16Inputs = !std::get<MixedTypedIndex<_Float16>::index>(inputs).empty(); |
| 107 | if (hasRelaxedFloat32Model || hasFloat16Inputs) { |
| 108 | // TODO: Adjust the error limit based on testing. |
| 109 | // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16. |
| 110 | fpAtol = 5.0f * 0.0009765625f; |
| 111 | // Set the relative tolerance to be 5ULP of the corresponding FP precision. |
| 112 | fpRtol = 5.0f * 0.0009765625f; |
| 113 | } |
| 114 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 115 | std::vector<RequestArgument> inputs_info, outputs_info; |
| 116 | uint32_t inputSize = 0, outputSize = 0; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 117 | // This function only partially specifies the metadata (vector of RequestArguments). |
| 118 | // The contents are copied over below. |
| 119 | for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) { |
| 120 | if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1); |
| 121 | RequestArgument arg = { |
| 122 | .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)}, |
| 123 | .dimensions = {}, |
| 124 | }; |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 125 | RequestArgument arg_empty = { |
| 126 | .hasNoValue = true, |
| 127 | }; |
| 128 | inputs_info[index] = s ? arg : arg_empty; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 129 | inputSize += s; |
| 130 | }); |
| 131 | // Compute offset for inputs 1 and so on |
| 132 | { |
| 133 | size_t offset = 0; |
| 134 | for (auto& i : inputs_info) { |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 135 | if (!i.hasNoValue) i.location.offset = offset; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 136 | offset += i.location.length; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | MixedTyped test; // holding test results |
| 141 | |
| 142 | // Go through all outputs, initialize RequestArgument descriptors |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 143 | resize_accordingly(golden, test); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 144 | for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) { |
| 145 | if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1); |
| 146 | RequestArgument arg = { |
| 147 | .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)}, |
| 148 | .dimensions = {}, |
| 149 | }; |
| 150 | outputs_info[index] = arg; |
| 151 | outputSize += s; |
| 152 | }); |
| 153 | // Compute offset for outputs 1 and so on |
| 154 | { |
| 155 | size_t offset = 0; |
| 156 | for (auto& i : outputs_info) { |
| 157 | i.location.offset = offset; |
| 158 | offset += i.location.length; |
| 159 | } |
| 160 | } |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 161 | std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize), |
| 162 | nn::allocateSharedMemory(outputSize)}; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 163 | ASSERT_NE(0ull, pools[INPUT].size()); |
| 164 | ASSERT_NE(0ull, pools[OUTPUT].size()); |
| 165 | |
| 166 | // load data |
| 167 | sp<IMemory> inputMemory = mapMemory(pools[INPUT]); |
| 168 | sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]); |
| 169 | ASSERT_NE(nullptr, inputMemory.get()); |
| 170 | ASSERT_NE(nullptr, outputMemory.get()); |
| 171 | char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer())); |
| 172 | char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer())); |
| 173 | ASSERT_NE(nullptr, inputPtr); |
| 174 | ASSERT_NE(nullptr, outputPtr); |
| 175 | inputMemory->update(); |
| 176 | outputMemory->update(); |
| 177 | |
| 178 | // Go through all inputs, copy the values |
| 179 | for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) { |
| 180 | char* begin = (char*)p; |
| 181 | char* end = begin + s; |
| 182 | // TODO: handle more than one input |
| 183 | std::copy(begin, end, inputPtr + inputs_info[index].location.offset); |
| 184 | }); |
| 185 | |
| 186 | inputMemory->commit(); |
| 187 | outputMemory->commit(); |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 188 | |
| 189 | // launch execution |
| 190 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 191 | ASSERT_NE(nullptr, executionCallback.get()); |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 192 | Return<ErrorStatus> executionLaunchStatus = ExecutePreparedModel( |
| 193 | preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools}, |
| 194 | executionCallback); |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 195 | ASSERT_TRUE(executionLaunchStatus.isOk()); |
| 196 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus)); |
| 197 | |
| 198 | // retrieve execution status |
| 199 | executionCallback->wait(); |
| 200 | ErrorStatus executionReturnStatus = executionCallback->getStatus(); |
| 201 | EXPECT_EQ(ErrorStatus::NONE, executionReturnStatus); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 202 | |
| 203 | // validate results |
| 204 | outputMemory->read(); |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 205 | copy_back(&test, outputs_info, outputPtr); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 206 | outputMemory->commit(); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 207 | // Filter out don't cares |
I-Jui (Ray) Sung | 5bf4edf | 2017-10-06 13:22:39 -0700 | [diff] [blame] | 208 | MixedTyped filtered_golden = filter(golden, is_ignored); |
| 209 | MixedTyped filtered_test = filter(test, is_ignored); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 210 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 211 | // We want "close-enough" results for float |
Xusong Wang | 10d77e4 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 212 | compare(filtered_golden, filtered_test, fpAtol, fpRtol); |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 213 | |
| 214 | if (example.expectedMultinomialDistributionTolerance > 0) { |
| 215 | expectMultinomialDistributionWithinTolerance(test, example); |
| 216 | } |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 220 | static void getPreparedModel(sp<PreparedModelCallback> callback, |
| 221 | sp<V1_0::IPreparedModel>* preparedModel) { |
| 222 | *preparedModel = callback->getPreparedModel(); |
| 223 | } |
| 224 | static void getPreparedModel(sp<PreparedModelCallback> callback, |
| 225 | sp<V1_2::IPreparedModel>* preparedModel) { |
| 226 | sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel(); |
| 227 | *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr); |
| 228 | } |
| 229 | |
Michael Butler | f76acd0 | 2018-03-22 16:37:57 -0700 | [diff] [blame] | 230 | void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model, |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 231 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 232 | V1_0::Model model = create_model(); |
| 233 | |
| 234 | // see if service can handle model |
| 235 | bool fullySupportsModel = false; |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 236 | Return<void> supportedCall = device->getSupportedOperations( |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 237 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 238 | ASSERT_EQ(ErrorStatus::NONE, status); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 239 | ASSERT_NE(0ul, supported.size()); |
| 240 | fullySupportsModel = |
| 241 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 242 | }); |
| 243 | ASSERT_TRUE(supportedCall.isOk()); |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 244 | |
| 245 | // launch prepare model |
| 246 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 247 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 248 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback); |
| 249 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 250 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 251 | |
| 252 | // retrieve prepared model |
| 253 | preparedModelCallback->wait(); |
| 254 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 255 | sp<V1_0::IPreparedModel> preparedModel; |
| 256 | getPreparedModel(preparedModelCallback, &preparedModel); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 257 | |
| 258 | // early termination if vendor service cannot fully prepare model |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 259 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 260 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 261 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 262 | "prepare model that it does not support."; |
| 263 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 264 | "prepare model that it does not support." |
| 265 | << std::endl; |
| 266 | return; |
| 267 | } |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 268 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 269 | ASSERT_NE(nullptr, preparedModel.get()); |
| 270 | |
Xusong Wang | 10d77e4 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 271 | float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f; |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 272 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
| 273 | /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Michael Butler | f76acd0 | 2018-03-22 16:37:57 -0700 | [diff] [blame] | 276 | void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model, |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 277 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 278 | V1_1::Model model = create_model(); |
| 279 | |
| 280 | // see if service can handle model |
| 281 | bool fullySupportsModel = false; |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 282 | Return<void> supportedCall = device->getSupportedOperations_1_1( |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 283 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 284 | ASSERT_EQ(ErrorStatus::NONE, status); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 285 | ASSERT_NE(0ul, supported.size()); |
| 286 | fullySupportsModel = |
| 287 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 288 | }); |
| 289 | ASSERT_TRUE(supportedCall.isOk()); |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 290 | |
| 291 | // launch prepare model |
| 292 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 293 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
Michael Butler | 2504c2f | 2018-04-11 16:30:09 -0700 | [diff] [blame] | 294 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1( |
| 295 | model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 296 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 297 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 298 | |
| 299 | // retrieve prepared model |
| 300 | preparedModelCallback->wait(); |
| 301 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 302 | sp<V1_0::IPreparedModel> preparedModel; |
| 303 | getPreparedModel(preparedModelCallback, &preparedModel); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 304 | |
| 305 | // early termination if vendor service cannot fully prepare model |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 306 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 307 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 308 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 309 | "prepare model that it does not support."; |
| 310 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 311 | "prepare model that it does not support." |
| 312 | << std::endl; |
| 313 | return; |
| 314 | } |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 315 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 316 | ASSERT_NE(nullptr, preparedModel.get()); |
| 317 | |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 318 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
| 319 | model.relaxComputationFloat32toFloat16); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 322 | // TODO: Reduce code duplication. |
| 323 | void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model, |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 324 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 325 | V1_2::Model model = create_model(); |
| 326 | |
| 327 | // see if service can handle model |
| 328 | bool fullySupportsModel = false; |
| 329 | Return<void> supportedCall = device->getSupportedOperations_1_2( |
| 330 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 331 | ASSERT_EQ(ErrorStatus::NONE, status); |
| 332 | ASSERT_NE(0ul, supported.size()); |
| 333 | fullySupportsModel = |
| 334 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 335 | }); |
| 336 | ASSERT_TRUE(supportedCall.isOk()); |
| 337 | |
| 338 | // launch prepare model |
| 339 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 340 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
| 341 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2( |
| 342 | model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback); |
| 343 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 344 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
| 345 | |
| 346 | // retrieve prepared model |
| 347 | preparedModelCallback->wait(); |
| 348 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | b5cb8f7 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 349 | sp<V1_2::IPreparedModel> preparedModel; |
| 350 | getPreparedModel(preparedModelCallback, &preparedModel); |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 351 | |
| 352 | // early termination if vendor service cannot fully prepare model |
| 353 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
| 354 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 355 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 356 | "prepare model that it does not support."; |
| 357 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 358 | "prepare model that it does not support." |
| 359 | << std::endl; |
| 360 | return; |
| 361 | } |
| 362 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
| 363 | ASSERT_NE(nullptr, preparedModel.get()); |
| 364 | |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 365 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
| 366 | model.relaxComputationFloat32toFloat16); |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 367 | } |
| 368 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 369 | } // namespace generated_tests |
| 370 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 371 | } // namespace neuralnetworks |
| 372 | } // namespace hardware |
| 373 | } // namespace android |