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