• 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 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/Casting.h"
19 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
20 #include "mlir/IR/Attributes.h"  // from @llvm-project
21 #include "mlir/IR/Block.h"  // from @llvm-project
22 #include "mlir/IR/Builders.h"  // from @llvm-project
23 #include "mlir/IR/BuiltinOps.h"  // from @llvm-project
24 #include "mlir/IR/Operation.h"  // from @llvm-project
25 #include "mlir/IR/Value.h"  // from @llvm-project
26 #include "mlir/Pass/Pass.h"  // from @llvm-project
27 #include "mlir/Pass/PassRegistry.h"  // from @llvm-project
28 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
29 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
30 #include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h"
31 #include "tensorflow/compiler/mlir/tensorflow/transforms/tf_device_passes_detail.h"
32 
33 namespace mlir {
34 namespace TFDevice {
35 
36 namespace {
37 
38 constexpr char kReplicationAttr[] = "mhlo.is_same_data_across_replicas";
39 constexpr char kMirroredVariableIndicesAttr[] = "_mirrored_variable_indices";
40 
41 // Analyzes the inputs to ClusterFuncOps in the module, and annotates their
42 // invoked functions whether each input has the same data across replicas.
43 struct AnnotateParameterReplicationPass
44     : public AnnotateParameterReplicationPassBase<
45           AnnotateParameterReplicationPass> {
46   void runOnOperation() override;
47 };
48 
49 // Returns the first value in the chain of operands, which is not defined by a
50 // tf.IdentityOp or a tf.ReadVariableOp.
SkipIdentityAndReadVariable(Value v)51 Value SkipIdentityAndReadVariable(Value v) {
52   while (auto op = v.getDefiningOp()) {
53     if (!isa<TF::IdentityOp, TF::ReadVariableOp>(op)) break;
54     v = op->getOperand(0);
55   }
56   return v;
57 }
58 
runOnOperation()59 void AnnotateParameterReplicationPass::runOnOperation() {
60   ModuleOp m = getOperation();
61   OpBuilder builder(m.getContext());
62   m.walk([&](tf_device::ClusterFuncOp cluster_func) {
63     auto replicate = cluster_func->getParentOfType<tf_device::ReplicateOp>();
64     if (!replicate) return;
65     auto mirrored_variable_indices_attr =
66         replicate->getAttrOfType<ArrayAttr>(kMirroredVariableIndicesAttr);
67     llvm::SmallDenseSet<int64_t, 8> mirrored_replicate_args;
68     if (mirrored_variable_indices_attr) {
69       for (const auto& mirrored_index : mirrored_variable_indices_attr) {
70         mirrored_replicate_args.insert(
71             mirrored_index.cast<IntegerAttr>().getInt());
72       }
73     }
74     auto func = llvm::cast<func::FuncOp>(m.lookupSymbol(cluster_func.func()));
75     for (auto entry : llvm::enumerate(cluster_func.getOperands())) {
76       auto operand = SkipIdentityAndReadVariable(entry.value());
77       auto block_arg = operand.dyn_cast<BlockArgument>();
78       if (block_arg && block_arg.getOwner() == &replicate.GetBody()) {
79         // Only mirrored args of ReplicateOp can be annotated.
80         if (mirrored_replicate_args.count(block_arg.getArgNumber()) == 0) {
81           continue;
82         }
83       } else if (!operand.getParentRegion()->isProperAncestor(
84                      &replicate.body())) {
85         // Not a replication-invariant operand.
86         continue;
87       }
88       func.setArgAttr(entry.index(), kReplicationAttr, builder.getUnitAttr());
89     }
90   });
91 }
92 
93 }  // namespace
94 
95 std::unique_ptr<OperationPass<ModuleOp>>
CreateAnnotateParameterReplicationPass()96 CreateAnnotateParameterReplicationPass() {
97   return std::make_unique<AnnotateParameterReplicationPass>();
98 }
99 
100 }  // namespace TFDevice
101 }  // namespace mlir
102