forked from triton-lang/triton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTMAUtilities.cpp
More file actions
308 lines (271 loc) · 10.3 KB
/
TMAUtilities.cpp
File metadata and controls
308 lines (271 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <triton/Dialect/TritonNvidiaGPU/IR/Dialect.h>
#include <triton/Dialect/TritonNvidiaGPU/Transforms/TMAUtilities.h>
#include <triton/Tools/LayoutUtils.h>
namespace tt = mlir::triton;
namespace ttg = mlir::triton::gpu;
namespace mlir::triton::nvidia_gpu {
ttg::CGAEncodingAttr updateCGALayoutForShape(ttg::CGAEncodingAttr cgaLayout,
ArrayRef<int64_t> shape) {
auto rank = shape.size();
if (cgaLayout.getRank() == rank)
return cgaLayout;
auto ctx = cgaLayout.getContext();
if (cgaLayout.getRank() > rank) {
auto ll = cgaLayout.getLinearLayout();
// Broadcast over the first rankDiff dims
unsigned rankDiff = cgaLayout.getRank() - rank;
for (int i = 0; i < rankDiff; ++i) {
ll = removeStandardDim(ll, 0);
}
return ttg::CGAEncodingAttr::get(ctx, std::move(ll));
}
// For rank-reducing loads, we need to rank-increase the CTA Layout
auto rankDiff = rank - cgaLayout.getRank();
for (unsigned i = 0; i < rankDiff; ++i) {
assert(shape[i] == 1 && "Should only happen for rank-reducing loads");
}
auto ll = cgaLayout.getLinearLayout();
auto kBlock = *ll.getInDimNames().begin();
auto standardOuts = standardOutDimNames(ctx, rank);
// Append to front
for (int i = cgaLayout.getRank(); i < rank; ++i) {
ll = LinearLayout::identity1D(1, kBlock, standardOuts[i]) * ll;
}
// Rename out dims to dim0..dimn-1
auto dimSizes = ll.getOutDims();
for (auto [i, dim] : llvm::enumerate(standardOuts)) {
dimSizes[i].first = dim;
}
ll = LinearLayout(ll.getBases(), dimSizes, false);
return ttg::CGAEncodingAttr::get(ctx, std::move(ll));
}
ttg::SharedEncodingTrait
updateEncodingForShape(Operation *op, ttg::SharedEncodingTrait encoding,
RankedTensorType tensorType) {
auto ctx = encoding.getContext();
auto cgaLayout = ttg::getCGALayout(encoding);
if (auto nvmmaEnc = dyn_cast<ttg::NVMMASharedEncodingAttr>(encoding)) {
auto existingCga = nvmmaEnc.getCGALayout();
if (!existingCga)
return nvmmaEnc;
auto newCgaEnc = updateCGALayoutForShape(cgaLayout, tensorType.getShape());
return ttg::NVMMASharedEncodingAttr::get(
ctx, nvmmaEnc.getSwizzlingByteWidth(), nvmmaEnc.getTransposed(),
nvmmaEnc.getElementBitWidth(), nvmmaEnc.getFp4Padded(), newCgaEnc);
}
if (auto swizEnc = dyn_cast<ttg::SwizzledSharedEncodingAttr>(encoding)) {
auto existingCga = swizEnc.getCGALayout();
if (!existingCga)
return swizEnc;
auto rank = tensorType.getRank();
auto oldOrder = swizEnc.getOrder();
SmallVector<unsigned> order;
for (int i = 0; i + oldOrder.size() < rank; ++i)
order.push_back(rank - i - 1);
for (int i = 0; i < oldOrder.size(); ++i) {
// If it is a rank-reducing load, we need to drop the last dimensions.
if (oldOrder[i] >= rank)
continue;
order.push_back(oldOrder[i]);
}
auto newCgaEnc = updateCGALayoutForShape(cgaLayout, tensorType.getShape());
return ttg::SwizzledSharedEncodingAttr::get(
ctx, swizEnc.getVec(), swizEnc.getPerPhase(), swizEnc.getMaxPhase(),
order, newCgaEnc);
}
constexpr auto msg = "Internal Error: Unhandled tensor descriptor encoding";
if (op)
op->emitError() << msg;
llvm::report_fatal_error(msg);
}
ttg::SharedEncodingTrait getEncodingFromDescriptor(Operation *op,
RankedTensorType tensorType,
Value desc) {
auto descBlockType = cast<TensorDescType>(desc.getType()).getBlockType();
Attribute encoding = descBlockType.getEncoding();
if (!encoding) {
constexpr auto msg =
"Internal Error: Tensor descriptor should have encoding set";
if (op)
op->emitError() << msg;
llvm::report_fatal_error(msg);
}
auto sharedEnc = cast<ttg::SharedEncodingTrait>(encoding);
if (descBlockType.getShape() == tensorType.getShape())
return sharedEnc;
return updateEncodingForShape(op, sharedEnc, tensorType);
}
static FailureOr<int> getTMASwizzleModeImpl(Location loc,
RankedTensorType blockType) {
auto encoding = blockType.getEncoding();
auto mmaEncoding = dyn_cast<ttg::NVMMASharedEncodingAttr>(encoding);
unsigned swizzleBytes = mmaEncoding ? mmaEncoding.getSwizzlingByteWidth() : 0;
if (!mmaEncoding) {
auto swizzledEnc = dyn_cast<ttg::SwizzledSharedEncodingAttr>(encoding);
if (!swizzledEnc || swizzledEnc.getVec() != 1 ||
swizzledEnc.getPerPhase() != 1 || swizzledEnc.getMaxPhase() != 1) {
return emitError(loc)
<< "unhandled shared memory layout for TMA descriptor: "
<< encoding;
}
}
bool fp4Padded = isFp4Padded(encoding);
if (fp4Padded && swizzleBytes != 128) {
return emitError(loc) << "fp4 padded operands (elem type .b4x16_p64) only "
"supports 128-byte swizzling, but got "
<< swizzleBytes;
}
int32_t swizzleMode = 0;
if (swizzleBytes == 128) {
swizzleMode = 3;
} else if (swizzleBytes == 64) {
swizzleMode = 2;
} else if (swizzleBytes == 32) {
swizzleMode = 1;
} else {
assert(swizzleBytes == 0);
}
return swizzleMode;
}
FailureOr<int> getTMASwizzleMode(Location loc, TensorDescType ty) {
return getTMASwizzleModeImpl(loc, ty.getBlockType());
}
FailureOr<int> getTMASwizzleMode(Location loc, TensorDescIm2ColType ty) {
return getTMASwizzleModeImpl(loc, ty.getBlockType());
}
enum TMA_ELEMENT_TYPES {
TMA_U8 = 0,
TMA_U16 = 1,
TMA_U32 = 2,
TMA_S32 = 3,
TMA_U64 = 4,
TMA_S64 = 5,
TMA_F16 = 6,
TMA_F32 = 7,
TMA_F32_FTZ = 8,
TMA_F64 = 9,
TMA_BF16 = 10,
TMA_TF32 = 11,
TMA_TF32_FTZ = 12,
TMA_B4X16 = 13,
TMA_B4X16_P64 = 14,
TMA_B6X16_P32 = 15,
TMA_B6P2X16 = 15,
};
static FailureOr<int> getTMAElementTypeImpl(Location loc,
RankedTensorType blockType) {
auto encoding = blockType.getEncoding();
bool fp4Padded = isFp4Padded(encoding);
if (fp4Padded)
return TMA_B4X16_P64;
auto elemTy = blockType.getElementType();
if (elemTy.isBF16()) {
return TMA_BF16;
} else if (elemTy.isF16()) {
return TMA_F16;
} else if (elemTy.isF32()) {
return TMA_F32;
} else if (elemTy.isF64()) {
return TMA_F64;
}
auto elemSize = elemTy.getIntOrFloatBitWidth() / 8;
switch (elemSize) {
case 1:
return TMA_U8;
case 2:
return TMA_U16;
case 4:
return elemTy.isSignedInteger() ? TMA_S32 : TMA_U32;
case 8:
return elemTy.isSignedInteger() ? TMA_S64 : TMA_U64;
default:
break;
}
return emitError(loc)
<< "Tensor descriptor element type must have size 1, 2, or 4 but got "
<< elemSize;
}
FailureOr<int> getTMAElementType(Location loc, TensorDescType ty) {
return getTMAElementTypeImpl(loc, ty.getBlockType());
}
FailureOr<int> getTMAElementType(Location loc, TensorDescIm2ColType ty) {
return getTMAElementTypeImpl(loc, ty.getBlockType());
}
LogicalResult createTMADesc(Value tmaPtr, MakeTensorDescOp op,
OpBuilder &builder) {
using namespace mlir;
MLIRContext *ctx = op.getContext();
auto loc = op.getLoc();
auto mkI32Constant = [&](int32_t val) {
return arith::ConstantOp::create(builder, loc, builder.getI32Type(),
builder.getI32IntegerAttr(val));
};
auto elemType = op.getBase().getType().getPointeeType();
auto elemSize = elemType.getIntOrFloatBitWidth() / 8;
auto encoding = op.getType().getBlockType().getEncoding();
auto mmaEncoding =
llvm::dyn_cast_or_null<gpu::NVMMASharedEncodingAttr>(encoding);
bool fp4Padded = mmaEncoding && mmaEncoding.getFp4Padded();
int paddingScale = fp4Padded ? 2 : 1;
auto shapePerCTA = gpu::getShapePerCTA(encoding, op.getTensorShape());
// MakeTensorDescOp creates tiled descriptors (not im2col)
auto blockShape = getTMABlockShape(encoding, shapePerCTA,
/*packedSize=*/false, gpu::TMAMode::Tiled);
auto contigDimSize = blockShape.back();
llvm::SmallVector<Value> boxDim;
if (fp4Padded && contigDimSize != 128) {
return op->emitError(
"FP4 padded loads require 128 elements or more in the last dim");
}
boxDim.push_back(mkI32Constant(contigDimSize));
for (int k = shapePerCTA.size() - 2; k >= 0; --k)
boxDim.push_back(mkI32Constant(blockShape[k]));
unsigned swizzleBytes = mmaEncoding ? mmaEncoding.getSwizzlingByteWidth() : 0;
if (!mmaEncoding) {
auto swizzledEnc = dyn_cast<gpu::SwizzledSharedEncodingAttr>(
op.getType().getBlockType().getEncoding());
if (!swizzledEnc || swizzledEnc.getVec() != 1 ||
swizzledEnc.getPerPhase() != 1 || swizzledEnc.getMaxPhase() != 1) {
op->emitError() << "Unhandled encoding type";
return failure();
}
}
auto maybeSwizzleMode = getTMASwizzleMode(loc, op.getType());
if (failed(maybeSwizzleMode))
return failure();
auto swizzleMode = *maybeSwizzleMode;
Value elemSizeVal = arith::ConstantOp::create(
builder, loc, builder.getI64Type(), builder.getI64IntegerAttr(elemSize));
SmallVector<Value> globalDim(llvm::reverse(op.getShape()));
SmallVector<Value> globalStride;
for (int k = op.getStrides().size() - 2; k >= 0; --k) {
globalStride.push_back(op.getStrides()[k]);
}
if (fp4Padded) {
// Convert number of bytes to number of mxfp4 elements
globalDim[0] =
arith::MulIOp::create(builder, loc, globalDim[0], mkI32Constant(2));
}
SmallVector<Value> elementStride(globalDim.size(), mkI32Constant(1));
for (int i = 0; i < globalStride.size(); ++i)
globalStride[i] =
arith::MulIOp::create(builder, loc, globalStride[i], elemSizeVal);
auto elemTypeEnum = getTMAElementType(loc, op.getType());
if (failed(elemTypeEnum))
return failure();
auto fillMode = (op.getPadding() == triton::PaddingOption::PAD_NAN) ? 1 : 0;
TensormapCreateOp::create(
builder, loc,
/*desc_ptr=*/tmaPtr,
/*global_address=*/op.getBase(),
/*box_dim=*/boxDim,
/*global_dim=*/globalDim,
/*global_stride=*/globalStride,
/*element_strides=*/elementStride,
/*elem_type*/ builder.getI32IntegerAttr(*elemTypeEnum),
/*interleave_layout*/ builder.getI32IntegerAttr(0),
/*swizzle_mode=*/builder.getI32IntegerAttr(swizzleMode),
/*fill_mode=*/builder.getI32IntegerAttr(fillMode));
return success();
}
} // namespace mlir::triton::nvidia_gpu