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 | |
Xusong Wang | 3405878 | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 17 | #include "GeneratedTestHarness.h" |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 18 | #include "1.0/Callbacks.h" |
| 19 | #include "1.0/Utils.h" |
| 20 | #include "MemoryUtils.h" |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 21 | #include "TestHarness.h" |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 22 | |
| 23 | #include <android-base/logging.h> |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 24 | #include <android/hardware/neuralnetworks/1.0/IDevice.h> |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 25 | #include <android/hardware/neuralnetworks/1.0/IPreparedModel.h> |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 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> |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 30 | |
Michael Butler | 0897ab3 | 2017-10-04 02:38:42 -0700 | [diff] [blame] | 31 | #include <iostream> |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace hardware { |
| 35 | namespace neuralnetworks { |
Slava Shklyaev | 2bcfdc8 | 2019-07-17 15:50:57 +0100 | [diff] [blame] | 36 | namespace V1_0 { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 37 | namespace generated_tests { |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 38 | |
| 39 | using ::android::hardware::neuralnetworks::V1_0::ErrorStatus; |
| 40 | using ::android::hardware::neuralnetworks::V1_0::IDevice; |
| 41 | using ::android::hardware::neuralnetworks::V1_0::IPreparedModel; |
| 42 | using ::android::hardware::neuralnetworks::V1_0::Model; |
| 43 | using ::android::hardware::neuralnetworks::V1_0::Request; |
| 44 | using ::android::hardware::neuralnetworks::V1_0::RequestArgument; |
| 45 | using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback; |
| 46 | using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback; |
| 47 | using ::android::hidl::memory::V1_0::IMemory; |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 48 | using ::test_helper::compare; |
Mika Raento | de16694 | 2018-04-17 16:49:50 +0100 | [diff] [blame] | 49 | using ::test_helper::filter; |
| 50 | using ::test_helper::for_all; |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 51 | using ::test_helper::MixedTyped; |
| 52 | using ::test_helper::MixedTypedExample; |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 53 | using ::test_helper::resize_accordingly; |
Xusong Wang | d293315 | 2019-03-12 14:40:32 -0700 | [diff] [blame] | 54 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 55 | // Top level driver for models and examples generated by test_generator.py |
| 56 | // Test driver for those generated from ml/nn/runtime/test/spec |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 57 | void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored, |
| 58 | const std::vector<MixedTypedExample>& examples, float fpAtol, |
| 59 | float fpRtol) { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 60 | const uint32_t INPUT = 0; |
| 61 | const uint32_t OUTPUT = 1; |
| 62 | |
| 63 | int example_no = 1; |
| 64 | for (auto& example : examples) { |
| 65 | SCOPED_TRACE(example_no++); |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 66 | const MixedTyped& inputs = example.operands.first; |
| 67 | const MixedTyped& golden = example.operands.second; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 68 | |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 69 | CHECK(inputs.float16Operands.empty()) << "float16 is not supported in 1.0"; |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 70 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 71 | std::vector<RequestArgument> inputs_info, outputs_info; |
| 72 | uint32_t inputSize = 0, outputSize = 0; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 73 | // This function only partially specifies the metadata (vector of RequestArguments). |
| 74 | // The contents are copied over below. |
| 75 | for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) { |
| 76 | if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1); |
| 77 | RequestArgument arg = { |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 78 | .location = {.poolIndex = INPUT, |
| 79 | .offset = 0, |
| 80 | .length = static_cast<uint32_t>(s)}, |
| 81 | .dimensions = {}, |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 82 | }; |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 83 | RequestArgument arg_empty = { |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 84 | .hasNoValue = true, |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 85 | }; |
| 86 | inputs_info[index] = s ? arg : arg_empty; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 87 | inputSize += s; |
| 88 | }); |
| 89 | // Compute offset for inputs 1 and so on |
| 90 | { |
| 91 | size_t offset = 0; |
| 92 | for (auto& i : inputs_info) { |
I-Jui (Ray) Sung | 959cd78 | 2017-10-04 20:49:57 -0700 | [diff] [blame] | 93 | if (!i.hasNoValue) i.location.offset = offset; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 94 | offset += i.location.length; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | MixedTyped test; // holding test results |
| 99 | |
| 100 | // Go through all outputs, initialize RequestArgument descriptors |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 101 | resize_accordingly(golden, test); |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 102 | for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 103 | if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1); |
| 104 | RequestArgument arg = { |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 105 | .location = {.poolIndex = OUTPUT, |
| 106 | .offset = 0, |
| 107 | .length = static_cast<uint32_t>(s)}, |
| 108 | .dimensions = {}, |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 109 | }; |
| 110 | outputs_info[index] = arg; |
| 111 | outputSize += s; |
| 112 | }); |
| 113 | // Compute offset for outputs 1 and so on |
| 114 | { |
| 115 | size_t offset = 0; |
| 116 | for (auto& i : outputs_info) { |
| 117 | i.location.offset = offset; |
| 118 | offset += i.location.length; |
| 119 | } |
| 120 | } |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 121 | std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize), |
| 122 | nn::allocateSharedMemory(outputSize)}; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 123 | ASSERT_NE(0ull, pools[INPUT].size()); |
| 124 | ASSERT_NE(0ull, pools[OUTPUT].size()); |
| 125 | |
| 126 | // load data |
| 127 | sp<IMemory> inputMemory = mapMemory(pools[INPUT]); |
| 128 | sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]); |
| 129 | ASSERT_NE(nullptr, inputMemory.get()); |
| 130 | ASSERT_NE(nullptr, outputMemory.get()); |
| 131 | char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer())); |
| 132 | char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer())); |
| 133 | ASSERT_NE(nullptr, inputPtr); |
| 134 | ASSERT_NE(nullptr, outputPtr); |
| 135 | inputMemory->update(); |
| 136 | outputMemory->update(); |
| 137 | |
| 138 | // Go through all inputs, copy the values |
| 139 | for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) { |
| 140 | char* begin = (char*)p; |
| 141 | char* end = begin + s; |
| 142 | // TODO: handle more than one input |
| 143 | std::copy(begin, end, inputPtr + inputs_info[index].location.offset); |
| 144 | }); |
| 145 | |
| 146 | inputMemory->commit(); |
| 147 | outputMemory->commit(); |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 148 | |
Michael Butler | 814d837 | 2019-01-15 11:02:55 -0800 | [diff] [blame] | 149 | const Request request = {.inputs = inputs_info, .outputs = outputs_info, .pools = pools}; |
| 150 | |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 151 | // launch execution |
| 152 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 153 | ASSERT_NE(nullptr, executionCallback.get()); |
| 154 | Return<ErrorStatus> executionLaunchStatus = |
| 155 | preparedModel->execute(request, executionCallback); |
| 156 | ASSERT_TRUE(executionLaunchStatus.isOk()); |
| 157 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus)); |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 158 | |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 159 | // retrieve execution status |
| 160 | executionCallback->wait(); |
| 161 | ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus()); |
Xusong Wang | 187c597 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 162 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 163 | // validate results |
| 164 | outputMemory->read(); |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame] | 165 | copy_back(&test, outputs_info, outputPtr); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 166 | outputMemory->commit(); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 167 | // Filter out don't cares |
I-Jui (Ray) Sung | 5bf4edf | 2017-10-06 13:22:39 -0700 | [diff] [blame] | 168 | MixedTyped filtered_golden = filter(golden, is_ignored); |
| 169 | MixedTyped filtered_test = filter(test, is_ignored); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 170 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 171 | // We want "close-enough" results for float |
Xusong Wang | 10d77e4 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 172 | compare(filtered_golden, filtered_test, fpAtol, fpRtol); |
Xusong Wang | 3405878 | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 176 | void Execute(const sp<IDevice>& device, std::function<Model(void)> create_model, |
Michael K. Sanders | 941d61a | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 177 | std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) { |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 178 | Model model = create_model(); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 179 | |
| 180 | // see if service can handle model |
| 181 | bool fullySupportsModel = false; |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 182 | Return<void> supportedCall = device->getSupportedOperations( |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 183 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 184 | ASSERT_EQ(ErrorStatus::NONE, status); |
| 185 | ASSERT_NE(0ul, supported.size()); |
| 186 | fullySupportsModel = std::all_of(supported.begin(), supported.end(), |
| 187 | [](bool valid) { return valid; }); |
| 188 | }); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 189 | ASSERT_TRUE(supportedCall.isOk()); |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 190 | |
| 191 | // launch prepare model |
| 192 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
| 193 | ASSERT_NE(nullptr, preparedModelCallback.get()); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 194 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback); |
| 195 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 196 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 197 | |
| 198 | // retrieve prepared model |
| 199 | preparedModelCallback->wait(); |
| 200 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 201 | sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel(); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 202 | |
| 203 | // early termination if vendor service cannot fully prepare model |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 204 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 205 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 206 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 207 | "prepare model that it does not support."; |
| 208 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 209 | "prepare model that it does not support." |
| 210 | << std::endl; |
Miao Wang | bb685a4 | 2019-01-08 12:27:35 -0800 | [diff] [blame] | 211 | GTEST_SKIP(); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 212 | } |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 213 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 214 | ASSERT_NE(nullptr, preparedModel.get()); |
| 215 | |
Xusong Wang | 10d77e4 | 2018-08-28 16:50:01 -0700 | [diff] [blame] | 216 | float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f; |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 217 | EvaluatePreparedModel(preparedModel, is_ignored, examples, fpAtol, fpRtol); |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 218 | } |
| 219 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 220 | } // namespace generated_tests |
Slava Shklyaev | 2bcfdc8 | 2019-07-17 15:50:57 +0100 | [diff] [blame] | 221 | } // namespace V1_0 |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 222 | } // namespace neuralnetworks |
| 223 | } // namespace hardware |
| 224 | } // namespace android |