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