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