• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This pass folds the tf.Identity op if the operation has the same device as
17 // its operand.
18 
19 #include "mlir/Dialect/StandardOps/IR/Ops.h"
20 #include "mlir/Pass/PassManager.h"
21 #include "mlir/Transforms/DialectConversion.h"
22 #include "mlir/Transforms/Passes.h"
23 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
24 #include "mlir/IR/OperationSupport.h"  // from @llvm-project
25 #include "mlir/IR/Types.h"  // from @llvm-project
26 #include "mlir/Pass/PassOptions.h"  // from @llvm-project
27 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
28 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h"
29 #include "tensorflow/compiler/mlir/tensorflow/utils/convert_tensor.h"
30 
31 namespace mlir {
32 namespace TF {
33 namespace {
34 
35 constexpr const char *kDeviceAttr = "device";
36 constexpr const char *kTFDeviceAttr = "tf.device";
37 
38 class TensorDeviceCopyConversionPass
39     : public PassWrapper<TensorDeviceCopyConversionPass, FunctionPass> {
40  public:
runOnFunction()41   void runOnFunction() override {
42     FuncOp func_op = getFunction();
43     StringAttr empty_string = StringAttr::get(func_op.getContext(), "");
44     func_op.walk([&](TF::IdentityOp op) {
45       StringAttr arg_device = empty_string;
46       mlir::Value arg = op.getOperand();
47       if (BlockArgument block_arg = arg.dyn_cast<BlockArgument>()) {
48         // Skip the folding logic if the block argument is not from the function
49         // arguments. This can happen when the argument is from a while loop.
50         if (block_arg.getParentRegion() != &func_op.getRegion()) {
51           return WalkResult::advance();
52         }
53         if (StringAttr attr = func_op.getArgAttrOfType<StringAttr>(
54                 block_arg.getArgNumber(), kTFDeviceAttr)) {
55           arg_device = attr;
56         }
57       } else if (StringAttr attr =
58                      arg.getDefiningOp()->getAttrOfType<StringAttr>(
59                          kDeviceAttr)) {
60         arg_device = attr;
61       }
62 
63       StringAttr op_device = op->getAttrOfType<StringAttr>(kDeviceAttr);
64       if (!op_device) op_device = empty_string;
65       // Skip the folding logic if the argument's device is different from the
66       // operation's device.
67       if (op_device != arg_device) return WalkResult::advance();
68 
69       op.replaceAllUsesWith(op.getOperand());
70       op.erase();
71       return WalkResult::advance();
72     });
73   }
74 };
75 
76 }  // namespace
77 
78 std::unique_ptr<OperationPass<mlir::FuncOp>>
CreateTensorDeviceCopyConversionPass()79 CreateTensorDeviceCopyConversionPass() {
80   return std::make_unique<TensorDeviceCopyConversionPass>();
81 }
82 
83 static mlir::PassRegistration<TensorDeviceCopyConversionPass>
84     tensor_device_copy_pass(
85         "tf-tensor-device-copy",
86         "Fold the tf.Identity op if the op has the same device as its operand");
87 
88 }  // namespace TF
89 }  // namespace mlir
90