-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Backend] Implement scaled_dot(mxfp4, fp8)
#4904
Changes from all commits
881209d
720c4d6
4b27371
5706c7d
b6426ec
e4a4ed9
bd5c2fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#include "mlir/Support/LLVM.h" | ||
#include "triton/Analysis/Utility.h" | ||
#include "triton/Dialect/Triton/IR/Utility.h" | ||
#include "triton/Dialect/TritonGPU/IR/Attributes.h" | ||
#include "triton/Dialect/TritonGPU/IR/Dialect.h" | ||
#include "triton/Dialect/TritonGPU/IR/LinearLayoutConversions.h" | ||
#include "triton/Dialect/TritonGPU/Transforms/Utility.h" | ||
|
@@ -234,8 +235,31 @@ static SmallVector<unsigned> eraseOrder(ArrayRef<unsigned> order, | |
return resOrder; | ||
} | ||
|
||
SmallVector<unsigned> getOrderForDotOperand(unsigned opIdx, unsigned rank, | ||
bool kMajor) { | ||
// kMajor: if true, the matrix is fastest-running on k, | ||
// otherwise it is on m (resp. n) | ||
// opIdx=0: [batch, m, k] if rank == 3 else [m, k] | ||
// opIdx=1: [batch, k, n] if rank == 3 else [k, n] | ||
// batch (if rank == 3) is always the slowest running dimension | ||
assert(rank == 2 || rank == 3); | ||
assert(opIdx == 0 || opIdx == 1); | ||
SmallVector<unsigned> order(rank); | ||
std::iota(order.rbegin(), order.rend(), 0); | ||
// If opIdx is 1 and kMajor is true, the order is [0, 1] | ||
// (resp. [1, 2, 0] if rank == 3) | ||
// Same if opIdx is 0 and kMajor is false | ||
if (bool(opIdx) == kMajor) { | ||
std::swap(order[0], order[1]); | ||
} | ||
return order; | ||
} | ||
|
||
SmallVector<unsigned> getWarpOrder(Attribute layout) { | ||
auto order = getOrder(layout); | ||
// FIXME: This mmaLayout if should just return | ||
// getOrderForDotOperand(0, order.size(), kMajor=false) | ||
// as mma has the same order as DotOperand(opIdx=0) | ||
if (auto mmaLayout = dyn_cast<NvidiaMmaEncodingAttr>(layout)) { | ||
Jokeren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (mmaLayout.isHopper()) { | ||
// Hopper MMA instructions force a warp order of [0, 1]. See docs: | ||
|
@@ -245,40 +269,8 @@ SmallVector<unsigned> getWarpOrder(Attribute layout) { | |
order.insert(order.begin(), 0); | ||
} | ||
} else if (auto dotOpLayout = dyn_cast<DotOperandEncodingAttr>(layout)) { | ||
// opIdx=0: [/*dim0*/batch, /*dim1=*/m, /*dim2=*/k] -> order=[1, 2, 0] | ||
// opIdx=1: [/*dim0*/batch, /*dim1=*/k, /*dim2=*/n] -> order=[2, 1, 0] | ||
std::iota(order.rbegin(), order.rend(), 0); | ||
if (dotOpLayout.getOpIdx() == 0) { | ||
std::swap(order[0], order[1]); | ||
} | ||
} | ||
return order; | ||
} | ||
|
||
SmallVector<unsigned> getOrderForDotOperand(unsigned opIdx, unsigned rank) { | ||
assert((rank == 2 || rank == 3) && | ||
"Invalid rank for dot operand order computation"); | ||
SmallVector<unsigned> order(rank); | ||
// The 'order' field typically represents a descending sorted array of | ||
// dimensions based on contiguity. For instance, in axisInfo utilities that | ||
// retrieve tensor contiguity, it's assumed that the dimension with the | ||
// highest contiguity corresponds to order[0]. | ||
// | ||
// The relation between contiguity and order is only relevant if the layout | ||
// interfaces with HBM, as is the case when we load tensor from HBM to | ||
// registers in the dot layout to bypass LDS. When bypassing LDS, we make | ||
// the following assumptions about tensor layouts: | ||
// - Tensor A (opIdx == 0) is considered to be row-major. | ||
// - Tensor B (opIdx == 1) is considered to be column-major. | ||
// | ||
// Based on these assumptions, we define the following orders: | ||
// - For opIdx == 0, batch=dim0, m=dim1, and k=dim2, we assume an order of [2, | ||
// 1, 0] for 3D tensors. | ||
// - For opIdx == 1, batch=dim0, k=dim1, and n=dim2, we assume an order of [1, | ||
// 2, 0] for 3D tensors. | ||
std::iota(order.rbegin(), order.rend(), 0); | ||
if (opIdx == 1) { | ||
std::swap(order[0], order[1]); | ||
order = getOrderForDotOperand(dotOpLayout.getOpIdx(), order.size(), | ||
/*kMajor*/ false); | ||
Comment on lines
+272
to
+273
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is kMajor always false here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is getting the warp order but not the element order. So m is the fastest changing dimension in opIdx=0. I think confusion may arise from the variable name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a suggestion for improvement though. Maybe just add some additional comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, similarly to in wgmma, we want the warps have the exterior dimension (i.e. not K) as their fastest running dimension. |
||
} | ||
return order; | ||
} | ||
|
@@ -295,8 +287,8 @@ SmallVector<unsigned> getOrder(Attribute layout) { | |
return order; | ||
} | ||
if (auto dotLayout = dyn_cast<DotOperandEncodingAttr>(layout)) { | ||
auto rank = getWarpsPerCTA(dotLayout.getParent()).size(); | ||
return getOrderForDotOperand(dotLayout.getOpIdx(), rank); | ||
auto rank = dotLayout.getWarpsPerCTA().size(); | ||
return getOrderForDotOperand(dotLayout.getOpIdx(), rank, /*kMajor*/ true); | ||
} | ||
if (auto sliceLayout = dyn_cast<SliceEncodingAttr>(layout)) { | ||
SmallVector<unsigned> parentOrder = getOrder(sliceLayout.getParent()); | ||
|
@@ -1048,7 +1040,8 @@ SmallVector<unsigned> DotOperandEncodingAttr::getWarpOrder() const { | |
return ::getWarpOrder(*this); | ||
} | ||
SmallVector<unsigned> DotOperandEncodingAttr::getThreadOrder() const { | ||
return ::getOrder(*this); | ||
return getOrderForDotOperand(getOpIdx(), getWarpsPerCTA().size(), | ||
/*kMajor*/ true); | ||
} | ||
SmallVector<unsigned> DotOperandEncodingAttr::getShapePerCTATile( | ||
ArrayRef<int64_t> tensorShape) const { | ||
|
@@ -2019,6 +2012,7 @@ SmallVector<int64_t> NvidiaMmaEncodingAttr::getMMAv2RepForOperand( | |
ArrayRef<int64_t> shape, int bitwidth, int kWidth, int opIdx) const { | ||
auto rank = shape.size(); | ||
auto warpsPerCTA = getWarpsPerCTA(); | ||
|
||
SmallVector<int> shapePerWarp = {1, 16, 8, 4 * 64 / bitwidth}; | ||
int numRepBatch = | ||
rank == 3 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -406,7 +406,7 @@ class ScaledBlockedToMMAv2 | |
auto ctx = dotOp.getContext(); | ||
|
||
// Check that rhs scale is null | ||
assert(dotOp.getRhsScale() == nullptr && "rhs scale must be null"); | ||
assert(dotOp.getRhsScale() == nullptr && "rhs scale NYI"); | ||
|
||
// operands | ||
auto a = dotOp.getLhs(); | ||
|
@@ -426,10 +426,11 @@ class ScaledBlockedToMMAv2 | |
} | ||
}; | ||
|
||
assert(aType == F8F6F4Type::E4M3 || | ||
aType == F8F6F4Type::E5M2 && "lhs just supports fp8"); | ||
assert((aType == F8F6F4Type::E4M3 || aType == F8F6F4Type::E5M2 || | ||
aType == F8F6F4Type::E2M1) && | ||
"NYI: lhs supports fp4 or fp8"); | ||
assert(bType == F8F6F4Type::E4M3 || | ||
bType == F8F6F4Type::E5M2 && "rhs just supports fp8"); | ||
bType == F8F6F4Type::E5M2 && "NYI: rhs supports fp8"); | ||
|
||
// TODO run accelerate matmul on A and B first to choose their layouts | ||
// Set return type | ||
|
@@ -440,6 +441,7 @@ class ScaledBlockedToMMAv2 | |
auto instrShape = mmaVersionToInstrShape(versionMajor, retShapePerCTA, | ||
rewriter.getBF16Type(), numWarps); | ||
auto CTALayout = getCTALayout(oldRetType.getEncoding()); | ||
// TODO Use warpsPerTileV2 | ||
SmallVector<unsigned> warpsPerCTA = {numWarps, 1}; | ||
auto mmaEnc = NvidiaMmaEncodingAttr::get(ctx, /*versionMajor=*/versionMajor, | ||
/*versionMinor=*/0, warpsPerCTA, | ||
|
@@ -452,27 +454,39 @@ class ScaledBlockedToMMAv2 | |
auto newAcc = | ||
rewriter.create<ConvertLayoutOp>(oldAcc.getLoc(), newRetType, oldAcc); | ||
|
||
auto toMMABf16 = [&newRetType, &rewriter, &ctx, | ||
&enumToType](TypedValue<RankedTensorType> v, int idx, | ||
F8F6F4Type type) { | ||
// MMAv2 Layout | ||
auto toMMABf16 = [&newRetType, &rewriter, &ctx, &enumToType]( | ||
TypedValue<RankedTensorType> v, int idx, | ||
F8F6F4Type type) -> TypedValue<RankedTensorType> { | ||
auto vType = v.getType(); | ||
auto newVEncoding = DotOperandEncodingAttr::get( | ||
ctx, idx, newRetType.getEncoding(), enumToType((type))); | ||
auto newVType = RankedTensorType::get( | ||
v.getType().getShape(), v.getType().getElementType(), newVEncoding); | ||
v = rewriter.create<ConvertLayoutOp>(v.getLoc(), newVType, v); | ||
|
||
// Bitcast | ||
auto vTypeFp8 = RankedTensorType::get( | ||
vType.getShape(), rewriter.getFloat8E4M3FNType(), newVEncoding); | ||
v = cast<TypedValue<RankedTensorType>>( | ||
rewriter.create<BitcastOp>(v.getLoc(), vTypeFp8, v).getResult()); | ||
|
||
// Convert to bf16 | ||
auto vTypeBf16 = RankedTensorType::get( | ||
vType.getShape(), rewriter.getBF16Type(), newVEncoding); | ||
return rewriter.create<FpToFpOp>(v.getLoc(), vTypeBf16, v); | ||
if (type == F8F6F4Type::E2M1) { | ||
// A bit too dynamically typed... | ||
// perhaps return ints in both cases? | ||
|
||
auto retEnc = dyn_cast<NvidiaMmaEncodingAttr>(newRetType.getEncoding()); | ||
auto newVEncoding = DotOperandEncodingAttr::get( | ||
ctx, idx, newRetType.getEncoding(), /*kWidth=*/4); | ||
auto newVType = RankedTensorType::get( | ||
vType.getShape(), vType.getElementType(), newVEncoding); | ||
return rewriter.create<ConvertLayoutOp>(v.getLoc(), newVType, v); | ||
} else { | ||
assert(type == F8F6F4Type::E5M2 || type == F8F6F4Type::E4M3); | ||
auto newVEncoding = DotOperandEncodingAttr::get( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: assert that this is a fp8 type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, although it's a bit redundant, as we are already asserting this at the beginning of the function and in |
||
ctx, idx, newRetType.getEncoding(), /*kWidth=*/8); | ||
auto newVType = RankedTensorType::get( | ||
vType.getShape(), vType.getElementType(), newVEncoding); | ||
v = rewriter.create<ConvertLayoutOp>(v.getLoc(), newVType, v); | ||
|
||
// Bitcast | ||
auto vTypeFp8 = RankedTensorType::get(vType.getShape(), | ||
enumToType(type), newVEncoding); | ||
v = cast<TypedValue<RankedTensorType>>( | ||
rewriter.create<BitcastOp>(v.getLoc(), vTypeFp8, v).getResult()); | ||
|
||
// Convert to bf16 | ||
auto vTypeBf16 = RankedTensorType::get( | ||
vType.getShape(), rewriter.getBF16Type(), newVEncoding); | ||
return rewriter.create<FpToFpOp>(v.getLoc(), vTypeBf16, v); | ||
} | ||
}; | ||
a = toMMABf16(a, 0, aType); | ||
b = toMMABf16(b, 1, bType); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we assume
getElemOrder == getOrder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getThreadOrder
is same asgetOrder
except for AMD'sAMDMfmaEncodingAttr
. I haven't taken a deep investigation.pin @zhanglx13 for expertise maybe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See that I changed the definition of
getThreadOrder
in this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be specific I was referring to:
I'm not sure if we should use
getOrder
orgetThreadOrder
for this encoding