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 <algorithm> 17 18 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project 19 #include "mlir/IR/Builders.h" // from @llvm-project 20 #include "mlir/IR/Operation.h" // from @llvm-project 21 #include "mlir/Interfaces/SideEffectInterfaces.h" // from @llvm-project 22 #include "mlir/Support/DebugStringHelper.h" // from @llvm-project 23 #include "mlir/Support/LogicalResult.h" // from @llvm-project 24 #include "mlir/Transforms/Passes.h" // from @llvm-project 25 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" 26 #include "tensorflow/dtensor/cc/tensor_layout.h" 27 #include "tensorflow/dtensor/mlir/dtensor_mlir_passes.h" 28 #include "tensorflow/dtensor/mlir/dtensor_mlir_passes_classes.h" 29 #include "tensorflow/dtensor/mlir/ir/tf_dtensor.h" 30 31 namespace tensorflow { 32 namespace dtensor { 33 34 namespace { 35 36 // MLIR pass that undoes unintended const merging across different meshes within 37 // the same Block by canonicalization passes. 38 struct DTensorUndoMergeConstAcrossMesh 39 : public DTensorUndoMergeConstAcrossMeshBase< 40 DTensorUndoMergeConstAcrossMesh> { runOnOperationtensorflow::dtensor::__anon2d42e4710111::DTensorUndoMergeConstAcrossMesh41 void runOnOperation() override { 42 mlir::MLIRContext& context = getContext(); 43 mlir::OpBuilder builder(&context); 44 getOperation().walk([&builder](mlir::TF::ConstOp const_op) { 45 llvm::SmallVector<Mesh> known_meshes; 46 llvm::SmallVector<mlir::TF::DTensorLayout> unique_layout_ops; 47 for (mlir::Operation* consumer : const_op->getUsers()) { 48 mlir::TF::DTensorLayout layout_op = 49 mlir::dyn_cast<mlir::TF::DTensorLayout>(consumer); 50 if (!layout_op) continue; 51 52 const Layout layout = layout_op.layout(); // keep-alive for mesh. 53 const Mesh& mesh = layout.mesh(); 54 if (std::find(known_meshes.begin(), known_meshes.end(), mesh) == 55 known_meshes.end()) { 56 if (!known_meshes.empty()) { 57 // We skip the first layout_op to preserve its original ConstOp. 58 unique_layout_ops.push_back(layout_op); 59 } 60 known_meshes.emplace_back(mesh); 61 } 62 } 63 for (auto& layout_op : unique_layout_ops) { 64 builder.setInsertionPoint(layout_op); 65 layout_op->replaceUsesOfWith(const_op, 66 builder.cloneWithoutRegions(const_op)); 67 } 68 }); 69 } 70 }; 71 72 } // namespace 73 74 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>> CreateDTensorUndoMergeConstAcrossMesh()75CreateDTensorUndoMergeConstAcrossMesh() { 76 return std::make_unique<DTensorUndoMergeConstAcrossMesh>(); 77 } 78 } // namespace dtensor 79 } // namespace tensorflow 80