[WIP] Add constant optimizer#108
Open
Muteages wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new Grappler custom graph optimizer that can “freeze” readonly variables by replacing them (or their read paths) with Const nodes populated from a checkpoint, and wires it into MetaOptimizer so it can be enabled via environment variables.
Changes:
- Introduces
FreezeReadonlyVariablesOptimizerimplementation + public factory/enablement helpers. - Registers the optimizer in
MetaOptimizerwhen enabled by env var and not already configured. - Adds Bazel targets for the new optimizer and links it into the meta optimizer build.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
tensorflow/core/grappler/optimizers/meta_optimizer.cc |
Auto-registers the freeze optimizer when env-enabled and not already initialized. |
tensorflow/core/grappler/optimizers/freeze_readonly_variables_optimizer.h |
Declares the new optimizer and related factory/feature-detection helpers. |
tensorflow/core/grappler/optimizers/freeze_readonly_variables_optimizer.cc |
Implements checkpoint lookup, safety analysis, and graph rewrites to freeze variables/read paths. |
tensorflow/core/grappler/optimizers/BUILD |
Adds a new cc_library target and links it into meta_optimizer. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1
to
+8
| #include "tensorflow/core/grappler/optimizers/freeze_readonly_variables_optimizer.h" | ||
|
|
||
| #include "absl/container/flat_hash_map.h" | ||
| #include "absl/strings/match.h" | ||
| #include "absl/strings/numbers.h" | ||
| #include "absl/strings/str_join.h" | ||
| #include "absl/strings/string_view.h" | ||
| #include "tensorflow/core/framework/attr_value.pb.h" |
Comment on lines
+64
to
+75
| int64_t MaxTensorBytes() { | ||
| const char* value = std::getenv(kOptimizerMaxTensorBytesEnvVar); | ||
| if (value == nullptr || value[0] == '\0') return kDefaultMaxTensorBytes; | ||
|
|
||
| int64_t parsed = 0; | ||
| if (!absl::SimpleAtoi(value, &parsed) || parsed < 0) { | ||
| LOG(WARNING) << "Ignoring invalid " << kOptimizerMaxTensorBytesEnvVar | ||
| << "=" << value << "; using " << kDefaultMaxTensorBytes; | ||
| return kDefaultMaxTensorBytes; | ||
| } | ||
| return parsed; | ||
| } |
Comment on lines
+1178
to
+1186
| std::vector<std::string> node_names; | ||
| node_names.reserve(optimized_graph->node_size()); | ||
| for (const NodeDef& node : optimized_graph->node()) { | ||
| node_names.push_back(node.name()); | ||
| } | ||
|
|
||
| for (const std::string& node_name : node_names) { | ||
| const NodeDef* node = FindNode(*optimized_graph, node_name); | ||
| if (node == nullptr || !IsVariableNode(*node)) continue; |
Comment on lines
+1159
to
+1169
| absl::Status FreezeReadonlyVariablesOptimizer::Optimize( | ||
| Cluster* cluster, const GrapplerItem& item, GraphDef* optimized_graph) { | ||
| *optimized_graph = item.graph; | ||
| if (checkpoint_prefix_.empty()) return absl::OkStatus(); | ||
|
|
||
| CheckpointTensorReader tensor_reader(checkpoint_prefix_); | ||
| if (!tensor_reader.status().ok()) { | ||
| LOG(WARNING) | ||
| << "FreezeReadonlyVariablesOptimizer skipped: cannot open checkpoint " | ||
| << checkpoint_prefix_ << ": " << tensor_reader.status(); | ||
| return absl::OkStatus(); |
| "//tensorflow/core/grappler/clusters:cluster", | ||
| "//tensorflow/core/util/tensor_bundle", | ||
| "@com_google_absl//absl/container:flat_hash_map", | ||
| "@com_google_absl//absl/container:flat_hash_set", |
Comment on lines
+77
to
+87
| bool EstimateTensorBytes(DataType dtype, const TensorShape& shape, | ||
| int64_t* estimated_bytes) { | ||
| const int64_t elements = shape.num_elements(); | ||
| const int dtype_size = DataTypeSize(dtype); | ||
| if (elements < 0 || dtype_size <= 0) return false; | ||
| if (elements > std::numeric_limits<int64_t>::max() / dtype_size) { | ||
| *estimated_bytes = std::numeric_limits<int64_t>::max(); | ||
| return true; | ||
| } | ||
| *estimated_bytes = elements * dtype_size; | ||
| return true; |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.