Skip to content

Commit

Permalink
[luci/service] Add tests for Conv2D and DepthwiseConv2D
Browse files Browse the repository at this point in the history
This commit adds tests related to shape inference for Conv2D and DepthwiseConv2D

ONE-DCO-1.0-Signed-off-by: Chansu Park <1265pcs@gmail.com>
  • Loading branch information
pcs1265 committed Sep 8, 2024
1 parent 52a2f8a commit c4f11fd
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
57 changes: 57 additions & 0 deletions compiler/luci/service/src/Nodes/CircleConv2D.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "luci/Service/CircleNodeClone.h"
#include "luci/Service/CircleShapeInference.h"

#include <gtest/gtest.h>

Expand Down Expand Up @@ -59,3 +60,59 @@ TEST(CloneNodeTest, clone_Conv2D_padding_NEG)
auto cloned = luci::clone_node(node_conv2d, gc.get());
ASSERT_EQ(nullptr, cloned);
}


TEST(ShapeRuleTest, Conv2D_sinf_dynamic)
{
luci::CircleInput ifm;
ifm.shape({3, 28, 28, 4});
ifm.dim(0).unset();
ifm.dim(1).unset();
ifm.shape_status(luci::ShapeStatus::VALID);
luci::CircleInput ker;
ker.shape({2, 3, 3, 4});
ker.shape_status(luci::ShapeStatus::VALID);
luci::CircleInput bias;
bias.shape({2});
bias.shape_status(luci::ShapeStatus::VALID);

luci::CircleConv2D node_conv2d;
node_conv2d.input(&ifm);
node_conv2d.filter(&ker);
node_conv2d.bias(&bias);
node_conv2d.padding(luci::Padding::VALID);

loco::TensorShape shape;
luci::sinf::Rule shape_inf_rule;
shape_inf_rule.infer(&node_conv2d, shape);


ASSERT_EQ(false, shape.dim(0).known());
ASSERT_EQ(false, shape.dim(1).known());
ASSERT_EQ(true, shape.dim(2).known());
ASSERT_EQ(true, shape.dim(3).known());
ASSERT_EQ(0, shape.dim(0).value());
ASSERT_EQ(0, shape.dim(1).value());
ASSERT_EQ(26, shape.dim(2).value());
ASSERT_EQ(2, shape.dim(3).value());
}

TEST(ShapeRuleTest, Conv2D_sinf_without_input_NEG)
{
luci::CircleInput ker;
ker.shape({2, 3, 3, 4});
ker.shape_status(luci::ShapeStatus::VALID);
luci::CircleInput bias;
bias.shape({2});
bias.shape_status(luci::ShapeStatus::VALID);

luci::CircleConv2D node_conv2d;
node_conv2d.input(nullptr);
node_conv2d.filter(&ker);
node_conv2d.bias(&bias);
node_conv2d.padding(luci::Padding::VALID);

loco::TensorShape shape;
luci::sinf::Rule shape_inf_rule;
ASSERT_ANY_THROW(shape_inf_rule.infer(&node_conv2d, shape));
}
60 changes: 60 additions & 0 deletions compiler/luci/service/src/Nodes/CircleDepthwiseConv2D.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "luci/Service/CircleNodeClone.h"
#include "luci/Service/CircleShapeInference.h"

#include <gtest/gtest.h>

Expand Down Expand Up @@ -59,3 +60,62 @@ TEST(CloneNodeTest, clone_DepthwiseConv2D_padding_NEG)
auto cloned = luci::clone_node(node_dwconv2d, gc.get());
ASSERT_EQ(nullptr, cloned);
}


TEST(ShapeRuleTest, DepthwiseConv2D_sinf_dynamic)
{
luci::CircleInput ifm;
ifm.shape({3, 28, 28, 4});
ifm.dim(0).unset();
ifm.shape_status(luci::ShapeStatus::VALID);
luci::CircleInput ker;
ker.shape({1, 3, 3, 4});
ker.shape_status(luci::ShapeStatus::VALID);
luci::CircleInput bias;
bias.shape({2});
bias.shape_status(luci::ShapeStatus::VALID);

luci::CircleDepthwiseConv2D node_dwconv2d;
node_dwconv2d.input(&ifm);
node_dwconv2d.filter(&ker);
node_dwconv2d.bias(&bias);
node_dwconv2d.padding(luci::Padding::VALID);
node_dwconv2d.depthMultiplier(1);

loco::TensorShape shape;
luci::sinf::Rule shape_inf_rule;
shape_inf_rule.infer(&node_dwconv2d, shape);


ASSERT_EQ(false, shape.dim(0).known());
ASSERT_EQ(true, shape.dim(1).known());
ASSERT_EQ(true, shape.dim(2).known());
ASSERT_EQ(true, shape.dim(3).known());
ASSERT_EQ(0, shape.dim(0).value());
ASSERT_EQ(26, shape.dim(1).value());
ASSERT_EQ(26, shape.dim(2).value());
ASSERT_EQ(4, shape.dim(3).value());
}

TEST(ShapeRuleTest, DwConv2D_ifm_not_ready_NEG)
{
luci::CircleInput ifm;
ifm.shape_status(luci::ShapeStatus::UNDEFINED);
luci::CircleInput ker;
ker.shape({2, 3, 3, 4});
ker.shape_status(luci::ShapeStatus::VALID);
luci::CircleInput bias;
bias.shape({2});
bias.shape_status(luci::ShapeStatus::VALID);

luci::CircleDepthwiseConv2D node_dwconv2d;
node_dwconv2d.input(&ifm);
node_dwconv2d.filter(&ker);
node_dwconv2d.bias(&bias);
node_dwconv2d.padding(luci::Padding::VALID);
node_dwconv2d.depthMultiplier(1);

loco::TensorShape shape;
luci::sinf::Rule shape_inf_rule;
ASSERT_FALSE(shape_inf_rule.infer(&node_dwconv2d, shape));
}

0 comments on commit c4f11fd

Please sign in to comment.