-
Notifications
You must be signed in to change notification settings - Fork 157
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] Migrate reshape shape inference rule to sinf::Algorithm #13989
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -14,8 +14,36 @@ | |||||||
* limitations under the License. | ||||||||
*/ | ||||||||
|
||||||||
#include "luci/Service/CircleShapeInference.h" | ||||||||
#include "Check.h" | ||||||||
|
||||||||
#include "CircleShapeInferenceHelper.h" | ||||||||
#include "CircleCloneNode.h" | ||||||||
|
||||||||
#include <luci/Log.h> | ||||||||
|
||||||||
namespace | ||||||||
{ | ||||||||
|
||||||||
std::ostream &operator<<(std::ostream &os, const loco::TensorShape &tensor_shape) | ||||||||
{ | ||||||||
os << "["; | ||||||||
for (uint32_t r = 0; r < tensor_shape.rank(); ++r) | ||||||||
{ | ||||||||
if (r) | ||||||||
os << ","; | ||||||||
|
||||||||
if (tensor_shape.dim(r).known()) | ||||||||
os << tensor_shape.dim(r).value(); | ||||||||
else | ||||||||
os << "?"; | ||||||||
} | ||||||||
os << "]"; | ||||||||
return os; | ||||||||
} | ||||||||
|
||||||||
} // namespace | ||||||||
|
||||||||
namespace luci | ||||||||
{ | ||||||||
|
||||||||
|
@@ -34,4 +62,101 @@ luci::CircleNode *CloneNodeLet<CN::OPQR>::visit(const luci::CircleReshape *node) | |||||||
return cloned; | ||||||||
} | ||||||||
|
||||||||
namespace sinf | ||||||||
{ | ||||||||
|
||||||||
loco::TensorShape Algorithm::visit(const luci::CircleReshape *node) | ||||||||
{ | ||||||||
LOGGER(l); | ||||||||
|
||||||||
const loco::DataType S32 = loco::DataType::S32; | ||||||||
|
||||||||
loco::TensorShape shape_by_input; | ||||||||
{ | ||||||||
LUCI_ASSERT(node->shape(), "2nd input shape() should not be nullptr"); | ||||||||
|
||||||||
// Only support node's shape() is CircleConst with S32 | ||||||||
// TODO support other node with other types | ||||||||
auto const_shape_node = dynamic_cast<luci::CircleConst *>(node->shape()); | ||||||||
if (const_shape_node != nullptr) | ||||||||
{ | ||||||||
LUCI_ASSERT(const_shape_node->dtype() == S32, "Only support int32 CircleConst"); | ||||||||
|
||||||||
shape_by_input.rank(const_shape_node->size<S32>()); | ||||||||
|
||||||||
for (uint32_t axis = 0; axis < shape_by_input.rank(); ++axis) | ||||||||
{ | ||||||||
shape_by_input.dim(axis) = const_shape_node->at<S32>(axis); | ||||||||
} | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
// We use shape from the node itself | ||||||||
loco::TensorShape shape; | ||||||||
shape.rank(node->rank()); | ||||||||
for (uint32_t r = 0; r < node->rank(); ++r) | ||||||||
{ | ||||||||
// TODO remove this copy from `use_own(node);` | ||||||||
// Shape inference rules in this file did not consider unknown dimension. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
like #13987 (review) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have applied this suggestion. Thanks! |
||||||||
// If some node has unknown dimension, 0 is inserted and wrong shape | ||||||||
// inference was done as a result. | ||||||||
// To fix this, new shape inference algorithm is being implemented. | ||||||||
// Until new inference algorithm is fully implemented, unknown dimension | ||||||||
// would be represented as 1 along with TFLite expression. | ||||||||
shape.dim(r) = node->dim(r).known() ? node->dim(r).value() : 1; | ||||||||
} | ||||||||
shape_by_input = shape; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
loco::TensorShape shape_by_attr; | ||||||||
{ | ||||||||
shape_by_attr.rank(node->newShape()->rank()); | ||||||||
|
||||||||
for (uint32_t axis = 0; axis < shape_by_attr.rank(); ++axis) | ||||||||
{ | ||||||||
shape_by_attr.dim(axis) = node->newShape()->dim(axis); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
if (!(shape_by_input == shape_by_attr)) | ||||||||
{ | ||||||||
INFO(l) << "CircleReshape: Two new shape information mismatched : " << std::endl; | ||||||||
INFO(l) << " shape_by_input : " << shape_by_input << std::endl; | ||||||||
INFO(l) << " shape_by_attr : " << shape_by_attr << std::endl; | ||||||||
} | ||||||||
|
||||||||
loco::TensorShape output_shape = shape_by_input; | ||||||||
|
||||||||
// One of the dimensions can have special value -1, meaning its actual value should be inferred. | ||||||||
const auto input = loco::must_cast<luci::CircleNode *>(node->tensor()); | ||||||||
const auto input_shape = circle_shape(input); | ||||||||
uint32_t input_element_count = 1; | ||||||||
uint32_t output_element_count = 1; | ||||||||
uint32_t unknown_dim_index = UINT32_MAX; | ||||||||
for (uint32_t i = 0; i < input_shape.rank(); ++i) | ||||||||
input_element_count *= (input_shape.dim(i).known() ? input_shape.dim(i).value() : 1); | ||||||||
for (uint32_t dim_index = 0; dim_index < output_shape.rank(); ++dim_index) | ||||||||
{ | ||||||||
const uint32_t dim_value = output_shape.dim(dim_index).value(); | ||||||||
if (static_cast<int>(dim_value) == -1) | ||||||||
{ | ||||||||
LUCI_ASSERT(unknown_dim_index == UINT32_MAX, "More than one unknown dimension"); | ||||||||
unknown_dim_index = dim_index; | ||||||||
} | ||||||||
else | ||||||||
{ | ||||||||
output_element_count *= dim_value; | ||||||||
} | ||||||||
} | ||||||||
if (unknown_dim_index != UINT32_MAX) | ||||||||
{ | ||||||||
output_shape.dim(unknown_dim_index) = input_element_count / output_element_count; | ||||||||
} | ||||||||
|
||||||||
return output_shape; | ||||||||
} | ||||||||
|
||||||||
} // namespace sinf | ||||||||
|
||||||||
} // namespace luci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Not for this PR)
I find out that there are many
std::ostream &operator<<
overloadings forTensorShape
inluci/services
.operator<<(std::ostream &os, const loco::TensorShape
incompiler/luci/service/
How about introduce new file name
compiler/luci/service/src/logHelper.h
and making one implementation?