xref: /aosp_15_r20/external/tensorflow/tensorflow/core/grappler/optimizers/data/auto_shard.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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_DATA_AUTO_SHARD_H_
17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_AUTO_SHARD_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "tensorflow/core/framework/dataset_options.pb.h"
23 #include "tensorflow/core/framework/node_def.pb.h"
24 #include "tensorflow/core/grappler/mutable_graph_view.h"
25 #include "tensorflow/core/grappler/optimizers/data/optimizer_base.h"
26 
27 namespace tensorflow {
28 namespace grappler {
29 
30 class AutoShard : public TFDataOptimizerBase {
31  public:
32   AutoShard() = default;
33   ~AutoShard() override = default;
34 
name()35   string name() const override { return "tf_auto_shard"; }
36 
UsesFunctionLibrary()37   bool UsesFunctionLibrary() const override { return true; }
38 
39   Status Init(
40       const tensorflow::RewriterConfig_CustomGraphOptimizer* config) override;
41 
42   Status OptimizeAndCollectStats(Cluster* cluster, const GrapplerItem& item,
43                                  GraphDef* output,
44                                  OptimizationStats* stats) override;
45 
46  private:
47   int64_t num_workers_;
48   int64_t num_replicas_;
49   int64_t index_;
50   tensorflow::data::AutoShardPolicy auto_shard_policy_;
51 };
52 
53 // For testing only
54 namespace internal {
55 bool IsEligibleRewriteBatchSize(const NodeDef& sink_node,
56                                 const MutableGraphView& graph,
57                                 std::vector<std::string>* ineligible_reason);
58 }
59 
60 }  // namespace grappler
61 }  // namespace tensorflow
62 
63 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_AUTO_SHARD_H_
64