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 #ifndef TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_AUTO_PARALLEL_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_AUTO_PARALLEL_H_ 18 19 #include "tensorflow/core/framework/variable.pb.h" 20 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h" 21 #include "tensorflow/core/lib/core/status.h" 22 23 namespace tensorflow { 24 namespace grappler { 25 26 // Automatically parallelize a graph by splitting in the batch dimension. 27 class AutoParallel : public GraphOptimizer { 28 public: AutoParallel(int num_replicas)29 AutoParallel(int num_replicas) : num_replicas_(num_replicas) { 30 CHECK(num_replicas_ >= 2); 31 } ~AutoParallel()32 ~AutoParallel() override {} 33 name()34 string name() const override { return "autoparallel"; }; 35 36 Status Optimize(Cluster* cluster, const GrapplerItem& item, 37 GraphDef* output) override; 38 39 void Feedback(Cluster* cluster, const GrapplerItem& item, 40 const GraphDef& optimize_output, double result) override; 41 42 private: 43 GraphDef graph_; 44 std::map<string, NodeDef*> all_nodes_; 45 std::set<string> apply_gradients_nodes_; 46 std::set<string> replica_nodes_; 47 std::set<string> shared_nodes_; 48 const GrapplerItem* item_; 49 int num_replicas_; 50 int num_gpus_; 51 Status Initialize(const GrapplerItem& item); 52 NodeDef* AddNodeDivConst(); 53 NodeDef* AddNodeDiv(const string& name, const string& input_a, 54 const string& input_b); 55 NodeDef* AddNodeControl(const string& name, const std::set<string>& deps, 56 GraphDef* graph); 57 bool NotSharedNode(const string& name); 58 void AddSharedNodes(GraphDef* graph); 59 void AddOneReplica(GraphDef* graph, int number); 60 void BuildGraph(GraphDef* graph); 61 }; 62 63 } // end namespace grappler 64 } // end namespace tensorflow 65 66 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_AUTO_PARALLEL_H_ 67