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 "TestMemory.h"
18
19 #include <android-base/scopeguard.h>
20 #include <gtest/gtest.h>
21 #include <sys/mman.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include "TestNeuralNetworksWrapper.h"
26
27 using WrapperCompilation = ::android::nn::test_wrapper::Compilation;
28 using WrapperExecution = ::android::nn::test_wrapper::Execution;
29 using WrapperMemory = ::android::nn::test_wrapper::Memory;
30 using WrapperModel = ::android::nn::test_wrapper::Model;
31 using WrapperOperandType = ::android::nn::test_wrapper::OperandType;
32 using WrapperResult = ::android::nn::test_wrapper::Result;
33 using WrapperType = ::android::nn::test_wrapper::Type;
34
35 namespace {
36
37 // Tests the various ways to pass weights and input/output data.
38 class MemoryTest : public ::testing::Test {
39 protected:
SetUp()40 void SetUp() override {}
41 };
42
TEST_F(MemoryTest,TestFd)43 TEST_F(MemoryTest, TestFd) {
44 // Create a file that contains matrix2 and matrix3.
45 char path[] = "/data/local/tmp/TestMemoryXXXXXX";
46 int fd = mkstemp(path);
47 const uint32_t offsetForMatrix2 = 20;
48 const uint32_t offsetForMatrix3 = 200;
49 static_assert(offsetForMatrix2 + sizeof(matrix2) < offsetForMatrix3, "matrices overlap");
50 lseek(fd, offsetForMatrix2, SEEK_SET);
51 write(fd, matrix2, sizeof(matrix2));
52 lseek(fd, offsetForMatrix3, SEEK_SET);
53 write(fd, matrix3, sizeof(matrix3));
54 fsync(fd);
55
56 WrapperMemory weights(offsetForMatrix3 + sizeof(matrix3), PROT_READ, fd, 0);
57 ASSERT_TRUE(weights.isValid());
58
59 WrapperModel model;
60 WrapperOperandType matrixType(WrapperType::TENSOR_FLOAT32, {3, 4});
61 WrapperOperandType scalarType(WrapperType::INT32, {});
62 int32_t activation(0);
63 auto a = model.addOperand(&matrixType);
64 auto b = model.addOperand(&matrixType);
65 auto c = model.addOperand(&matrixType);
66 auto d = model.addOperand(&matrixType);
67 auto e = model.addOperand(&matrixType);
68 auto f = model.addOperand(&scalarType);
69
70 model.setOperandValueFromMemory(e, &weights, offsetForMatrix2, sizeof(Matrix3x4));
71 model.setOperandValueFromMemory(a, &weights, offsetForMatrix3, sizeof(Matrix3x4));
72 model.setOperandValue(f, &activation, sizeof(activation));
73 model.addOperation(ANEURALNETWORKS_ADD, {a, c, f}, {b});
74 model.addOperation(ANEURALNETWORKS_ADD, {b, e, f}, {d});
75 model.identifyInputsAndOutputs({c}, {d});
76 ASSERT_TRUE(model.isValid());
77 model.finish();
78
79 // Test the three node model.
80 Matrix3x4 actual;
81 memset(&actual, 0, sizeof(actual));
82 WrapperCompilation compilation2(&model);
83 ASSERT_EQ(compilation2.finish(), WrapperResult::NO_ERROR);
84 WrapperExecution execution2(&compilation2);
85 ASSERT_EQ(execution2.setInput(0, matrix1, sizeof(Matrix3x4)), WrapperResult::NO_ERROR);
86 ASSERT_EQ(execution2.setOutput(0, actual, sizeof(Matrix3x4)), WrapperResult::NO_ERROR);
87 ASSERT_EQ(execution2.compute(), WrapperResult::NO_ERROR);
88 ASSERT_EQ(CompareMatrices(expected3, actual), 0);
89
90 close(fd);
91 unlink(path);
92 }
93
94 // Hardware buffers are an Android concept, which aren't necessarily
95 // available on other platforms such as ChromeOS, which also build NNAPI.
96 #if defined(__ANDROID__)
TEST_F(MemoryTest,TestAHardwareBuffer)97 TEST_F(MemoryTest, TestAHardwareBuffer) {
98 const uint32_t offsetForMatrix2 = 20;
99 const uint32_t offsetForMatrix3 = 200;
100
101 AHardwareBuffer_Desc desc{
102 .width = offsetForMatrix3 + sizeof(matrix3),
103 .height = 1,
104 .layers = 1,
105 .format = AHARDWAREBUFFER_FORMAT_BLOB,
106 .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
107 };
108 AHardwareBuffer* buffer = nullptr;
109 ASSERT_EQ(AHardwareBuffer_allocate(&desc, &buffer), 0);
110 auto allocateGuard =
111 android::base::make_scope_guard([buffer]() { AHardwareBuffer_release(buffer); });
112
113 void* bufferPtr = nullptr;
114 ASSERT_EQ(AHardwareBuffer_lock(buffer, desc.usage, -1, NULL, &bufferPtr), 0);
115 memcpy((uint8_t*)bufferPtr + offsetForMatrix2, matrix2, sizeof(matrix2));
116 memcpy((uint8_t*)bufferPtr + offsetForMatrix3, matrix3, sizeof(matrix3));
117 ASSERT_EQ(AHardwareBuffer_unlock(buffer, nullptr), 0);
118
119 WrapperMemory weights(buffer);
120 ASSERT_TRUE(weights.isValid());
121
122 WrapperModel model;
123 WrapperOperandType matrixType(WrapperType::TENSOR_FLOAT32, {3, 4});
124 WrapperOperandType scalarType(WrapperType::INT32, {});
125 int32_t activation(0);
126 auto a = model.addOperand(&matrixType);
127 auto b = model.addOperand(&matrixType);
128 auto c = model.addOperand(&matrixType);
129 auto d = model.addOperand(&matrixType);
130 auto e = model.addOperand(&matrixType);
131 auto f = model.addOperand(&scalarType);
132
133 model.setOperandValueFromMemory(e, &weights, offsetForMatrix2, sizeof(Matrix3x4));
134 model.setOperandValueFromMemory(a, &weights, offsetForMatrix3, sizeof(Matrix3x4));
135 model.setOperandValue(f, &activation, sizeof(activation));
136 model.addOperation(ANEURALNETWORKS_ADD, {a, c, f}, {b});
137 model.addOperation(ANEURALNETWORKS_ADD, {b, e, f}, {d});
138 model.identifyInputsAndOutputs({c}, {d});
139 ASSERT_TRUE(model.isValid());
140 model.finish();
141
142 // Test the three node model.
143 Matrix3x4 actual;
144 memset(&actual, 0, sizeof(actual));
145 WrapperCompilation compilation2(&model);
146 ASSERT_EQ(compilation2.finish(), WrapperResult::NO_ERROR);
147 WrapperExecution execution2(&compilation2);
148 ASSERT_EQ(execution2.setInput(0, matrix1, sizeof(Matrix3x4)), WrapperResult::NO_ERROR);
149 ASSERT_EQ(execution2.setOutput(0, actual, sizeof(Matrix3x4)), WrapperResult::NO_ERROR);
150 ASSERT_EQ(execution2.compute(), WrapperResult::NO_ERROR);
151 ASSERT_EQ(CompareMatrices(expected3, actual), 0);
152 }
153 #endif
154
155 } // end namespace
156