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 | |
| 17 | #include "Event.h" |
| 18 | #include "TestHarness.h" |
| 19 | #include "VtsHalNeuralnetworksV1_0TargetTest.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | #include <android/hidl/memory/1.0/IMemory.h> |
| 23 | #include <hidlmemory/mapping.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace neuralnetworks { |
| 28 | namespace V1_0 { |
| 29 | namespace vts { |
| 30 | namespace functional { |
| 31 | // allocator helper |
| 32 | hidl_memory allocateSharedMemory(int64_t size, const std::string& type = "ashmem"); |
| 33 | |
| 34 | namespace generated_tests { |
| 35 | using ::android::hardware::neuralnetworks::V1_0::implementation::Event; |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 36 | using ::generated_tests::filter; |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 37 | using ::generated_tests::for_all; |
| 38 | using ::generated_tests::for_each; |
| 39 | using ::generated_tests::resize_accordingly; |
| 40 | using ::generated_tests::MixedTyped; |
| 41 | using ::generated_tests::MixedTypedExampleType; |
| 42 | using ::generated_tests::Float32Operands; |
| 43 | using ::generated_tests::Int32Operands; |
| 44 | using ::generated_tests::Quant8Operands; |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame^] | 45 | using ::generated_tests::compare; |
| 46 | |
| 47 | template <typename ty> |
| 48 | void copy_back_(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) { |
| 49 | MixedTyped& test = *dst; |
| 50 | for_each(test, [&ra, src](int index, std::vector<ty>& m) { |
| 51 | ASSERT_EQ(m.size(), ra[index].location.length / sizeof(ty)); |
| 52 | char* begin = src + ra[index].location.offset; |
| 53 | memcpy(m.data(), begin, ra[index].location.length); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | void copy_back(MixedTyped* dst, const std::vector<RequestArgument>& ra, char* src) { |
| 58 | copy_back_<float>(dst, ra, src); |
| 59 | copy_back_<int32_t>(dst, ra, src); |
| 60 | copy_back_<uint8_t>(dst, ra, src); |
| 61 | } |
| 62 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 63 | // Top level driver for models and examples generated by test_generator.py |
| 64 | // Test driver for those generated from ml/nn/runtime/test/spec |
| 65 | void Execute(const sp<IDevice>& device, std::function<Model(void)> create_model, |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 66 | std::function<bool(int)> is_ignored, |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 67 | const std::vector<MixedTypedExampleType>& examples) { |
| 68 | Model model = create_model(); |
| 69 | sp<IPreparedModel> preparedModel; |
| 70 | sp<Event> preparationEvent = new Event(); |
| 71 | ASSERT_NE(nullptr, preparationEvent.get()); |
| 72 | Return<void> prepareRet = device->prepareModel( |
| 73 | model, preparationEvent, [&](ErrorStatus status, const sp<IPreparedModel>& prepared) { |
| 74 | EXPECT_EQ(ErrorStatus::NONE, status); |
| 75 | preparedModel = prepared; |
| 76 | }); |
| 77 | ASSERT_TRUE(prepareRet.isOk()); |
| 78 | ASSERT_NE(nullptr, preparedModel.get()); |
| 79 | Event::Status preparationStatus = preparationEvent->wait(); |
| 80 | EXPECT_EQ(Event::Status::SUCCESS, preparationStatus); |
| 81 | |
| 82 | const uint32_t INPUT = 0; |
| 83 | const uint32_t OUTPUT = 1; |
| 84 | |
| 85 | int example_no = 1; |
| 86 | for (auto& example : examples) { |
| 87 | SCOPED_TRACE(example_no++); |
| 88 | |
| 89 | const MixedTyped& inputs = example.first; |
| 90 | const MixedTyped& golden = example.second; |
| 91 | |
| 92 | std::vector<RequestArgument> inputs_info, outputs_info; |
| 93 | uint32_t inputSize = 0, outputSize = 0; |
| 94 | |
| 95 | // This function only partially specifies the metadata (vector of RequestArguments). |
| 96 | // The contents are copied over below. |
| 97 | for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) { |
| 98 | if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1); |
| 99 | RequestArgument arg = { |
| 100 | .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)}, |
| 101 | .dimensions = {}, |
| 102 | }; |
| 103 | inputs_info[index] = arg; |
| 104 | inputSize += s; |
| 105 | }); |
| 106 | // Compute offset for inputs 1 and so on |
| 107 | { |
| 108 | size_t offset = 0; |
| 109 | for (auto& i : inputs_info) { |
| 110 | i.location.offset = offset; |
| 111 | offset += i.location.length; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | MixedTyped test; // holding test results |
| 116 | |
| 117 | // Go through all outputs, initialize RequestArgument descriptors |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame^] | 118 | resize_accordingly(golden, test); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 119 | for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) { |
| 120 | if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1); |
| 121 | RequestArgument arg = { |
| 122 | .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)}, |
| 123 | .dimensions = {}, |
| 124 | }; |
| 125 | outputs_info[index] = arg; |
| 126 | outputSize += s; |
| 127 | }); |
| 128 | // Compute offset for outputs 1 and so on |
| 129 | { |
| 130 | size_t offset = 0; |
| 131 | for (auto& i : outputs_info) { |
| 132 | i.location.offset = offset; |
| 133 | offset += i.location.length; |
| 134 | } |
| 135 | } |
| 136 | std::vector<hidl_memory> pools = {allocateSharedMemory(inputSize), |
| 137 | allocateSharedMemory(outputSize)}; |
| 138 | ASSERT_NE(0ull, pools[INPUT].size()); |
| 139 | ASSERT_NE(0ull, pools[OUTPUT].size()); |
| 140 | |
| 141 | // load data |
| 142 | sp<IMemory> inputMemory = mapMemory(pools[INPUT]); |
| 143 | sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]); |
| 144 | ASSERT_NE(nullptr, inputMemory.get()); |
| 145 | ASSERT_NE(nullptr, outputMemory.get()); |
| 146 | char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer())); |
| 147 | char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer())); |
| 148 | ASSERT_NE(nullptr, inputPtr); |
| 149 | ASSERT_NE(nullptr, outputPtr); |
| 150 | inputMemory->update(); |
| 151 | outputMemory->update(); |
| 152 | |
| 153 | // Go through all inputs, copy the values |
| 154 | for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) { |
| 155 | char* begin = (char*)p; |
| 156 | char* end = begin + s; |
| 157 | // TODO: handle more than one input |
| 158 | std::copy(begin, end, inputPtr + inputs_info[index].location.offset); |
| 159 | }); |
| 160 | |
| 161 | inputMemory->commit(); |
| 162 | outputMemory->commit(); |
| 163 | // execute request |
| 164 | sp<Event> executionEvent = new Event(); |
| 165 | ASSERT_NE(nullptr, executionEvent.get()); |
| 166 | Return<ErrorStatus> executeStatus = preparedModel->execute( |
| 167 | {.inputs = inputs_info, .outputs = outputs_info, .pools = pools}, executionEvent); |
| 168 | ASSERT_TRUE(executeStatus.isOk()); |
| 169 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executeStatus)); |
| 170 | Event::Status eventStatus = executionEvent->wait(); |
| 171 | EXPECT_EQ(Event::Status::SUCCESS, eventStatus); |
| 172 | |
| 173 | // validate results |
| 174 | outputMemory->read(); |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame^] | 175 | copy_back(&test, outputs_info, outputPtr); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 176 | outputMemory->commit(); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 177 | // Filter out don't cares |
| 178 | MixedTyped filtered_golden; |
| 179 | MixedTyped filtered_test; |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame^] | 180 | filter(golden, &filtered_golden, is_ignored); |
| 181 | filter(test, &filtered_test, is_ignored); |
I-Jui (Ray) Sung | 7d765bd | 2017-09-13 18:47:12 -0700 | [diff] [blame] | 182 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 183 | // We want "close-enough" results for float |
I-Jui (Ray) Sung | f6b8550 | 2017-09-20 13:45:50 -0700 | [diff] [blame^] | 184 | compare(filtered_golden, filtered_test); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | } // namespace generated_tests |
| 189 | |
| 190 | } // namespace functional |
| 191 | } // namespace vts |
| 192 | } // namespace V1_0 |
| 193 | } // namespace neuralnetworks |
| 194 | } // namespace hardware |
| 195 | } // namespace android |