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 "NeuralNetworksWrapper.h"
18
19#include <android/sharedmem.h>
20//#include <android-base/logging.h>
21#include <gtest/gtest.h>
22#include <sys/mman.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26using namespace android::nn::wrapper;
27
28namespace {
29
30typedef float Matrix3x4[3][4];
31
32// Tests the various ways to pass weights and input/output data.
33class MemoryTest : public ::testing::Test {
34protected:
35    virtual void SetUp() {}
36
37    const Matrix3x4 matrix1 = {{1.f, 2.f, 3.f, 4.f}, {5.f, 6.f, 7.f, 8.f}, {9.f, 10.f, 11.f, 12.f}};
38    const Matrix3x4 matrix2 = {{100.f, 200.f, 300.f, 400.f},
39                               {500.f, 600.f, 700.f, 800.f},
40                               {900.f, 1000.f, 1100.f, 1200.f}};
41    const Matrix3x4 matrix3 = {{20.f, 30.f, 40.f, 50.f},
42                               {21.f, 22.f, 23.f, 24.f},
43                               {31.f, 32.f, 33.f, 34.f}};
44    const Matrix3x4 expected3 = {{121.f, 232.f, 343.f, 454.f},
45                                 {526.f, 628.f, 730.f, 832.f},
46                                 {940.f, 1042.f, 1144.f, 1246.f}};
47    const Matrix3x4 expected3b = {{22.f, 34.f, 46.f, 58.f},
48                                  {31.f, 34.f, 37.f, 40.f},
49                                  {49.f, 52.f, 55.f, 58.f}};
50};
51
52// Check that the values are the same. This works only if dealing with integer
53// value, otherwise we should accept values that are similar if not exact.
54int CompareMatrices(const Matrix3x4& expected, const Matrix3x4& actual) {
55    int errors = 0;
56    for (int i = 0; i < 3; i++) {
57        for (int j = 0; j < 4; j++) {
58            if (expected[i][j] != actual[i][j]) {
59                printf("expected[%d][%d] != actual[%d][%d], %f != %f\n", i, j, i, j,
60                       static_cast<double>(expected[i][j]), static_cast<double>(actual[i][j]));
61                errors++;
62            }
63        }
64    }
65    return errors;
66}
67
68// TODO: test non-zero offset.
69TEST_F(MemoryTest, TestASharedMemory) {
70    // Layout where to place matrix2 and matrix3 in the memory we'll allocate.
71    // We have gaps to test that we don't assume contiguity.
72    constexpr uint32_t offsetForMatrix2 = 20;
73    constexpr uint32_t offsetForMatrix3 = offsetForMatrix2 + sizeof(matrix2) + 30;
74    constexpr uint32_t memorySize = offsetForMatrix3 + sizeof(matrix3) + 60;
75
76    int weightsFd = ASharedMemory_create("weights", memorySize);
77    ASSERT_GT(weightsFd, -1);
78    uint8_t* weightsData = (uint8_t*)mmap(nullptr, memorySize, PROT_READ | PROT_WRITE,
79                                          MAP_SHARED, weightsFd, 0);
80    ASSERT_NE(weightsData, nullptr);
81    memcpy(weightsData + offsetForMatrix2, matrix2, sizeof(matrix2));
82    memcpy(weightsData + offsetForMatrix3, matrix3, sizeof(matrix3));
83    Memory weights(memorySize, PROT_READ | PROT_WRITE, weightsFd, 0);
84    ASSERT_TRUE(weights.isValid());
85
86    Model model;
87    OperandType matrixType(Type::TENSOR_FLOAT32, {3, 4});
88    OperandType scalarType(Type::INT32, {});
89    int32_t activation(0);
90    auto a = model.addOperand(&matrixType);
91    auto b = model.addOperand(&matrixType);
92    auto c = model.addOperand(&matrixType);
93    auto d = model.addOperand(&matrixType);
94    auto e = model.addOperand(&matrixType);
95    auto f = model.addOperand(&scalarType);
96
97    model.setOperandValueFromMemory(e, &weights, offsetForMatrix2, sizeof(Matrix3x4));
98    model.setOperandValueFromMemory(a, &weights, offsetForMatrix3, sizeof(Matrix3x4));
99    model.setOperandValue(f, &activation, sizeof(activation));
100    model.addOperation(ANEURALNETWORKS_ADD, {a, c, f}, {b});
101    model.addOperation(ANEURALNETWORKS_ADD, {b, e, f}, {d});
102    model.identifyInputsAndOutputs({c}, {d});
103    ASSERT_TRUE(model.isValid());
104    model.finish();
105
106    // Test the two node model.
107    constexpr uint32_t offsetForMatrix1 = 20;
108    int inputFd = ASharedMemory_create("input", offsetForMatrix1 + sizeof(Matrix3x4));
109    ASSERT_GT(inputFd, -1);
110    uint8_t* inputData = (uint8_t*)mmap(nullptr, offsetForMatrix1 + sizeof(Matrix3x4),
111                                        PROT_READ | PROT_WRITE, MAP_SHARED, inputFd, 0);
112    ASSERT_NE(inputData, nullptr);
113    memcpy(inputData + offsetForMatrix1, matrix1, sizeof(Matrix3x4));
114    Memory input(offsetForMatrix1 + sizeof(Matrix3x4), PROT_READ, inputFd, 0);
115    ASSERT_TRUE(input.isValid());
116
117    constexpr uint32_t offsetForActual = 32;
118    int outputFd = ASharedMemory_create("output", offsetForActual + sizeof(Matrix3x4));
119    ASSERT_GT(outputFd, -1);
120    uint8_t* outputData = (uint8_t*)mmap(nullptr, offsetForActual + sizeof(Matrix3x4),
121                                         PROT_READ | PROT_WRITE, MAP_SHARED, outputFd, 0);
122    ASSERT_NE(outputData, nullptr);
123    memset(outputData, 0, offsetForActual + sizeof(Matrix3x4));
124    Memory actual(offsetForActual + sizeof(Matrix3x4), PROT_READ | PROT_WRITE, outputFd, 0);
125    ASSERT_TRUE(actual.isValid());
126
127    Compilation compilation2(&model);
128    ASSERT_EQ(compilation2.finish(), Result::NO_ERROR);
129
130    Execution execution2(&compilation2);
131    ASSERT_EQ(execution2.setInputFromMemory(0, &input, offsetForMatrix1, sizeof(Matrix3x4)),
132              Result::NO_ERROR);
133    ASSERT_EQ(execution2.setOutputFromMemory(0, &actual, offsetForActual, sizeof(Matrix3x4)),
134              Result::NO_ERROR);
135    ASSERT_EQ(execution2.compute(), Result::NO_ERROR);
136    ASSERT_EQ(CompareMatrices(expected3, *reinterpret_cast<Matrix3x4*>(outputData + offsetForActual)), 0);
137    close(weightsFd);
138    close(inputFd);
139    close(outputFd);
140}
141
142TEST_F(MemoryTest, TestFd) {
143    // Create a file that contains matrix2 and matrix3.
144    char path[] = "/data/local/tmp/TestMemoryXXXXXX";
145    int fd = mkstemp(path);
146    const uint32_t offsetForMatrix2 = 20;
147    const uint32_t offsetForMatrix3 = 200;
148    static_assert(offsetForMatrix2 + sizeof(matrix2) < offsetForMatrix3, "matrices overlap");
149    lseek(fd, offsetForMatrix2, SEEK_SET);
150    write(fd, matrix2, sizeof(matrix2));
151    lseek(fd, offsetForMatrix3, SEEK_SET);
152    write(fd, matrix3, sizeof(matrix3));
153    fsync(fd);
154
155    Memory weights(offsetForMatrix3 + sizeof(matrix3), PROT_READ, fd, 0);
156    ASSERT_TRUE(weights.isValid());
157
158    Model model;
159    OperandType matrixType(Type::TENSOR_FLOAT32, {3, 4});
160    OperandType scalarType(Type::INT32, {});
161    int32_t activation(0);
162    auto a = model.addOperand(&matrixType);
163    auto b = model.addOperand(&matrixType);
164    auto c = model.addOperand(&matrixType);
165    auto d = model.addOperand(&matrixType);
166    auto e = model.addOperand(&matrixType);
167    auto f = model.addOperand(&scalarType);
168
169    model.setOperandValueFromMemory(e, &weights, offsetForMatrix2, sizeof(Matrix3x4));
170    model.setOperandValueFromMemory(a, &weights, offsetForMatrix3, sizeof(Matrix3x4));
171    model.setOperandValue(f, &activation, sizeof(activation));
172    model.addOperation(ANEURALNETWORKS_ADD, {a, c, f}, {b});
173    model.addOperation(ANEURALNETWORKS_ADD, {b, e, f}, {d});
174    model.identifyInputsAndOutputs({c}, {d});
175    ASSERT_TRUE(model.isValid());
176    model.finish();
177
178    // Test the three node model.
179    Matrix3x4 actual;
180    memset(&actual, 0, sizeof(actual));
181    Compilation compilation2(&model);
182    ASSERT_EQ(compilation2.finish(), Result::NO_ERROR);
183    Execution execution2(&compilation2);
184    ASSERT_EQ(execution2.setInput(0, matrix1, sizeof(Matrix3x4)), Result::NO_ERROR);
185    ASSERT_EQ(execution2.setOutput(0, actual, sizeof(Matrix3x4)), Result::NO_ERROR);
186    ASSERT_EQ(execution2.compute(), Result::NO_ERROR);
187    ASSERT_EQ(CompareMatrices(expected3, actual), 0);
188
189    close(fd);
190    unlink(path);
191}
192
193}  // end namespace
194