xref: /aosp_15_r20/external/armnn/src/armnnTfLiteParser/test/Activations.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ParserFlatbuffersFixture.hpp"
7 
8 #include <doctest/doctest.h>
9 
10 TEST_SUITE("TensorflowLiteParser_Activations")
11 {
12 struct ActivationFixture : ParserFlatbuffersFixture
13 {
14 
ActivationFixtureActivationFixture15     explicit ActivationFixture(std::string activationFunction, std::string dataType)
16     {
17         m_JsonString = R"(
18             {
19                 "version": 3,
20                 "operator_codes": [ { "builtin_code": )" + activationFunction + R"( } ],
21                 "subgraphs": [ {
22                     "tensors": [
23                         {
24                             "shape": [ 1, 7 ],
25                             "type": )" + dataType + R"(,
26                             "buffer": 0,
27                             "name": "inputTensor",
28                             "quantization": {
29                                 "min": [ 0.0 ],
30                                 "max": [ 255.0 ],
31                                 "scale": [ 1.0 ],
32                                 "zero_point": [ 0 ],
33                             }
34                         },
35                         {
36                             "shape": [ 1, 7 ],
37                             "type": )" + dataType + R"(,
38                             "buffer": 1,
39                             "name": "outputTensor",
40                             "quantization": {
41                                 "min": [ 0.0 ],
42                                 "max": [ 255.0 ],
43                                 "scale": [ 1.0 ],
44                                 "zero_point": [ 0 ],
45                             }
46                         }
47                     ],
48                     "inputs": [ 0 ],
49                     "outputs": [ 1 ],
50                     "operators": [
51                         {
52                           "opcode_index": 0,
53                           "inputs": [ 0 ],
54                           "outputs": [ 1 ],
55                           "custom_options_format": "FLEXBUFFERS"
56                         }
57                     ],
58                 } ],
59                 "buffers" : [ {}, {} ]
60             }
61         )";
62         SetupSingleInputSingleOutput("inputTensor", "outputTensor");
63     }
64 
65 };
66 
67 struct ReLuFixture : ActivationFixture
68 {
ReLuFixtureReLuFixture69     ReLuFixture() : ActivationFixture("RELU", "FLOAT32") {}
70 };
71 TEST_CASE_FIXTURE(ReLuFixture, "ParseReLu")
72 {
73     RunTest<2, armnn::DataType::Float32>(0, { -1.0f, -0.5f, 1.25f, -3.0f, 0.0f, 0.5f, -0.75f },
74                                          { 0.0f, 0.0f, 1.25f, 0.0f, 0.0f, 0.5f, 0.0f });
75 }
76 
77 struct ReLu6Fixture : ActivationFixture
78 {
ReLu6FixtureReLu6Fixture79     ReLu6Fixture() : ActivationFixture("RELU6", "FLOAT32") {}
80 };
81 TEST_CASE_FIXTURE(ReLu6Fixture, "ParseReLu6")
82 {
83     RunTest<2, armnn::DataType::Float32>(0, { -1.0f, -0.5f, 7.25f, -3.0f, 0.0f, 0.5f, -0.75f },
84                                          { 0.0f, 0.0f, 6.0f, 0.0f, 0.0f, 0.5f, 0.0f });
85 }
86 
87 struct SigmoidFixture : ActivationFixture
88 {
SigmoidFixtureSigmoidFixture89     SigmoidFixture() : ActivationFixture("LOGISTIC", "FLOAT32") {}
90 };
91 TEST_CASE_FIXTURE(SigmoidFixture, "ParseLogistic")
92 {
93     RunTest<2, armnn::DataType::Float32>(0, { -1.0f,     -0.5f,      4.0f,       -4.0f,  0.0f,      0.5f,     -0.75f },
94                                          {0.268941f, 0.377541f, 0.982013f,  0.0179862f,  0.5f, 0.622459f,  0.320821f });
95 }
96 
97 struct TanHFixture : ActivationFixture
98 {
TanHFixtureTanHFixture99     TanHFixture() : ActivationFixture("TANH", "FLOAT32") {}
100 };
101 
102 TEST_CASE_FIXTURE(TanHFixture, "ParseTanH")
103 {
104     RunTest<2, armnn::DataType::Float32>(0,
105         { -0.1f,       -0.2f,         -0.3f,       -0.4f,    0.1f,         0.2f,              0.3f },
106         { -0.09966799f, -0.19737528f, -0.29131261f, -0.379949f, 0.09966799f, 0.19737528f, 0.29131261f });
107 }
108 
109 struct EluFixture : ActivationFixture
110 {
EluFixtureEluFixture111     EluFixture() : ActivationFixture("ELU", "FLOAT32") {}
112 };
113 
114 TEST_CASE_FIXTURE(EluFixture, "ParseElu")
115 {
116     RunTest<2, armnn::DataType::Float32>(0,
117                                          { -2.0f,           -1.0f,           -0.0f, 0.0f, 1.0f, 2.0f, 3.0f },
118                                          { -0.86466471676f, -0.63212055882f, -0.0f, 0.0f, 1.0f, 2.0f, 3.0f });
119 }
120 
121 struct HardSwishFixture : ActivationFixture
122 {
HardSwishFixtureHardSwishFixture123     HardSwishFixture() : ActivationFixture("HARD_SWISH", "FLOAT32") {}
124 };
125 
126 TEST_CASE_FIXTURE(HardSwishFixture, "ParseHardSwish")
127 {
128     RunTest<2, armnn::DataType::Float32>(0,
129                                          { -4.0f, -3.0f,        -2.9f,  1.2f,        2.2f, 3.0f, 4.0f },
130                                          { -0.0f, -0.0f, -0.04833334f, 0.84f, 1.90666667f, 3.0f, 4.0f });
131 }
132 
133 }
134