1 /* Copyright 2018 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 #ifndef TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_PIN_TO_HOST_OPTIMIZER_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_PIN_TO_HOST_OPTIMIZER_H_ 18 19 #include <unordered_set> 20 #include "tensorflow/core/grappler/costs/graph_properties.h" 21 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h" 22 #include "tensorflow/core/lib/gtl/flatset.h" 23 #include "tensorflow/core/protobuf/rewriter_config.pb.h" 24 25 namespace tensorflow { 26 namespace grappler { 27 namespace internal { 28 // Try and find an appropriate Host device in `devices` given `device`. 29 string TryFindHostDevice(const gtl::FlatSet<string>& devices, 30 bool has_device_cpu, const string& device); 31 } // end namespace internal 32 33 // Optimize TensorFlow ops that should be swapped into the CPU to avoid 34 // excessive cpu<->gpu memcpy/sync. 35 // 36 // TODO(williamchan): The current heuristic will swap any small integer Const to 37 // CPU. This may cause a problem cpu->cpu->gpu wherein the original behaviour of 38 // gpu->gpu->gpu may have been better/faster. We should probably fix this. 39 class PinToHostOptimizer : public GraphOptimizer { 40 public: PinToHostOptimizer()41 PinToHostOptimizer() {} PinToHostOptimizer(RewriterConfig::Toggle opt_level)42 explicit PinToHostOptimizer(RewriterConfig::Toggle opt_level) {} 43 ~PinToHostOptimizer()44 ~PinToHostOptimizer() override {} 45 name()46 string name() const override { return "pin_to_host_optimizer"; }; 47 48 Status Optimize(Cluster* cluster, const GrapplerItem& item, 49 GraphDef* optimized_graph) override; 50 Feedback(Cluster * cluster,const GrapplerItem & item,const GraphDef & optimized_graph,double result)51 void Feedback(Cluster* cluster, const GrapplerItem& item, 52 const GraphDef& optimized_graph, double result) override {} 53 }; 54 55 } // end namespace grappler 56 } // end namespace tensorflow 57 58 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_PIN_TO_HOST_OPTIMIZER_H_ 59