xref: /aosp_15_r20/external/tensorflow/tensorflow/core/grappler/optimizers/data/make_deterministic.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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_MAKE_DETERMINISTIC_H_
17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_MAKE_DETERMINISTIC_H_
18 
19 #include "tensorflow/core/grappler/optimizers/data/optimizer_base.h"
20 
21 namespace tensorflow {
22 namespace grappler {
23 
24 // Removes sources on nondeterminism from dataset ops. Nondeterminism can occur
25 // in the follow ways, each which this pass addresses:
26 //
27 // 1. The datasets ParallelInterleave, ParallelMap, and MapAndBatch can
28 //    introduce nondeterminism by running a function multiple times in parallel.
29 //    Specifically, if the function can mutate state, it is potentially
30 //    nondeterministic. In such cases, this pass converts such dataset ops to a
31 //    non-parallel version. As a performance optimization, in certain cases this
32 //    pass will instead move nondeterministic ops to a separate non-parallel Map
33 //    op, so that most of the ops can still run in parallel.
34 //
35 // 2. Certain datasets, such as Prefetch, can introduce asynchrony by running a
36 //    dataset iterator in a background thread while ops outside the dataset are
37 //    also running. This can introduce nondeterminism if the input pipeline has
38 //    certain stateful ops. Other than Prefetch, datasets with a
39 //    `num_parallel_calls` argument also introduce asynchrony, which includes
40 //    the parallel datasets mentioned in (1) above.
41 //
42 //    This pass modifies nodes to remove asynchrony when there are any datasets
43 //    in the graph with problematic stateful ops. This is done by converting
44 //    parallel ops into non-parallel versions, as in (1), and by removing
45 //    Prefetch nodes. Unlike (1), legacy random ops such as RandomUniform are
46 //    not problematic despite being stateful, as if the op is within a dataset's
47 //    function, ops outside the dataset cannot access the state. Also unlike
48 //    (1), nondeterministic ops are never moved to a separate Map op, since
49 //    doing so would not remove asynchrony.
50 //
51 // 3. Nondeterminism occurs if an op has a "deterministic" attribute that is
52 //    false or a "sloppy" attribute that is true. This pass changes such
53 //    attributes to be deterministic.
54 class MakeDeterministic : public TFDataOptimizerBase {
55  public:
56   MakeDeterministic() = default;
57   ~MakeDeterministic() override = default;
58 
name()59   string name() const override { return "make_deterministic"; };
60 
UsesFunctionLibrary()61   bool UsesFunctionLibrary() const override { return false; }
62 
Init(const tensorflow::RewriterConfig_CustomGraphOptimizer * config)63   Status Init(
64       const tensorflow::RewriterConfig_CustomGraphOptimizer* config) override {
65     return OkStatus();
66   }
67 
68   Status OptimizeAndCollectStats(Cluster* cluster, const GrapplerItem& item,
69                                  GraphDef* output,
70                                  OptimizationStats* stats) override;
71 };
72 
73 }  // namespace grappler
74 }  // namespace tensorflow
75 
76 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_MAKE_DETERMINISTIC_H_
77