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 | #include "llvm/ADT/STLExtras.h" |
17 | #include "llvm/ADT/StringRef.h" |
18 | #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project |
19 | #include "mlir/IR/Attributes.h" // from @llvm-project |
20 | #include "mlir/IR/Builders.h" // from @llvm-project |
21 | #include "mlir/IR/Operation.h" // from @llvm-project |
22 | #include "mlir/IR/Visitors.h" // from @llvm-project |
23 | #include "mlir/Transforms/Passes.h" // from @llvm-project |
24 | #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h" |
25 | #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" |
26 | #include "tensorflow/dtensor/cc/constants.h" |
27 | #include "tensorflow/dtensor/cc/tensor_layout.h" |
28 | #include "tensorflow/dtensor/mlir/dtensor_mlir_passes.h" |
29 | #include "tensorflow/dtensor/mlir/ir/tf_dtensor.h" |
30 | |
31 | namespace tensorflow { |
32 | namespace dtensor { |
33 | |
34 | namespace { |
35 | #define GEN_PASS_DEF_DTENSORDESIGNATERESOURCEHANDLEMESH |
36 | #include "tensorflow/dtensor/mlir/dtensor_passes.h.inc" |
37 | |
38 | mlir::LogicalResult SetMeshForResourceCreatingCluster( |
39 | mlir::tf_device::ClusterOp cluster, mlir::OpBuilder* builder) { |
40 | auto result = cluster.walk([](mlir::Operation* op) { |
41 | if (llvm::isa<mlir::TF::VarHandleOp, mlir::TF::DestroyResourceOp>(op)) |
42 | return mlir::WalkResult::interrupt(); |
43 | return mlir::WalkResult::advance(); |
44 | }); |
45 | |
46 | if (!result.wasInterrupted()) return mlir::success(); |
47 | |
48 | const auto& cluster_ops = cluster.GetBody().without_terminator(); |
49 | |
50 | bool has_single_tf_op = |
51 | llvm::count_if(cluster_ops, [](auto& operation) { |
52 | return !llvm::isa<mlir::TF::DTensorLayout>(&operation); |
53 | }) == 1; |
54 | |
55 | if (!has_single_tf_op) { |
56 | return cluster.emitOpError( |
57 | "cluster containing tf.VarHandleOp/DestroyResourceOp must contain " |
58 | "single operation and a terminator"); |
59 | } |
60 | |
61 | if (!cluster->hasAttr(kMeshAttr)) { |
62 | cluster->setAttr(kMeshAttr, builder->getStringAttr(Mesh::kEmptyMeshString)); |
63 | } |
64 | return mlir::success(); |
65 | } |
66 | |
67 | struct DTensorDesignateResourceHandleMesh |
68 | : public impl::DTensorDesignateResourceHandleMeshBase< |
69 | DTensorDesignateResourceHandleMesh> { |
70 | void runOnOperation() override { |
71 | mlir::MLIRContext& context = getContext(); |
72 | mlir::OpBuilder builder(&context); |
73 | |
74 | auto walk_result = |
75 | getOperation().walk([&](mlir::tf_device::ClusterOp cluster) { |
76 | if (mlir::failed( |
77 | SetMeshForResourceCreatingCluster(cluster, &builder))) |
78 | return mlir::WalkResult::interrupt(); |
79 | return mlir::WalkResult::advance(); |
80 | }); |
81 | |
82 | if (walk_result.wasInterrupted()) return signalPassFailure(); |
83 | } |
84 | }; |
85 | |
86 | } // namespace |
87 | |
88 | std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>> |
89 | CreateDTensorDesignateResourceHandleMesh() { |
90 | return std::make_unique<DTensorDesignateResourceHandleMesh>(); |
91 | } |
92 | |
93 | } // namespace dtensor |
94 | } // namespace tensorflow |
95 |