• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/grappler/clusters/cluster.h"
17 #include "tensorflow/core/protobuf/rewriter_config.pb.h"
18 
19 namespace tensorflow {
20 namespace grappler {
21 
Cluster(int timeout_s)22 Cluster::Cluster(int timeout_s) : timeout_s_(timeout_s) {
23   DisableDetailedStats(false);
24 }
25 
~Cluster()26 Cluster::~Cluster() {}
27 
AllowSoftPlacement(bool soft_placement_state)28 void Cluster::AllowSoftPlacement(bool soft_placement_state) {
29   options_.config.set_allow_soft_placement(soft_placement_state);
30 }
31 
SetNumInterOpThreads(int num_threads)32 void Cluster::SetNumInterOpThreads(int num_threads) {
33   for (int i = 0; i < options_.config.session_inter_op_thread_pool_size();
34        ++i) {
35     options_.config.mutable_session_inter_op_thread_pool(i)->set_num_threads(
36         num_threads);
37   }
38 }
39 
SetNumWarmupSteps(int num_steps)40 void Cluster::SetNumWarmupSteps(int num_steps) {
41   options_.config.mutable_graph_options()->set_build_cost_model_after(
42       num_steps);
43 }
44 
45 // Set executor type to instantiate
SetExecutorType(const string * executor_type)46 void Cluster::SetExecutorType(const string* executor_type) {
47   options_.config.mutable_experimental()->set_executor_type(*executor_type);
48 }
49 
NumWarmupSteps() const50 int Cluster::NumWarmupSteps() const {
51   return options_.config.graph_options().build_cost_model_after();
52 }
53 
DisableDetailedStats(bool disable)54 void Cluster::DisableDetailedStats(bool disable) {
55   if (disable) {
56     options_.config.mutable_graph_options()->set_build_cost_model(0);
57     run_options_.set_trace_level(RunOptions::NO_TRACE);
58   } else {
59     options_.config.mutable_graph_options()->set_build_cost_model(1);
60     run_options_.set_trace_level(RunOptions::HARDWARE_TRACE);
61   }
62 }
63 
DetailedStatsEnabled() const64 bool Cluster::DetailedStatsEnabled() const {
65   return options_.config.graph_options().build_cost_model() != 0;
66 }
67 
DisableOptimizer(bool disable)68 void Cluster::DisableOptimizer(bool disable) {
69   OptimizerOptions* options =
70       options_.config.mutable_graph_options()->mutable_optimizer_options();
71   if (disable) {
72     options->set_opt_level(OptimizerOptions::L0);
73     // Disable Grappler optimizations.
74     auto rewriter_config =
75         options_.config.mutable_graph_options()->mutable_rewrite_options();
76     rewriter_config->set_layout_optimizer(RewriterConfig::OFF);
77     rewriter_config->set_disable_model_pruning(true);
78     rewriter_config->set_function_optimization(RewriterConfig::OFF);
79     rewriter_config->set_arithmetic_optimization(RewriterConfig::OFF);
80     rewriter_config->set_loop_optimization(RewriterConfig::OFF);
81     rewriter_config->set_dependency_optimization(RewriterConfig::OFF);
82     rewriter_config->set_constant_folding(RewriterConfig::OFF);
83     rewriter_config->set_memory_optimization(RewriterConfig::NO_MEM_OPT);
84     rewriter_config->set_shape_optimization(RewriterConfig::OFF);
85     rewriter_config->set_remapping(RewriterConfig::OFF);
86     rewriter_config->set_pin_to_host_optimization(RewriterConfig::OFF);
87     rewriter_config->mutable_auto_parallel()->set_enable(false);
88     rewriter_config->clear_optimizers();
89   } else {
90     options->set_opt_level(OptimizerOptions::L1);
91     auto rewriter_config =
92         options_.config.mutable_graph_options()->mutable_rewrite_options();
93     rewriter_config->set_constant_folding(RewriterConfig::DEFAULT);
94     rewriter_config->set_memory_optimization(RewriterConfig::DEFAULT_MEM_OPT);
95   }
96 }
97 
GetDeviceNames() const98 const std::vector<string> Cluster::GetDeviceNames() const {
99   std::vector<string> device_names;
100   device_names.reserve(devices_.size());
101   for (const auto& device : devices_) {
102     device_names.push_back(device.first);
103   }
104   std::sort(device_names.begin(), device_names.end());
105   return device_names;
106 }
107 
108 }  // end namespace grappler
109 }  // end namespace tensorflow
110