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 "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project 17 #include "mlir/IR/BuiltinOps.h" // from @llvm-project 18 #include "mlir/Pass/PassManager.h" // from @llvm-project 19 #include "mlir/Transforms/Passes.h" // from @llvm-project 20 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h" 21 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h" 22 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h" 23 #include "tensorflow/compiler/mlir/tensorflow/utils/attribute_utils.h" 24 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_cluster_util.h" 25 #include "tensorflow/compiler/mlir/tensorflow/utils/tpu_rewrite_device_util.h" 26 27 namespace mlir { 28 namespace TFTPU { 29 30 namespace { 31 32 constexpr char kDeviceAttr[] = "device"; 33 constexpr char kClassAttr[] = "_class"; 34 35 class TPUCleanupClusterAttributesPass 36 : public TF::TPUCleanupClusterAttributesPassBase< 37 TPUCleanupClusterAttributesPass> { 38 public: runOnOperation()39 void runOnOperation() override { 40 auto traverse_op = [&](Operation* op, tf_device::ClusterOp tpu_cluster) { 41 if (isa<tf_device::ClusterOp>(op)) return WalkResult::advance(); 42 op->removeAttr(TF::kReplicationInfoAttr); 43 op->removeAttr(TF::kCompileDeviceTypeAttr); 44 // This attribute is used for op colocation. Since all ops are located 45 // on a single device cluster, this private attribute is no longer 46 // needed. 47 op->removeAttr(kClassAttr); 48 if (auto attr = op->getAttrOfType<StringAttr>(kDeviceAttr)) { 49 // Preserve device attribute if the op is placed on a replicated core 50 // device. Device attribute is used to infer the appropriate sharding 51 // within TPUs for this op. 52 // TODO(b/183598857): Use explicit sharding ops from the front-end. 53 // For example, dequeue ops generated by 54 // tensorflow/python/tpu/tpu_feed.py 55 if (!tensorflow::IsTPUReplicatedCore(attr.getValue()) && 56 !isa<tf_device::LaunchOp>(op)) { 57 op->removeAttr(kDeviceAttr); 58 } 59 } 60 return WalkResult::advance(); 61 }; 62 63 if (failed(TFTPU::WalkReachableFromTpuCluster(getOperation(), traverse_op))) 64 return signalPassFailure(); 65 } 66 }; 67 68 } // namespace 69 70 std::unique_ptr<OperationPass<ModuleOp>> CreateTPUClusterCleanupAttributesPass()71CreateTPUClusterCleanupAttributesPass() { 72 return std::make_unique<TPUCleanupClusterAttributesPass>(); 73 } 74 75 } // namespace TFTPU 76 } // namespace mlir 77