Repository navigation
Replace static recomputation op list with dynamic cost model - #109358
copybara-service[bot] merged 2 commits into
Conversation
|
Hi @ayush10, Could you kindly try to resolve the conflicts? Thanks ! |
|
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
Hi, I was busy with hackathons, so I couldn't do it. Finishing it right now. |
b278456 to
4d1862b
Compare
|
Resolved merge conflicts by rebasing onto latest master. |
e91c9bd to
a86790d
Compare
|
@googlebot I signed it! |
…y optimizer The Grappler memory optimizer previously used a hardcoded list of "cheap to recompute" ops (GetCheapToRecomputeOps). This was inadequate because it could not account for actual tensor sizes -- e.g. a Tile op on a small tensor is cheap, but on a large tensor it is expensive. This change adds IsCheapToRecompute() which uses OpLevelCostEstimator to dynamically estimate the compute cost of each candidate node based on its shape information from GraphProperties. Nodes with estimated compute time below 100 microseconds are considered cheap to recompute. When shape inference fails or the cost estimate is marked inaccurate, the function falls back to the original static op list. Fixes tensorflow#94653
a86790d to
3e3bd03
Compare
There was a problem hiding this comment.
Thanks for the PR! This is a great improvement to make the recomputation decisions dynamic based on estimated execution costs.
However, during verification against the TensorFlow test suites, we identified a critical design defect in the IsCheapToRecompute implementation that causes test regressions (e.g., Attempting to use uninitialized value Recomputed/filter and scope matching errors).
Core Finding & Fix
The Issue:
In the proposed implementation of IsCheapToRecompute, if input shape properties are available and the cost model successfully predicts a cost (!costs.inaccurate), the function evaluates:
return costs.compute_time.count() < kRecomputeCostThresholdNs;
This check returns true for any operation that is cheap, regardless of its operation type. As a result, safe-guards are bypassed for non-recomputable ops (e.g., stateful ops, variable reads like VariableV2 or weights, and helper ops), marking them for recomputation which is invalid.
The Solution:
We must restrict dynamic cost estimation specifically to operations in the static GetCheapToRecomputeOps() list. The dynamic cost estimation should decide whether a candidate op is cheap enough to recompute, rather than opening recomputation to any arbitrary op under the cost threshold.
Here is the updated implementation of IsCheapToRecompute that resolves this issue:
bool IsCheapToRecompute(
const NodeDef& node,
const GraphProperties& properties,
const std::unordered_map<std::string, const NodeDef*>& name_to_node,
const OpLevelCostEstimator& estimator) {
// Only evaluate ops that are in the static cheap ops list.
if (GetCheapToRecomputeOps().count(node.op()) == 0) {
return false;
}
// Try dynamic cost estimation if input properties are available.
if (properties.HasInputProperties(node.name())) {
const auto& input_properties = properties.GetInputProperties(node.name());
OpInfo op_info =
BuildOpInfoWithoutDevice(node, name_to_node, input_properties);
OpContext op_context;
op_context.name = node.name();
op_context.op_info = op_info;
Costs costs = estimator.PredictCosts(op_context);
if (!costs.inaccurate) {
return costs.compute_time.count() < kRecomputeCostThresholdNs;
}
}
// Fallback to static list (which we already checked) for ops without
// reliable cost estimates.
return true;
}Unit Verification
To protect this behavior, we added a C++ unit test in tensorflow/core/grappler/optimizers/memory_optimizer_test.cc. This test verifies that:
- A small
Reluop (e.g., shape{2, 3, 4}) is successfully recomputed because its dynamic cost is below the 100 microseconds threshold. - A large
Reluop (e.g., shape{1000, 1000}) with 1M elements is not recomputed because its dynamic cost exceeds the threshold.
Please add the following test case:
TEST_F(RecomputeSubgraphTest, DynamicHeuristicRecomputation) {
// 1. Small Tensor: Should be recomputed because compute cost is under
// threshold.
{
tensorflow::Scope s = tensorflow::Scope::NewRootScope();
Output a = ops::Variable(s.WithOpName("a"), {2, 3, 4}, DT_FLOAT);
Output b = ops::Identity(s.WithOpName("b"), a);
Output c = ops::AddN(s.WithOpName("gradients/c"), {b});
GrapplerItem item;
TF_CHECK_OK(s.ToGraphDef(&item.graph));
NodeMap pre_transform_node_map(&item.graph);
// Change op type to one listed in GetCheapToRecomputeOps
pre_transform_node_map.GetNode("b")->set_op("Relu");
MemoryOptimizer optimizer(RewriterConfig::RECOMPUTATION_HEURISTICS);
GraphDef output;
TF_EXPECT_OK(optimizer.Optimize(nullptr, item, &output));
NodeMap post_transform_node_map(&output);
EXPECT_NE(post_transform_node_map.GetNode("Recomputed/b"), nullptr);
}
// 2. Large Tensor: Should NOT be recomputed because compute cost exceeds
// threshold.
{
tensorflow::Scope s = tensorflow::Scope::NewRootScope();
Output a = ops::Variable(s.WithOpName("a"), {1000, 1000}, DT_FLOAT);
Output b = ops::Identity(s.WithOpName("b"), a);
Output c = ops::AddN(s.WithOpName("gradients/c"), {b});
GrapplerItem item;
TF_CHECK_OK(s.ToGraphDef(&item.graph));
NodeMap pre_transform_node_map(&item.graph);
// Change op type to one listed in GetCheapToRecomputeOps
pre_transform_node_map.GetNode("b")->set_op("Relu");
MemoryOptimizer optimizer(RewriterConfig::RECOMPUTATION_HEURISTICS);
GraphDef output;
TF_EXPECT_OK(optimizer.Optimize(nullptr, item, &output));
NodeMap post_transform_node_map(&output);
EXPECT_EQ(post_transform_node_map.GetNode("Recomputed/b"), nullptr);
}
}Please update the pull request with these corrections and the new unit test. Let us know when it's updated!
|
@dmiltr3 Updated this PR to address the recomputation-safety issue from your review. What changed:
Validation performed locally:
I could not run the Bazel target locally because |
27c8704
into
tensorflow:master
Summary
GetCheapToRecomputeOps), which could not account for actual tensor sizesIsCheapToRecompute()which usesOpLevelCostEstimatorto dynamically estimate compute cost based on shape information fromGraphPropertiesTest plan
Fixes #94653