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/SmallVector.h"
17 #include "mlir/Analysis/CallGraph.h" // from @llvm-project
18 #include "mlir/Pass/Pass.h" // from @llvm-project
19 #include "mlir/Pass/PassRegistry.h" // from @llvm-project
20 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
21 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
22 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_cluster_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 struct OutsideCompiledToHostLaunchPass
34 : public TF::OutsideCompiledToHostLaunchPassBase<
35 OutsideCompiledToHostLaunchPass> {
36 void runOnOperation() override;
37 };
38
WrapOpInLaunch(Operation * host_op,llvm::StringRef host_device)39 void WrapOpInLaunch(Operation* host_op, llvm::StringRef host_device) {
40 OpBuilder builder(host_op);
41
42 auto launch_op = builder.create<tf_device::LaunchOp>(
43 host_op->getLoc(), builder.getStringAttr(host_device),
44 /*result_types=*/host_op->getResultTypes());
45 host_op->replaceAllUsesWith(launch_op);
46
47 launch_op.body().push_back(new Block);
48 builder.setInsertionPointToEnd(&launch_op.GetBody());
49 auto* return_op =
50 builder
51 .create<tf_device::ReturnOp>(host_op->getLoc(), host_op->getResults())
52 .getOperation();
53 MLIRContext* context = launch_op.getContext();
54 host_op->removeAttr(StringAttr::get(context, kXlaOutsideCompilationAttr));
55 host_op->removeAttr(StringAttr::get(context, kDeviceAttr));
56 host_op->moveBefore(return_op);
57 }
58
runOnOperation()59 void OutsideCompiledToHostLaunchPass::runOnOperation() {
60 // traverse_op is applied to each op reachable from each tf_device::ClusterOp
61 // in the module returned by getOperation().
62 auto traverse_op = [&](Operation* op, tf_device::ClusterOp tpu_cluster,
63 std::optional<std::string> host_device) {
64 // Apply WrapOpInLaunch when the op has _xla_outside_compilation.
65 if (op->hasAttrOfType<StringAttr>(kXlaOutsideCompilationAttr)) {
66 if (!host_device) {
67 tpu_cluster.emitOpError(
68 "outside compilation is not supported with model parallelism.");
69 return WalkResult::interrupt();
70 }
71 WrapOpInLaunch(op, *host_device);
72 }
73 return WalkResult::advance();
74 };
75 if (failed(WalkReachableFromTpuCluster(getOperation(), traverse_op)))
76 return signalPassFailure();
77 }
78
79 } // anonymous namespace
80
81 std::unique_ptr<OperationPass<ModuleOp>>
CreateOutsideCompiledToHostLaunchPass()82 CreateOutsideCompiledToHostLaunchPass() {
83 return std::make_unique<OutsideCompiledToHostLaunchPass>();
84 }
85
86 } // namespace TFTPU
87 } // namespace mlir
88