From d016650ca7636c96c6664bed2cf3a2fa8a3c674b Mon Sep 17 00:00:00 2001 From: Tong Shen Date: Fri, 5 Oct 2018 12:17:31 -0700 Subject: Revert constant folding to previous state. PiperOrigin-RevId: 215946205 --- tensorflow/core/common_runtime/constant_folding.cc | 35 +++++----------------- tensorflow/core/common_runtime/constant_folding.h | 4 --- tensorflow/core/common_runtime/graph_optimizer.cc | 5 +--- tensorflow/core/common_runtime/graph_optimizer.h | 5 +--- 4 files changed, 10 insertions(+), 39 deletions(-) (limited to 'tensorflow/core') diff --git a/tensorflow/core/common_runtime/constant_folding.cc b/tensorflow/core/common_runtime/constant_folding.cc index db137f1a19..e81e61b633 100644 --- a/tensorflow/core/common_runtime/constant_folding.cc +++ b/tensorflow/core/common_runtime/constant_folding.cc @@ -466,23 +466,23 @@ Graph* GetConstantGraph( bool ReplaceTensorWithConstant( Graph* graph, Device* partition_device, NodeAndOutput tensor, const Tensor& constant, const gtl::FlatSet& control_deps, - int64 max_constant_size_in_bytes, bool disable_memory_output_type_check, + int64 max_constant_size_in_bytes, const ConstantFoldNameGenerator& generate_new_name) { // Be conservative when replacing a tensor with a constant, when not // running on CPU. // 1) Do not replace another constant. // 2) If the destination tensor is not an int32 tensor, and has HOST_MEMORY // constraint, do not replace it. - // 3) If the size of the constant in bytes is too large (> + // 3) If the destination tensor is an int32 tensor, and has DEVICE_MEMORY + // constraint, do not replace it. + // 4) If the size of the constant in bytes is too large (> // max_constant_in_bytes), do not replace it. This prevents the size of the // Graph from growing too large. - // 4) If the constant op created does not have a kernel implementation + // 5) If the constant op created does not have a kernel implementation // for the device, do not use it. // TODO(keveman): Consider adding a new constant op that has a kernel // implementation for all types, but with HostMemory constraint on it's // output. - // 5) If the constant op for the device has different output memory type - // from the original op output memory type, do not replace it. if (tensor.first->IsConstant()) { return false; } @@ -497,7 +497,8 @@ bool ReplaceTensorWithConstant( return false; } bool is_int32 = tensor.first->output_type(tensor.second) == DT_INT32; - if (memory_type == HOST_MEMORY && !is_int32) { + if ((memory_type == HOST_MEMORY && !is_int32) || + (memory_type == DEVICE_MEMORY && is_int32)) { return false; } } @@ -535,25 +536,6 @@ bool ReplaceTensorWithConstant( if (!NodeBuilder(builder).Finalize(graph, &constant_node).ok()) { return false; } - if (!disable_memory_output_type_check) { - if (partition_device && device_type != DEVICE_CPU) { - MemoryType original_output_memory_type; - if (!MemoryTypeForOutput(device_type, graph, tensor.first, tensor.second, - &original_output_memory_type) - .ok()) { - return false; - } - MemoryType const_output_memory_type; - if (!MemoryTypeForOutput(device_type, graph, constant_node, 0, - &const_output_memory_type) - .ok()) { - return false; - } - if (original_output_memory_type != const_output_memory_type) { - return false; - } - } - } for (auto edge : edges_to_remove) { graph->AddEdge(constant_node, 0, edge->dst(), edge->dst_input()); graph->RemoveEdge(edge); @@ -660,8 +642,7 @@ Status ConstantFold(const ConstantFoldingOptions& opts, constant_control_deps[tensors_to_replace[c].first]; if (ReplaceTensorWithConstant( graph, partition_device, tensors_to_replace[c], outputs[c], - control_deps, opts.max_constant_size_in_bytes, - opts.disable_memory_output_type_check, generate_new_name)) { + control_deps, opts.max_constant_size_in_bytes, generate_new_name)) { ++num_nodes_replaced; } } diff --git a/tensorflow/core/common_runtime/constant_folding.h b/tensorflow/core/common_runtime/constant_folding.h index 4c71b7bd27..a9a84f761b 100644 --- a/tensorflow/core/common_runtime/constant_folding.h +++ b/tensorflow/core/common_runtime/constant_folding.h @@ -45,10 +45,6 @@ struct ConstantFoldingOptions { // optimization. int64 max_constant_size_in_bytes = 10 * 1024 * 1024; - // If disable_memory_output_type_check is true, we will disable output memory - // type check for constant node replacement. - bool disable_memory_output_type_check = false; - // A generator for the name suffix of constant folded nodes. A // default id generator that monotonically increases is used if nullptr is // passed. diff --git a/tensorflow/core/common_runtime/graph_optimizer.cc b/tensorflow/core/common_runtime/graph_optimizer.cc index 91194bc86f..37a979a8f1 100644 --- a/tensorflow/core/common_runtime/graph_optimizer.cc +++ b/tensorflow/core/common_runtime/graph_optimizer.cc @@ -39,8 +39,7 @@ void GraphOptimizer::Optimize( const std::unordered_map>* shape_map, const std::function& cse_consider_fn, - const std::function& cf_consider_fn, - bool cf_disable_memory_output_type_check) { + const std::function& cf_consider_fn) { Graph* g = graph->get(); DumpGraph("Initial", g); @@ -65,8 +64,6 @@ void GraphOptimizer::Optimize( ConstantFoldingOptions cf_opts; cf_opts.shape_map = shape_map; cf_opts.consider = cf_consider_fn; - cf_opts.disable_memory_output_type_check = - cf_disable_memory_output_type_check; if (opts_.max_folded_constant_in_bytes() > 0) { cf_opts.max_constant_size_in_bytes = opts_.max_folded_constant_in_bytes(); diff --git a/tensorflow/core/common_runtime/graph_optimizer.h b/tensorflow/core/common_runtime/graph_optimizer.h index 8954e9612d..789cc56942 100644 --- a/tensorflow/core/common_runtime/graph_optimizer.h +++ b/tensorflow/core/common_runtime/graph_optimizer.h @@ -47,16 +47,13 @@ class GraphOptimizer { // returns true will be considered for CSE. // If cf_consider_fn is not null then only nodes for which cf_consider_fn // returns true will be considered for CF. - // If cf_disable_memory_output_type_check is true, CF will discard output - // memory type check for constant node replacement. void Optimize( FunctionLibraryRuntime* runtime, Env* env, Device* device, std::unique_ptr* graph, const std::unordered_map>* shape_map, const std::function& cse_consider_fn = nullptr, - const std::function& cf_consider_fn = nullptr, - bool cf_disable_memory_output_type_check = false); + const std::function& cf_consider_fn = nullptr); const OptimizerOptions& options() { return opts_; } -- cgit v1.2.3