1 /* Copyright 2020 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 "mlir/Pass/Pass.h" // from @llvm-project
20 #include "mlir/Pass/PassRegistry.h" // from @llvm-project
21 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
22 #include "tensorflow/compiler/mlir/tensorflow/utils/device_util.h"
23 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_rewrite_device_util.h"
24
25 namespace mlir {
26 namespace TFTPU {
27
28 namespace {
29
30 constexpr char kDeviceAttr[] = "device";
31 constexpr char kXlaOutsideCompilationAttr[] = "_xla_outside_compilation";
32
33 // This pass wraps ops with the same `_xla_outside_compilation`
34 // attribute value in a tf_device.launch op with host device assignment.
35 //
36 // A simple example:
37 // "tf_device.cluster"() ( {
38 // "tf.A"()
39 // "tf.B"() {_xla_outside_compilation = "cluster1"}
40 // "tf.C"()
41 // tf_device.return
42 // }) {num_cores_per_replica = 1, topology = "", device_assignment = []}
43 //
44 // Would become the following ops (unimportant attribute, type are omitted):
45 // "tf_device.cluster"() ( {
46 // "tf.A"()
47 // "tf_device.launch"() {
48 // "tf.B"() {_xla_outside_compilation = "cluster1"}
49 // tf_device.return
50 // } {device = "TPU_REPLICATED_HOST"} : () -> ()
51 // "tf.C"()
52 // tf_device.return
53 // }) {num_cores_per_replica = 1, topology = "", device_assignment = []}
54 //
55
56 struct OutsideCompiledToHostLaunch
57 : public PassWrapper<OutsideCompiledToHostLaunch, OperationPass<ModuleOp>> {
58 void runOnOperation() override;
59 };
60
WrapOpInLaunch(Operation * host_op,llvm::StringRef host_device)61 void WrapOpInLaunch(Operation* host_op, llvm::StringRef host_device) {
62 OpBuilder builder(host_op);
63
64 auto launch_op = builder.create<tf_device::LaunchOp>(
65 host_op->getLoc(), builder.getStringAttr(host_device),
66 /*result_types=*/host_op->getResultTypes());
67 host_op->replaceAllUsesWith(launch_op);
68
69 launch_op.body().push_back(new Block);
70 builder.setInsertionPointToEnd(&launch_op.GetBody());
71 auto* return_op =
72 builder
73 .create<tf_device::ReturnOp>(host_op->getLoc(), host_op->getResults())
74 .getOperation();
75 MLIRContext* context = launch_op.getContext();
76 host_op->removeAttr(Identifier::get(kXlaOutsideCompilationAttr, context));
77 host_op->removeAttr(Identifier::get(kDeviceAttr, context));
78 host_op->moveBefore(return_op);
79 }
80
runOnOperation()81 void OutsideCompiledToHostLaunch::runOnOperation() {
82 // Get runtime devices information from the closest parent module.
83 auto module = getOperation();
84 mlir::TF::RuntimeDevices devices;
85 if (failed(tensorflow::GetDevicesFromOp(module, &devices)))
86 return signalPassFailure();
87
88 auto result = module.walk([&](tf_device::ClusterOp tpu_cluster) {
89 std::string host_device;
90 (void)tensorflow::GetHostDeviceOutsideComputation(devices, tpu_cluster,
91 &host_device);
92 tpu_cluster.walk([&](Operation* op) {
93 if (op->hasAttrOfType<StringAttr>(kXlaOutsideCompilationAttr))
94 WrapOpInLaunch(op, host_device);
95 });
96 return WalkResult::advance();
97 });
98 if (result.wasInterrupted()) return signalPassFailure();
99 }
100
101 } // anonymous namespace
102
103 std::unique_ptr<OperationPass<ModuleOp>>
CreateOutsideCompiledToHostLaunchPass()104 CreateOutsideCompiledToHostLaunchPass() {
105 return std::make_unique<OutsideCompiledToHostLaunch>();
106 }
107
108 static PassRegistration<OutsideCompiledToHostLaunch> pass(
109 "tf-outside-compiled-to-host-launch",
110 "Wraps each op with the _xla_outside_compiled attribute in "
111 "a separate tf_device.launch on replicated host device.");
112
113 } // namespace TFTPU
114 } // namespace mlir
115