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 // Converts DeviceIndex to constant device.
17 
18 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"  // from @llvm-project
19 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
20 #include "mlir/IR/Attributes.h"  // from @llvm-project
21 #include "mlir/IR/Builders.h"  // from @llvm-project
22 #include "mlir/IR/Operation.h"  // from @llvm-project
23 #include "mlir/IR/PatternMatch.h"  // from @llvm-project
24 #include "mlir/Pass/Pass.h"  // from @llvm-project
25 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
26 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h"
27 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes_detail.h"
28 
29 namespace mlir {
30 namespace TF {
31 namespace {
32 
33 // Folds the DeviceIndex op to a constant value. The DeviceIndex return the
34 // index of the device the op should run on. The user can use this to provide
35 // different op specializations. E.g.,
36 //
37 // ```mlir
38 //  %1 = "tf.DeviceIndex"()
39 //          {device = "", device_names = ["CPU", "GPU"]} : () -> tensor<i32>
40 //  %4 = "tf.Case"(%1, %arg0, %arg1)
41 //          {branches = [@foo, @baz], output_shapes = [#tf_type.shape<>]} :
42 //            (tensor<i32>, tensor<f32>, tensor<f32>) -> tensor<f32>
43 // ```
44 //
45 // Shows an example where there are 2 different functions which could be
46 // executed to produce the same values but with different functions optimized
47 // for CPU or GPU.
48 struct DeviceIndexSelector
49     : public DeviceIndexSelectorPassBase<DeviceIndexSelector> {
50   void runOnOperation() override;
51 };
52 
53 }  // namespace
54 
runOnOperation()55 void DeviceIndexSelector::runOnOperation() {
56   func::FuncOp func = getOperation();
57   // Convert all the DeviceIndex ops to constant values.
58   func.getBody().walk([](TF::DeviceIndexOp op) {
59     // This just selects the default in all cases where DeviceIndex feeds into
60     // tf.Case. This could be enhanced to have some sort of policy in the
61     // future.
62     OpBuilder b(op);
63     RankedTensorType type = RankedTensorType::get({}, b.getIntegerType(32));
64     int index = op.device_names().size();
65     for (auto use : op.getOperation()->getUsers()) {
66       // Skip if it doesn't feed into case. Alternatively this could always
67       // return the CPU device index if it exists.
68       if (!isa<TF::CaseOp>(use)) return;
69     }
70     DenseElementsAttr attr =
71         DenseElementsAttr::get(type, b.getI32IntegerAttr(index));
72     auto constant = b.create<arith::ConstantOp>(op.getLoc(), type, attr);
73     op.replaceAllUsesWith(constant.getOperation());
74     op.erase();
75   });
76 }
77 
78 // Creates an instance of the TensorFlow DeviceIndex selector pass.
CreateDeviceIndexSelectorPass()79 std::unique_ptr<OperationPass<func::FuncOp>> CreateDeviceIndexSelectorPass() {
80   return std::make_unique<DeviceIndexSelector>();
81 }
82 
83 }  // namespace TF
84 }  // namespace mlir
85