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/dtensor_mlir_passes_classes.h"
30 #include "tensorflow/dtensor/mlir/ir/tf_dtensor.h"
31
32 namespace tensorflow {
33 namespace dtensor {
34 namespace {
35
SetMeshForResourceCreatingCluster(mlir::tf_device::ClusterOp cluster,mlir::OpBuilder * builder)36 mlir::LogicalResult SetMeshForResourceCreatingCluster(
37 mlir::tf_device::ClusterOp cluster, mlir::OpBuilder* builder) {
38 auto result = cluster.walk([](mlir::Operation* op) {
39 if (llvm::isa<mlir::TF::VarHandleOp, mlir::TF::DestroyResourceOp>(op))
40 return mlir::WalkResult::interrupt();
41 return mlir::WalkResult::advance();
42 });
43
44 if (!result.wasInterrupted()) return mlir::success();
45
46 const auto& cluster_ops = cluster.GetBody().without_terminator();
47
48 bool has_single_tf_op =
49 llvm::count_if(cluster_ops, [](auto& operation) {
50 return !llvm::isa<mlir::TF::DTensorLayout>(&operation);
51 }) == 1;
52
53 if (!has_single_tf_op) {
54 return cluster.emitOpError(
55 "cluster containing tf.VarHandleOp/DestroyResourceOp must contain "
56 "single operation and a terminator");
57 }
58
59 if (!cluster->hasAttr(kMeshAttr)) {
60 cluster->setAttr(kMeshAttr, builder->getStringAttr(Mesh::kEmptyMeshString));
61 }
62 return mlir::success();
63 }
64
65 struct DTensorDesignateResourceHandleMesh
66 : public DTensorDesignateResourceHandleMeshBase<
67 DTensorDesignateResourceHandleMesh> {
runOnOperationtensorflow::dtensor::__anon5d395ca20111::DTensorDesignateResourceHandleMesh68 void runOnOperation() override {
69 mlir::MLIRContext& context = getContext();
70 mlir::OpBuilder builder(&context);
71
72 auto walk_result =
73 getOperation().walk([&](mlir::tf_device::ClusterOp cluster) {
74 if (mlir::failed(
75 SetMeshForResourceCreatingCluster(cluster, &builder)))
76 return mlir::WalkResult::interrupt();
77 return mlir::WalkResult::advance();
78 });
79
80 if (walk_result.wasInterrupted()) return signalPassFailure();
81 }
82 };
83
84 } // namespace
85
86 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>>
CreateDTensorDesignateResourceHandleMesh()87 CreateDTensorDesignateResourceHandleMesh() {
88 return std::make_unique<DTensorDesignateResourceHandleMesh>();
89 }
90
91 } // namespace dtensor
92 } // namespace tensorflow
93