• 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 replicates the tf._TPUCompileMlir op on each host that needs the
17 // compiled program. It helps avoid transferring the compiled binary between
18 // hosts.
19 
20 #include "mlir/IR/Builders.h"
21 #include "mlir/Pass/Pass.h"
22 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
23 
24 namespace mlir {
25 namespace TFTPU {
26 namespace {
27 
28 using DeviceNameUtils = ::tensorflow::DeviceNameUtils;
29 using ParsedName = ::tensorflow::DeviceNameUtils::ParsedName;
30 
31 constexpr char kDeviceAttr[] = "device";
32 constexpr int kStatusResultIndex = 0;
33 constexpr int kProgramResultIndex = 1;
34 
GetHost(Operation * op)35 static std::string GetHost(Operation *op) {
36   if (StringAttr device = op->getAttrOfType<StringAttr>(kDeviceAttr)) {
37     ParsedName parsed_name;
38     DeviceNameUtils::ParseFullName(device.getValue().str(), &parsed_name);
39     return DeviceNameUtils::ParsedNameToString(
40         DeviceNameUtils::AddressSpace(parsed_name));
41   }
42   return "";
43 }
44 
45 class TPUCompileOpReplicationPass
46     : public PassWrapper<TPUCompileOpReplicationPass, OperationPass<ModuleOp>> {
runOnOperation()47   void runOnOperation() override {
48     getOperation().walk([&](TF::_TPUCompileMlirOp tpu_compile_op) {
49       Value compiled_program = tpu_compile_op.getResult(kProgramResultIndex);
50       std::string tpu_compile_op_host = GetHost(tpu_compile_op.getOperation());
51       llvm::StringMap<Operation *> compile_op_by_host;
52       llvm::SmallVector<OpOperand *, 4> usages;
53 
54       for (OpOperand &usage : compiled_program.getUses()) {
55         usages.push_back(&usage);
56       }
57 
58       // For any op which uses the program compiled on a different host than the
59       // original tf._TPUCompileMlir op, replicate the tf._TPUCompileMlir op on
60       // that host and update the op to use the program compiled on the same
61       // host.
62       for (OpOperand *usage : usages) {
63         std::string usage_op_host = GetHost(usage->getOwner());
64         if (usage_op_host == tpu_compile_op_host) continue;
65 
66         Operation *&new_compile_op = compile_op_by_host[usage_op_host];
67         // If it is not already created, create a tf._TPUCompileMlir op and a
68         // tf.TPUCompileSucceededAssert op on the first CPU of the target host.
69         if (!new_compile_op) {
70           std::string device_name = usage_op_host + "/device:CPU:0";
71           OpBuilder builder(tpu_compile_op);
72           new_compile_op = builder.clone(*tpu_compile_op.getOperation());
73           new_compile_op->setAttr(kDeviceAttr,
74                                   StringAttr::get(&getContext(), device_name));
75           TF::TPUCompileSucceededAssertOp new_assert_op =
76               builder.create<TF::TPUCompileSucceededAssertOp>(
77                   new_compile_op->getLoc(),
78                   new_compile_op->getResult(kStatusResultIndex));
79           new_assert_op->setAttr(kDeviceAttr,
80                                  new_compile_op->getAttr(kDeviceAttr));
81         }
82         // Updates the operand to use the result of the newly created
83         // tf._TPUCompileMlir op.
84         usage->set(new_compile_op->getResult(kProgramResultIndex));
85       }
86       return WalkResult::advance();
87     });
88   }
89 };
90 
91 }  // namespace
92 
CreateTPUCompileOpReplicationPass()93 std::unique_ptr<OperationPass<ModuleOp>> CreateTPUCompileOpReplicationPass() {
94   return std::make_unique<TPUCompileOpReplicationPass>();
95 }
96 
97 static PassRegistration<TPUCompileOpReplicationPass> pass(
98     "tf-tpu-compile-replication",
99     "Replicate the TPU compile op to avoid sending the compiled binary between "
100     "hosts.");
101 
102 }  // namespace TFTPU
103 }  // namespace mlir
104