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

Fix concat optimization infinite loop #23408

Merged
merged 1 commit into from Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions tensorflow/core/grappler/optimizers/constant_folding.cc
Expand Up @@ -3022,11 +3022,11 @@ bool ConstantFolding::PartialConcatConstFolding(GraphDef* optimized_graph,
for (auto interval : constant_input_runs) {
// Push the constant inputs in the interval to a child node than can be
// constant folded.
const string new_node_name = OptimizedNodeName(
*node, strings::StrCat("_partial_split_", interval.first));
if (node_map_->NodeExists(new_node_name)) {
break;
}
string new_node_name = OptimizedNodeName(*node, "_partial_split");
do {
new_node_name += strings::StrCat("_", interval.first);
} while (node_map_->NodeExists(new_node_name));

NodeDef* added_node = optimized_graph->add_node();
*added_node = *node;
added_node->set_name(new_node_name);
Expand Down
34 changes: 34 additions & 0 deletions tensorflow/core/grappler/optimizers/constant_folding_test.cc
Expand Up @@ -2154,6 +2154,40 @@ TEST_F(ConstantFoldingTest, MergeConcat_AxisMismatch) {
CompareGraphs(want, got);
}

TEST_F(ConstantFoldingTest, MergeConcat_PartialFolding) {
Scope scope = Scope::NewRootScope();
Output c1 = ops::Const(scope.WithOpName("c1"), 1.0f, {2, 2});
Output c2 = ops::Const(scope.WithOpName("c2"), 2.0f, {2, 2});
Output c3 = ops::Const(scope.WithOpName("c3"), 3.0f, {2, 2});
Output c4 = ops::Const(scope.WithOpName("c4"), 4.0f, {2, 2});
Output ph = ops::Placeholder(scope.WithOpName("ph"), DT_FLOAT,
ops::Placeholder::Shape(TensorShape({2, 2})));
Output axis = ops::Const(scope.WithOpName("axis"), 0, {});

ops::Concat concat1(scope.WithOpName("concat1"), {c1, c2, ph}, axis);
ops::Concat concat2(scope.WithOpName("concat2"), {c3, c4, Output(concat1)},
axis);

GrapplerItem item;
item.fetch = {"concat2"};
TF_CHECK_OK(scope.ToGraphDef(&item.graph));

ConstantFolding optimizer(nullptr);
GraphDef got;
Status status = optimizer.Optimize(nullptr, item, &got);
TF_EXPECT_OK(status);

GraphDef want;
AddNode("ConstantFolding/concat2_partial_split_0_0", "Const", {}, {}, &want);
AddNode("axis", "Const", {}, {}, &want);
AddNode("ph", "Placeholder", {}, {}, &want);
AddNode("concat2", "ConcatV2",
{"ConstantFolding/concat2_partial_split_0_0", "ph", "axis"}, {},
&want);

CompareGraphs(want, got);
}

TEST_F(ConstantFoldingTest, PaddingWithZeroSize) {
tensorflow::Scope scope = tensorflow::Scope::NewRootScope();

Expand Down