1 /*
2  * Copyright (C) 2019 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 #define LOG_TAG "TestControlFlow"
18 
19 #include <android-base/logging.h>
20 #include <gtest/gtest.h>
21 
22 #include "ControlFlow.h"
23 #include "TestNeuralNetworksWrapper.h"
24 
25 namespace android {
26 namespace nn {
27 namespace {
28 
29 using namespace test_wrapper;
30 
31 constexpr uint64_t kMillisecondsInNanosecond = 1'000'000;
32 constexpr int32_t kNoActivation = ANEURALNETWORKS_FUSED_NONE;
33 
34 class ControlFlowTest : public ::testing::Test {};
35 
TEST_F(ControlFlowTest,InfiniteLoop)36 TEST_F(ControlFlowTest, InfiniteLoop) {
37     // Expected result: execution aborted after the specified timeout.
38     // Model: given n <= 1.0, never returns.
39     //
40     // i = 1.0
41     // while i >= n:
42     //     i = i + 1.0
43 
44     OperandType boolType(Type::TENSOR_BOOL8, {1});
45     OperandType activationType(Type::INT32, {});
46     OperandType counterType(Type::TENSOR_FLOAT32, {1});
47 
48     Model conditionModel;
49     {
50         uint32_t i = conditionModel.addOperand(&counterType);
51         uint32_t n = conditionModel.addOperand(&counterType);
52         uint32_t out = conditionModel.addOperand(&boolType);
53         conditionModel.addOperation(ANEURALNETWORKS_GREATER_EQUAL, {i, n}, {out});
54         conditionModel.identifyInputsAndOutputs({i, n}, {out});
55         ASSERT_EQ(conditionModel.finish(), Result::NO_ERROR);
56         ASSERT_TRUE(conditionModel.isValid());
57     }
58 
59     Model bodyModel;
60     {
61         uint32_t i = bodyModel.addOperand(&counterType);
62         uint32_t n = bodyModel.addOperand(&counterType);
63         uint32_t one = bodyModel.addConstantOperand(&counterType, 1.0f);
64         uint32_t noActivation = bodyModel.addConstantOperand(&activationType, kNoActivation);
65         uint32_t iOut = bodyModel.addOperand(&counterType);
66         bodyModel.addOperation(ANEURALNETWORKS_ADD, {i, one, noActivation}, {iOut});
67         bodyModel.identifyInputsAndOutputs({i, n}, {iOut});
68         ASSERT_EQ(bodyModel.finish(), Result::NO_ERROR);
69         ASSERT_TRUE(bodyModel.isValid());
70     }
71 
72     Model model;
73     {
74         uint32_t iInit = model.addConstantOperand(&counterType, 1.0f);
75         uint32_t n = model.addOperand(&counterType);
76         uint32_t conditionOperand = model.addModelOperand(&conditionModel);
77         uint32_t bodyOperand = model.addModelOperand(&bodyModel);
78         uint32_t iOut = model.addOperand(&counterType);
79         model.addOperation(ANEURALNETWORKS_WHILE, {conditionOperand, bodyOperand, iInit, n},
80                            {iOut});
81         model.identifyInputsAndOutputs({n}, {iOut});
82         ASSERT_EQ(model.finish(), Result::NO_ERROR);
83         ASSERT_TRUE(model.isValid());
84     }
85 
86     Compilation compilation(&model);
87     ASSERT_EQ(compilation.finish(), Result::NO_ERROR);
88 
89     float input = 0;
90     float output;
91     Execution execution(&compilation);
92     ASSERT_EQ(execution.setInput(0, &input), Result::NO_ERROR);
93     ASSERT_EQ(execution.setOutput(0, &output), Result::NO_ERROR);
94     ASSERT_EQ(execution.setLoopTimeout(1 * kMillisecondsInNanosecond), Result::NO_ERROR);
95     Result result = execution.compute();
96     ASSERT_TRUE(result == Result::MISSED_DEADLINE_TRANSIENT ||
97                 result == Result::MISSED_DEADLINE_PERSISTENT)
98             << "result = " << static_cast<int>(result);
99 }
100 
TEST_F(ControlFlowTest,GetLoopTimeouts)101 TEST_F(ControlFlowTest, GetLoopTimeouts) {
102     uint64_t defaultTimeout = ANeuralNetworks_getDefaultLoopTimeout();
103     uint64_t maximumTimeout = ANeuralNetworks_getMaximumLoopTimeout();
104     ASSERT_EQ(defaultTimeout, operation_while::kTimeoutNsDefault);
105     ASSERT_EQ(maximumTimeout, operation_while::kTimeoutNsMaximum);
106 }
107 
108 }  // end namespace
109 }  // namespace nn
110 }  // namespace android
111