• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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 file implements device assignment in TF dialect.
17 #include "mlir/IR/Builders.h"
18 #include "mlir/Pass/Pass.h"
19 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
20 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h"
21 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
22 
23 namespace mlir {
24 namespace TF {
25 namespace {
26 
27 constexpr char kDeviceAttr[] = "device";
28 constexpr char kTFDeviceAttr[] = "tf.device";
29 
30 class SimpleTFDeviceAssignmentPass
31     : public SimpleTFDeviceAssignmentPassBase<SimpleTFDeviceAssignmentPass> {
32  public:
33   SimpleTFDeviceAssignmentPass() = default;
SimpleTFDeviceAssignmentPass(const SimpleTFDeviceAssignmentPass &)34   SimpleTFDeviceAssignmentPass(const SimpleTFDeviceAssignmentPass&) {}
SimpleTFDeviceAssignmentPass(llvm::StringRef default_device)35   explicit SimpleTFDeviceAssignmentPass(llvm::StringRef default_device) {
36     default_device_ = std::string(default_device);
37   }
38 
runOnOperation()39   void runOnOperation() override {
40     Builder builder(&getContext());
41     Dialect* tf = getContext().getLoadedDialect<TensorFlowDialect>();
42     getOperation().walk([&](Operation* op) {
43       if (auto device_attr = op->getAttrOfType<StringAttr>(kDeviceAttr)) {
44         // We assign default device to ops with device attribute that is empty.
45         if (device_attr.getValue().empty()) {
46           op->setAttr(kDeviceAttr, builder.getStringAttr(default_device_));
47         }
48       } else if (op->getDialect() == tf) {
49         // Assign default device to all ops in Tensorflow dialect that do not
50         // have device attribute.
51         op->setAttr(kDeviceAttr, builder.getStringAttr(default_device_));
52       }
53     });
54   }
55 };
56 
57 // A pass to perform device assignment for TF dialect ops that do not
58 // have device assignment, by using the device attribute of the function.
59 // If device attribute is not found from the function, nothing is done.
60 class TFDeviceAssignmentByFuncAttrPass
61     : public TFDeviceAssignmentByFuncAttrPassBase<
62           TFDeviceAssignmentByFuncAttrPass> {
63  public:
64   TFDeviceAssignmentByFuncAttrPass() = default;
TFDeviceAssignmentByFuncAttrPass(const TFDeviceAssignmentByFuncAttrPass &)65   TFDeviceAssignmentByFuncAttrPass(const TFDeviceAssignmentByFuncAttrPass&) {}
66 
runOnOperation()67   void runOnOperation() override {
68     func::FuncOp func = getOperation();
69     auto func_device_attr = func->getAttrOfType<StringAttr>(kTFDeviceAttr);
70 
71     // Skip device assignment if there is no device specified in the function
72     // attribute.
73     if (!func_device_attr || func_device_attr.getValue().empty()) {
74       return;
75     }
76 
77     Builder builder(&getContext());
78     Dialect* tf = getContext().getLoadedDialect<TensorFlowDialect>();
79     getOperation().walk([&](Operation* op) {
80       if (auto device_attr = op->getAttrOfType<StringAttr>(kDeviceAttr)) {
81         // Assign device to ops with device attribute that is empty.
82         if (device_attr.getValue().empty()) {
83           op->setAttr(kDeviceAttr,
84                       builder.getStringAttr(func_device_attr.getValue()));
85         }
86       } else if (op->getDialect() == tf) {
87         // Assign device to all ops in Tensorflow dialect that do not have
88         // device attribute.
89         op->setAttr(kDeviceAttr,
90                     builder.getStringAttr(func_device_attr.getValue()));
91       }
92     });
93   }
94 };
95 }  // namespace
96 
CreateSimpleTFDeviceAssignmentPass(llvm::StringRef default_device)97 std::unique_ptr<OperationPass<func::FuncOp>> CreateSimpleTFDeviceAssignmentPass(
98     llvm::StringRef default_device) {
99   return std::make_unique<SimpleTFDeviceAssignmentPass>(default_device);
100 }
101 
102 std::unique_ptr<OperationPass<func::FuncOp>>
CreateTFDeviceAssignmentByFuncAttrPass()103 CreateTFDeviceAssignmentByFuncAttrPass() {
104   return std::make_unique<TFDeviceAssignmentByFuncAttrPass>();
105 }
106 
107 }  // namespace TF
108 }  // namespace mlir
109