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 | 4862d61 | 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 | 4862d61 | 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 | 1a06e77 | 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 | 4862d61 | 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 | 1a06e77 | 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 | dc98cb0 | 2018-11-30 17:55:12 +0000 | [diff] [blame] | 44 | using ::test_helper::bool8; |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 45 | using ::test_helper::compare; |
| 46 | using ::test_helper::expectMultinomialDistributionWithinTolerance; |
Mika Raento | d534d32 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 47 | using ::test_helper::filter; |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 48 | using ::test_helper::Float32Operands; |
Mika Raento | d534d32 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 49 | using ::test_helper::for_all; |
| 50 | using ::test_helper::for_each; |
Mika Raento | d534d32 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 51 | using ::test_helper::Int32Operands; |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 52 | using ::test_helper::MixedTyped; |
| 53 | using ::test_helper::MixedTypedExample; |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 54 | using ::test_helper::MixedTypedIndex; |
Mika Raento | d534d32 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 55 | using ::test_helper::Quant8Operands; |
Michael K. Sanders | da3bdbc | 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 | ed7ce7a | 2018-11-05 13:20:06 +0000 | [diff] [blame] | 72 | copy_back_<int16_t>(dst, ra, src); |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 73 | copy_back_<_Float16>(dst, ra, src); |
Slava Shklyaev | dc98cb0 | 2018-11-30 17:55:12 +0000 | [diff] [blame] | 74 | copy_back_<bool8>(dst, ra, src); |
Przemyslaw Szczepaniak | c3da78a | 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 | d36b7a8 | 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 | 1a06e77 | 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 | } |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 92 | static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>&, const Request&) { |
| 93 | ADD_FAILURE() << "asking for synchronous execution at V1_0"; |
| 94 | return ErrorStatus::GENERAL_FAILURE; |
| 95 | } |
| 96 | static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel, |
| 97 | const Request& request) { |
| 98 | return preparedModel->executeSynchronously(request); |
| 99 | } |
| 100 | enum class Synchronously { NO, YES }; |
| 101 | const float kDefaultAtol = 1e-5f; |
| 102 | const float kDefaultRtol = 1e-5f; |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 103 | template <typename T_IPreparedModel> |
| 104 | void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored, |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 105 | const std::vector<MixedTypedExample>& examples, |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 106 | bool hasRelaxedFloat32Model = false, float fpAtol = kDefaultAtol, |
| 107 | float fpRtol = kDefaultRtol, Synchronously sync = Synchronously::NO) { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 108 | const uint32_t INPUT = 0; |
| 109 | const uint32_t OUTPUT = 1; |
| 110 | |
| 111 | int example_no = 1; |
| 112 | for (auto& example : examples) { |
| 113 | SCOPED_TRACE(example_no++); |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 114 | const MixedTyped& inputs = example.operands.first; |
| 115 | const MixedTyped& golden = example.operands.second; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 116 | |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 117 | const bool hasFloat16Inputs = !std::get<MixedTypedIndex<_Float16>::index>(inputs).empty(); |
| 118 | if (hasRelaxedFloat32Model || hasFloat16Inputs) { |
| 119 | // TODO: Adjust the error limit based on testing. |
| 120 | // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16. |
| 121 | fpAtol = 5.0f * 0.0009765625f; |
| 122 | // Set the relative tolerance to be 5ULP of the corresponding FP precision. |
| 123 | fpRtol = 5.0f * 0.0009765625f; |
| 124 | } |
| 125 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 126 | std::vector<RequestArgument> inputs_info, outputs_info; |
| 127 | uint32_t inputSize = 0, outputSize = 0; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 128 | // This function only partially specifies the metadata (vector of RequestArguments). |
| 129 | // The contents are copied over below. |
| 130 | for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) { |
| 131 | if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1); |
| 132 | RequestArgument arg = { |
| 133 | .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)}, |
| 134 | .dimensions = {}, |
| 135 | }; |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 136 | RequestArgument arg_empty = { |
| 137 | .hasNoValue = true, |
| 138 | }; |
| 139 | inputs_info[index] = s ? arg : arg_empty; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 140 | inputSize += s; |
| 141 | }); |
| 142 | // Compute offset for inputs 1 and so on |
| 143 | { |
| 144 | size_t offset = 0; |
| 145 | for (auto& i : inputs_info) { |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 146 | if (!i.hasNoValue) i.location.offset = offset; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 147 | offset += i.location.length; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | MixedTyped test; // holding test results |
| 152 | |
| 153 | // Go through all outputs, initialize RequestArgument descriptors |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 154 | resize_accordingly(golden, test); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 155 | for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) { |
| 156 | if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1); |
| 157 | RequestArgument arg = { |
| 158 | .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)}, |
| 159 | .dimensions = {}, |
| 160 | }; |
| 161 | outputs_info[index] = arg; |
| 162 | outputSize += s; |
| 163 | }); |
| 164 | // Compute offset for outputs 1 and so on |
| 165 | { |
| 166 | size_t offset = 0; |
| 167 | for (auto& i : outputs_info) { |
| 168 | i.location.offset = offset; |
| 169 | offset += i.location.length; |
| 170 | } |
| 171 | } |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 172 | std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize), |
| 173 | nn::allocateSharedMemory(outputSize)}; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 174 | ASSERT_NE(0ull, pools[INPUT].size()); |
| 175 | ASSERT_NE(0ull, pools[OUTPUT].size()); |
| 176 | |
| 177 | // load data |
| 178 | sp<IMemory> inputMemory = mapMemory(pools[INPUT]); |
| 179 | sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]); |
| 180 | ASSERT_NE(nullptr, inputMemory.get()); |
| 181 | ASSERT_NE(nullptr, outputMemory.get()); |
| 182 | char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer())); |
| 183 | char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer())); |
| 184 | ASSERT_NE(nullptr, inputPtr); |
| 185 | ASSERT_NE(nullptr, outputPtr); |
| 186 | inputMemory->update(); |
| 187 | outputMemory->update(); |
| 188 | |
| 189 | // Go through all inputs, copy the values |
| 190 | for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) { |
| 191 | char* begin = (char*)p; |
| 192 | char* end = begin + s; |
| 193 | // TODO: handle more than one input |
| 194 | std::copy(begin, end, inputPtr + inputs_info[index].location.offset); |
| 195 | }); |
| 196 | |
| 197 | inputMemory->commit(); |
| 198 | outputMemory->commit(); |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 199 | |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 200 | if (sync == Synchronously::NO) { |
| 201 | SCOPED_TRACE("asynchronous"); |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 202 | |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 203 | // launch execution |
| 204 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 205 | ASSERT_NE(nullptr, executionCallback.get()); |
| 206 | Return<ErrorStatus> executionLaunchStatus = ExecutePreparedModel( |
| 207 | preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools}, |
| 208 | executionCallback); |
| 209 | ASSERT_TRUE(executionLaunchStatus.isOk()); |
| 210 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus)); |
| 211 | |
| 212 | // retrieve execution status |
| 213 | executionCallback->wait(); |
| 214 | ErrorStatus executionReturnStatus = executionCallback->getStatus(); |
| 215 | EXPECT_EQ(ErrorStatus::NONE, executionReturnStatus); |
| 216 | } else { |
| 217 | SCOPED_TRACE("synchronous"); |
| 218 | |
| 219 | // execute |
| 220 | Return<ErrorStatus> executionStatus = ExecutePreparedModel( |
| 221 | preparedModel, {.inputs = inputs_info, .outputs = outputs_info, .pools = pools}); |
| 222 | ASSERT_TRUE(executionStatus.isOk()); |
| 223 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionStatus)); |
| 224 | } |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 225 | |
| 226 | // validate results |
| 227 | outputMemory->read(); |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 228 | copy_back(&test, outputs_info, outputPtr); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 229 | outputMemory->commit(); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 230 | // Filter out don't cares |
I-Jui (Ray) Sung | 5bf4edf | 2017-10-06 13:22:39 -0700 | [diff] [blame] | 231 | MixedTyped filtered_golden = filter(golden, is_ignored); |
| 232 | MixedTyped filtered_test = filter(test, is_ignored); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 233 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 234 | // We want "close-enough" results for float |
Xusong Wang | f6235f8 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 235 | compare(filtered_golden, filtered_test, fpAtol, fpRtol); |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 236 | |
| 237 | if (example.expectedMultinomialDistributionTolerance > 0) { |
| 238 | expectMultinomialDistributionWithinTolerance(test, example); |
| 239 | } |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 242 | template <typename T_IPreparedModel> |
| 243 | void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored, |
| 244 | const std::vector<MixedTypedExample>& examples, |
| 245 | bool hasRelaxedFloat32Model, Synchronously sync) { |
| 246 | EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model, kDefaultAtol, |
| 247 | kDefaultRtol, sync); |
| 248 | } |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 249 | |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 250 | static void getPreparedModel(sp<PreparedModelCallback> callback, |
| 251 | sp<V1_0::IPreparedModel>* preparedModel) { |
| 252 | *preparedModel = callback->getPreparedModel(); |
| 253 | } |
| 254 | static void getPreparedModel(sp<PreparedModelCallback> callback, |
| 255 | sp<V1_2::IPreparedModel>* preparedModel) { |
| 256 | sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel(); |
| 257 | *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr); |
| 258 | } |
| 259 | |
Michael Butler | 7ed6135 | 2018-03-22 16:37:57 -0700 | [diff] [blame] | 260 | void Execute(const sp<V1_0::IDevice>& device, std::function<V1_0::Model(void)> create_model, |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 261 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 262 | V1_0::Model model = create_model(); |
| 263 | |
| 264 | // see if service can handle model |
| 265 | bool fullySupportsModel = false; |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 266 | Return<void> supportedCall = device->getSupportedOperations( |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 267 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 268 | ASSERT_EQ(ErrorStatus::NONE, status); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 269 | ASSERT_NE(0ul, supported.size()); |
| 270 | fullySupportsModel = |
| 271 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 272 | }); |
| 273 | ASSERT_TRUE(supportedCall.isOk()); |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 274 | |
| 275 | // launch prepare model |
| 276 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 277 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 278 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback); |
| 279 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 280 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 281 | |
| 282 | // retrieve prepared model |
| 283 | preparedModelCallback->wait(); |
| 284 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 285 | sp<V1_0::IPreparedModel> preparedModel; |
| 286 | getPreparedModel(preparedModelCallback, &preparedModel); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 287 | |
| 288 | // early termination if vendor service cannot fully prepare model |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 289 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 290 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 291 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 292 | "prepare model that it does not support."; |
| 293 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 294 | "prepare model that it does not support." |
| 295 | << std::endl; |
| 296 | return; |
| 297 | } |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 298 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 299 | ASSERT_NE(nullptr, preparedModel.get()); |
| 300 | |
Xusong Wang | f6235f8 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 301 | float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f; |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 302 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
| 303 | /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Michael Butler | 7ed6135 | 2018-03-22 16:37:57 -0700 | [diff] [blame] | 306 | void Execute(const sp<V1_1::IDevice>& device, std::function<V1_1::Model(void)> create_model, |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 307 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 308 | V1_1::Model model = create_model(); |
| 309 | |
| 310 | // see if service can handle model |
| 311 | bool fullySupportsModel = false; |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 312 | Return<void> supportedCall = device->getSupportedOperations_1_1( |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 313 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 314 | ASSERT_EQ(ErrorStatus::NONE, status); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 315 | ASSERT_NE(0ul, supported.size()); |
| 316 | fullySupportsModel = |
| 317 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 318 | }); |
| 319 | ASSERT_TRUE(supportedCall.isOk()); |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 320 | |
| 321 | // launch prepare model |
| 322 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 323 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
Michael Butler | f02692d | 2018-04-11 16:30:09 -0700 | [diff] [blame] | 324 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1( |
| 325 | model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 326 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 327 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 328 | |
| 329 | // retrieve prepared model |
| 330 | preparedModelCallback->wait(); |
| 331 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 332 | sp<V1_0::IPreparedModel> preparedModel; |
| 333 | getPreparedModel(preparedModelCallback, &preparedModel); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 334 | |
| 335 | // early termination if vendor service cannot fully prepare model |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 336 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 337 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 338 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 339 | "prepare model that it does not support."; |
| 340 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 341 | "prepare model that it does not support." |
| 342 | << std::endl; |
| 343 | return; |
| 344 | } |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 345 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 346 | ASSERT_NE(nullptr, preparedModel.get()); |
| 347 | |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 348 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
| 349 | model.relaxComputationFloat32toFloat16); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 352 | // TODO: Reduce code duplication. |
| 353 | void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model, |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 354 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 355 | V1_2::Model model = create_model(); |
| 356 | |
| 357 | // see if service can handle model |
| 358 | bool fullySupportsModel = false; |
| 359 | Return<void> supportedCall = device->getSupportedOperations_1_2( |
| 360 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 361 | ASSERT_EQ(ErrorStatus::NONE, status); |
| 362 | ASSERT_NE(0ul, supported.size()); |
| 363 | fullySupportsModel = |
| 364 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 365 | }); |
| 366 | ASSERT_TRUE(supportedCall.isOk()); |
| 367 | |
| 368 | // launch prepare model |
| 369 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 370 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
| 371 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2( |
| 372 | model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback); |
| 373 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 374 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
| 375 | |
| 376 | // retrieve prepared model |
| 377 | preparedModelCallback->wait(); |
| 378 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 379 | sp<V1_2::IPreparedModel> preparedModel; |
| 380 | getPreparedModel(preparedModelCallback, &preparedModel); |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 381 | |
| 382 | // early termination if vendor service cannot fully prepare model |
| 383 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
| 384 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 385 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 386 | "prepare model that it does not support."; |
| 387 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 388 | "prepare model that it does not support." |
| 389 | << std::endl; |
| 390 | return; |
| 391 | } |
| 392 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
| 393 | ASSERT_NE(nullptr, preparedModel.get()); |
| 394 | |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 395 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 396 | model.relaxComputationFloat32toFloat16, Synchronously::NO); |
| 397 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
| 398 | model.relaxComputationFloat32toFloat16, Synchronously::YES); |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 399 | } |
| 400 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 401 | } // namespace generated_tests |
| 402 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 403 | } // namespace neuralnetworks |
| 404 | } // namespace hardware |
| 405 | } // namespace android |