1//===- ShardingInterfaceImpl.cpp - Arith sharding models ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mlir/Dialect/Arith/Extensions/ShardingInterfaceImpl.h"
10#include "mlir/Dialect/Arith/IR/Arith.h"
11#include "mlir/Dialect/Shard/Interfaces/ShardingInterface.h"
12#include "mlir/Dialect/Shard/Interfaces/ShardingInterfaceImpl.h"
13#include "mlir/IR/DialectRegistry.h"
14
15using namespace mlir;
16using namespace mlir::arith;
17using namespace mlir::shard;
18
19namespace {
20// Sharding of arith.constant
21// RankedTensor constants can be sharded like any other tensor.
22// %cst = arith.constant dense<0.000000e+00> : tensor<1024x1024xf32>
23// %sharding = shard.sharding @grid4x4 split_axes = [[0]] : !shard.sharding
24// Scalar constants are always replicated and need no sharding annotation.
25
26struct ConstantShardingInterface
27 : public ShardingInterface::ExternalModel<ConstantShardingInterface,
28 ConstantOp> {
29 SmallVector<utils::IteratorType> getLoopIteratorTypes(Operation *op) const {
30 auto ndims = 0;
31 if (auto type = dyn_cast<RankedTensorType>(Val: op->getResult(idx: 0).getType())) {
32 ndims = type.getRank();
33 }
34 return SmallVector<utils::IteratorType>(ndims,
35 utils::IteratorType::parallel);
36 }
37
38 SmallVector<AffineMap> getIndexingMaps(Operation *op) const {
39 if (auto type = dyn_cast<RankedTensorType>(Val: op->getResult(idx: 0).getType())) {
40 return SmallVector<AffineMap>(1, {AffineMap::getMultiDimIdentityMap(
41 numDims: type.getRank(), context: op->getContext())});
42 }
43 return {};
44 }
45
46 // Indicate failure if no result sharding exists.
47 // Otherwise mirror result sharding if it is a tensor constant.
48 // Otherwise return replication option.
49 FailureOr<ShardingOption>
50 getShardingOption(Operation *op, ArrayRef<Sharding> operandShardings,
51 ArrayRef<Sharding> resultShardings) const {
52 assert(resultShardings.size() == 1 &&
53 "Expecting exactly one result sharding for arith.constant");
54 const auto &resultSharding = resultShardings[0];
55 if (!resultSharding) {
56 return failure();
57 }
58 if (auto type = dyn_cast<RankedTensorType>(Val: op->getResult(idx: 0).getType())) {
59 ShardingArray axesArray(resultSharding.getSplitAxes().size());
60 for (auto [i, axes] : llvm::enumerate(First: resultSharding.getSplitAxes())) {
61 axesArray[i].append(in_start: axes.asArrayRef().begin(), in_end: axes.asArrayRef().end());
62 }
63 return ShardingOption(axesArray, resultSharding.getGridAttr());
64 }
65 return ShardingOption({}, resultSharding.getGridAttr());
66 }
67
68 LogicalResult partition(Operation *op, ArrayRef<Value> partitiondOperands,
69 ArrayRef<Sharding> operandShardings,
70 ArrayRef<Sharding> resultShardings,
71 IRMapping &partitionMap,
72 SymbolTableCollection &symbolTable,
73 OpBuilder &builder) const {
74 auto cOp = cast<ConstantOp>(Val: op);
75 if (auto value = dyn_cast<DenseTypedElementsAttr>(Val: cOp.getValue())) {
76 if (!value.isSplat() || !resultShardings[0]) {
77 // Currently non-splat constants are not supported.
78 return failure();
79 }
80 const auto &sharding = resultShardings[0];
81 auto newType = cast<RankedTensorType>(Val: shardType(
82 type: cOp.getType(), grid: getGrid(op, gridSymbol: sharding.getGridAttr(), symbolTableCollection&: symbolTable),
83 sharding));
84 auto newValue = value.resizeSplat(newType);
85 auto newOp = ConstantOp::create(builder, location: op->getLoc(), result: newType, value: newValue);
86 partitionMap.map(from: op->getResult(idx: 0), to: newOp.getResult());
87 partitionMap.map(from: op, to: newOp.getOperation());
88 } else {
89 // `clone` will populate the mapping of old to new results.
90 (void)builder.clone(op&: *op, mapper&: partitionMap);
91 }
92 return success();
93 }
94};
95} // namespace
96
97void mlir::arith::registerShardingInterfaceExternalModels(
98 DialectRegistry &registry) {
99
100 registry.addExtension(extensionFn: +[](MLIRContext *ctx, ArithDialect *dialect) {
101 ConstantOp::template attachInterface<ConstantShardingInterface>(context&: *ctx);
102 });
103}
104