1//===- ShardingInterfaceImpl.cpp - Tensor 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/Tensor/Extensions/ShardingInterfaceImpl.h"
10#include "mlir/Dialect/Shard/Interfaces/ShardingInterface.h"
11#include "mlir/Dialect/Shard/Interfaces/ShardingInterfaceImpl.h"
12#include "mlir/Dialect/Tensor/IR/Tensor.h"
13#include "mlir/IR/DialectRegistry.h"
14
15using namespace mlir;
16using namespace mlir::tensor;
17using namespace mlir::shard;
18
19namespace {
20// Sharding of tensor.empty/tensor.splat
21template <typename OpTy>
22struct CreatorOpShardingInterface
23 : public ShardingInterface::ExternalModel<CreatorOpShardingInterface<OpTy>,
24 OpTy> {
25 SmallVector<utils::IteratorType> getLoopIteratorTypes(Operation *op) const {
26 auto ndims = mlir::cast<ShapedType>(Val: op->getResult(idx: 0).getType()).getRank();
27 return SmallVector<utils::IteratorType>(ndims,
28 utils::IteratorType::parallel);
29 }
30
31 SmallVector<AffineMap> getIndexingMaps(Operation *op) const {
32 MLIRContext *ctx = op->getContext();
33 Value val = op->getResult(idx: 0);
34 auto type = dyn_cast<RankedTensorType>(Val: val.getType());
35 if (!type)
36 return {};
37 return SmallVector<AffineMap>(
38 op->getNumOperands() + op->getNumResults(),
39 {AffineMap::getMultiDimIdentityMap(numDims: type.getRank(), context: ctx)});
40 }
41
42 LogicalResult partition(Operation *op, ArrayRef<Value> partitionedOperands,
43 ArrayRef<Sharding> operandShardings,
44 ArrayRef<Sharding> resultShardings,
45 IRMapping &partitionMap,
46 SymbolTableCollection &symbolTable,
47 OpBuilder &builder) const {
48 assert(resultShardings.size() == 1);
49 auto resType = cast<RankedTensorType>(Val: op->getResult(idx: 0).getType());
50 mlir::shard::GridOp grid;
51 ShapedType shardType;
52 if (resType.getRank() > 0) {
53 grid = shard::getGrid(op, gridSymbol: resultShardings[0].getGridAttr(), symbolTableCollection&: symbolTable);
54 shardType =
55 cast<ShapedType>(Val: shard::shardType(type: resType, grid, sharding: resultShardings[0]));
56 } else {
57 shardType = resType;
58 }
59 Operation *newOp = nullptr;
60 // if the sharding introduces a new dynamic dimension, we take it from
61 // the dynamic sharding info. For now bail out if it's not
62 // provided.
63 if (!shardType.hasStaticShape()) {
64 assert(op->getResult(0).hasOneUse());
65 SmallVector<Value> newOperands;
66 auto oldType = cast<ShapedType>(Val&: resType);
67 assert(oldType.getRank() == shardType.getRank());
68 int currOldOprndNum = -1;
69 shard::ShardShapeOp shapeForDevice;
70 ValueRange device;
71 Operation *newSharding = nullptr;
72 for (auto i = 0; i < oldType.getRank(); ++i) {
73 if (!oldType.isDynamicDim(idx: i) && shardType.isDynamicDim(idx: i)) {
74 if (!newSharding) {
75 newSharding =
76 ShardingOp::create(builder, location: op->getLoc(), from: resultShardings[0]);
77 device =
78 shard::ProcessMultiIndexOp::create(builder, location: op->getLoc(), grid)
79 .getResults();
80 shapeForDevice = shard::ShardShapeOp::create(
81 builder, location: op->getLoc(), dims: oldType.getShape(), dims_dyn: partitionedOperands,
82 sharding: newSharding->getResult(idx: 0), device);
83 }
84 newOperands.emplace_back(Args: shapeForDevice.getResult()[i]);
85 } else if (oldType.isDynamicDim(idx: i)) {
86 assert(shardType.isDynamicDim(i));
87 newOperands.emplace_back(Args: partitionedOperands[++currOldOprndNum]);
88 }
89 }
90 newOp = OpTy::create(builder, op->getLoc(), shardType, newOperands);
91 partitionMap.map(from: op->getResult(idx: 0), to: newOp->getResult(idx: 0));
92 } else {
93 // `clone` will populate the mapping of old to new results.
94 newOp = builder.clone(op&: *op, mapper&: partitionMap);
95 }
96 newOp->getResult(idx: 0).setType(shardType);
97
98 return success();
99 }
100};
101} // namespace
102
103void mlir::tensor::registerShardingInterfaceExternalModels(
104 DialectRegistry &registry) {
105
106 registry.addExtension(extensionFn: +[](MLIRContext *ctx, TensorDialect *dialect) {
107 EmptyOp::template attachInterface<CreatorOpShardingInterface<EmptyOp>>(
108 context&: *ctx);
109 SplatOp::template attachInterface<CreatorOpShardingInterface<SplatOp>>(
110 context&: *ctx);
111 });
112}
113