Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[luci/service] Support SOFTMAX dynamic shape inference #13784

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Algorithm final : public luci::CircleNodeVisitor<loco::TensorShape>
// loco::TensorShape visit(const luci::CircleShape *node) final;
// loco::TensorShape visit(const luci::CircleSin *node) final;
// loco::TensorShape visit(const luci::CircleSlice *node) final;
// loco::TensorShape visit(const luci::CircleSoftmax *node) final;
loco::TensorShape visit(const luci::CircleSoftmax *node) final;
// loco::TensorShape visit(const luci::CircleSpaceToBatchND *node) final;
// loco::TensorShape visit(const luci::CircleSpaceToDepth *node) final;
// loco::TensorShape visit(const luci::CircleSparseToDense *node) final;
Expand Down
2 changes: 0 additions & 2 deletions compiler/luci/service/src/CircleShapeInferenceRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2360,8 +2360,6 @@ class ShapeInferenceAlgorithm final : public luci::CircleNodeVisitor<loco::NodeS

loco::NodeShape visit(const luci::CircleSlice *node) final { return infer_slice(node); }

loco::NodeShape visit(const luci::CircleSoftmax *node) final { return use_logits(node); }

loco::NodeShape visit(const luci::CircleSpaceToBatchND *node) final
{
return infer_space_to_batch_nd(node);
Expand Down
11 changes: 11 additions & 0 deletions compiler/luci/service/src/Nodes/CircleSoftmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
* limitations under the License.
*/

#include <luci/Service/CircleShapeInference.h>

#include "CircleCloneNode.h"

#include "CircleShapeInferenceHelper.h"

Copy link
Contributor

@seanshpark seanshpark Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed adding includes in #13780 and found that we need some updates

Suggested change
#include <luci/Service/CircleShapeInference.h>
#include "CircleCloneNode.h"
#include "CircleShapeInferenceHelper.h"
#include "luci/Service/CircleShapeInference.h"
#include "CircleCloneNode.h"
#include "CircleShapeInferenceHelper.h"

I couldn't find how the relation of this source and headers was... and now I see.

order of headers;

  • direct header for this source file with "...", should be in same module
  • in same folder(module) with "..."
  • in another module with <...>

namespace luci
{

Expand All @@ -27,4 +31,11 @@ luci::CircleNode *CloneNodeLet<CN::STUV>::visit(const luci::CircleSoftmax *node)
return cloned;
}

loco::TensorShape sinf::Algorithm::visit(const luci::CircleSoftmax *node)
{
const auto logits = loco::must_cast<luci::CircleNode *>(node->logits());

return sinf::circle_shape(logits);
}

} // namespace luci
78 changes: 78 additions & 0 deletions compiler/luci/service/src/Nodes/CircleSoftmax.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "luci/Service/CircleNodeClone.h"

#include <luci/Service/CircleShapeInference.h>

#include <gtest/gtest.h>

TEST(CloneNodeTest, clone_Softmax)
Expand All @@ -33,3 +35,79 @@ TEST(CloneNodeTest, clone_Softmax)
ASSERT_NE(nullptr, cloned_sm);
ASSERT_EQ(node_sm->beta(), cloned_sm->beta());
}

TEST(ShapeRuleTest, softmax_static_shape)
{
luci::CircleInput input;
luci::CircleSoftmax softmax;

input.shape({1, 4, 3, 8});
input.shape_status(luci::ShapeStatus::VALID);

softmax.logits(&input);

loco::TensorShape shape;
luci::sinf::Rule shape_inf_rule;

ASSERT_TRUE(shape_inf_rule.infer(&softmax, shape));
ASSERT_EQ(4, shape.rank());
ASSERT_TRUE(shape.dim(0).known());
ASSERT_TRUE(shape.dim(1).known());
ASSERT_TRUE(shape.dim(2).known());
ASSERT_TRUE(shape.dim(3).known());
ASSERT_EQ(1, shape.dim(0).value());
ASSERT_EQ(4, shape.dim(1).value());
ASSERT_EQ(3, shape.dim(2).value());
ASSERT_EQ(8, shape.dim(3).value());
}

TEST(ShapeRuleTest, softmax_dynamic_shape)
{
luci::CircleInput input;
luci::CircleSoftmax softmax;

input.shape({1, 4, 3, 8});
input.shape_status(luci::ShapeStatus::VALID);
input.dim(1).unset();

softmax.logits(&input);

loco::TensorShape shape;
luci::sinf::Rule shape_inf_rule;

ASSERT_TRUE(shape_inf_rule.infer(&softmax, shape));
ASSERT_EQ(4, shape.rank());
ASSERT_TRUE(shape.dim(0).known());
ASSERT_FALSE(shape.dim(1).known());
ASSERT_TRUE(shape.dim(2).known());
ASSERT_TRUE(shape.dim(3).known());
ASSERT_EQ(1, shape.dim(0).value());
ASSERT_EQ(0, shape.dim(1).value());
ASSERT_EQ(3, shape.dim(2).value());
ASSERT_EQ(8, shape.dim(3).value());
}

TEST(ShapeRuleTest, softmax_wrong_input_NEG)
{
luci::CircleSoftmax softmax;

softmax.logits(nullptr);

loco::TensorShape shape;
luci::sinf::Rule shape_inf_rule;

ASSERT_ANY_THROW(shape_inf_rule.infer(&softmax, shape));
}

TEST(ShapeRuleTest, softmax_wrong_input_2_NEG)
{
luci::CircleInput *input = nullptr;
luci::CircleSoftmax softmax;

softmax.logits(input);

loco::TensorShape shape;
luci::sinf::Rule shape_inf_rule;

ASSERT_ANY_THROW(shape_inf_rule.infer(&softmax, shape));
}