aboutsummaryrefslogtreecommitdiff
path: root/src/armnnOnnxParser/test/Shape.cpp
blob: b033b2d8bf9f6204d3fbdc670a09d5f18653f482 (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
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "armnnOnnxParser/IOnnxParser.hpp"
#include "ParserPrototxtFixture.hpp"

TEST_SUITE("OnnxParser_Shape")
{
struct ShapeMainFixture : public armnnUtils::ParserPrototxtFixture<armnnOnnxParser::IOnnxParser>
{
    ShapeMainFixture(const std::string& inputType,
                     const std::string& outputType,
                     const std::string& outputDim,
                     const std::vector<int>& inputShape)
    {
        m_Prototext = R"(
                    ir_version: 8
                    producer_name: "onnx-example"
                    graph {
                      node {
                        input: "Input"
                        output: "Output"
                        op_type: "Shape"
                      }
                      name: "shape-model"
                      input {
                        name: "Input"
                        type {
                          tensor_type {
                            elem_type: )" + inputType + R"(
                            shape {
                              )" + ConstructShapeString(inputShape) + R"(
                            }
                          }
                        }
                      }
                      output {
                        name: "Output"
                        type {
                          tensor_type {
                            elem_type: )" + outputType + R"(
                            shape {
                              dim {
                                dim_value: )" + outputDim + R"(
                              }
                            }
                          }
                        }
                      }
                    }
                    opset_import {
                      version: 10
                    })";
    }
    std::string ConstructShapeString(const std::vector<int>& shape)
    {
        std::string shapeStr;
        for (int i : shape)
        {
            shapeStr = fmt::format("{} dim {{ dim_value: {} }}", shapeStr, i);
        }
        return shapeStr;
    }
};

struct ShapeValidFloatFixture : ShapeMainFixture
{
    ShapeValidFloatFixture() : ShapeMainFixture("1", "7", "4", { 1, 3, 1, 5 }) {
        Setup();
    }
};

struct ShapeValidIntFixture : ShapeMainFixture
{
    ShapeValidIntFixture() : ShapeMainFixture("7", "7", "4", { 1, 3, 1, 5 }) {
        Setup();
    }
};

struct Shape3DFixture : ShapeMainFixture
{
    Shape3DFixture() : ShapeMainFixture("1", "7", "3", { 3, 2, 3 }) {
        Setup();
    }
};

struct Shape2DFixture : ShapeMainFixture
{
    Shape2DFixture() : ShapeMainFixture("1", "7", "2", { 2, 3 }) {
        Setup();
    }
};

struct Shape1DFixture : ShapeMainFixture
{
    Shape1DFixture() : ShapeMainFixture("1", "7", "1", { 5 }) {
        Setup();
    }
};

struct ShapeInvalidFixture : ShapeMainFixture
{
    ShapeInvalidFixture() : ShapeMainFixture("1", "1", "4", { 1, 3, 1, 5 }) {}
};

TEST_CASE_FIXTURE(ShapeValidFloatFixture, "FloatValidShapeTest")
{
    RunTest<2, int>({{"Input", { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f,
                                 4.0f, 3.0f, 2.0f, 1.0f, 0.0f,
                                 0.0f, 1.0f, 2.0f, 3.0f, 4.0f }}}, {{"Output", { 1, 3, 1, 5 }}});
}

TEST_CASE_FIXTURE(ShapeValidIntFixture, "IntValidShapeTest")
{
    RunTest<2, int>({{"Input", { 0, 1, 2, 3, 4,
                                 4, 3, 2, 1, 0,
                                 0, 1, 2, 3, 4 }}}, {{"Output", { 1, 3, 1, 5 }}});
}

TEST_CASE_FIXTURE(Shape3DFixture, "Shape3DTest")
{
    RunTest<2, int>({{"Input", { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
                                 5.0f, 4.0f, 3.0f, 2.0f, 1.0f, 0.0f,
                                 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }}}, {{"Output", { 3, 2, 3 }}});
}

TEST_CASE_FIXTURE(Shape2DFixture, "Shape2DTest")
{
    RunTest<2, int>({{"Input", { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }}}, {{"Output", { 2, 3 }}});
}

TEST_CASE_FIXTURE(Shape1DFixture, "Shape1DTest")
{
    RunTest<2, int>({{"Input", { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }}}, {{"Output", { 5 }}});
}

TEST_CASE_FIXTURE(ShapeInvalidFixture, "IncorrectOutputDataShapeTest")
{
    CHECK_THROWS_AS(Setup(), armnn::ParseException);
}

}