Skip to content

[WIP] Add constant optimizer#108

Open
Muteages wants to merge 2 commits into
joeyye-work:for-serving-2.20from
Muteages:chaoshan.constant_converting
Open

[WIP] Add constant optimizer#108
Muteages wants to merge 2 commits into
joeyye-work:for-serving-2.20from
Muteages:chaoshan.constant_converting

Conversation

@Muteages
Copy link
Copy Markdown
Collaborator

No description provided.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 FreezeReadonlyVariablesOptimizer implementation + public factory/enablement helpers.
  • Registers the optimizer in MetaOptimizer when 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants