blob: 0fd9947ede5fd0e793973509221e6c4797482678 [file] [log] [blame]
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -07001/*
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 Wang34058782019-01-18 17:28:26 -080017#include "GeneratedTestHarness.h"
Xusong Wang8e8b70c2019-08-09 16:38:14 -070018
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010019#include "1.0/Callbacks.h"
20#include "1.0/Utils.h"
21#include "MemoryUtils.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070022#include "TestHarness.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070023
24#include <android-base/logging.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080025#include <android/hardware/neuralnetworks/1.0/IDevice.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080026#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080027#include <android/hardware/neuralnetworks/1.0/types.h>
28#include <android/hidl/allocator/1.0/IAllocator.h>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070029#include <android/hidl/memory/1.0/IMemory.h>
30#include <hidlmemory/mapping.h>
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010031
Xusong Wang8e8b70c2019-08-09 16:38:14 -070032#include <gtest/gtest.h>
Michael Butler0897ab32017-10-04 02:38:42 -070033#include <iostream>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070034
35namespace android {
36namespace hardware {
37namespace neuralnetworks {
Slava Shklyaev2bcfdc82019-07-17 15:50:57 +010038namespace V1_0 {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070039namespace generated_tests {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010040
Xusong Wang8e8b70c2019-08-09 16:38:14 -070041using namespace test_helper;
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010042using ::android::hardware::neuralnetworks::V1_0::ErrorStatus;
43using ::android::hardware::neuralnetworks::V1_0::IDevice;
44using ::android::hardware::neuralnetworks::V1_0::IPreparedModel;
45using ::android::hardware::neuralnetworks::V1_0::Model;
46using ::android::hardware::neuralnetworks::V1_0::Request;
47using ::android::hardware::neuralnetworks::V1_0::RequestArgument;
48using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
49using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
50using ::android::hidl::memory::V1_0::IMemory;
Xusong Wang8e8b70c2019-08-09 16:38:14 -070051
52Model 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 Wangd2933152019-03-12 14:40:32 -0700131
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700132// 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 Wang8e8b70c2019-08-09 16:38:14 -0700134void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel) {
135 const Request request = createRequest(testModel);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700136
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700137 // 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) Sung2c4e1362017-09-06 02:15:54 -0700142
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700143 // Retrieve execution status.
144 executionCallback->wait();
145 ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus());
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000146
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700147 // Retrieve execution results.
148 const std::vector<TestBuffer> outputs = getOutputBuffers(request);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700149
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700150 // We want "close-enough" results.
151 checkResults(testModel, outputs);
Xusong Wang34058782019-01-18 17:28:26 -0800152}
153
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700154void Execute(const sp<IDevice>& device, const TestModel& testModel) {
155 Model model = createModel(testModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800156
157 // see if service can handle model
158 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800159 Return<void> supportedCall = device->getSupportedOperations(
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100160 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 Wanga2d04c82018-02-05 17:26:54 -0800166 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800167
168 // launch prepare model
169 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
Miao Wanga2d04c82018-02-05 17:26:54 -0800170 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
171 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800172 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800173
174 // retrieve prepared model
175 preparedModelCallback->wait();
176 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100177 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
Miao Wanga2d04c82018-02-05 17:26:54 -0800178
179 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800180 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800181 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 Wangbb685a42019-01-08 12:27:35 -0800187 GTEST_SKIP();
Miao Wanga2d04c82018-02-05 17:26:54 -0800188 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800189 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800190 ASSERT_NE(nullptr, preparedModel.get());
191
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700192 EvaluatePreparedModel(preparedModel, testModel);
Slava Shklyaev871be942018-09-12 14:52:02 +0100193}
194
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700195} // namespace generated_tests
Slava Shklyaev2bcfdc82019-07-17 15:50:57 +0100196} // namespace V1_0
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700197} // namespace neuralnetworks
198} // namespace hardware
199} // namespace android