xref: /aosp_15_r20/external/tensorflow/tensorflow/dtensor/mlir/set_default_sharding.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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 <string>
17 
18 #include "llvm/ADT/StringRef.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/Builders.h"  // from @llvm-project
22 #include "mlir/IR/Operation.h"  // from @llvm-project
23 #include "mlir/Transforms/Passes.h"  // from @llvm-project
24 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_device.h"
25 #include "tensorflow/compiler/xla/client/sharding_builder.h"
26 #include "tensorflow/dtensor/mlir/dtensor_mlir_passes.h"
27 #include "tensorflow/dtensor/mlir/dtensor_mlir_passes_classes.h"
28 #include "tensorflow/dtensor/mlir/ir/tf_dtensor.h"
29 
30 namespace tensorflow {
31 namespace dtensor {
32 namespace {
33 
34 // Assigns inputs/outputs for TPU computation to logical core 0.
SetDefaultSharding(mlir::tf_device::ClusterFuncOp cluster,mlir::OpBuilder * builder)35 void SetDefaultSharding(mlir::tf_device::ClusterFuncOp cluster,
36                         mlir::OpBuilder* builder) {
37   const std::string logical_core_0_sharding =
38       xla::sharding_builder::AssignDevice(0).SerializeAsString();
39 
40   llvm::SmallVector<llvm::StringRef, 4> input_sharding(cluster.getNumOperands(),
41                                                        logical_core_0_sharding);
42   llvm::SmallVector<llvm::StringRef, 4> output_sharding(
43       cluster.getNumResults(), logical_core_0_sharding);
44 
45   cluster->setAttr("input_sharding_configuration",
46                    builder->getStrArrayAttr(input_sharding));
47   cluster->setAttr("output_sharding_configuration",
48                    builder->getStrArrayAttr(output_sharding));
49 }
50 
51 // MLIR pass that sets xla sharding of TPU computation input/outputs to
52 // maximally assigned to logical core 0.
53 struct DTensorSetDefaultSharding
54     : public DTensorSetDefaultShardingBase<DTensorSetDefaultSharding> {
runOnOperationtensorflow::dtensor::__anon4559f12f0111::DTensorSetDefaultSharding55   void runOnOperation() override {
56     mlir::MLIRContext& context = getContext();
57     mlir::OpBuilder builder(&context);
58 
59     getOperation().walk([&](mlir::tf_device::ClusterFuncOp cluster_func) {
60       // Skip non-tpu device cluster_func.
61       auto replicate_attr =
62           cluster_func->getAttrOfType<mlir::StringAttr>("_tpu_replicate");
63       if (!replicate_attr) return;
64 
65       SetDefaultSharding(cluster_func, &builder);
66     });
67   }
68 };
69 
70 }  // namespace
71 
72 std::unique_ptr<mlir::OperationPass<mlir::func::FuncOp>>
CreateDTensorSetDefaultSharding()73 CreateDTensorSetDefaultSharding() {
74   return std::make_unique<DTensorSetDefaultSharding>();
75 }
76 
77 }  // namespace dtensor
78 }  // namespace tensorflow
79