2022-08-18 12:49:37 -07:00
|
|
|
#include <numeric>
|
|
|
|
|
2022-05-01 22:06:54 +08:00
|
|
|
#include "mlir/IR/DialectImplementation.h"
|
2022-07-26 10:50:11 -07:00
|
|
|
#include "mlir/IR/OpImplementation.h"
|
Keren/tensor slice insert alloc (#94)
This branch defines three new triton_gpu operations to partially solve #87. Below is an overview:
```
%tensor = triton_gpu.alloc_tensor : tensor<2x16x16xf16, #A>
%b = triton_gpu.insert_slice_async %a_ptr, %tensor, %offset {axis = 0 : i32, cache = 1 : i32, evict = 1 : i32, isVolatile = false} : tensor<16x16x!tt.ptr<f16>, #AL> -> tensor<2x16x16xf16, #A>
%c = triton_gpu.extract_slice %b, %offset {axis = 0 : i32} : tensor<2x16x16xf16, #A> -> tensor<16x16xf16, #A>
```
We plan to fully replace `copy_async` with `insert_slice_async`. **This hasn't been done yet.**
2022-09-01 12:37:17 -07:00
|
|
|
#include "triton/Analysis/Utility.h"
|
2022-08-18 12:49:37 -07:00
|
|
|
#include "triton/Dialect/TritonGPU/IR/Dialect.h"
|
2022-05-01 22:06:54 +08:00
|
|
|
#include "llvm/ADT/TypeSwitch.h"
|
2022-04-28 18:51:31 +08:00
|
|
|
|
|
|
|
#include "triton/Dialect/TritonGPU/IR/Dialect.cpp.inc"
|
|
|
|
|
2022-05-31 11:43:21 +00:00
|
|
|
using namespace mlir;
|
2022-04-28 18:51:31 +08:00
|
|
|
using namespace mlir::triton::gpu;
|
|
|
|
|
2022-08-24 12:55:49 -07:00
|
|
|
// Utility
|
|
|
|
namespace mlir {
|
|
|
|
namespace triton {
|
|
|
|
|
|
|
|
// Type inference
|
|
|
|
static Type getI1SameShape(Type type) {
|
|
|
|
auto i1Type = IntegerType::get(type.getContext(), 1);
|
|
|
|
if (auto tensorType = type.dyn_cast<RankedTensorType>())
|
|
|
|
return RankedTensorType::get(tensorType.getShape(), i1Type,
|
|
|
|
tensorType.getEncoding());
|
|
|
|
return Type();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Type getPointeeType(Type type) {
|
|
|
|
if (auto tensorType = type.dyn_cast<RankedTensorType>()) {
|
|
|
|
// Tensor of pointers
|
|
|
|
auto shape = tensorType.getShape();
|
|
|
|
auto ptrType = tensorType.getElementType().dyn_cast<PointerType>();
|
|
|
|
Type pointeeType = ptrType.getPointeeType();
|
|
|
|
return RankedTensorType::get(shape, pointeeType, tensorType.getEncoding());
|
|
|
|
} else if (auto ptrType = type.dyn_cast<PointerType>()) {
|
|
|
|
// scalar pointer
|
|
|
|
Type pointeeType = ptrType.getPointeeType();
|
|
|
|
return pointeeType;
|
|
|
|
}
|
|
|
|
return Type();
|
|
|
|
}
|
|
|
|
|
2022-09-18 05:58:42 +08:00
|
|
|
namespace gpu {
|
|
|
|
|
|
|
|
// TODO: Inheritation of layout attributes
|
|
|
|
unsigned getElemsPerThread(Attribute layout, ArrayRef<int64_t> shape) {
|
|
|
|
size_t rank = shape.size();
|
|
|
|
if (auto blockedLayout = layout.dyn_cast<BlockedEncodingAttr>()) {
|
|
|
|
return blockedLayout.getElemsPerThread(shape);
|
|
|
|
} else if (auto sliceLayout = layout.dyn_cast<SliceEncodingAttr>()) {
|
|
|
|
return sliceLayout.getElemsPerThread(shape);
|
|
|
|
} else if (auto mmaLayout = layout.dyn_cast<MmaEncodingAttr>()) {
|
|
|
|
return mmaLayout.getElemsPerThread(shape);
|
|
|
|
} else if (auto sharedLayout = layout.dyn_cast<SharedEncodingAttr>()) {
|
|
|
|
return sharedLayout.getElemsPerThread(shape);
|
|
|
|
} else {
|
|
|
|
assert(0 && "getElemsPerThread not implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getShapePerCTA(const Attribute &layout, unsigned d) {
|
|
|
|
if (auto blockedLayout = layout.dyn_cast<BlockedEncodingAttr>()) {
|
|
|
|
return blockedLayout.getSizePerThread()[d] *
|
|
|
|
blockedLayout.getThreadsPerWarp()[d] *
|
|
|
|
blockedLayout.getWarpsPerCTA()[d];
|
|
|
|
} else {
|
|
|
|
assert(0 && "Unimplemented usage of getShapePerCTA");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gpu
|
2022-08-24 12:55:49 -07:00
|
|
|
} // namespace triton
|
|
|
|
} // namespace mlir
|
|
|
|
|
2022-08-11 21:20:47 -07:00
|
|
|
static LogicalResult parseIntAttrValue(AsmParser &parser, const Attribute &attr,
|
|
|
|
unsigned &value, StringRef desc) {
|
|
|
|
auto intAttr = attr.dyn_cast<IntegerAttr>();
|
|
|
|
if (!intAttr) {
|
|
|
|
parser.emitError(parser.getNameLoc(), "expected an integer type in ")
|
|
|
|
<< desc;
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
if (intAttr.getType().isSignedInteger()) {
|
|
|
|
int64_t attrVal = intAttr.getSInt();
|
|
|
|
if (attrVal < 0) {
|
|
|
|
parser.emitError(parser.getNameLoc(),
|
|
|
|
"expected an unsigned integer value in ")
|
|
|
|
<< desc;
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
value = attrVal;
|
|
|
|
} else if (intAttr.getType().isSignlessInteger()) {
|
|
|
|
int64_t attrVal = intAttr.getInt();
|
|
|
|
if (attrVal < 0) {
|
|
|
|
parser.emitError(parser.getNameLoc(),
|
|
|
|
"expected an unsigned integer value in ")
|
|
|
|
<< desc;
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
value = attrVal;
|
|
|
|
} else {
|
|
|
|
value = intAttr.getUInt();
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:43:21 +00:00
|
|
|
// parse an array of integers
|
|
|
|
static LogicalResult parseIntArrayAttr(AsmParser &parser,
|
|
|
|
const NamedAttribute &attr,
|
2022-07-27 01:32:10 -07:00
|
|
|
SmallVector<unsigned, 2> &res,
|
2022-07-26 17:25:03 -07:00
|
|
|
StringRef desc) {
|
2022-05-31 11:43:21 +00:00
|
|
|
auto arrayAttr = attr.getValue().dyn_cast<ArrayAttr>();
|
|
|
|
if (!arrayAttr) {
|
2022-07-26 17:25:03 -07:00
|
|
|
parser.emitError(parser.getNameLoc(), "expected an array for ") << desc;
|
2022-05-31 11:43:21 +00:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
for (Attribute i : arrayAttr) {
|
2022-08-11 21:20:47 -07:00
|
|
|
unsigned value;
|
|
|
|
if (parseIntAttrValue(parser, i, value, desc).failed())
|
2022-05-31 11:43:21 +00:00
|
|
|
return failure();
|
2022-08-11 21:20:47 -07:00
|
|
|
res.push_back(value);
|
2022-05-31 11:43:21 +00:00
|
|
|
}
|
|
|
|
return success();
|
|
|
|
};
|
|
|
|
|
2022-07-31 13:59:44 -07:00
|
|
|
static LogicalResult parseUInt(AsmParser &parser, const NamedAttribute &attr,
|
|
|
|
unsigned &value, StringRef desc) {
|
2022-08-11 21:20:47 -07:00
|
|
|
return parseIntAttrValue(parser, attr.getValue(), value, desc);
|
2022-07-31 13:59:44 -07:00
|
|
|
};
|
|
|
|
|
2022-05-01 22:06:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Attribute methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#define GET_ATTRDEF_CLASSES
|
|
|
|
#include "triton/Dialect/TritonGPU/IR/TritonGPUAttrDefs.cpp.inc"
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
SliceEncodingAttr BlockedEncodingAttr::squeeze(int axis) {
|
|
|
|
return SliceEncodingAttr::get(getContext(), axis, *this);
|
|
|
|
}
|
|
|
|
|
2022-09-18 05:58:42 +08:00
|
|
|
unsigned BlockedEncodingAttr::getElemsPerThread(ArrayRef<int64_t> shape) const {
|
|
|
|
size_t rank = shape.size();
|
|
|
|
assert(rank == getSizePerThread().size() &&
|
|
|
|
"unexpected rank in BlockedEncodingAttr::getElemsPerThread");
|
|
|
|
SmallVector<unsigned> elemsPerThreadPerDim(rank);
|
|
|
|
for (size_t i = 0; i < rank; ++i) {
|
|
|
|
unsigned t =
|
|
|
|
getSizePerThread()[i] * getThreadsPerWarp()[i] * getWarpsPerCTA()[i];
|
|
|
|
elemsPerThreadPerDim[i] =
|
|
|
|
ceil<unsigned>(shape[i], t) * getSizePerThread()[i];
|
|
|
|
}
|
|
|
|
return product<unsigned>(elemsPerThreadPerDim);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SliceEncodingAttr::getElemsPerThread(ArrayRef<int64_t> shape) const {
|
|
|
|
size_t rank = shape.size();
|
|
|
|
auto parent = getParent();
|
|
|
|
unsigned dim = getDim();
|
|
|
|
if (auto blockedParent = parent.dyn_cast<BlockedEncodingAttr>()) {
|
|
|
|
assert(rank == blockedParent.getSizePerThread().size() - 1 &&
|
|
|
|
"unexpected rank in SliceEncodingAttr::getElemsPerThread");
|
|
|
|
SmallVector<int64_t> paddedShape(rank + 1);
|
|
|
|
for (unsigned d = 0; d < rank + 1; ++d) {
|
|
|
|
if (d < dim)
|
|
|
|
paddedShape[d] = shape[d];
|
|
|
|
else if (d == dim)
|
|
|
|
paddedShape[d] = 1;
|
|
|
|
else
|
|
|
|
paddedShape[d] = shape[d - 1];
|
|
|
|
}
|
|
|
|
return blockedParent.getElemsPerThread(paddedShape);
|
|
|
|
} else {
|
|
|
|
assert(0 && "getElemsPerThread not implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MmaEncodingAttr::getElemsPerThread(ArrayRef<int64_t> shape) const {
|
|
|
|
// TODO:
|
|
|
|
assert(0 && "MmaEncodingAttr::getElemsPerThread not implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned SharedEncodingAttr::getElemsPerThread(ArrayRef<int64_t> shape) const {
|
|
|
|
// TODO:
|
|
|
|
assert(0 && "SharedEncodingAttr::getElemsPerThread not implemented");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-31 13:59:44 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Blocked Encoding
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
Attribute BlockedEncodingAttr::parse(AsmParser &parser, Type type) {
|
2022-05-31 11:43:21 +00:00
|
|
|
if (parser.parseLess().failed())
|
|
|
|
return {};
|
|
|
|
// Parse the data as a dictionary
|
|
|
|
DictionaryAttr dict;
|
|
|
|
if (parser.parseAttribute(dict).failed())
|
|
|
|
return {};
|
|
|
|
if (parser.parseGreater().failed())
|
|
|
|
return {};
|
2022-07-26 17:25:03 -07:00
|
|
|
|
2022-07-31 13:59:44 -07:00
|
|
|
SmallVector<unsigned, 2> sizePerThread;
|
|
|
|
SmallVector<unsigned, 2> threadsPerWarp;
|
|
|
|
SmallVector<unsigned, 2> warpsPerCTA;
|
2022-05-31 11:43:21 +00:00
|
|
|
SmallVector<unsigned, 2> order;
|
|
|
|
|
|
|
|
for (const NamedAttribute &attr : dict) {
|
2022-07-31 13:59:44 -07:00
|
|
|
if (attr.getName() == "sizePerThread") {
|
|
|
|
if (parseIntArrayAttr(parser, attr, sizePerThread,
|
|
|
|
"number of elements per thread")
|
2022-07-26 17:25:03 -07:00
|
|
|
.failed())
|
2022-05-31 11:43:21 +00:00
|
|
|
return {};
|
2022-07-31 13:59:44 -07:00
|
|
|
} else if (attr.getName() == "threadsPerWarp") {
|
|
|
|
if (parseIntArrayAttr(parser, attr, threadsPerWarp,
|
|
|
|
"number of threads per warp")
|
2022-07-26 17:25:03 -07:00
|
|
|
.failed())
|
2022-05-31 11:43:21 +00:00
|
|
|
return {};
|
2022-07-31 13:59:44 -07:00
|
|
|
} else if (attr.getName() == "warpsPerCTA") {
|
|
|
|
if (parseIntArrayAttr(parser, attr, warpsPerCTA,
|
|
|
|
"number of warps per CTA")
|
2022-07-26 17:25:03 -07:00
|
|
|
.failed())
|
2022-05-31 11:43:21 +00:00
|
|
|
return {};
|
|
|
|
} else if (attr.getName() == "order") {
|
|
|
|
if (parseIntArrayAttr(parser, attr, order, "order").failed())
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
parser.emitError(parser.getNameLoc(), "unexpected key: ")
|
|
|
|
<< attr.getName().strref();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
return parser.getChecked<BlockedEncodingAttr>(
|
2022-07-31 13:59:44 -07:00
|
|
|
parser.getContext(), sizePerThread, threadsPerWarp, warpsPerCTA, order);
|
2022-05-01 22:06:54 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
void BlockedEncodingAttr::print(mlir::AsmPrinter &printer) const {
|
2022-06-05 14:25:09 +08:00
|
|
|
printer << "<{"
|
2022-07-31 13:59:44 -07:00
|
|
|
<< "sizePerThread = [" << getSizePerThread() << "]"
|
|
|
|
<< ", threadsPerWarp = [" << getThreadsPerWarp() << "]"
|
|
|
|
<< ", warpsPerCTA = [" << getWarpsPerCTA() << "]"
|
|
|
|
<< ", order = [" << getOrder() << "]"
|
2022-06-05 14:25:09 +08:00
|
|
|
<< "}>";
|
2022-05-01 22:06:54 +08:00
|
|
|
}
|
|
|
|
|
2022-07-31 13:59:44 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MMA encoding
|
|
|
|
//===----------------------------------------------------------------------===//
|
2022-06-18 21:16:45 +08:00
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
Attribute MmaEncodingAttr::parse(AsmParser &parser, Type type) {
|
2022-06-06 21:03:58 +08:00
|
|
|
if (parser.parseLess().failed())
|
|
|
|
return {};
|
|
|
|
DictionaryAttr dict;
|
|
|
|
if (parser.parseAttribute(dict).failed())
|
|
|
|
return {};
|
|
|
|
if (parser.parseGreater().failed())
|
|
|
|
return {};
|
|
|
|
|
2022-07-31 13:59:44 -07:00
|
|
|
unsigned version = 0;
|
|
|
|
SmallVector<unsigned, 2> warpsPerCTA;
|
2022-06-06 21:03:58 +08:00
|
|
|
|
|
|
|
for (const NamedAttribute &attr : dict) {
|
2022-07-31 13:59:44 -07:00
|
|
|
if (attr.getName() == "version") {
|
|
|
|
if (parseUInt(parser, attr, version, "version").failed())
|
2022-06-06 21:03:58 +08:00
|
|
|
return {};
|
2022-07-31 13:59:44 -07:00
|
|
|
}
|
|
|
|
if (attr.getName() == "warpsPerCTA") {
|
|
|
|
if (parseIntArrayAttr(parser, attr, warpsPerCTA, "warpsPerCTA").failed())
|
2022-06-06 21:03:58 +08:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
return parser.getChecked<MmaEncodingAttr>(parser.getContext(), version,
|
|
|
|
warpsPerCTA);
|
2022-05-01 22:06:54 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
void MmaEncodingAttr::print(AsmPrinter &printer) const {
|
2022-06-06 21:03:58 +08:00
|
|
|
printer << "<{"
|
2022-07-31 13:59:44 -07:00
|
|
|
<< "version = " << getVersion() << ", "
|
|
|
|
<< "warpsPerCTA = [" << getWarpsPerCTA() << "]"
|
2022-06-06 21:03:58 +08:00
|
|
|
<< "}>";
|
2022-05-01 22:06:54 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Sliced Encoding
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Attribute SliceEncodingAttr::parse(AsmParser &parser, Type type) {
|
|
|
|
if (parser.parseLess().failed())
|
|
|
|
return {};
|
|
|
|
// Parse the data as a dictionary
|
|
|
|
DictionaryAttr dict;
|
|
|
|
if (parser.parseAttribute(dict).failed())
|
|
|
|
return {};
|
|
|
|
if (parser.parseGreater().failed())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
unsigned dim = 0;
|
|
|
|
Attribute parent;
|
|
|
|
|
|
|
|
for (const NamedAttribute &attr : dict) {
|
|
|
|
if (attr.getName() == "dim") {
|
|
|
|
if (parseUInt(parser, attr, dim, "dim").failed())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (attr.getName() == "parent") {
|
|
|
|
if (parser.parseAttribute(parent).failed())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parser.getChecked<SliceEncodingAttr>(parser.getContext(), dim, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SliceEncodingAttr::print(mlir::AsmPrinter &printer) const {
|
|
|
|
printer << "<{"
|
|
|
|
<< "dim = " << getDim() << ", "
|
|
|
|
<< "parent = " << getParent() << "}>";
|
|
|
|
}
|
|
|
|
|
2022-07-31 13:59:44 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Shared encoding
|
|
|
|
//===----------------------------------------------------------------------===//
|
2022-06-18 21:16:45 +08:00
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
Attribute SharedEncodingAttr::parse(AsmParser &parser, Type type) {
|
2022-05-31 11:43:21 +00:00
|
|
|
if (parser.parseLess().failed())
|
|
|
|
return {};
|
|
|
|
// Parse the data as a dictionary
|
|
|
|
DictionaryAttr dict;
|
|
|
|
if (parser.parseAttribute(dict).failed())
|
|
|
|
return {};
|
|
|
|
if (parser.parseGreater().failed())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
unsigned vec = 0;
|
|
|
|
unsigned perPhase = 0;
|
|
|
|
unsigned maxPhase = 0;
|
|
|
|
SmallVector<unsigned, 2> order;
|
|
|
|
|
|
|
|
for (const NamedAttribute &attr : dict) {
|
|
|
|
if (attr.getName() == "vec") {
|
2022-07-31 13:59:44 -07:00
|
|
|
if (parseUInt(parser, attr, vec, "vec").failed())
|
2022-05-31 11:43:21 +00:00
|
|
|
return {};
|
|
|
|
} else if (attr.getName() == "perPhase") {
|
2022-07-31 13:59:44 -07:00
|
|
|
if (parseUInt(parser, attr, perPhase, "perPhase").failed())
|
2022-05-31 11:43:21 +00:00
|
|
|
return {};
|
|
|
|
} else if (attr.getName() == "maxPhase") {
|
2022-07-31 13:59:44 -07:00
|
|
|
if (parseUInt(parser, attr, maxPhase, "maxPhase").failed())
|
2022-05-31 11:43:21 +00:00
|
|
|
return {};
|
|
|
|
} else if (attr.getName() == "order") {
|
|
|
|
if (parseIntArrayAttr(parser, attr, order, "order").failed())
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
parser.emitError(parser.getNameLoc(), "unexpected key: ")
|
2022-07-26 17:25:03 -07:00
|
|
|
<< attr.getName().strref();
|
2022-05-31 11:43:21 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
return parser.getChecked<SharedEncodingAttr>(parser.getContext(), vec,
|
|
|
|
perPhase, maxPhase, order);
|
2022-05-01 22:06:54 +08:00
|
|
|
}
|
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
void SharedEncodingAttr::print(AsmPrinter &printer) const {
|
2022-06-05 14:25:09 +08:00
|
|
|
printer << "<{"
|
2022-07-26 17:25:03 -07:00
|
|
|
<< "vec = " << getVec() << ", perPhase = " << getPerPhase()
|
|
|
|
<< ", maxPhase = " << getMaxPhase() << ", order = [" << getOrder()
|
|
|
|
<< "]"
|
2022-06-05 14:25:09 +08:00
|
|
|
<< "}>";
|
2022-05-01 22:06:54 +08:00
|
|
|
}
|
|
|
|
|
Keren/tensor slice insert alloc (#94)
This branch defines three new triton_gpu operations to partially solve #87. Below is an overview:
```
%tensor = triton_gpu.alloc_tensor : tensor<2x16x16xf16, #A>
%b = triton_gpu.insert_slice_async %a_ptr, %tensor, %offset {axis = 0 : i32, cache = 1 : i32, evict = 1 : i32, isVolatile = false} : tensor<16x16x!tt.ptr<f16>, #AL> -> tensor<2x16x16xf16, #A>
%c = triton_gpu.extract_slice %b, %offset {axis = 0 : i32} : tensor<2x16x16xf16, #A> -> tensor<16x16xf16, #A>
```
We plan to fully replace `copy_async` with `insert_slice_async`. **This hasn't been done yet.**
2022-09-01 12:37:17 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// InsertSliceAsyncOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
ParseResult parseInsertSliceAsyncOp(OpAsmParser &parser,
|
|
|
|
OperationState &result) {
|
|
|
|
SmallVector<OpAsmParser::OperandType, 4> allOperands;
|
|
|
|
Type srcType, dstType;
|
|
|
|
SMLoc allOperandLoc = parser.getCurrentLocation();
|
|
|
|
if (parser.parseOperandList(allOperands) ||
|
|
|
|
parser.parseOptionalAttrDict(result.attributes) || parser.parseColon() ||
|
|
|
|
parser.parseCustomTypeWithFallback(srcType) || parser.parseArrow() ||
|
|
|
|
parser.parseCustomTypeWithFallback(dstType))
|
|
|
|
return failure();
|
|
|
|
result.addTypes(dstType);
|
|
|
|
|
|
|
|
SmallVector<Type> operandTypes;
|
|
|
|
operandTypes.push_back(srcType); // src
|
|
|
|
operandTypes.push_back(dstType); // dst
|
|
|
|
operandTypes.push_back(
|
2022-09-09 12:03:41 -07:00
|
|
|
IntegerType::get(parser.getBuilder().getContext(), 32)); // index
|
Keren/tensor slice insert alloc (#94)
This branch defines three new triton_gpu operations to partially solve #87. Below is an overview:
```
%tensor = triton_gpu.alloc_tensor : tensor<2x16x16xf16, #A>
%b = triton_gpu.insert_slice_async %a_ptr, %tensor, %offset {axis = 0 : i32, cache = 1 : i32, evict = 1 : i32, isVolatile = false} : tensor<16x16x!tt.ptr<f16>, #AL> -> tensor<2x16x16xf16, #A>
%c = triton_gpu.extract_slice %b, %offset {axis = 0 : i32} : tensor<2x16x16xf16, #A> -> tensor<16x16xf16, #A>
```
We plan to fully replace `copy_async` with `insert_slice_async`. **This hasn't been done yet.**
2022-09-01 12:37:17 -07:00
|
|
|
if (allOperands.size() >= 4)
|
|
|
|
operandTypes.push_back(triton::getI1SameShape(srcType)); // mask
|
|
|
|
if (allOperands.size() >= 5)
|
|
|
|
operandTypes.push_back(triton::getPointeeType(srcType)); // other
|
|
|
|
|
|
|
|
if (parser.resolveOperands(allOperands, operandTypes, allOperandLoc,
|
|
|
|
result.operands))
|
|
|
|
return failure();
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
void printInsertSliceAsyncOp(OpAsmPrinter &printer,
|
|
|
|
InsertSliceAsyncOp insertSliceAsyncOp) {
|
|
|
|
printer << " ";
|
|
|
|
printer << insertSliceAsyncOp.getOperation()->getOperands();
|
|
|
|
printer.printOptionalAttrDict(insertSliceAsyncOp->getAttrs(),
|
|
|
|
/*elidedAttrs=*/{});
|
|
|
|
printer << " : ";
|
|
|
|
printer.printStrippedAttrOrType(insertSliceAsyncOp.src().getType());
|
|
|
|
printer << " -> ";
|
|
|
|
printer.printStrippedAttrOrType(insertSliceAsyncOp.result().getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ExtractSliceOp
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
mlir::LogicalResult ExtractSliceOp::inferReturnTypes(
|
|
|
|
::mlir::MLIRContext *context, llvm::Optional<::mlir::Location> location,
|
|
|
|
::mlir::ValueRange operands, mlir::DictionaryAttr attributes,
|
|
|
|
::mlir::RegionRange regions,
|
|
|
|
llvm::SmallVectorImpl<::mlir::Type> &inferredReturnTypes) {
|
|
|
|
auto srcType = operands[0].getType().cast<RankedTensorType>();
|
|
|
|
auto encoding = srcType.getEncoding();
|
|
|
|
auto srcShape = srcType.getShape();
|
|
|
|
auto axis = attributes.get("axis").cast<IntegerAttr>().getInt();
|
|
|
|
if (axis < 0 || axis > srcShape.size())
|
|
|
|
return failure();
|
2022-09-09 12:03:41 -07:00
|
|
|
// Since we only extract a slice from a certain index on the axis,
|
|
|
|
// the dims before the axis can be dropped.
|
Keren/tensor slice insert alloc (#94)
This branch defines three new triton_gpu operations to partially solve #87. Below is an overview:
```
%tensor = triton_gpu.alloc_tensor : tensor<2x16x16xf16, #A>
%b = triton_gpu.insert_slice_async %a_ptr, %tensor, %offset {axis = 0 : i32, cache = 1 : i32, evict = 1 : i32, isVolatile = false} : tensor<16x16x!tt.ptr<f16>, #AL> -> tensor<2x16x16xf16, #A>
%c = triton_gpu.extract_slice %b, %offset {axis = 0 : i32} : tensor<2x16x16xf16, #A> -> tensor<16x16xf16, #A>
```
We plan to fully replace `copy_async` with `insert_slice_async`. **This hasn't been done yet.**
2022-09-01 12:37:17 -07:00
|
|
|
auto dstShape = srcShape.drop_front(axis + 1);
|
|
|
|
auto returnType =
|
|
|
|
RankedTensorType::get(dstShape, srcType.getElementType(), encoding);
|
|
|
|
inferredReturnTypes.assign({returnType});
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-07-31 13:59:44 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ASM Interface (i.e.: alias)
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-07-26 10:50:11 -07:00
|
|
|
class TritonGPUOpAsmInterface : public OpAsmDialectInterface {
|
2022-07-26 17:25:03 -07:00
|
|
|
public:
|
2022-07-26 10:50:11 -07:00
|
|
|
using OpAsmDialectInterface::OpAsmDialectInterface;
|
|
|
|
|
|
|
|
AliasResult getAlias(Attribute attr, raw_ostream &os) const override {
|
2022-08-18 12:49:37 -07:00
|
|
|
if (auto mmaAttr = attr.dyn_cast<MmaEncodingAttr>()) {
|
2022-07-26 10:50:11 -07:00
|
|
|
os << "mma";
|
|
|
|
return AliasResult::FinalAlias;
|
2022-08-18 12:49:37 -07:00
|
|
|
} else if (auto sharedAttr = attr.dyn_cast<SharedEncodingAttr>()) {
|
2022-07-26 10:50:11 -07:00
|
|
|
os << "shared";
|
|
|
|
return AliasResult::FinalAlias;
|
2022-08-18 12:49:37 -07:00
|
|
|
} else if (auto blockedAttr = attr.dyn_cast<BlockedEncodingAttr>()) {
|
2022-07-26 10:50:11 -07:00
|
|
|
os << "blocked";
|
2022-07-27 01:32:10 -07:00
|
|
|
return AliasResult::FinalAlias;
|
2022-08-18 12:49:37 -07:00
|
|
|
} /* else if (auto sliceAttr = attr.dyn_cast<SliceEncodingAttr>()) {
|
|
|
|
os << "slice";
|
|
|
|
return AliasResult::FinalAlias;
|
|
|
|
} */
|
2022-08-24 12:55:49 -07:00
|
|
|
return OpAsmDialectInterface::getAlias(attr, os);
|
2022-07-26 10:50:11 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-28 18:51:31 +08:00
|
|
|
void TritonGPUDialect::initialize() {
|
2022-05-02 21:51:00 +08:00
|
|
|
addAttributes<
|
|
|
|
#define GET_ATTRDEF_LIST
|
|
|
|
#include "triton/Dialect/TritonGPU/IR/TritonGPUAttrDefs.cpp.inc"
|
|
|
|
>();
|
2022-04-28 18:51:31 +08:00
|
|
|
addOperations<
|
|
|
|
#define GET_OP_LIST
|
|
|
|
#include "triton/Dialect/TritonGPU/IR/Ops.cpp.inc"
|
2022-07-26 17:25:03 -07:00
|
|
|
>();
|
2022-07-26 10:50:11 -07:00
|
|
|
addInterfaces<TritonGPUOpAsmInterface>();
|
2022-04-28 18:51:31 +08:00
|
|
|
}
|
2022-05-01 22:06:54 +08:00
|
|
|
|
2022-08-18 12:49:37 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Verification
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
Keren/tensor slice insert alloc (#94)
This branch defines three new triton_gpu operations to partially solve #87. Below is an overview:
```
%tensor = triton_gpu.alloc_tensor : tensor<2x16x16xf16, #A>
%b = triton_gpu.insert_slice_async %a_ptr, %tensor, %offset {axis = 0 : i32, cache = 1 : i32, evict = 1 : i32, isVolatile = false} : tensor<16x16x!tt.ptr<f16>, #AL> -> tensor<2x16x16xf16, #A>
%c = triton_gpu.extract_slice %b, %offset {axis = 0 : i32} : tensor<2x16x16xf16, #A> -> tensor<16x16xf16, #A>
```
We plan to fully replace `copy_async` with `insert_slice_async`. **This hasn't been done yet.**
2022-09-01 12:37:17 -07:00
|
|
|
static LogicalResult verify(InsertSliceAsyncOp op) {
|
|
|
|
if (!isSharedEncoding(op.getResult())) {
|
2022-09-09 12:03:41 -07:00
|
|
|
return op.emitOpError(
|
|
|
|
"insert_slice_async should return a shared memory tensor");
|
Keren/tensor slice insert alloc (#94)
This branch defines three new triton_gpu operations to partially solve #87. Below is an overview:
```
%tensor = triton_gpu.alloc_tensor : tensor<2x16x16xf16, #A>
%b = triton_gpu.insert_slice_async %a_ptr, %tensor, %offset {axis = 0 : i32, cache = 1 : i32, evict = 1 : i32, isVolatile = false} : tensor<16x16x!tt.ptr<f16>, #AL> -> tensor<2x16x16xf16, #A>
%c = triton_gpu.extract_slice %b, %offset {axis = 0 : i32} : tensor<2x16x16xf16, #A> -> tensor<16x16xf16, #A>
```
We plan to fully replace `copy_async` with `insert_slice_async`. **This hasn't been done yet.**
2022-09-01 12:37:17 -07:00
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(ExtractSliceOp op) {
|
|
|
|
if (!isSharedEncoding(op.getResult())) {
|
|
|
|
return op.emitOpError("extract_slice should return a shared memory tensor");
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogicalResult verify(AllocTensorOp op) {
|
|
|
|
if (!isSharedEncoding(op.getResult())) {
|
|
|
|
return op.emitOpError("alloc_tensor should return a shared memory tensor");
|
|
|
|
}
|
2022-06-07 19:34:59 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2022-05-01 22:06:54 +08:00
|
|
|
#define GET_OP_CLASSES
|
|
|
|
#include "triton/Dialect/TritonGPU/IR/Ops.cpp.inc"
|
2022-05-24 19:48:56 +08:00
|
|
|
|
|
|
|
// verify TritonGPU ops
|
2022-07-26 17:25:03 -07:00
|
|
|
LogicalResult TritonGPUDialect::verifyOperationAttribute(Operation *op,
|
|
|
|
NamedAttribute attr) {
|
2022-05-24 19:48:56 +08:00
|
|
|
// TODO: fill this.
|
|
|
|
return success();
|
2022-08-24 12:55:49 -07:00
|
|
|
}
|