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