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 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Support/FormatVariadic.h"
20 #include "mlir/Pass/Pass.h" // from @llvm-project
21 #include "mlir/Pass/PassRegistry.h" // from @llvm-project
22 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
23 #include "tensorflow/compiler/mlir/tensorflow/transforms/tf_device_passes_detail.h"
24 #include "tensorflow/compiler/mlir/tensorflow/utils/device_util.h"
25 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_rewrite_device_util.h"
26
27 namespace mlir {
28 namespace TFDevice {
29
30 namespace {
31
32 constexpr char kDeviceAttr[] = "device";
33 constexpr char kXlaOutsideCompilationAttr[] = "_xla_outside_compilation";
34
35 struct HostLaunchToOutsideCompiledPass
36 : public HostLaunchToOutsideCompiledPassBase<
37 HostLaunchToOutsideCompiledPass> {
38 void runOnOperation() override;
39 };
40
41 // Assign all ops in region with _xla_outside_compilation attribute.
MarkOutsideCompiledInRegion(Region & region)42 void MarkOutsideCompiledInRegion(Region& region) {
43 region.walk([&](Operation* op) {
44 op->setAttr(kXlaOutsideCompilationAttr,
45 StringAttr::get(op->getContext(), "from_launch"));
46 });
47 }
48
HoistOpsAndAnnotateWithOutsideCompilation(tf_device::LaunchOp launch)49 void HoistOpsAndAnnotateWithOutsideCompilation(tf_device::LaunchOp launch) {
50 // Forward launch inner op results to launch op results.
51 launch.replaceAllUsesWith(launch.GetBody().getTerminator()->getOperands());
52
53 // For all inner ops, assign the launch device as a `device` attribute.
54 MarkOutsideCompiledInRegion(launch.body());
55
56 // Move all inner ops of the launch to the block containing the launch.
57 auto body = launch.GetBody().without_terminator();
58 Operation* launch_op = launch.getOperation();
59 launch_op->getBlock()->getOperations().splice(
60 launch_op->getIterator(), launch.GetBody().getOperations(), body.begin(),
61 body.end());
62
63 launch.erase();
64 }
65
runOnOperation()66 void HostLaunchToOutsideCompiledPass::runOnOperation() {
67 ModuleOp module = getOperation();
68 mlir::TF::RuntimeDevices devices;
69 if (failed(tensorflow::GetDevicesFromOp(module, &devices)))
70 return signalPassFailure();
71
72 module.walk([&](tf_device::ClusterOp tpu_cluster) {
73 std::string host_device;
74 // If there is model parallelism, we return early since
75 // GetHostDeviceOutsideComputation will fail and an error should have been
76 // returned in an earlier pass.
77 // TODO(b/186420116): Remove this check once outside compilation and model
78 // parallelism work together.
79 if (tensorflow::HasModelParallelism(tpu_cluster)) return;
80 if (failed(tensorflow::GetHostDeviceOutsideComputation(devices, tpu_cluster,
81 &host_device)))
82 return signalPassFailure();
83 tpu_cluster.walk([&](tf_device::LaunchOp launch) {
84 StringAttr device_attr = launch->getAttrOfType<StringAttr>(kDeviceAttr);
85 if (!device_attr) return;
86 if (!device_attr.getValue().equals(host_device)) return;
87 HoistOpsAndAnnotateWithOutsideCompilation(launch);
88 });
89 });
90 }
91
92 } // anonymous namespace
93
94 std::unique_ptr<OperationPass<ModuleOp>>
CreateHostLaunchToOutsideCompiledPass()95 CreateHostLaunchToOutsideCompiledPass() {
96 return std::make_unique<HostLaunchToOutsideCompiledPass>();
97 }
98
99 } // namespace TFDevice
100 } // namespace mlir
101