1 /* Copyright 2021 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 #include "tensorflow/compiler/mlir/tfrt/transforms/set_shape_invariant_in_while_ops.h" 16 17 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project 18 #include "mlir/Transforms/Passes.h" // from @llvm-project 19 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" 20 21 namespace tensorflow { 22 namespace tfrt_compiler { 23 namespace { 24 25 class SetShapeInvariantInWhileOps 26 : public mlir::PassWrapper<SetShapeInvariantInWhileOps, 27 mlir::OperationPass<mlir::func::FuncOp>> { 28 public: MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SetShapeInvariantInWhileOps)29 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SetShapeInvariantInWhileOps) 30 31 void runOnOperation() override { 32 mlir::func::FuncOp func_op = getOperation(); 33 34 auto shape_invariant = mlir::UnitAttr::get(&getContext()); 35 36 func_op.walk([&](mlir::TF::WhileOp op) { 37 // Skip tf.While op on TPU. 38 if (!op->hasAttr("_tpu_replicate")) { 39 op.shape_invariantAttr(shape_invariant); 40 } 41 }); 42 } 43 }; 44 45 } // namespace 46 47 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>> CreateSetShapeInvariantInWhileOps()48CreateSetShapeInvariantInWhileOps() { 49 return std::make_unique<SetShapeInvariantInWhileOps>(); 50 } 51 52 } // namespace tfrt_compiler 53 } // namespace tensorflow 54