blob: 0824a27f875f5d317798a75d011539354e859bcf [file] [log] [blame]
Sadikb94967b2018-09-19 15:30:00 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Sadikb94967b2018-09-19 15:30:00 +01006#include "ParserFlatbuffersFixture.hpp"
7#include "../TfLiteParser.hpp"
8
9#include <string>
10#include <iostream>
11
Sadik Armagan1625efc2021-06-10 18:24:34 +010012TEST_SUITE("TensorflowLiteParser_Reshape")
13{
Sadikb94967b2018-09-19 15:30:00 +010014struct ReshapeFixture : public ParserFlatbuffersFixture
15{
16 explicit ReshapeFixture(const std::string& inputShape,
17 const std::string& outputShape,
18 const std::string& newShape)
19 {
20 m_JsonString = R"(
21 {
22 "version": 3,
23 "operator_codes": [ { "builtin_code": "RESHAPE" } ],
24 "subgraphs": [ {
25 "tensors": [
26 {)";
27 m_JsonString += R"(
28 "shape" : )" + inputShape + ",";
29 m_JsonString += R"(
30 "type": "UINT8",
31 "buffer": 0,
32 "name": "inputTensor",
33 "quantization": {
34 "min": [ 0.0 ],
35 "max": [ 255.0 ],
36 "scale": [ 1.0 ],
37 "zero_point": [ 0 ],
38 }
39 },
40 {)";
41 m_JsonString += R"(
42 "shape" : )" + outputShape;
43 m_JsonString += R"(,
44 "type": "UINT8",
45 "buffer": 1,
46 "name": "outputTensor",
47 "quantization": {
48 "min": [ 0.0 ],
49 "max": [ 255.0 ],
50 "scale": [ 1.0 ],
51 "zero_point": [ 0 ],
52 }
53 }
54 ],
55 "inputs": [ 0 ],
56 "outputs": [ 1 ],
57 "operators": [
58 {
59 "opcode_index": 0,
60 "inputs": [ 0 ],
61 "outputs": [ 1 ],
62 "builtin_options_type": "ReshapeOptions",
63 "builtin_options": {)";
64 if (!newShape.empty())
65 {
66 m_JsonString += R"("new_shape" : )" + newShape;
67 }
68 m_JsonString += R"(},
69 "custom_options_format": "FLEXBUFFERS"
70 }
71 ],
72 } ],
73 "buffers" : [ {}, {} ]
74 }
75 )";
76
77 }
78};
79
80struct ReshapeFixtureWithReshapeDims : ReshapeFixture
81{
82 ReshapeFixtureWithReshapeDims() : ReshapeFixture("[ 1, 9 ]", "[ 3, 3 ]", "[ 3, 3 ]") {}
83};
84
Sadik Armagan1625efc2021-06-10 18:24:34 +010085TEST_CASE_FIXTURE(ReshapeFixtureWithReshapeDims, "ParseReshapeWithReshapeDims")
Sadikb94967b2018-09-19 15:30:00 +010086{
87 SetupSingleInputSingleOutput("inputTensor", "outputTensor");
Derek Lambertif90c56d2020-01-10 17:14:08 +000088 RunTest<2, armnn::DataType::QAsymmU8>(0,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +000089 { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
90 { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Sadik Armagan1625efc2021-06-10 18:24:34 +010091 CHECK((m_Parser->GetNetworkOutputBindingInfo(0, "outputTensor").second.GetShape()
Sadikb94967b2018-09-19 15:30:00 +010092 == armnn::TensorShape({3,3})));
93}
94
95struct ReshapeFixtureWithReshapeDimsFlatten : ReshapeFixture
96{
Narumol Prangnawarat672de572019-04-23 15:28:06 +010097 ReshapeFixtureWithReshapeDimsFlatten() : ReshapeFixture("[ 3, 3 ]", "[ 9 ]", "[ -1 ]") {}
Sadikb94967b2018-09-19 15:30:00 +010098};
99
Sadik Armagan1625efc2021-06-10 18:24:34 +0100100TEST_CASE_FIXTURE(ReshapeFixtureWithReshapeDimsFlatten, "ParseReshapeWithReshapeDimsFlatten")
Sadikb94967b2018-09-19 15:30:00 +0100101{
102 SetupSingleInputSingleOutput("inputTensor", "outputTensor");
Derek Lambertif90c56d2020-01-10 17:14:08 +0000103 RunTest<1, armnn::DataType::QAsymmU8>(0,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000104 { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
105 { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
Sadik Armagan1625efc2021-06-10 18:24:34 +0100106 CHECK((m_Parser->GetNetworkOutputBindingInfo(0, "outputTensor").second.GetShape()
Narumol Prangnawarat672de572019-04-23 15:28:06 +0100107 == armnn::TensorShape({9})));
Sadikb94967b2018-09-19 15:30:00 +0100108}
109
110struct ReshapeFixtureWithReshapeDimsFlattenTwoDims : ReshapeFixture
111{
112 ReshapeFixtureWithReshapeDimsFlattenTwoDims() : ReshapeFixture("[ 3, 2, 3 ]", "[ 2, 9 ]", "[ 2, -1 ]") {}
113};
114
Sadik Armagan1625efc2021-06-10 18:24:34 +0100115TEST_CASE_FIXTURE(ReshapeFixtureWithReshapeDimsFlattenTwoDims, "ParseReshapeWithReshapeDimsFlattenTwoDims")
Sadikb94967b2018-09-19 15:30:00 +0100116{
117 SetupSingleInputSingleOutput("inputTensor", "outputTensor");
Derek Lambertif90c56d2020-01-10 17:14:08 +0000118 RunTest<2, armnn::DataType::QAsymmU8>(0,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000119 { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 },
120 { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 });
Sadik Armagan1625efc2021-06-10 18:24:34 +0100121 CHECK((m_Parser->GetNetworkOutputBindingInfo(0, "outputTensor").second.GetShape()
Sadikb94967b2018-09-19 15:30:00 +0100122 == armnn::TensorShape({2,9})));
123}
124
125struct ReshapeFixtureWithReshapeDimsFlattenOneDim : ReshapeFixture
126{
127 ReshapeFixtureWithReshapeDimsFlattenOneDim() : ReshapeFixture("[ 2, 9 ]", "[ 2, 3, 3 ]", "[ 2, -1, 3 ]") {}
128};
129
Sadik Armagan1625efc2021-06-10 18:24:34 +0100130TEST_CASE_FIXTURE(ReshapeFixtureWithReshapeDimsFlattenOneDim, "ParseReshapeWithReshapeDimsFlattenOneDim")
Sadikb94967b2018-09-19 15:30:00 +0100131{
132 SetupSingleInputSingleOutput("inputTensor", "outputTensor");
Derek Lambertif90c56d2020-01-10 17:14:08 +0000133 RunTest<3, armnn::DataType::QAsymmU8>(0,
Nattapat Chaimanowong649dd952019-01-22 16:10:44 +0000134 { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 },
135 { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 });
Sadik Armagan1625efc2021-06-10 18:24:34 +0100136 CHECK((m_Parser->GetNetworkOutputBindingInfo(0, "outputTensor").second.GetShape()
Sadikb94967b2018-09-19 15:30:00 +0100137 == armnn::TensorShape({2,3,3})));
138}
139
Sadik Armagand109a4d2020-07-28 10:42:13 +0100140struct DynamicReshapeFixtureWithReshapeDimsFlattenOneDim : ReshapeFixture
141{
142 DynamicReshapeFixtureWithReshapeDimsFlattenOneDim() : ReshapeFixture("[ 2, 9 ]",
143 "[ ]",
144 "[ 2, -1, 3 ]") {}
145};
146
Sadik Armagan1625efc2021-06-10 18:24:34 +0100147TEST_CASE_FIXTURE(DynamicReshapeFixtureWithReshapeDimsFlattenOneDim, "DynParseReshapeWithReshapeDimsFlattenOneDim")
Sadik Armagand109a4d2020-07-28 10:42:13 +0100148{
149 SetupSingleInputSingleOutput("inputTensor", "outputTensor");
150 RunTest<3,
151 armnn::DataType::QAsymmU8,
152 armnn::DataType::QAsymmU8>(0,
153 { { "inputTensor", { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 } } },
154 { { "outputTensor", { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6 } } },
155 true);
156}
157
Sadik Armagan1625efc2021-06-10 18:24:34 +0100158}