TestTrivialModel.cpp revision f23352521cb0834f33f3802b7b2626db64d0eea3
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 <gtest/gtest.h>
20
21using namespace android::nn::wrapper;
22
23namespace {
24
25typedef float Matrix3x4[3][4];
26typedef float Matrix4[4];
27
28class TrivialTest : public ::testing::Test {
29protected:
30    virtual void SetUp() { ASSERT_EQ(Initialize(), Result::NO_ERROR); }
31    virtual void TearDown() { Shutdown(); }
32
33    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}};
34    const Matrix3x4 matrix2 = {{100.f, 200.f, 300.f, 400.f},
35                               {500.f, 600.f, 700.f, 800.f},
36                               {900.f, 1000.f, 1100.f, 1200.f}};
37    const Matrix4 matrix2b = {100.f, 200.f, 300.f, 400.f};
38    const Matrix3x4 matrix3 = {{20.f, 30.f, 40.f, 50.f},
39                               {21.f, 22.f, 23.f, 24.f},
40                               {31.f, 32.f, 33.f, 34.f}};
41    const Matrix3x4 expected2 = {{101.f, 202.f, 303.f, 404.f},
42                                 {505.f, 606.f, 707.f, 808.f},
43                                 {909.f, 1010.f, 1111.f, 1212.f}};
44    const Matrix3x4 expected2b = {{101.f, 202.f, 303.f, 404.f},
45                                  {105.f, 206.f, 307.f, 408.f},
46                                  {109.f, 210.f, 311.f, 412.f}};
47    const Matrix3x4 expected2c = {{100.f, 400.f, 900.f, 1600.f},
48                                  {500.f, 1200.f, 2100.f, 3200.f},
49                                  {900.f, 2000.f, 3300.f, 4800.f}};
50
51    const Matrix3x4 expected3 = {{121.f, 232.f, 343.f, 454.f},
52                                 {526.f, 628.f, 730.f, 832.f},
53                                 {940.f, 1042.f, 1144.f, 1246.f}};
54    const Matrix3x4 expected3b = {{22.f, 34.f, 46.f, 58.f},
55                                  {31.f, 34.f, 37.f, 40.f},
56                                  {49.f, 52.f, 55.f, 58.f}};
57};
58
59// Create a model that can add two tensors using a one node graph.
60void CreateAddTwoTensorModel(Model* model) {
61    OperandType matrixType(Type::TENSOR_FLOAT32, {3, 4});
62    OperandType scalarType(Type::INT32, {});
63    int32_t activation(0);
64    auto a = model->addOperand(&matrixType);
65    auto b = model->addOperand(&matrixType);
66    auto c = model->addOperand(&matrixType);
67    auto d = model->addOperand(&scalarType);
68    model->setOperandValue(d, &activation, sizeof(activation));
69    model->addOperation(ANEURALNETWORKS_ADD, {a, b, d}, {c});
70    model->setInputsAndOutputs({a, b}, {c});
71    ASSERT_TRUE(model->isValid());
72}
73
74// Create a model that can add three tensors using a two node graph,
75// with one tensor set as part of the model.
76void CreateAddThreeTensorModel(Model* model, const Matrix3x4 bias) {
77    OperandType matrixType(Type::TENSOR_FLOAT32, {3, 4});
78    OperandType scalarType(Type::INT32, {});
79    int32_t activation(0);
80    auto a = model->addOperand(&matrixType);
81    auto b = model->addOperand(&matrixType);
82    auto c = model->addOperand(&matrixType);
83    auto d = model->addOperand(&matrixType);
84    auto e = model->addOperand(&matrixType);
85    auto f = model->addOperand(&scalarType);
86    model->setOperandValue(e, bias, sizeof(Matrix3x4));
87    model->setOperandValue(f, &activation, sizeof(activation));
88    model->addOperation(ANEURALNETWORKS_ADD, {a, c, f}, {b});
89    model->addOperation(ANEURALNETWORKS_ADD, {b, e, f}, {d});
90    model->setInputsAndOutputs({c, a}, {d});
91    ASSERT_TRUE(model->isValid());
92}
93
94// Check that the values are the same. This works only if dealing with integer
95// value, otherwise we should accept values that are similar if not exact.
96int CompareMatrices(const Matrix3x4& expected, const Matrix3x4& actual) {
97    int errors = 0;
98    for (int i = 0; i < 3; i++) {
99        for (int j = 0; j < 4; j++) {
100            if (expected[i][j] != actual[i][j]) {
101                printf("expected[%d][%d] != actual[%d][%d], %f != %f\n", i, j, i, j,
102                       static_cast<double>(expected[i][j]), static_cast<double>(actual[i][j]));
103                errors++;
104            }
105        }
106    }
107    return errors;
108}
109
110TEST_F(TrivialTest, AddTwo) {
111    Model modelAdd2;
112    CreateAddTwoTensorModel(&modelAdd2);
113
114    // Test the one node model.
115    Matrix3x4 actual;
116    memset(&actual, 0, sizeof(actual));
117    Request request(&modelAdd2);
118    ASSERT_EQ(request.setInput(0, matrix1, sizeof(Matrix3x4)), Result::NO_ERROR);
119    ASSERT_EQ(request.setInput(1, matrix2, sizeof(Matrix3x4)), Result::NO_ERROR);
120    ASSERT_EQ(request.setOutput(0, actual, sizeof(Matrix3x4)), Result::NO_ERROR);
121    ASSERT_EQ(request.compute(), Result::NO_ERROR);
122    ASSERT_EQ(CompareMatrices(expected2, actual), 0);
123}
124
125TEST_F(TrivialTest, AddThree) {
126    Model modelAdd3;
127    CreateAddThreeTensorModel(&modelAdd3, matrix3);
128
129    // Test the three node model.
130    Matrix3x4 actual;
131    memset(&actual, 0, sizeof(actual));
132    Request request2(&modelAdd3);
133    ASSERT_EQ(request2.setInput(0, matrix1, sizeof(Matrix3x4)), Result::NO_ERROR);
134    ASSERT_EQ(request2.setInput(1, matrix2, sizeof(Matrix3x4)), Result::NO_ERROR);
135    ASSERT_EQ(request2.setOutput(0, actual, sizeof(Matrix3x4)), Result::NO_ERROR);
136    ASSERT_EQ(request2.compute(), Result::NO_ERROR);
137    ASSERT_EQ(CompareMatrices(expected3, actual), 0);
138
139    // Test it a second time to make sure the model is reusable.
140    memset(&actual, 0, sizeof(actual));
141    Request request3(&modelAdd3);
142    ASSERT_EQ(request3.setInput(0, matrix1, sizeof(Matrix3x4)), Result::NO_ERROR);
143    ASSERT_EQ(request3.setInput(1, matrix1, sizeof(Matrix3x4)), Result::NO_ERROR);
144    ASSERT_EQ(request3.setOutput(0, actual, sizeof(Matrix3x4)), Result::NO_ERROR);
145    ASSERT_EQ(request3.compute(), Result::NO_ERROR);
146    ASSERT_EQ(CompareMatrices(expected3b, actual), 0);
147}
148
149TEST_F(TrivialTest, BroadcastAddTwo) {
150    Model modelBroadcastAdd2;
151    // activation: NONE.
152    int32_t activation_init[] = {0};
153    OperandType scalarType(Type::INT32, {1});
154    auto activation = modelBroadcastAdd2.addOperand(&scalarType);
155    modelBroadcastAdd2.setOperandValue(activation, activation_init, sizeof(int32_t) * 1);
156
157    OperandType matrixType(Type::TENSOR_FLOAT32, {1, 1, 3, 4});
158    OperandType matrixType2(Type::TENSOR_FLOAT32, {4});
159
160    auto a = modelBroadcastAdd2.addOperand(&matrixType);
161    auto b = modelBroadcastAdd2.addOperand(&matrixType2);
162    auto c = modelBroadcastAdd2.addOperand(&matrixType);
163    modelBroadcastAdd2.addOperation(ANEURALNETWORKS_ADD, {a, b, activation}, {c});
164    modelBroadcastAdd2.setInputsAndOutputs({a, b}, {c});
165    ASSERT_TRUE(modelBroadcastAdd2.isValid());
166
167    // Test the one node model.
168    Matrix3x4 actual;
169    memset(&actual, 0, sizeof(actual));
170    Request request(&modelBroadcastAdd2);
171    ASSERT_EQ(request.setInput(0, matrix1, sizeof(Matrix3x4)), Result::NO_ERROR);
172    ASSERT_EQ(request.setInput(1, matrix2b, sizeof(Matrix4)), Result::NO_ERROR);
173    ASSERT_EQ(request.setOutput(0, actual, sizeof(Matrix3x4)), Result::NO_ERROR);
174    ASSERT_EQ(request.compute(), Result::NO_ERROR);
175    ASSERT_EQ(CompareMatrices(expected2b, actual), 0);
176}
177
178TEST_F(TrivialTest, BroadcastMulTwo) {
179    Model modelBroadcastMul2;
180    // activation: NONE.
181    int32_t activation_init[] = {0};
182    OperandType scalarType(Type::INT32, {1});
183    auto activation = modelBroadcastMul2.addOperand(&scalarType);
184    modelBroadcastMul2.setOperandValue(activation, activation_init, sizeof(int32_t) * 1);
185
186    OperandType matrixType(Type::TENSOR_FLOAT32, {1, 1, 3, 4});
187    OperandType matrixType2(Type::TENSOR_FLOAT32, {4});
188
189    auto a = modelBroadcastMul2.addOperand(&matrixType);
190    auto b = modelBroadcastMul2.addOperand(&matrixType2);
191    auto c = modelBroadcastMul2.addOperand(&matrixType);
192    modelBroadcastMul2.addOperation(ANEURALNETWORKS_MUL, {a, b, activation}, {c});
193    modelBroadcastMul2.setInputsAndOutputs({a, b}, {c});
194    ASSERT_TRUE(modelBroadcastMul2.isValid());
195
196    // Test the one node model.
197    Matrix3x4 actual;
198    memset(&actual, 0, sizeof(actual));
199    Request request(&modelBroadcastMul2);
200    ASSERT_EQ(request.setInput(0, matrix1, sizeof(Matrix3x4)), Result::NO_ERROR);
201    ASSERT_EQ(request.setInput(1, matrix2b, sizeof(Matrix4)), Result::NO_ERROR);
202    ASSERT_EQ(request.setOutput(0, actual, sizeof(Matrix3x4)), Result::NO_ERROR);
203    ASSERT_EQ(request.compute(), Result::NO_ERROR);
204    ASSERT_EQ(CompareMatrices(expected2c, actual), 0);
205}
206
207}  // end namespace
208