aboutsummaryrefslogtreecommitdiff
path: root/src/armnnTfParser/test/Gather.cpp
blob: f40dc57556a75132b3cbd08f8bd6de5dd9ac8368 (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
//
// 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)

// helper for setting the dimensions in prototxt
void dimsHelper(const std::vector<int>& dims, std::string& text){
    for(u_int i=0; i<dims.size(); ++i){
        text.append(R"(dim {
      size: )");
        text.append(std::to_string(dims[i]));
        text.append(R"(
    })");
    }
}

// helper for converting from integer to octal representation
void octalHelper(const std::vector<int>& indicesContent, std::string& text){
    for (unsigned int i = 0; i < indicesContent.size(); ++i)
    {
        text.append(armnnUtils::ConvertInt32ToOctalString(static_cast<int>(indicesContent[i])));
    }
}

struct GatherFixture : public armnnUtils::ParserPrototxtFixture<armnnTfParser::ITfParser>
{
    GatherFixture(const armnn::TensorShape& inputShape0,
                  const armnn::TensorShape& inputShape1,
                  const std::vector<int>& input1Content,
                  const std::vector<int>& input0Dims,
                  const std::vector<int>& input1Dims)
    {
        m_Prototext = R"(
node {
  name: "input0"
  op: "Placeholder"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "shape"
    value {
      shape {
)";
        dimsHelper(input0Dims, m_Prototext);
        m_Prototext.append(R"(
      }
    }
  }
}
node {
  name: "input1"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
     tensor {
      dtype: DT_INT32
        tensor_shape {
)");
        dimsHelper(input1Dims, m_Prototext);
        m_Prototext.append(R"(
        }
        tensor_content: ")");
        octalHelper(input1Content, m_Prototext);
        m_Prototext.append(R"("
      }
    }
  }
}
node {
  name: "output"
  op: "Gather"
  input: "input0"
  input: "input1"
  attr {
    key: "Tindices"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "Tparams"
    value {
      type: DT_FLOAT
    }
  }
}
        )");
        Setup({ { "input0", inputShape0 },
                { "input1", inputShape1 } },
              { "output" });

    }
};


struct GatherFixture1DParams1DIndices : public GatherFixture
{
    GatherFixture1DParams1DIndices() : GatherFixture(
            { 4, 1, 1, 1 },
            { 4, 0, 0, 0 },
            { 0, 2, 1, 3 },
            { 4 },
            { 4 }) {}
};

struct GatherFixture1DParamsMultiDimIndices : public GatherFixture
{
    GatherFixture1DParamsMultiDimIndices() : GatherFixture(
            { 4, 1, 1 },
            { 2, 2, 1, 1 },
            { 0, 1, 1, 3 },
            { 4 },
            { 2, 2 }) {}
};

struct GatherFixtureMultiDimParamMultiDimIndices : public GatherFixture
{
    GatherFixtureMultiDimParamMultiDimIndices() : GatherFixture(
            { 5, 2, 1 },
            { 2, 1, 4 },
            { 1, 3, 0, 2 },
            { 5, 2 },
            { 2, 2 }) {}
};

BOOST_FIXTURE_TEST_CASE(ParseGather1DParams1DIndices, GatherFixture1DParams1DIndices)
{
    RunTest<4>({ { "input0", { 1, 2, 3, 4 } } },

               { { "output", { 1, 3, 2, 4 } } });
}

BOOST_FIXTURE_TEST_CASE(ParseGather1DParamsMultiDimIndices, GatherFixture1DParamsMultiDimIndices)
{
    RunTest<4>({ { "input0", { 1, 2, 3, 4 } } },

               { { "output", { 1, 2, 2, 4 } } });
}

BOOST_FIXTURE_TEST_CASE(ParseGatherMultiDimParamMultiDimIndices, GatherFixtureMultiDimParamMultiDimIndices)
{
    RunTest<4>({ { "input0", { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } } },

               { { "output", { 3, 4, 7, 8, 1, 2, 5, 6} } });
}

BOOST_AUTO_TEST_SUITE_END()