Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 17 | #include "1.0/Utils.h" |
| 18 | |
| 19 | #include "MemoryUtils.h" |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 20 | #include "TestHarness.h" |
| 21 | |
Xusong Wang | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 23 | #include <android/hardware/neuralnetworks/1.0/types.h> |
Xusong Wang | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 24 | #include <android/hidl/allocator/1.0/IAllocator.h> |
| 25 | #include <android/hidl/memory/1.0/IMemory.h> |
| 26 | #include <hidlmemory/mapping.h> |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 27 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame^] | 28 | #include <algorithm> |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace neuralnetworks { |
| 34 | |
Xusong Wang | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 35 | using namespace test_helper; |
| 36 | using ::android::hardware::neuralnetworks::V1_0::DataLocation; |
| 37 | using ::android::hardware::neuralnetworks::V1_0::Request; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 38 | using ::android::hardware::neuralnetworks::V1_0::RequestArgument; |
Xusong Wang | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 39 | using ::android::hidl::memory::V1_0::IMemory; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 40 | |
Xusong Wang | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 41 | constexpr uint32_t kInputPoolIndex = 0; |
| 42 | constexpr uint32_t kOutputPoolIndex = 1; |
| 43 | |
| 44 | Request createRequest(const TestModel& testModel) { |
| 45 | // Model inputs. |
| 46 | hidl_vec<RequestArgument> inputs(testModel.inputIndexes.size()); |
| 47 | size_t inputSize = 0; |
| 48 | for (uint32_t i = 0; i < testModel.inputIndexes.size(); i++) { |
| 49 | const auto& op = testModel.operands[testModel.inputIndexes[i]]; |
| 50 | if (op.data.size() == 0) { |
| 51 | // Omitted input. |
| 52 | inputs[i] = {.hasNoValue = true}; |
| 53 | } else { |
| 54 | DataLocation loc = {.poolIndex = kInputPoolIndex, |
| 55 | .offset = static_cast<uint32_t>(inputSize), |
| 56 | .length = static_cast<uint32_t>(op.data.size())}; |
| 57 | inputSize += op.data.alignedSize(); |
| 58 | inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}}; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Model outputs. |
| 63 | hidl_vec<RequestArgument> outputs(testModel.outputIndexes.size()); |
| 64 | size_t outputSize = 0; |
| 65 | for (uint32_t i = 0; i < testModel.outputIndexes.size(); i++) { |
| 66 | const auto& op = testModel.operands[testModel.outputIndexes[i]]; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame^] | 67 | |
| 68 | // In the case of zero-sized output, we should at least provide a one-byte buffer. |
| 69 | // This is because zero-sized tensors are only supported internally to the driver, or |
| 70 | // reported in output shapes. It is illegal for the client to pre-specify a zero-sized |
| 71 | // tensor as model output. Otherwise, we will have two semantic conflicts: |
| 72 | // - "Zero dimension" conflicts with "unspecified dimension". |
| 73 | // - "Omitted operand buffer" conflicts with "zero-sized operand buffer". |
| 74 | size_t bufferSize = std::max<size_t>(op.data.size(), 1); |
| 75 | |
Xusong Wang | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 76 | DataLocation loc = {.poolIndex = kOutputPoolIndex, |
| 77 | .offset = static_cast<uint32_t>(outputSize), |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame^] | 78 | .length = static_cast<uint32_t>(bufferSize)}; |
| 79 | outputSize += op.data.size() == 0 ? TestBuffer::kAlignment : op.data.alignedSize(); |
Xusong Wang | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 80 | outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}}; |
| 81 | } |
| 82 | |
| 83 | // Allocate memory pools. |
| 84 | hidl_vec<hidl_memory> pools = {nn::allocateSharedMemory(inputSize), |
| 85 | nn::allocateSharedMemory(outputSize)}; |
| 86 | CHECK_NE(pools[kInputPoolIndex].size(), 0u); |
| 87 | CHECK_NE(pools[kOutputPoolIndex].size(), 0u); |
| 88 | sp<IMemory> inputMemory = mapMemory(pools[kInputPoolIndex]); |
| 89 | CHECK(inputMemory.get() != nullptr); |
| 90 | uint8_t* inputPtr = static_cast<uint8_t*>(static_cast<void*>(inputMemory->getPointer())); |
| 91 | CHECK(inputPtr != nullptr); |
| 92 | |
| 93 | // Copy input data to the memory pool. |
| 94 | for (uint32_t i = 0; i < testModel.inputIndexes.size(); i++) { |
| 95 | const auto& op = testModel.operands[testModel.inputIndexes[i]]; |
| 96 | if (op.data.size() > 0) { |
| 97 | const uint8_t* begin = op.data.get<uint8_t>(); |
| 98 | const uint8_t* end = begin + op.data.size(); |
| 99 | std::copy(begin, end, inputPtr + inputs[i].location.offset); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return {.inputs = std::move(inputs), .outputs = std::move(outputs), .pools = std::move(pools)}; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Xusong Wang | 7df4306 | 2019-08-09 16:38:14 -0700 | [diff] [blame] | 106 | std::vector<TestBuffer> getOutputBuffers(const Request& request) { |
| 107 | sp<IMemory> outputMemory = mapMemory(request.pools[kOutputPoolIndex]); |
| 108 | CHECK(outputMemory.get() != nullptr); |
| 109 | uint8_t* outputPtr = static_cast<uint8_t*>(static_cast<void*>(outputMemory->getPointer())); |
| 110 | CHECK(outputPtr != nullptr); |
| 111 | |
| 112 | // Copy out output results. |
| 113 | std::vector<TestBuffer> outputBuffers; |
| 114 | for (const auto& output : request.outputs) { |
| 115 | outputBuffers.emplace_back(output.location.length, outputPtr + output.location.offset); |
| 116 | } |
| 117 | |
| 118 | return outputBuffers; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | } // namespace neuralnetworks |
| 122 | } // namespace hardware |
| 123 | } // namespace android |