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/transforms/passes_detail.h"
23 #include "tensorflow/compiler/mlir/tensorflow/utils/device_util.h"
24 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_rewrite_device_util.h"
25
26 namespace mlir {
27 namespace TFTPU {
28
29 namespace {
30
31 constexpr char kDeviceAttr[] = "device";
32 constexpr char kXlaOutsideCompilationAttr[] = "_xla_outside_compilation";
33
34 struct OutsideCompiledToHostLaunchPass
35 : public TF::OutsideCompiledToHostLaunchPassBase<
36 OutsideCompiledToHostLaunchPass> {
37 void runOnOperation() override;
38 };
39
WrapOpInLaunch(Operation * host_op,llvm::StringRef host_device)40 void WrapOpInLaunch(Operation* host_op, llvm::StringRef host_device) {
41 OpBuilder builder(host_op);
42
43 auto launch_op = builder.create<tf_device::LaunchOp>(
44 host_op->getLoc(), builder.getStringAttr(host_device),
45 /*result_types=*/host_op->getResultTypes());
46 host_op->replaceAllUsesWith(launch_op);
47
48 launch_op.body().push_back(new Block);
49 builder.setInsertionPointToEnd(&launch_op.GetBody());
50 auto* return_op =
51 builder
52 .create<tf_device::ReturnOp>(host_op->getLoc(), host_op->getResults())
53 .getOperation();
54 MLIRContext* context = launch_op.getContext();
55 host_op->removeAttr(Identifier::get(kXlaOutsideCompilationAttr, context));
56 host_op->removeAttr(Identifier::get(kDeviceAttr, context));
57 host_op->moveBefore(return_op);
58 }
59
runOnOperation()60 void OutsideCompiledToHostLaunchPass::runOnOperation() {
61 // Get runtime devices information from the closest parent module.
62 auto module = getOperation();
63 mlir::TF::RuntimeDevices devices;
64 if (failed(tensorflow::GetDevicesFromOp(module, &devices)))
65 return signalPassFailure();
66
67 auto result = module.walk([&](tf_device::ClusterOp tpu_cluster) {
68 auto inner_result = tpu_cluster.walk([&](Operation* op) {
69 if (op->hasAttrOfType<StringAttr>(kXlaOutsideCompilationAttr))
70 return WalkResult::interrupt();
71 return WalkResult::advance();
72 });
73 if (inner_result.wasInterrupted()) {
74 if (tensorflow::HasModelParallelism(tpu_cluster)) {
75 tpu_cluster.emitOpError(
76 "outside compilation is not supported with model parallelism.");
77 return WalkResult::interrupt();
78 }
79 std::string host_device;
80 if (failed(tensorflow::GetHostDeviceOutsideComputation(
81 devices, tpu_cluster, &host_device)))
82 return WalkResult::interrupt();
83 tpu_cluster.walk([&](Operation* op) {
84 if (op->hasAttrOfType<StringAttr>(kXlaOutsideCompilationAttr))
85 WrapOpInLaunch(op, host_device);
86 });
87 }
88 return WalkResult::advance();
89 });
90 if (result.wasInterrupted()) return signalPassFailure();
91 }
92
93 } // anonymous namespace
94
95 std::unique_ptr<OperationPass<ModuleOp>>
CreateOutsideCompiledToHostLaunchPass()96 CreateOutsideCompiledToHostLaunchPass() {
97 return std::make_unique<OutsideCompiledToHostLaunchPass>();
98 }
99
100 } // namespace TFTPU
101 } // namespace mlir
102