-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathPrefetch.cpp
461 lines (403 loc) · 15.8 KB
/
Prefetch.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//===----------------------------------------------------------------------===//
//
// This pass tries to prefetch operands (a and b) of tt.dot.
// Those ConvertLayoutOps will be lowered to shared memory loads.
//
// For example:
// %a: tensor<128x32xf16, #enc>
// scf.for %iv = ... iter_args(%a_arg = %a, ...) {
// %d = tt.dot %a_arg, %b, %c
// ...
// scf.yield %a_next, ...
// }
//
// will be translated to
//
// %a: tensor<128x32xf16, #enc>
// %a_tmp = tensor.subview %a[0, 0] [128, 16]
// %a_prefetch = ttg.local_load %a_tmp
// scf.for %iv = ... iter_args(%a_buf = %a, ..., %a_prefetch_arg = %a_prefetch)
// {
// %x = tt.dot %a_prefetch_arg, %b, %c
// %a_tmp_rem = tensor.subview %a_buf[0, 16] [128, 16]
// %a_prefetch_next = ttg.local_load %a_tmp_rem
// ...
// scf.yield %next_a, ..., %a_prefetch_next
// }
//===----------------------------------------------------------------------===//
#include "mlir/IR/IRMapping.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "triton/Dialect/TritonGPU/IR/Dialect.h"
#include "triton/Dialect/TritonGPU/Transforms/Passes.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "tritongpu-prefetch"
#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")
#define LDBG(X) LLVM_DEBUG(DBGS() << X << "\n")
namespace mlir {
namespace triton {
namespace gpu {
#define GEN_PASS_DEF_TRITONGPUPREFETCH
#include "triton/Dialect/TritonGPU/Transforms/Passes.h.inc"
namespace {
class Prefetcher {
/// cache the ForOp we are working on
scf::ForOp forOp;
/// cache the YieldOp of this ForOp
scf::YieldOp yieldOp;
///
// TODO: add a hook to infer prefetchWidth
unsigned prefetchWidth = 32;
/// dots to be prefetched
SetVector<triton::DotOp> dots;
/// dot => dot operand
DenseMap<Value, Value> dot2aLoopArg;
DenseMap<Value, Value> dot2aHeaderDef;
DenseMap<Value, Value> dot2bLoopArg;
DenseMap<Value, Value> dot2bHeaderDef;
DenseMap<Value, Value> dot2aYield;
DenseMap<Value, Value> dot2bYield;
DenseMap<Value, SmallVector<Value>> dot2aVals;
DenseMap<Value, SmallVector<Value>> dot2bVals;
/// operand => defining
DenseMap<Value, Value> operand2headPrefetch;
LogicalResult isForOpOperand(Value v);
Value generatePrefetch(Value v, unsigned opIdx, bool isPrologue,
Attribute dotEncoding, OpBuilder &builder,
std::optional<int64_t> offsetK = std::nullopt,
std::optional<int64_t> shapeK = std::nullopt);
void cloneElementwiseOps(Value &bRem, const SmallVector<Value> &vals,
OpBuilder &builder);
public:
Prefetcher() = delete;
Prefetcher(scf::ForOp forOp) : forOp(forOp) {
yieldOp = cast<scf::YieldOp>(forOp.getBody()->getTerminator());
}
LogicalResult initialize();
void emitPrologue();
scf::ForOp createNewForOp();
};
void Prefetcher::cloneElementwiseOps(Value &ret, const SmallVector<Value> &vals,
OpBuilder &builder) {
IRMapping mapping;
mapping.map(vals[1], ret);
for (int i = 2; i < vals.size(); i++) {
Value v = vals[i];
Value curr = builder.clone(*v.getDefiningOp(), mapping)->getResult(0);
if (isa<RankedTensorType>(curr.getType())) {
auto retType = RankedTensorType::get(
cast<RankedTensorType>(ret.getType()).getShape(),
cast<RankedTensorType>(curr.getType()).getElementType(),
cast<RankedTensorType>(curr.getDefiningOp()->getOperand(0).getType())
.getEncoding());
curr.setType(retType);
}
mapping.map(v, curr);
}
if (vals.size() > 1)
ret = mapping.lookup(vals.back());
}
Value Prefetcher::generatePrefetch(Value v, unsigned opIdx, bool isPrologue,
Attribute dotEncoding, OpBuilder &builder,
std::optional<int64_t> offsetK,
std::optional<int64_t> shapeK) {
// opIdx: 0 => a, 1 => b
auto type = cast<triton::gpu::MemDescType>(v.getType());
SmallVector<int64_t> shape{type.getShape().begin(), type.getShape().end()};
auto rank = shape.size();
SmallVector<int64_t> offset(rank, 0);
Type elementType = type.getElementType();
// k => (prefetchWidth, k - prefetchWidth)
int64_t kIdx = opIdx == 0 ? rank - 1 : rank - 2;
offset[kIdx] = isPrologue ? 0 : prefetchWidth;
shape[kIdx] = isPrologue ? prefetchWidth : (shape[kIdx] - prefetchWidth);
if (shapeK)
shape[kIdx] = *shapeK;
if (offsetK)
offset[kIdx] = *offsetK;
SmallVector<Value> offsetsVal;
for (int64_t off : offset)
offsetsVal.push_back(
builder.create<arith::ConstantIntOp>(v.getLoc(), off, 32));
Value newSmem = builder.create<triton::gpu::MemDescSubviewOp>(
v.getLoc(),
triton::gpu::MemDescType::get(
shape, elementType, type.getEncoding(), type.getMemorySpace(),
type.getMutableMemory(), type.getAllocShape()),
v, offsetsVal);
auto dotOperandEnc = triton::gpu::DotOperandEncodingAttr::get(
builder.getContext(), opIdx, dotEncoding, prefetchWidth / 8);
Value prefetchSlice = builder.create<triton::gpu::LocalLoadOp>(
v.getLoc(), RankedTensorType::get(shape, elementType, dotOperandEnc),
newSmem);
return prefetchSlice;
}
LogicalResult Prefetcher::initialize() {
Block *loop = forOp.getBody();
auto getEncoding = [](Value v) {
return cast<TensorOrMemDesc>(v.getType()).getEncoding();
};
SmallVector<triton::DotOp> dotsInFor;
for (Operation &op : *loop)
if (auto dotOp = dyn_cast<triton::DotOp>(op)) {
// Only accepts dotOps encoded as Nvidia MMA v2 or AMD MFMA
auto dstMmaEnc =
dyn_cast<NvidiaMmaEncodingAttr>(getEncoding(dotOp.getResult()));
auto dstMfmaEnc =
dyn_cast<AMDMfmaEncodingAttr>(getEncoding(dotOp.getResult()));
if (!dstMfmaEnc && (!dstMmaEnc || dstMmaEnc.getVersionMajor() != 2))
// Don't rewrite if any other type is found.
return failure();
dotsInFor.push_back(dotOp);
}
if (dotsInFor.empty())
return failure();
// TODO: segfault (original for still has uses)
// when used in flash attention that has 2 dots in the loop
if (dotsInFor.size() > 1)
return failure();
// returns source of cvt
auto getPrefetchSrc = [](Value v) -> SmallVector<Value> {
// walk back to conversion
Operation *op = v.getDefiningOp();
bool foundConvertFromShared = false;
SmallVector<Value> rets;
rets.push_back(op->getResult(0));
LDBG("Prefetch src: " << *op);
while (op) {
if (op->getNumOperands() != 1)
break;
if (!op->getResult(0).hasOneUse())
break;
rets.push_back(op->getOperand(0));
if (auto cvt = dyn_cast<triton::gpu::LocalLoadOp>(op)) {
// NYI for other encodings, for example if we have transpose
// in the chain
if (isa<DotOperandEncodingAttr>(cvt.getType().getEncoding()))
foundConvertFromShared = true;
break;
}
op = op->getOperand(0).getDefiningOp();
if (op)
LDBG("op: " << *op);
}
std::reverse(rets.begin(), rets.end());
if (foundConvertFromShared)
return rets;
return {};
};
auto getIncomingOp = [this](Value v) -> Value {
if (auto arg = mlir::dyn_cast<BlockArgument>(v))
if (arg.getOwner()->getParentOp() == forOp.getOperation())
return forOp.getTiedLoopInit(arg)->get();
return Value();
};
auto getYieldOperand = [this](Value v) -> Value {
auto arg = mlir::cast<BlockArgument>(v);
unsigned yieldIdx = arg.getArgNumber() - forOp.getNumInductionVars();
return yieldOp.getOperand(yieldIdx);
};
for (triton::DotOp dot : dotsInFor) {
auto aType = dot.getA().getType();
auto bType = dot.getB().getType();
auto aEnc =
mlir::cast<triton::gpu::DotOperandEncodingAttr>(aType.getEncoding());
auto bEnc =
mlir::cast<triton::gpu::DotOperandEncodingAttr>(bType.getEncoding());
int aKWidth = aEnc.getKWidth();
int bKWidth = bEnc.getKWidth();
assert(aKWidth == bKWidth);
auto kSize = aType.getShape().back();
// works better with nvidia tensor cores
unsigned elementWidth = aType.getElementTypeBitWidth();
if (aKWidth == 0)
prefetchWidth = 256 / elementWidth;
else
prefetchWidth = 8 * aKWidth;
// Skip prefetching if kSize is less than prefetchWidth
if (kSize < prefetchWidth)
continue;
auto aVals = getPrefetchSrc(dot.getA());
auto bVals = getPrefetchSrc(dot.getB());
if (aVals.size() && bVals.size()) {
Value aSmem = aVals.front();
Value bSmem = bVals.front();
Value aHeaderDef = getIncomingOp(aSmem);
Value bHeaderDef = getIncomingOp(bSmem);
// Only prefetch loop arg
if (aHeaderDef && bHeaderDef) {
dots.insert(dot);
dot2aVals[dot] = aVals;
dot2bVals[dot] = bVals;
dot2aHeaderDef[dot] = aHeaderDef;
dot2bHeaderDef[dot] = bHeaderDef;
dot2aLoopArg[dot] = aSmem;
dot2bLoopArg[dot] = bSmem;
dot2aYield[dot] = getYieldOperand(aSmem);
dot2bYield[dot] = getYieldOperand(bSmem);
}
}
}
return success();
}
void Prefetcher::emitPrologue() {
OpBuilder builder(forOp);
for (triton::DotOp dot : dots) {
Attribute dotEncoding = dot.getType().getEncoding();
Value aPrefetched =
generatePrefetch(dot2aHeaderDef[dot], 0, true, dotEncoding, builder);
cloneElementwiseOps(aPrefetched, dot2aVals[dot], builder);
Value bPrefetched =
generatePrefetch(dot2bHeaderDef[dot], 1, true, dotEncoding, builder);
cloneElementwiseOps(bPrefetched, dot2bVals[dot], builder);
operand2headPrefetch[dot.getA()] = aPrefetched;
operand2headPrefetch[dot.getB()] = bPrefetched;
}
}
scf::ForOp Prefetcher::createNewForOp() {
OpBuilder builder(forOp);
SmallVector<Value> loopArgs;
for (auto v : forOp.getInitArgs())
loopArgs.push_back(v);
for (triton::DotOp dot : dots) {
loopArgs.push_back(operand2headPrefetch[dot.getA()]);
loopArgs.push_back(operand2headPrefetch[dot.getB()]);
}
auto newForOp = builder.create<scf::ForOp>(
forOp.getLoc(), forOp.getLowerBound(), forOp.getUpperBound(),
forOp.getStep(), loopArgs);
builder.setInsertionPointToStart(newForOp.getBody());
IRMapping mapping;
for (const auto &arg : llvm::enumerate(forOp.getRegionIterArgs()))
mapping.map(arg.value(), newForOp.getRegionIterArgs()[arg.index()]);
mapping.map(forOp.getInductionVar(), newForOp.getInductionVar());
// The insertion point should be placed before the yield op
auto setInsertionPointBeforeYield = [](OpBuilder &builder,
scf::ForOp newForOp) {
if (newForOp.getBody()->mightHaveTerminator()) {
builder.setInsertionPoint(newForOp.getBody()->getTerminator());
} else {
builder.setInsertionPointToEnd(newForOp.getBody());
}
};
for (Operation &op : forOp.getBody()->without_terminator()) {
// If we're currently trying to sink a prefetched dot, we need to stop
// sinking it (by resetting the insertion point to the end) if we find
// control flow, or anything that depends on the dot op.
if (op.getNumRegions() > 0) {
setInsertionPointBeforeYield(builder, newForOp);
}
for (auto operand : op.getOperands()) {
if (auto def = operand.getDefiningOp()) {
auto dot = dyn_cast<triton::DotOp>(def);
if (dot && dots.contains(dot)) {
setInsertionPointBeforeYield(builder, newForOp);
}
}
}
Operation *newOp = builder.clone(op, mapping);
auto dot = dyn_cast<triton::DotOp>(&op);
if (dot && dots.contains(dot)) {
Attribute dotEncoding = dot.getType().getEncoding();
// prefetched dot
Operation *firstDot = builder.clone(*dot, mapping);
if (Value a = operand2headPrefetch.lookup(dot.getA()))
firstDot->setOperand(
0, newForOp.getTiedLoopRegionIterArg(&*a.use_begin()));
if (Value b = operand2headPrefetch.lookup(dot.getB()))
firstDot->setOperand(
1, newForOp.getTiedLoopRegionIterArg(&*b.use_begin()));
// remaining part
int64_t kOff = prefetchWidth;
int64_t kRem = dot.getA().getType().getShape().back() - prefetchWidth;
Operation *prevDot = firstDot;
if (kRem == 0) {
// There is only one dot while prefetchWidth == kSize so delay issuing
// it. Meanwhile, newOp should be set to firstDot to make sure the dot
// result is updated to yield.
builder.setInsertionPoint(prevDot);
newOp = firstDot;
}
while (kRem != 0) {
// int64_t kShape = largestPow2(kRem);
int64_t kShape = prefetchWidth;
auto insertionPoint = builder.saveInsertionPoint();
builder.setInsertionPoint(prevDot);
Value aRem =
generatePrefetch(mapping.lookup(dot2aLoopArg[dot]), 0, false,
dotEncoding, builder, kOff, kShape);
cloneElementwiseOps(aRem, dot2aVals[dot], builder);
Value bRem =
generatePrefetch(mapping.lookup(dot2bLoopArg[dot]), 1, false,
dotEncoding, builder, kOff, kShape);
cloneElementwiseOps(bRem, dot2bVals[dot], builder);
builder.restoreInsertionPoint(insertionPoint);
newOp = builder.clone(*dot, mapping);
newOp->setOperand(0, aRem);
newOp->setOperand(1, bRem);
newOp->setOperand(2, prevDot->getResult(0));
prevDot = newOp;
kOff += kShape;
kRem -= kShape;
if (kRem == 0) {
// We want to delay issuing the last dot as long as possible, ideally
// until after the prefetch. To accomplish this, set the insertion
// point above the dot. If we find anything dependent on the dot (at
// the top of this loop), we resume inserting after it.
builder.setInsertionPoint(prevDot);
}
}
}
// update mapping of results
for (unsigned dstIdx : llvm::seq(unsigned(0), op.getNumResults()))
mapping.map(op.getResult(dstIdx), newOp->getResult(dstIdx));
}
// prefetch next iteration
SmallVector<Value> yieldValues;
for (Value v : forOp.getBody()->getTerminator()->getOperands())
yieldValues.push_back(mapping.lookupOrDefault(v));
for (triton::DotOp dot : dots) {
Attribute dotEncoding = dot.getType().getEncoding();
Value aToYield = generatePrefetch(mapping.lookup(dot2aYield[dot]), 0, true,
dotEncoding, builder);
cloneElementwiseOps(aToYield, dot2aVals[dot], builder);
yieldValues.push_back(aToYield);
// bToYield
Value bToYield = generatePrefetch(mapping.lookup(dot2bYield[dot]), 1, true,
dotEncoding, builder);
cloneElementwiseOps(bToYield, dot2bVals[dot], builder);
yieldValues.push_back(bToYield);
}
// Update ops of yield
builder.setInsertionPointToEnd(newForOp.getBody());
if (!yieldValues.empty())
builder.create<scf::YieldOp>(yieldOp.getLoc(), yieldValues);
return newForOp;
}
} // anonymous namespace
struct PrefetchPass : public impl::TritonGPUPrefetchBase<PrefetchPass> {
void runOnOperation() override {
// Canonicalize convert ops to make the pattern matching easier.
RewritePatternSet cleanUpPatterns(&getContext());
triton::gpu::ConvertLayoutOp::getCanonicalizationPatterns(cleanUpPatterns,
&getContext());
if (mlir::applyPatternsGreedily(getOperation(), std::move(cleanUpPatterns))
.failed()) {
signalPassFailure();
}
getOperation()->walk([&](scf::ForOp forOp) {
Prefetcher prefetcher(forOp);
if (prefetcher.initialize().failed())
return;
prefetcher.emitPrologue();
scf::ForOp newForOp = prefetcher.createNewForOp();
// replace the original loop
for (unsigned i = 0; i < forOp->getNumResults(); ++i)
forOp->getResult(i).replaceAllUsesWith(newForOp->getResult(i));
forOp->erase();
});
}
};
} // namespace gpu
} // namespace triton
} // namespace mlir