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/IR/BuiltinOps.h" // from @llvm-project 17 #include "mlir/Pass/PassManager.h" // from @llvm-project 18 #include "mlir/Transforms/Passes.h" // from @llvm-project 19 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h" 20 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h" 21 22 // This pass eliminate `_tpu_replicate` and `device` attribute on operations 23 // that are contained in a tf_device.cluster op. 24 25 namespace mlir { 26 namespace TFTPU { 27 28 namespace { 29 30 constexpr char kTPUReplicateAttr[] = "_tpu_replicate"; 31 constexpr char kDeviceAttr[] = "device"; 32 33 class TPUCleanupClusterAttributesPass 34 : public PassWrapper<TPUCleanupClusterAttributesPass, 35 OperationPass<ModuleOp>> { 36 public: runOnOperation()37 void runOnOperation() override { 38 getOperation().walk([](tf_device::ClusterOp cluster) { 39 cluster.walk([](Operation *op) { 40 if (isa<tf_device::ClusterOp>(op)) return; 41 for (StringRef attr : {kTPUReplicateAttr, kDeviceAttr}) 42 op->removeAttr(attr); 43 }); 44 }); 45 } 46 }; 47 48 PassRegistration<TPUCleanupClusterAttributesPass> pass( 49 "tf-tpu-cleanup-cluster-attributes", 50 "Eliminate _tpu_replicate and other attributes from ops in a cluster"); 51 52 } // namespace 53 54 std::unique_ptr<OperationPass<ModuleOp>> CreateTPUClusterCleanupAttributesPass()55CreateTPUClusterCleanupAttributesPass() { 56 return std::make_unique<TPUCleanupClusterAttributesPass>(); 57 } 58 59 } // namespace TFTPU 60 } // namespace mlir 61