xref: /aosp_15_r20/external/tensorflow/tensorflow/core/common_runtime/device_propagation.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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_COMMON_RUNTIME_DEVICE_PROPAGATION_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_PROPAGATION_H_
18 
19 #include <functional>
20 #include <string>
21 
22 #include "absl/container/flat_hash_set.h"
23 #include "tensorflow/core/graph/graph.h"
24 #include "tensorflow/core/platform/stringpiece.h"
25 
26 namespace tensorflow {
27 
28 namespace device_propagation {
29 
30 typedef std::function<bool(StringPiece)> DeviceFilter;
31 typedef std::function<bool(const Node&)> NodeFilter;
32 }  // namespace device_propagation
33 
34 // Propagates device assignments from a certain types of nodes to their outputs
35 // to avoid unnecessary D2H or H2D copies.
36 // If an node satisfies the following conditions, it will be placed on the same
37 // device as its inputs:
38 //   (1) The node can accept device update (`node_filter` returns true).
39 //   (2) The node itself has no requested or assigned devices.
40 //   (3) The source nodes of this node's input edges, except for edges that are
41 //   "LoopCond->Switch" or "Enter->Merge", are all placed on the same device.
42 //   (4) The device can be propagated (`device_filter` returns true)
43 void PropagateDevices(const device_propagation::NodeFilter& node_filter,
44                       const device_propagation::DeviceFilter& device_filter,
45                       Graph* graph);
46 
47 }  // namespace tensorflow
48 
49 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_DEVICE_PROPAGATION_H_
50