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" |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 18 | |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 19 | #include "1.0/Callbacks.h" |
| 20 | #include "1.0/Utils.h" |
| 21 | #include "MemoryUtils.h" |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 22 | #include "TestHarness.h" |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 23 | |
| 24 | #include <android-base/logging.h> |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 25 | #include <android/hardware/neuralnetworks/1.0/IDevice.h> |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 26 | #include <android/hardware/neuralnetworks/1.0/IPreparedModel.h> |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 27 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 28 | #include <android/hidl/allocator/1.0/IAllocator.h> |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 29 | #include <android/hidl/memory/1.0/IMemory.h> |
| 30 | #include <hidlmemory/mapping.h> |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 31 | |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 32 | #include <gtest/gtest.h> |
Michael Butler | 0897ab3 | 2017-10-04 02:38:42 -0700 | [diff] [blame] | 33 | #include <iostream> |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | namespace hardware { |
| 37 | namespace neuralnetworks { |
Slava Shklyaev | 2bcfdc8 | 2019-07-17 15:50:57 +0100 | [diff] [blame] | 38 | namespace V1_0 { |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 39 | namespace generated_tests { |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 40 | |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 41 | using namespace test_helper; |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 42 | using ::android::hardware::neuralnetworks::V1_0::ErrorStatus; |
| 43 | using ::android::hardware::neuralnetworks::V1_0::IDevice; |
| 44 | using ::android::hardware::neuralnetworks::V1_0::IPreparedModel; |
| 45 | using ::android::hardware::neuralnetworks::V1_0::Model; |
| 46 | using ::android::hardware::neuralnetworks::V1_0::Request; |
| 47 | using ::android::hardware::neuralnetworks::V1_0::RequestArgument; |
| 48 | using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback; |
| 49 | using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback; |
| 50 | using ::android::hidl::memory::V1_0::IMemory; |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 51 | |
| 52 | Model createModel(const TestModel& testModel) { |
| 53 | // Model operands. |
| 54 | hidl_vec<Operand> operands(testModel.operands.size()); |
| 55 | size_t constCopySize = 0, constRefSize = 0; |
| 56 | for (uint32_t i = 0; i < testModel.operands.size(); i++) { |
| 57 | const auto& op = testModel.operands[i]; |
| 58 | |
| 59 | DataLocation loc = {}; |
| 60 | if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) { |
| 61 | loc = {.poolIndex = 0, |
| 62 | .offset = static_cast<uint32_t>(constCopySize), |
| 63 | .length = static_cast<uint32_t>(op.data.size())}; |
| 64 | constCopySize += op.data.alignedSize(); |
| 65 | } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) { |
| 66 | loc = {.poolIndex = 0, |
| 67 | .offset = static_cast<uint32_t>(constRefSize), |
| 68 | .length = static_cast<uint32_t>(op.data.size())}; |
| 69 | constRefSize += op.data.alignedSize(); |
| 70 | } |
| 71 | |
| 72 | operands[i] = {.type = static_cast<OperandType>(op.type), |
| 73 | .dimensions = op.dimensions, |
| 74 | .numberOfConsumers = op.numberOfConsumers, |
| 75 | .scale = op.scale, |
| 76 | .zeroPoint = op.zeroPoint, |
| 77 | .lifetime = static_cast<OperandLifeTime>(op.lifetime), |
| 78 | .location = loc}; |
| 79 | } |
| 80 | |
| 81 | // Model operations. |
| 82 | hidl_vec<Operation> operations(testModel.operations.size()); |
| 83 | std::transform(testModel.operations.begin(), testModel.operations.end(), operations.begin(), |
| 84 | [](const TestOperation& op) -> Operation { |
| 85 | return {.type = static_cast<OperationType>(op.type), |
| 86 | .inputs = op.inputs, |
| 87 | .outputs = op.outputs}; |
| 88 | }); |
| 89 | |
| 90 | // Constant copies. |
| 91 | hidl_vec<uint8_t> operandValues(constCopySize); |
| 92 | for (uint32_t i = 0; i < testModel.operands.size(); i++) { |
| 93 | const auto& op = testModel.operands[i]; |
| 94 | if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) { |
| 95 | const uint8_t* begin = op.data.get<uint8_t>(); |
| 96 | const uint8_t* end = begin + op.data.size(); |
| 97 | std::copy(begin, end, operandValues.data() + operands[i].location.offset); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Shared memory. |
| 102 | hidl_vec<hidl_memory> pools; |
| 103 | if (constRefSize > 0) { |
| 104 | hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize)); |
| 105 | CHECK_NE(pools[0].size(), 0u); |
| 106 | |
| 107 | // load data |
| 108 | sp<IMemory> mappedMemory = mapMemory(pools[0]); |
| 109 | CHECK(mappedMemory.get() != nullptr); |
| 110 | uint8_t* mappedPtr = |
| 111 | reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer())); |
| 112 | CHECK(mappedPtr != nullptr); |
| 113 | |
| 114 | for (uint32_t i = 0; i < testModel.operands.size(); i++) { |
| 115 | const auto& op = testModel.operands[i]; |
| 116 | if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) { |
| 117 | const uint8_t* begin = op.data.get<uint8_t>(); |
| 118 | const uint8_t* end = begin + op.data.size(); |
| 119 | std::copy(begin, end, mappedPtr + operands[i].location.offset); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return {.operands = std::move(operands), |
| 125 | .operations = std::move(operations), |
| 126 | .inputIndexes = testModel.inputIndexes, |
| 127 | .outputIndexes = testModel.outputIndexes, |
| 128 | .operandValues = std::move(operandValues), |
| 129 | .pools = std::move(pools)}; |
| 130 | } |
Xusong Wang | d293315 | 2019-03-12 14:40:32 -0700 | [diff] [blame] | 131 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 132 | // Top level driver for models and examples generated by test_generator.py |
| 133 | // Test driver for those generated from ml/nn/runtime/test/spec |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 134 | void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel) { |
| 135 | const Request request = createRequest(testModel); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 136 | |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 137 | // Launch execution. |
| 138 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 139 | Return<ErrorStatus> executionLaunchStatus = preparedModel->execute(request, executionCallback); |
| 140 | ASSERT_TRUE(executionLaunchStatus.isOk()); |
| 141 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus)); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 142 | |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 143 | // Retrieve execution status. |
| 144 | executionCallback->wait(); |
| 145 | ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus()); |
Michael K. Sanders | efa4c81 | 2018-10-30 14:44:48 +0000 | [diff] [blame] | 146 | |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 147 | // Retrieve execution results. |
| 148 | const std::vector<TestBuffer> outputs = getOutputBuffers(request); |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 149 | |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 150 | // We want "close-enough" results. |
| 151 | checkResults(testModel, outputs); |
Xusong Wang | 3405878 | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 154 | void Execute(const sp<IDevice>& device, const TestModel& testModel) { |
| 155 | Model model = createModel(testModel); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 156 | |
| 157 | // see if service can handle model |
| 158 | bool fullySupportsModel = false; |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 159 | Return<void> supportedCall = device->getSupportedOperations( |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 160 | model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 161 | ASSERT_EQ(ErrorStatus::NONE, status); |
| 162 | ASSERT_NE(0ul, supported.size()); |
| 163 | fullySupportsModel = std::all_of(supported.begin(), supported.end(), |
| 164 | [](bool valid) { return valid; }); |
| 165 | }); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 166 | ASSERT_TRUE(supportedCall.isOk()); |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 167 | |
| 168 | // launch prepare model |
| 169 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 170 | Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback); |
| 171 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 172 | ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus)); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 173 | |
| 174 | // retrieve prepared model |
| 175 | preparedModelCallback->wait(); |
| 176 | ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus(); |
Slava Shklyaev | 1d6b465 | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 177 | sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel(); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 178 | |
| 179 | // early termination if vendor service cannot fully prepare model |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 180 | if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) { |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 181 | ASSERT_EQ(nullptr, preparedModel.get()); |
| 182 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 183 | "prepare model that it does not support."; |
| 184 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 185 | "prepare model that it does not support." |
| 186 | << std::endl; |
Miao Wang | bb685a4 | 2019-01-08 12:27:35 -0800 | [diff] [blame] | 187 | GTEST_SKIP(); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 188 | } |
Michael Butler | 4d5bb10 | 2018-02-26 15:24:46 -0800 | [diff] [blame] | 189 | EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus); |
Miao Wang | a2d04c8 | 2018-02-05 17:26:54 -0800 | [diff] [blame] | 190 | ASSERT_NE(nullptr, preparedModel.get()); |
| 191 | |
Xusong Wang | 8e8b70c | 2019-08-09 16:38:14 -0700 | [diff] [blame^] | 192 | EvaluatePreparedModel(preparedModel, testModel); |
Slava Shklyaev | 871be94 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 193 | } |
| 194 | |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 195 | } // namespace generated_tests |
Slava Shklyaev | 2bcfdc8 | 2019-07-17 15:50:57 +0100 | [diff] [blame] | 196 | } // namespace V1_0 |
I-Jui (Ray) Sung | 2c4e136 | 2017-09-06 02:15:54 -0700 | [diff] [blame] | 197 | } // namespace neuralnetworks |
| 198 | } // namespace hardware |
| 199 | } // namespace android |