aboutsummaryrefslogtreecommitdiff
path: root/delegate/test/ElementwiseUnaryTest.cpp
blob: 13d703913ae94e49009c98ff00b17a3dcd3da5e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ElementwiseUnaryTestHelper.hpp"

#include <armnn_delegate.hpp>

#include <flatbuffers/flatbuffers.h>
#include <tensorflow/lite/interpreter.h>
#include <tensorflow/lite/kernels/register.h>
#include <tensorflow/lite/model.h>
#include <tensorflow/lite/version.h>

#include <doctest/doctest.h>

namespace armnnDelegate
{

TEST_SUITE("ElementwiseUnary_Tests")
{

TEST_CASE ("Abs_Float32_Test")
{
    // Set input data
    std::vector<float> inputValues
    {
        -0.1f, -0.2f, -0.3f,
        0.1f,  0.2f,  0.3f
    };
    // Calculate output data
    std::vector<float> expectedOutputValues(inputValues.size());
    for (unsigned int i = 0; i < inputValues.size(); ++i)
    {
        expectedOutputValues[i] = std::abs(inputValues[i]);
    }

    ElementwiseUnaryFP32Test(tflite::BuiltinOperator_ABS, inputValues, expectedOutputValues);
}

TEST_CASE ("Ceil_Float32_Test")
{
    // Only works on CpuRef
    std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
    // Set input data
    std::vector<float> inputValues
    {
        0.0f, 1.1f, -16.1f,
        0.5f, -0.5f, -1.3f
    };
    // Set output data
    std::vector<float> expectedOutputValues
    {
        0.0f, 2.0f, -16.0f,
        1.0f, 0.0f, -1.0f
    };

    ElementwiseUnaryFP32Test(tflite::BuiltinOperator_CEIL, inputValues, expectedOutputValues, backends);
}

TEST_CASE ("Exp_Float32_Test")
{
    // Set input data
    std::vector<float> inputValues
    {
        5.0f, 4.0f,
        3.0f, 2.0f,
        1.0f, 1.1f
    };
    // Set output data
    std::vector<float> expectedOutputValues
    {
        148.413159102577f, 54.598150033144f,
        20.085536923188f,  7.389056098931f,
        2.718281828459f,  3.004166023946f
    };

    ElementwiseUnaryFP32Test(tflite::BuiltinOperator_EXP, inputValues, expectedOutputValues);
}

TEST_CASE ("Log_Float32_Test")
{
    // Set input data
    std::vector<float> inputValues
    {
        1.0f, 1.0f,  2.0f,
        3.0f,  4.0f, 2.71828f
    };
    // Set output data
    std::vector<float> expectedOutputValues
    {
        0.f,  0.f,  0.69314718056f,
        1.09861228867f, 1.38629436112f, 0.99999932734f
    };

    ElementwiseUnaryFP32Test(tflite::BuiltinOperator_LOG, inputValues, expectedOutputValues);
}

TEST_CASE ("Neg_Float32_Test")
{
    // Set input data
    std::vector<float> inputValues
    {
        1.f, 0.f, 3.f,
        25.f, 64.f, 100.f
    };
    // Set output data
    std::vector<float> expectedOutputValues
    {
        -1.f, 0.f, -3.f,
        -25.f, -64.f, -100.f
    };

    ElementwiseUnaryFP32Test(tflite::BuiltinOperator_NEG, inputValues, expectedOutputValues);
}

TEST_CASE ("Rsqrt_Float32_Test")
{
    // Set input data
    std::vector<float> inputValues
    {
        1.f, 4.f, 16.f,
        25.f, 64.f, 100.f
    };
    // Set output data
    std::vector<float> expectedOutputValues
    {
        1.f, 0.5f, 0.25f,
        0.2f, 0.125f, 0.1f
    };

    ElementwiseUnaryFP32Test(tflite::BuiltinOperator_RSQRT, inputValues, expectedOutputValues);
}

TEST_CASE ("Sqrt_Float32_Test")
{
    // Only works on CpuRef.
    std::vector<armnn::BackendId> backends = { armnn::Compute::CpuRef };
    // Set input data
    std::vector<float> inputValues
    {
        9.0f, 4.25f, 81.9f,
        0.1f,  0.9f,  169.0f
    };
    // Calculate output data
    std::vector<float> expectedOutputValues(inputValues.size());
    for (unsigned int i = 0; i < inputValues.size(); ++i)
    {
        expectedOutputValues[i] = std::sqrt(inputValues[i]);
    }

    ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SQRT, inputValues, expectedOutputValues, backends);
}

TEST_CASE ("Sin_Float32_Test")
{
    // Set input data
    std::vector<float> inputValues
    {
            0.0f, 1.0f, 16.0f,
            0.5f, 36.0f, -1.f
    };
    // Set output data
    std::vector<float> expectedOutputValues
    {
            0.0f, 0.8414709848f, -0.28790331666f,
            0.4794255386f, -0.99177885344f, -0.8414709848f
    };

    ElementwiseUnaryFP32Test(tflite::BuiltinOperator_SIN, inputValues, expectedOutputValues);
}
} // TEST_SUITE("ElementwiseUnary_Tests")

} // namespace armnnDelegate