ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ShapeInference.cpp File Reference
#include "mir/Graph.h"
#include "mir/ops/AddOp.h"
#include "mir/ops/ReshapeOp.h"
#include "mir/ops/ResizeOp.h"
#include "mir/ops/SqueezeOp.h"
#include "mir/ops/ReduceMeanOp.h"
#include "mir/Shape.h"
#include <vector>
#include "gtest/gtest.h"

Go to the source code of this file.

Functions

 TEST (ShapeInferenceTest, BidirectionalBroadcast)
 
 TEST (ShapeInferenceTest, ReshapeAutoDimension)
 
 TEST (ShapeInferenceTest, ResizeWithShape)
 
 TEST (ShapeInferenceTest, ResizeWithScale)
 
 TEST (ShapeInferenceTest, ReduceChangeRank)
 
 TEST (ShapeInferenceTest, ReshapeAutoDimensionShrink)
 
 TEST (ShapeInferenceTest, ReshapeAutoDimensionExpand)
 
 TEST (ShapeInferenceTest, ReshapeAutoDimensionUnsqueeze)
 
 TEST (ShapeInferenceTest, SqueezeTestAllDims)
 
 TEST (ShapeInferenceTest, ElementwiseBC)
 
 TEST (ShapeInferenceTest, SqueezeTestSpecificDims)
 
 TEST (ShapeInferenceTest, SqueezeTestScalarResult)
 

Function Documentation

◆ TEST() [1/12]

TEST ( ShapeInferenceTest  ,
BidirectionalBroadcast   
)

Definition at line 31 of file ShapeInference.cpp.

32{
33 const Shape shape1{2, 1, 2};
34 const Shape shape2{3, 1};
35 const Shape reference{2, 3, 2};
36
37 const Shape result1 = broadcastShapes(shape1, shape2);
38 const Shape result2 = broadcastShapes(shape2, shape1);
39
40 ASSERT_EQ(result1, reference);
41 ASSERT_EQ(result2, reference);
42}
Shape broadcastShapes(const Shape &lhs_shape, const Shape &rhs_shape)
Definition Shape.cpp:43
Definition Shape.h:28

References mir::broadcastShapes().

◆ TEST() [2/12]

TEST ( ShapeInferenceTest  ,
ElementwiseBC   
)

Definition at line 159 of file ShapeInference.cpp.

160{
161 Graph g;
162
163 Shape input_shape{1, 10, 10, 1};
164 Shape input2_shape{1, 1, 10, 10};
165
166 mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
167 mir::TensorType input2_type{mir::DataType::FLOAT32, input2_shape};
168
169 auto input = g.create<ops::InputOp>(input_type);
170 auto input2 = g.create<ops::InputOp>(input2_type);
171
172 auto add = g.create<ops::AddOp>(input->getOutput(0), input2->getOutput(0));
173
174 ASSERT_EQ(add->getOutputShape(0), Shape({1, 10, 10, 10}));
175}
Output * getOutput(std::size_t index)
Definition Operation.h:149

References mir::Operation::getOutput().

◆ TEST() [3/12]

TEST ( ShapeInferenceTest  ,
ReduceChangeRank   
)

Definition at line 89 of file ShapeInference.cpp.

90{
91 Graph g;
92
93 Shape resultShape{10, 10};
94
95 mir::TensorType input_type{mir::DataType::FLOAT32, Shape{10, 2, 10, 9}};
96 auto input = g.create<ops::InputOp>(input_type);
97
98 auto n = g.create<ops::ReduceMeanOp>(input->getOutput(0), std::vector<int32_t>{1, 3}, false);
99
100 ASSERT_EQ(resultShape, n->getOutputShape(0));
101}
const Shape & getOutputShape(std::size_t index) const
Definition Operation.h:163

References mir::Operation::getOutputShape().

◆ TEST() [4/12]

TEST ( ShapeInferenceTest  ,
ReshapeAutoDimension   
)

Definition at line 44 of file ShapeInference.cpp.

45{
46 Graph g;
47
48 Shape input_shape{10, 2, 5};
49 Shape expected_shape{10, 1, 10};
50
51 mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
52 auto input = g.create<ops::InputOp>(input_type);
53 auto op = g.create<ops::ReshapeOp>(input->getOutput(0), Shape{10, 1, Shape::autoDim});
54
55 ASSERT_EQ(expected_shape, op->getOutputShape(0));
56}

References mir::Operation::getOutputShape().

◆ TEST() [5/12]

TEST ( ShapeInferenceTest  ,
ReshapeAutoDimensionExpand   
)

Definition at line 117 of file ShapeInference.cpp.

118{
119 Graph g;
120
121 Shape input_shape{10, 2, 10};
122 Shape result_shape_expand{5, 10, 2, 2};
123
124 mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
125 auto input = g.create<ops::InputOp>(input_type);
126 auto op = g.create<ops::ReshapeOp>(input->getOutput(0), Shape{5, Shape::autoDim, 2, 2});
127
128 ASSERT_EQ(result_shape_expand, op->getOutputShape(0));
129}

References mir::Operation::getOutputShape().

◆ TEST() [6/12]

TEST ( ShapeInferenceTest  ,
ReshapeAutoDimensionShrink   
)

Definition at line 103 of file ShapeInference.cpp.

104{
105 Graph g;
106
107 Shape input_shape{10, 2, 10};
108 Shape result_shape_shrink{10, 20};
109
110 mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
111 auto input = g.create<ops::InputOp>(input_type);
112 auto op = g.create<ops::ReshapeOp>(input->getOutput(0), Shape{10, Shape::autoDim});
113
114 ASSERT_EQ(result_shape_shrink, op->getOutputShape(0));
115}

References mir::Operation::getOutputShape().

◆ TEST() [7/12]

TEST ( ShapeInferenceTest  ,
ReshapeAutoDimensionUnsqueeze   
)

Definition at line 131 of file ShapeInference.cpp.

132{
133 Graph g;
134
135 Shape input_shape{10, 2, 10};
136 Shape result_shape_expand{1, 10, 2, 1, 10, 1};
137
138 mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
139 auto input = g.create<ops::InputOp>(input_type);
140 auto op = g.create<ops::ReshapeOp>(input->getOutput(0), Shape{1, Shape::autoDim, 2, 1, 10, 1});
141
142 ASSERT_EQ(result_shape_expand, op->getOutputShape(0));
143}

References mir::Operation::getOutputShape().

◆ TEST() [8/12]

TEST ( ShapeInferenceTest  ,
ResizeWithScale   
)

Definition at line 73 of file ShapeInference.cpp.

74{
75 Graph g;
76
77 Shape result_shape{1, 30, 10, 3};
78
79 mir::TensorType input_type{mir::DataType::FLOAT32, Shape{1, 5, 5, 3}};
80 auto input = g.create<ops::InputOp>(input_type);
81
82 auto op =
83 g.create<ops::ResizeOp>(input->getOutput(0), ops::ResizeOp::ResizeMethod::nearestNeighbor,
84 std::vector<float>{1, 6, 2, 1});
85
86 ASSERT_EQ(result_shape, op->getOutputShape(0));
87}
Resize operation scales are such that output = input * scale for each dimension and the number of dim...
Definition ResizeOp.h:35

References mir::Operation::getOutputShape().

◆ TEST() [9/12]

TEST ( ShapeInferenceTest  ,
ResizeWithShape   
)

Definition at line 58 of file ShapeInference.cpp.

59{
60 Graph g;
61
62 Shape result_shape{2, 10, 10, 3};
63
64 mir::TensorType input_type{mir::DataType::FLOAT32, Shape{1, 5, 5, 3}};
65 auto input = g.create<ops::InputOp>(input_type);
66
67 auto op = g.create<ops::ResizeOp>(input->getOutput(0),
68 ops::ResizeOp::ResizeMethod::nearestNeighbor, result_shape);
69
70 ASSERT_EQ(result_shape, op->getOutputShape(0));
71}

References mir::Operation::getOutputShape().

◆ TEST() [10/12]

TEST ( ShapeInferenceTest  ,
SqueezeTestAllDims   
)

Definition at line 145 of file ShapeInference.cpp.

146{
147 Graph g;
148
149 Shape input_shape{1, 2, 1, 4};
150 Shape expected_shape{2, 4};
151
152 mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
153 auto input = g.create<ops::InputOp>(input_type);
154 auto sq1 = g.create<ops::SqueezeOp>(input->getOutput(0), std::vector<int32_t>{});
155
156 ASSERT_EQ(sq1->getOutputShape(0), expected_shape);
157}

References mir::Operation::getOutputShape().

◆ TEST() [11/12]

TEST ( ShapeInferenceTest  ,
SqueezeTestScalarResult   
)

Definition at line 191 of file ShapeInference.cpp.

192{
193 Graph g;
194
195 Shape input_shape{1, 1, 1, 1};
196 Shape expected_shape{1};
197
198 mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
199 auto input = g.create<ops::InputOp>(input_type);
200 auto sq1 = g.create<ops::SqueezeOp>(input->getOutput(0), std::vector<int32_t>{});
201
202 ASSERT_EQ(sq1->getOutputShape(0), expected_shape);
203}

References mir::Operation::getOutputShape().

◆ TEST() [12/12]

TEST ( ShapeInferenceTest  ,
SqueezeTestSpecificDims   
)

Definition at line 177 of file ShapeInference.cpp.

178{
179 Graph g;
180
181 Shape input_shape{1, 2, 1, 4};
182 Shape expected_shape{1, 2, 4};
183
184 mir::TensorType input_type{mir::DataType::FLOAT32, input_shape};
185 auto input = g.create<ops::InputOp>(input_type);
186 auto sq1 = g.create<ops::SqueezeOp>(input->getOutput(0), std::vector<int32_t>{2});
187
188 ASSERT_EQ(sq1->getOutputShape(0), expected_shape);
189}

References mir::Operation::getOutputShape().