Repository navigation
Validate that LoopCond nodes are inside a while loop frame - #129186
copybara-service[bot] merged 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request ensures that a LoopCond node defined outside of a while loop frame fails gracefully with an InvalidArgumentError instead of causing a crash or abort during graph partitioning. It adds validation checks in control_flow.cc and graph_partition.cc, along with corresponding C++ and Python tests. A critical issue was identified in graph_partition.cc where the helper function FormatNodeForError is called but is not accessible, which will cause a compilation error. A suggestion was provided to format the error message using the node's name and type directly.
| if (frame_name.empty()) { | ||
| return absl::InvalidArgumentError( | ||
| absl::StrCat(FormatNodeForError(*node), | ||
| " must be inside a while loop frame.")); | ||
| } |
There was a problem hiding this comment.
The helper function FormatNodeForError is defined in an anonymous namespace inside tensorflow/core/graph/control_flow.cc and is not exported or declared in any header file. Calling it here in graph_partition.cc will result in a compilation error (undeclared identifier). Instead, you can format the error message using the node's name() and type_string() directly.
| if (frame_name.empty()) { | |
| return absl::InvalidArgumentError( | |
| absl::StrCat(FormatNodeForError(*node), | |
| " must be inside a while loop frame.")); | |
| } | |
| if (frame_name.empty()) { | |
| return absl::InvalidArgumentError( | |
| absl::StrCat("Node '", node->name(), "' of type ", | |
| node->type_string(), | |
| " must be inside a while loop frame.")); | |
| } |
dmiltr3
left a comment
There was a problem hiding this comment.
- Keep the fix at the abort site in
graph_partition.ccand revert thecontrol_flow.ccchange
The added check in ValidateControlFlowInfo (tensorflow/core/graph/control_flow.cc) runs for every caller of BuildControlFlowInfo, not only graph partitioning. That includes XLA auto-clustering (tensorflow/compiler/jit/xla_cluster_util.cc), deadness analysis, function instantiation, Grappler, and while-loop functionalization. As a result, it breaks XlaCompilationTest.DontClusterTheSpecialIdentityDrivingConstsInLoop in tensorflow/compiler/jit/mark_for_compilation_pass_test.cc (lines 1746 and 1760), which intentionally constructs an incomplete graph containing a LoopCond node without an Enter node and caused the //tensorflow/compiler/jit:compilation_passes_test failure in the build-linux-x86-cuda and build-linux-x86-cuda13 CI jobs.
The abort reported in issue #62982 is the DCHECK(!frame_name.empty()) in AddControlFlow (tensorflow/core/graph/graph_partition.cc), and replacing that DCHECK with an InvalidArgumentError return resolves the issue without breaking callers of BuildControlFlowInfo. Please make the following changes:
- Revert the change in
tensorflow/core/graph/control_flow.ccand removeValidateControlFlowTest.LoopCondOutsideWhileLoopfromtensorflow/core/graph/control_flow_test.cc. - Keep the
InvalidArgumentErrorcheck inAddControlFlowintensorflow/core/graph/graph_partition.cc. - Add the C++ regression test to
tensorflow/core/graph/graph_partition_test.cc(afterPartitionIncompleteGraph, along with#include "absl/strings/match.h") so that it exercisesPartition->AddControlFlowdirectly:
TEST_F(GraphPartitionTest, LoopCondOutsideWhileLoop) {
auto a1 = BoolInput(in_.WithOpName("A1"));
LoopCond(in_.WithOpName("A2"), a1);
Graph g(OpRegistry::Global());
TF_ASSERT_OK(
ConvertGraphDefToGraph(GraphConstructorOptions(), ToGraphDef(), &g));
for (Node* node : g.nodes()) {
node->set_assigned_device_name(DeviceName(node));
}
PartitionOptions popts;
popts.node_to_loc = SplitByDevice;
popts.new_name = [&g](const std::string& prefix) {
return g.NewName(prefix);
};
popts.get_incarnation = [](const std::string&) { return 1; };
std::unordered_map<std::string, GraphDef> partitions;
absl::Status status = Partition(popts, &g, &partitions);
EXPECT_EQ(error::INVALID_ARGUMENT, status.code()) << status;
EXPECT_TRUE(absl::StrContains(status.message(),
"must be inside a while loop frame"))
<< status;
}- Pin the error message and run in deprecated v1 graph mode in the Python test
In tensorflow/python/kernel_tests/control_flow/control_flow_ops_py_test.py, testLoopCondOutsideWhileLoop uses a bare self.assertRaises(errors_impl.InvalidArgumentError) with op construction inside the with block, and @test_util.run_v1_only skips the test whenever TF2 behavior is enabled. Please construct the op before the with block, assert the specific error message substring "must be inside a while loop frame" around sess.run(cond) using self.assertRaisesRegex, and use @test_util.run_deprecated_v1 (like testRunLoopTensor in the same file) so the test runs in graph mode under both TF1 and TF2 configurations (untested sketch):
@test_util.run_deprecated_v1
def testLoopCondOutsideWhileLoop(self):
# LoopCond outside a while loop frame must fail with InvalidArgumentError
# rather than aborting during graph partitioning (issue #62982).
with self.cached_session() as sess:
cond = gen_control_flow_ops.loop_cond(input=True)
with self.assertRaisesRegex(errors_impl.InvalidArgumentError,
"must be inside a while loop frame"):
sess.run(cond)- Include
graph_node_util.hdirectly ingraph_partition.cc
tensorflow/core/graph/graph_partition.cc now calls FormatNodeForError, which is declared in tensorflow/core/graph/graph_node_util.h and is currently only available through a transitive include. Please add #include "tensorflow/core/graph/graph_node_util.h" in sorted order in tensorflow/core/graph/graph_partition.cc.
- Update the PR description
Please update the PR description to match the implementation:
- Replace
AddControlLoopswithAddControlFlow. - Remove the
ValidateControlFlowInfobullet once the change intensorflow/core/graph/control_flow.ccis dropped. - Remove the claim that release builds corrupt graph partitioning state; in
AddControlFlow, root-frame edges are skipped beforeframe_cond_mapis queried, so the empty-frame entry was never read in release builds.
…wInfo check Revert the empty frame check in ValidateControlFlowInfo to preserve incomplete graph handling in XLA compilation passes, keep the validation in AddControlFlow, and add a C++ partition regression test along with pinned Python test assertions under deprecated v1.
|
Reverted the check in ValidateControlFlowInfo, kept the validation in AddControlFlow, and added the partition regression test and updated the PR description. Thanks! |
dmiltr3
left a comment
There was a problem hiding this comment.
All four requests from the previous review have been resolved:
tensorflow/core/graph/control_flow.ccandtensorflow/core/graph/control_flow_test.ccare reverted to their original state onmaster, so callers ofBuildControlFlowInfooutside graph partitioning keep their existing behavior.AddControlFlowintensorflow/core/graph/graph_partition.ccreturnsabsl::InvalidArgumentErrorbefore mutatingframe_cond_map.GraphPartitionTest.LoopCondOutsideWhileLoopintensorflow/core/graph/graph_partition_test.ccexercisesPartitiondirectly and checks both the error code and the error message.testLoopCondOutsideWhileLoopintensorflow/python/kernel_tests/control_flow/control_flow_ops_py_test.pyruns under@test_util.run_deprecated_v1, constructsgen_control_flow_ops.loop_cond(input=True)outside thewithblock, and checks the error message substring.#include "tensorflow/core/graph/graph_node_util.h"is included directly intensorflow/core/graph/graph_partition.cc, and the pull request description matches the implementation.
One remaining change is required before approval:
Add direct includes for absl::InvalidArgumentError and absl::StrCat in tensorflow/core/graph/graph_partition.cc
The new check at tensorflow/core/graph/graph_partition.cc:650-654 calls absl::InvalidArgumentError and absl::StrCat, which are currently only available through transitive includes. Please add #include "absl/status/status.h" and #include "absl/strings/str_cat.h" in sorted order right after #include "absl/container/flat_hash_map.h" at tensorflow/core/graph/graph_partition.cc:26:
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "tensorflow/core/framework/function.h"Both headers are already used by other source files in tensorflow/core/graph, so no BUILD file change is needed.
Include absl/status/status.h and absl/strings/str_cat.h directly for absl::InvalidArgumentError and absl::StrCat.
|
Added the direct absl/status/status.h and absl/strings/str_cat.h includes. Thanks! |
Calling
tf.raw_ops.LoopCond(input=True)outside of a while loop frame aborts in debug builds (--config=dbg) with:Cause
A
LoopCondop placed directly in the graph outside of anEnterop has an emptyframe_name(""). InAddControlFlow(tensorflow/core/graph/graph_partition.cc), debug builds assertDCHECK(!frame_name.empty())when populatingframe_cond_map.Solution
DCHECK(!frame_name.empty())inAddControlFlow(tensorflow/core/graph/graph_partition.cc) with an explicitInvalidArgumentErrorreturn.tensorflow/core/graph/graph_partition_test.ccandtensorflow/python/kernel_tests/control_flow/control_flow_ops_py_test.py.Fixes #62982.