xref: /aosp_15_r20/external/tensorflow/tensorflow/core/grappler/optimizers/data/autotune_buffer_sizes.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_AUTOTUNE_BUFFER_SIZES_H_
17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_AUTOTUNE_BUFFER_SIZES_H_
18 
19 #include "tensorflow/core/framework/attr_value.pb.h"
20 #include "tensorflow/core/grappler/optimizers/data/optimizer_base.h"
21 
22 namespace tensorflow {
23 namespace grappler {
24 
25 constexpr char kAutotune[] = "autotune";
26 
27 // This optimization does the following:
28 //
29 // 1. Adds `prefetch(AUTOTUNE)` after all asynchronous tf.data transformations
30 // (e.g. parallel batch, parallel map, parallel interleave, and map + batch) if
31 // they are not followed by a `prefetch` yet.
32 //
33 // 2. If there exists any `prefetch(buffer_size=N)` for `N>=0`,  it will replace
34 // the transformation with autotunable version of `prefetch` which uses N as
35 // the minimum size of the buffer.
36 class AutotuneBufferSizes : public TFDataOptimizerBase {
37  public:
38   AutotuneBufferSizes() = default;
39   ~AutotuneBufferSizes() override = default;
40 
name()41   string name() const override { return "autotune_buffer_sizes"; };
42 
UsesFunctionLibrary()43   bool UsesFunctionLibrary() const override { return false; }
44 
Init(const tensorflow::RewriterConfig_CustomGraphOptimizer * config)45   Status Init(
46       const tensorflow::RewriterConfig_CustomGraphOptimizer* config) override {
47     if (!config) return OkStatus();
48 
49     const string& autotune = config->parameter_map().at(kAutotune).s();
50     if (autotune == "true") {
51       autotune_ = true;
52     } else if (autotune == "false") {
53       autotune_ = false;
54     } else {
55       return errors::InvalidArgument("Received an invalid value for parameter ",
56                                      kAutotune, ": ", autotune);
57     }
58     return OkStatus();
59   }
60 
61   Status OptimizeAndCollectStats(Cluster* cluster, const GrapplerItem& item,
62                                  GraphDef* output,
63                                  OptimizationStats* stats) override;
64 
65  private:
66   bool autotune_ = true;
67 };
68 
69 }  // namespace grappler
70 }  // namespace tensorflow
71 
72 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_AUTOTUNE_BUFFER_SIZES_H_
73