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" |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 18 | #include "ExecutionBurstController.h" |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 19 | #include "TestHarness.h" |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 20 | #include "Utils.h" |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 21 | |
| 22 | #include <android-base/logging.h> |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 23 | #include <android/hardware/neuralnetworks/1.0/IDevice.h> |
| 24 | #include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h> |
| 25 | #include <android/hardware/neuralnetworks/1.0/IPreparedModel.h> |
| 26 | #include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h> |
| 27 | #include <android/hardware/neuralnetworks/1.0/types.h> |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 28 | #include <android/hardware/neuralnetworks/1.1/IDevice.h> |
| 29 | #include <android/hardware/neuralnetworks/1.2/IDevice.h> |
| 30 | #include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h> |
| 31 | #include <android/hardware/neuralnetworks/1.2/IPreparedModel.h> |
| 32 | #include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h> |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 33 | #include <android/hidl/allocator/1.0/IAllocator.h> |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 34 | #include <android/hidl/memory/1.0/IMemory.h> |
| 35 | #include <hidlmemory/mapping.h> |
Michael Butler | 0897ab3 | 2017-10-04 02:38:42 -0700 | [diff] [blame] | 36 | #include <iostream> |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | namespace hardware { |
| 40 | namespace neuralnetworks { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 41 | |
| 42 | namespace generated_tests { |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 43 | using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback; |
| 44 | using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback; |
Slava Shklyaev | dc98cb0 | 2018-11-30 17:55:12 +0000 | [diff] [blame] | 45 | using ::test_helper::bool8; |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 46 | using ::test_helper::compare; |
| 47 | using ::test_helper::expectMultinomialDistributionWithinTolerance; |
Mika Raento | d534d32 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 48 | using ::test_helper::filter; |
| 49 | using ::test_helper::for_all; |
| 50 | using ::test_helper::for_each; |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 51 | using ::test_helper::MixedTyped; |
| 52 | using ::test_helper::MixedTypedExample; |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 53 | using ::test_helper::resize_accordingly; |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 54 | |
I-Jui (Ray) Sung | 5bf4edf | 2017-10-06 13:22:39 -0700 | [diff] [blame] | 55 | template <typename T> |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 56 | void copy_back_(std::map<int, std::vector<T>>* dst, const std::vector<RequestArgument>& ra, |
| 57 | char* src) { |
| 58 | for_each<T>(*dst, [&ra, src](int index, std::vector<T>& m) { |
I-Jui (Ray) Sung | 5bf4edf | 2017-10-06 13:22:39 -0700 | [diff] [blame] | 59 | ASSERT_EQ(m.size(), ra[index].location.length / sizeof(T)); |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 60 | char* begin = src + ra[index].location.offset; |
| 61 | memcpy(m.data(), begin, ra[index].location.length); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) { |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 66 | copy_back_(&dst->float32Operands, ra, src); |
| 67 | copy_back_(&dst->int32Operands, ra, src); |
| 68 | copy_back_(&dst->quant8AsymmOperands, ra, src); |
| 69 | copy_back_(&dst->quant16SymmOperands, ra, src); |
| 70 | copy_back_(&dst->float16Operands, ra, src); |
| 71 | copy_back_(&dst->bool8Operands, ra, src); |
| 72 | copy_back_(&dst->quant8ChannelOperands, ra, src); |
| 73 | copy_back_(&dst->quant16AsymmOperands, ra, src); |
| 74 | static_assert(8 == MixedTyped::kNumTypes, |
Lev Proleev | d36b7a8 | 2018-11-02 12:44:11 +0000 | [diff] [blame] | 75 | "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] | 76 | } |
| 77 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 78 | // Top level driver for models and examples generated by test_generator.py |
| 79 | // Test driver for those generated from ml/nn/runtime/test/spec |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 80 | static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>& preparedModel, |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 81 | const Request& request, MeasureTiming, |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 82 | sp<ExecutionCallback>& callback) { |
| 83 | return preparedModel->execute(request, callback); |
| 84 | } |
| 85 | static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel, |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 86 | const Request& request, MeasureTiming measure, |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 87 | sp<ExecutionCallback>& callback) { |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 88 | return preparedModel->execute_1_2(request, measure, callback); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 89 | } |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 90 | static Return<ErrorStatus> ExecutePreparedModel(sp<V1_0::IPreparedModel>&, const Request&, |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 91 | MeasureTiming, hidl_vec<OutputShape>*, Timing*) { |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 92 | ADD_FAILURE() << "asking for synchronous execution at V1_0"; |
| 93 | return ErrorStatus::GENERAL_FAILURE; |
| 94 | } |
| 95 | static Return<ErrorStatus> ExecutePreparedModel(sp<V1_2::IPreparedModel>& preparedModel, |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 96 | const Request& request, MeasureTiming measure, |
| 97 | hidl_vec<OutputShape>* outputShapes, |
| 98 | Timing* timing) { |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 99 | ErrorStatus result; |
| 100 | Return<void> ret = preparedModel->executeSynchronously( |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 101 | request, measure, |
| 102 | [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes, |
| 103 | const Timing& time) { |
| 104 | result = error; |
| 105 | *outputShapes = shapes; |
| 106 | *timing = time; |
| 107 | }); |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 108 | if (!ret.isOk()) { |
| 109 | return ErrorStatus::GENERAL_FAILURE; |
| 110 | } |
| 111 | return result; |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 112 | } |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 113 | static std::unique_ptr<::android::nn::ExecutionBurstController> CreateBurst( |
| 114 | const sp<V1_0::IPreparedModel>&) { |
| 115 | ADD_FAILURE() << "asking for burst execution at V1_0"; |
| 116 | return nullptr; |
| 117 | } |
| 118 | static std::unique_ptr<::android::nn::ExecutionBurstController> CreateBurst( |
| 119 | const sp<V1_2::IPreparedModel>& preparedModel) { |
| 120 | return ::android::nn::createExecutionBurstController(preparedModel, /*blocking=*/true); |
| 121 | } |
| 122 | enum class Executor { ASYNC, SYNC, BURST }; |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 123 | const float kDefaultAtol = 1e-5f; |
| 124 | const float kDefaultRtol = 1e-5f; |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 125 | template <typename T_IPreparedModel> |
| 126 | void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 127 | const std::vector<MixedTypedExample>& examples, bool hasRelaxedFloat32Model, float fpAtol, |
| 128 | float fpRtol, Executor executor, MeasureTiming measure, bool testDynamicOutputShape) { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 129 | const uint32_t INPUT = 0; |
| 130 | const uint32_t OUTPUT = 1; |
| 131 | |
| 132 | int example_no = 1; |
| 133 | for (auto& example : examples) { |
| 134 | SCOPED_TRACE(example_no++); |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 135 | const MixedTyped& inputs = example.operands.first; |
| 136 | const MixedTyped& golden = example.operands.second; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 137 | |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 138 | const bool hasFloat16Inputs = !inputs.float16Operands.empty(); |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 139 | if (hasRelaxedFloat32Model || hasFloat16Inputs) { |
| 140 | // TODO: Adjust the error limit based on testing. |
| 141 | // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16. |
| 142 | fpAtol = 5.0f * 0.0009765625f; |
| 143 | // Set the relative tolerance to be 5ULP of the corresponding FP precision. |
| 144 | fpRtol = 5.0f * 0.0009765625f; |
| 145 | } |
| 146 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 147 | std::vector<RequestArgument> inputs_info, outputs_info; |
| 148 | uint32_t inputSize = 0, outputSize = 0; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 149 | // This function only partially specifies the metadata (vector of RequestArguments). |
| 150 | // The contents are copied over below. |
| 151 | for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) { |
| 152 | if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1); |
| 153 | RequestArgument arg = { |
| 154 | .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)}, |
| 155 | .dimensions = {}, |
| 156 | }; |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 157 | RequestArgument arg_empty = { |
| 158 | .hasNoValue = true, |
| 159 | }; |
| 160 | inputs_info[index] = s ? arg : arg_empty; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 161 | inputSize += s; |
| 162 | }); |
| 163 | // Compute offset for inputs 1 and so on |
| 164 | { |
| 165 | size_t offset = 0; |
| 166 | for (auto& i : inputs_info) { |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 167 | if (!i.hasNoValue) i.location.offset = offset; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 168 | offset += i.location.length; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | MixedTyped test; // holding test results |
| 173 | |
| 174 | // Go through all outputs, initialize RequestArgument descriptors |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 175 | resize_accordingly(golden, test); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 176 | for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) { |
| 177 | if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1); |
| 178 | RequestArgument arg = { |
| 179 | .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)}, |
| 180 | .dimensions = {}, |
| 181 | }; |
| 182 | outputs_info[index] = arg; |
| 183 | outputSize += s; |
| 184 | }); |
| 185 | // Compute offset for outputs 1 and so on |
| 186 | { |
| 187 | size_t offset = 0; |
| 188 | for (auto& i : outputs_info) { |
| 189 | i.location.offset = offset; |
| 190 | offset += i.location.length; |
| 191 | } |
| 192 | } |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 193 | std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize), |
| 194 | nn::allocateSharedMemory(outputSize)}; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 195 | ASSERT_NE(0ull, pools[INPUT].size()); |
| 196 | ASSERT_NE(0ull, pools[OUTPUT].size()); |
| 197 | |
| 198 | // load data |
| 199 | sp<IMemory> inputMemory = mapMemory(pools[INPUT]); |
| 200 | sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]); |
| 201 | ASSERT_NE(nullptr, inputMemory.get()); |
| 202 | ASSERT_NE(nullptr, outputMemory.get()); |
| 203 | char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer())); |
| 204 | char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer())); |
| 205 | ASSERT_NE(nullptr, inputPtr); |
| 206 | ASSERT_NE(nullptr, outputPtr); |
| 207 | inputMemory->update(); |
| 208 | outputMemory->update(); |
| 209 | |
| 210 | // Go through all inputs, copy the values |
| 211 | for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) { |
| 212 | char* begin = (char*)p; |
| 213 | char* end = begin + s; |
| 214 | // TODO: handle more than one input |
| 215 | std::copy(begin, end, inputPtr + inputs_info[index].location.offset); |
| 216 | }); |
| 217 | |
| 218 | inputMemory->commit(); |
| 219 | outputMemory->commit(); |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 220 | |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 221 | const Request request = {.inputs = inputs_info, .outputs = outputs_info, .pools = pools}; |
| 222 | |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 223 | ErrorStatus executionStatus; |
| 224 | hidl_vec<OutputShape> outputShapes; |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 225 | Timing timing; |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 226 | switch (executor) { |
| 227 | case Executor::ASYNC: { |
| 228 | SCOPED_TRACE("asynchronous"); |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 229 | |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 230 | // launch execution |
| 231 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 232 | ASSERT_NE(nullptr, executionCallback.get()); |
| 233 | Return<ErrorStatus> executionLaunchStatus = |
| 234 | ExecutePreparedModel(preparedModel, request, measure, executionCallback); |
| 235 | ASSERT_TRUE(executionLaunchStatus.isOk()); |
| 236 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus)); |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 237 | |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 238 | // retrieve execution status |
| 239 | executionCallback->wait(); |
| 240 | executionStatus = executionCallback->getStatus(); |
| 241 | outputShapes = executionCallback->getOutputShapes(); |
| 242 | timing = executionCallback->getTiming(); |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 243 | |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 244 | break; |
| 245 | } |
| 246 | case Executor::SYNC: { |
| 247 | SCOPED_TRACE("synchronous"); |
| 248 | |
| 249 | // execute |
| 250 | Return<ErrorStatus> executionReturnStatus = ExecutePreparedModel( |
| 251 | preparedModel, request, measure, &outputShapes, &timing); |
| 252 | ASSERT_TRUE(executionReturnStatus.isOk()); |
| 253 | executionStatus = static_cast<ErrorStatus>(executionReturnStatus); |
| 254 | |
| 255 | break; |
| 256 | } |
| 257 | case Executor::BURST: { |
| 258 | SCOPED_TRACE("burst"); |
| 259 | |
| 260 | // create burst |
| 261 | const std::unique_ptr<::android::nn::ExecutionBurstController> controller = |
| 262 | CreateBurst(preparedModel); |
| 263 | ASSERT_NE(nullptr, controller.get()); |
| 264 | |
| 265 | // create memory keys |
| 266 | std::vector<intptr_t> keys(request.pools.size()); |
| 267 | for (size_t i = 0; i < keys.size(); ++i) { |
| 268 | keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]); |
| 269 | } |
| 270 | |
| 271 | // execute burst |
| 272 | std::tie(executionStatus, outputShapes, timing) = |
| 273 | controller->compute(request, measure, keys); |
| 274 | |
| 275 | break; |
| 276 | } |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 277 | } |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 278 | |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 279 | if (testDynamicOutputShape && executionStatus != ErrorStatus::NONE) { |
| 280 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 281 | "execute model that it does not support."; |
| 282 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 283 | "execute model that it does not support." |
| 284 | << std::endl; |
| 285 | return; |
| 286 | } |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 287 | ASSERT_EQ(ErrorStatus::NONE, executionStatus); |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 288 | if (measure == MeasureTiming::NO) { |
| 289 | EXPECT_EQ(UINT64_MAX, timing.timeOnDevice); |
| 290 | EXPECT_EQ(UINT64_MAX, timing.timeInDriver); |
| 291 | } else { |
| 292 | if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) { |
| 293 | EXPECT_LE(timing.timeOnDevice, timing.timeInDriver); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Go through all outputs, overwrite output dimensions with returned output shapes |
| 298 | if (testDynamicOutputShape) { |
| 299 | ASSERT_NE(outputShapes.size(), 0); |
| 300 | for_each<uint32_t>(test.operandDimensions, |
| 301 | [&outputShapes](int idx, std::vector<uint32_t>& dim) { |
| 302 | dim = outputShapes[idx].dimensions; |
| 303 | }); |
| 304 | } |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 305 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 306 | // validate results |
| 307 | outputMemory->read(); |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 308 | copy_back(&test, outputs_info, outputPtr); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 309 | outputMemory->commit(); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 310 | // Filter out don't cares |
I-Jui (Ray) Sung | 5bf4edf | 2017-10-06 13:22:39 -0700 | [diff] [blame] | 311 | MixedTyped filtered_golden = filter(golden, is_ignored); |
| 312 | MixedTyped filtered_test = filter(test, is_ignored); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 313 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 314 | // We want "close-enough" results for float |
Xusong Wang | f6235f8 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 315 | compare(filtered_golden, filtered_test, fpAtol, fpRtol); |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 316 | |
| 317 | if (example.expectedMultinomialDistributionTolerance > 0) { |
| 318 | expectMultinomialDistributionWithinTolerance(test, example); |
| 319 | } |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 322 | template <typename T_IPreparedModel> |
| 323 | void EvaluatePreparedModel(sp<T_IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 324 | const std::vector<MixedTypedExample>& examples, bool hasRelaxedFloat32Model, |
| 325 | Executor executor, MeasureTiming measure, bool testDynamicOutputShape) { |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 326 | EvaluatePreparedModel(preparedModel, is_ignored, examples, hasRelaxedFloat32Model, kDefaultAtol, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 327 | kDefaultRtol, executor, measure, testDynamicOutputShape); |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 328 | } |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 329 | |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 330 | static void getPreparedModel(sp<PreparedModelCallback> callback, |
| 331 | sp<V1_0::IPreparedModel>* preparedModel) { |
| 332 | *preparedModel = callback->getPreparedModel(); |
| 333 | } |
| 334 | static void getPreparedModel(sp<PreparedModelCallback> callback, |
| 335 | sp<V1_2::IPreparedModel>* preparedModel) { |
| 336 | sp<V1_0::IPreparedModel> preparedModelV1_0 = callback->getPreparedModel(); |
| 337 | *preparedModel = V1_2::IPreparedModel::castFrom(preparedModelV1_0).withDefault(nullptr); |
| 338 | } |
| 339 | |
Michael Butler | 7ed6135 | 2018-03-22 16:37:57 -0700 | [diff] [blame] | 340 | 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] | 341 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 342 | V1_0::Model model = create_model(); |
| 343 | |
| 344 | // see if service can handle model |
| 345 | bool fullySupportsModel = false; |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 346 | Return<void> supportedCall = device->getSupportedOperations( |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 347 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 348 | ASSERT_EQ(ErrorStatus::NONE, status); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 349 | ASSERT_NE(0ul, supported.size()); |
| 350 | fullySupportsModel = |
| 351 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 352 | }); |
| 353 | ASSERT_TRUE(supportedCall.isOk()); |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 354 | |
| 355 | // launch prepare model |
| 356 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 357 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 358 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback); |
| 359 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 360 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 361 | |
| 362 | // retrieve prepared model |
| 363 | preparedModelCallback->wait(); |
| 364 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 365 | sp<V1_0::IPreparedModel> preparedModel; |
| 366 | getPreparedModel(preparedModelCallback, &preparedModel); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 367 | |
| 368 | // early termination if vendor service cannot fully prepare model |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 369 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 370 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 371 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 372 | "prepare model that it does not support."; |
| 373 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 374 | "prepare model that it does not support." |
| 375 | << std::endl; |
| 376 | return; |
| 377 | } |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 378 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 379 | ASSERT_NE(nullptr, preparedModel.get()); |
| 380 | |
Xusong Wang | f6235f8 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 381 | float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f; |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 382 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 383 | /*hasRelaxedFloat32Model=*/false, fpAtol, fpRtol, Executor::ASYNC, MeasureTiming::NO, |
| 384 | /*testDynamicOutputShape=*/false); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Michael Butler | 7ed6135 | 2018-03-22 16:37:57 -0700 | [diff] [blame] | 387 | 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] | 388 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 389 | V1_1::Model model = create_model(); |
| 390 | |
| 391 | // see if service can handle model |
| 392 | bool fullySupportsModel = false; |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 393 | Return<void> supportedCall = device->getSupportedOperations_1_1( |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 394 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 395 | ASSERT_EQ(ErrorStatus::NONE, status); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 396 | ASSERT_NE(0ul, supported.size()); |
| 397 | fullySupportsModel = |
| 398 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 399 | }); |
| 400 | ASSERT_TRUE(supportedCall.isOk()); |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 401 | |
| 402 | // launch prepare model |
| 403 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 404 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
Michael Butler | f02692d | 2018-04-11 16:30:09 -0700 | [diff] [blame] | 405 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1( |
| 406 | model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 407 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 408 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 409 | |
| 410 | // retrieve prepared model |
| 411 | preparedModelCallback->wait(); |
| 412 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 413 | sp<V1_0::IPreparedModel> preparedModel; |
| 414 | getPreparedModel(preparedModelCallback, &preparedModel); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 415 | |
| 416 | // early termination if vendor service cannot fully prepare model |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 417 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 418 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 419 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 420 | "prepare model that it does not support."; |
| 421 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 422 | "prepare model that it does not support." |
| 423 | << std::endl; |
| 424 | return; |
| 425 | } |
Michael Butler | 1ae02d6 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 426 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 427 | ASSERT_NE(nullptr, preparedModel.get()); |
| 428 | |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 429 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 430 | model.relaxComputationFloat32toFloat16, 1e-5f, 1e-5f, Executor::ASYNC, |
| 431 | MeasureTiming::NO, /*testDynamicOutputShape=*/false); |
Miao Wang | 4862d61 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 434 | // TODO: Reduce code duplication. |
| 435 | void Execute(const sp<V1_2::IDevice>& device, std::function<V1_2::Model(void)> create_model, |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 436 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples, |
| 437 | bool testDynamicOutputShape) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 438 | V1_2::Model model = create_model(); |
| 439 | |
| 440 | // see if service can handle model |
| 441 | bool fullySupportsModel = false; |
| 442 | Return<void> supportedCall = device->getSupportedOperations_1_2( |
| 443 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 444 | ASSERT_EQ(ErrorStatus::NONE, status); |
| 445 | ASSERT_NE(0ul, supported.size()); |
| 446 | fullySupportsModel = |
| 447 | std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; }); |
| 448 | }); |
| 449 | ASSERT_TRUE(supportedCall.isOk()); |
| 450 | |
| 451 | // launch prepare model |
| 452 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 453 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
| 454 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2( |
| 455 | model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback); |
| 456 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 457 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
| 458 | |
| 459 | // retrieve prepared model |
| 460 | preparedModelCallback->wait(); |
| 461 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 462 | sp<V1_2::IPreparedModel> preparedModel; |
| 463 | getPreparedModel(preparedModelCallback, &preparedModel); |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 464 | |
| 465 | // early termination if vendor service cannot fully prepare model |
| 466 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
| 467 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 468 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 469 | "prepare model that it does not support."; |
| 470 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 471 | "prepare model that it does not support." |
| 472 | << std::endl; |
| 473 | return; |
| 474 | } |
| 475 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
| 476 | ASSERT_NE(nullptr, preparedModel.get()); |
| 477 | |
Michael K. Sanders | 650fd18 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 478 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 479 | model.relaxComputationFloat32toFloat16, Executor::ASYNC, MeasureTiming::NO, |
| 480 | testDynamicOutputShape); |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 481 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 482 | model.relaxComputationFloat32toFloat16, Executor::SYNC, MeasureTiming::NO, |
| 483 | testDynamicOutputShape); |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 484 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 485 | model.relaxComputationFloat32toFloat16, Executor::BURST, MeasureTiming::NO, |
| 486 | testDynamicOutputShape); |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 487 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame^] | 488 | model.relaxComputationFloat32toFloat16, Executor::ASYNC, MeasureTiming::YES, |
| 489 | testDynamicOutputShape); |
| 490 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
| 491 | model.relaxComputationFloat32toFloat16, Executor::SYNC, MeasureTiming::YES, |
| 492 | testDynamicOutputShape); |
| 493 | EvaluatePreparedModel(preparedModel, is_ignored, examples, |
| 494 | model.relaxComputationFloat32toFloat16, Executor::BURST, MeasureTiming::YES, |
| 495 | testDynamicOutputShape); |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 496 | } |
| 497 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 498 | } // namespace generated_tests |
| 499 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 500 | } // namespace neuralnetworks |
| 501 | } // namespace hardware |
| 502 | } // namespace android |