aboutsummaryrefslogtreecommitdiff
path: root/src/armnnTfParser/test/Mean.cpp
blob: d73682961f04ef3fcb3ba7f417837987db2beb8a (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
176
177
178
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "armnnTfParser/ITfParser.hpp"

#include <ParserPrototxtFixture.hpp>
#include <PrototxtConversions.hpp>

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(TensorflowParser)

struct MeanFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
{
    explicit MeanFixture(const armnn::TensorShape& inputShape, const armnn::TensorShape& outputShape,
                         const std::vector<unsigned int>& axis, bool keepDims)
    {
        std::string protobufAxisString;
        std::vector<unsigned int> protobufAxis(axis);

        // If no axis range is specified, the reduction is applied to
        // all dimensions of the input tensor
        if (protobufAxis.size() == 0)
        {
            for (unsigned int i = 0; i < inputShape.GetNumDimensions(); ++i)
            {
                protobufAxis.push_back(i);
            }
        }

        for (unsigned int i = 0; i < protobufAxis.size(); ++i)
        {
            protobufAxisString.append(armnnUtils::ConvertInt32ToOctalString(static_cast<int>(protobufAxis[i])));
        }

        m_Prototext = R"(node {
              name: "input"
              op: "Placeholder"
              attr {
              key: "dtype"
                value {
                  type: DT_FLOAT
                }
              }
              attr {
                key: "shape"
                value {
                  shape {
                  }
                }
              }
            }
            node {
              name: "Const"
              op: "Const"
              attr {
                key: "dtype"
                value {
                  type: DT_INT32
                }
              }
              attr {
                key: "value"
                value { )";

        if (axis.size() == 1)
        {
            m_Prototext.append(R"(      tensor {
                    dtype: DT_INT32
                    tensor_shape {
                    }
                    int_val: )").append(std::to_string(protobufAxis[0])).append(R"(
                  } )");
        }
        else
        {
            m_Prototext.append(R"(      tensor {
                    dtype: DT_INT32
                    tensor_shape {
                      dim {
                        size: 2
                      }
                    }
                    tensor_content: ")").append(protobufAxisString).append(R"("
                  } )");
        }

        m_Prototext.append(R"(    }
              }
            }
            node {
              name: "output"
              op: "Mean"
              input: "input"
              input: "Const"
              attr {
                key: "T"
                value {
                  type: DT_FLOAT
                }
              }
              attr {
                key: "Tidx"
                  value {
                    type: DT_INT32
                }
             }
             attr {
               key: "keep_dims"
                 value {
                   b: )").append(keepDims ? "true" : "false").append(R"(
               }
             }
            })");

        SetupSingleInputSingleOutput(inputShape, outputShape, "input", "output");
    }
};

struct MeanNoAxisNoKeepDimsFixture: MeanFixture
{
    MeanNoAxisNoKeepDimsFixture() : MeanFixture({ 2, 3 }, { 1 }, {}, false) {}
};

struct MeanWithAxis0NoKeepDimsFixture: MeanFixture
{
    MeanWithAxis0NoKeepDimsFixture() : MeanFixture({ 2, 3 }, { 3 }, { 0 }, false) {}
};

struct MeanWithAxis1NoKeepDimsFixture: MeanFixture
{
    MeanWithAxis1NoKeepDimsFixture() : MeanFixture({ 2, 3 }, { 2 }, { 1 }, false) {}
};

struct MeanWithAxis0KeepDimsFixture: MeanFixture
{
    MeanWithAxis0KeepDimsFixture() : MeanFixture({ 2, 3 }, { 1, 3 }, { 0 }, true) {}
};

struct MeanWithAxis1KeepDimsFixture: MeanFixture
{
    MeanWithAxis1KeepDimsFixture() : MeanFixture({ 2, 3 }, { 2, 1 }, { 1 }, true) {}
};


BOOST_FIXTURE_TEST_CASE(MeanNoAxisNoKeepDims, MeanNoAxisNoKeepDimsFixture)
{
    RunTest<1>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } },
               { { "output", { 1.5f } } });
}

BOOST_FIXTURE_TEST_CASE(MeanWithAxis0NoKeepDims, MeanWithAxis0NoKeepDimsFixture)
{
    RunTest<1>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } },
               { { "output", { 1.5f, 1.5f, 1.5f } } });
}

BOOST_FIXTURE_TEST_CASE(MeanWithAxis1NoKeepDims, MeanWithAxis1NoKeepDimsFixture)
{
    RunTest<1>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } },
               { { "output", { 1.f, 2.f } } });
}

BOOST_FIXTURE_TEST_CASE(MeanWithAxis0KeepDims, MeanWithAxis0KeepDimsFixture)
{
    RunTest<2>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } },
               { { "output", { 1.5f, 1.5f, 1.5f } } });
}

BOOST_FIXTURE_TEST_CASE(MeanWithAxis1KeepDims, MeanWithAxis1KeepDimsFixture)
{
    RunTest<2>({ { "input", { 1.0f, 1.0f, 1.0f, 2.0f, 2.0f, 2.0f } } },
               { { "output", { 1.f, 2.f } } });
}

BOOST_AUTO_TEST_SUITE_END()