|
| 1 | +#include "ir.h" |
| 2 | +#include "pybind11/pybind11.h" |
| 3 | +#include <pybind11/stl.h> |
| 4 | + |
| 5 | +#include "mlir/IR/BuiltinTypes.h" |
| 6 | +#include "mlir/IR/Types.h" |
| 7 | +#include "triton/Conversion/TritonGPUToLLVM/Utility.h" |
| 8 | +#include "triton/Dialect/TritonGPU/IR/Attributes.h" |
| 9 | +#include "triton/Dialect/TritonGPU/IR/Dialect.h" |
| 10 | + |
| 11 | +using namespace mlir; |
| 12 | +namespace py = pybind11; |
| 13 | +namespace ttg = triton::gpu; |
| 14 | + |
| 15 | +struct GluonOpBuilder : public TritonOpBuilder {}; |
| 16 | + |
| 17 | +void init_gluon_ir(py::module &&m) { |
| 18 | + py::class_<GluonOpBuilder, TritonOpBuilder>( |
| 19 | + m, "GluonOpBuilder", py::module_local(), py::dynamic_attr()) |
| 20 | + .def(py::init<MLIRContext *>()) |
| 21 | + .def("get_distributed_ty", |
| 22 | + [](GluonOpBuilder &self, Type &elementType, |
| 23 | + std::vector<int64_t> &shape, Attribute layout) -> Type { |
| 24 | + return RankedTensorType::get(shape, elementType, layout); |
| 25 | + }) |
| 26 | + .def("get_shared_mem_desc_ty", |
| 27 | + [](GluonOpBuilder &self, Type &elementType, |
| 28 | + std::vector<int64_t> &shape, Attribute layout, |
| 29 | + std::vector<int64_t> &allocShape) -> Type { |
| 30 | + auto ctx = self.getContext(); |
| 31 | + return ttg::MemDescType::get(shape, elementType, layout, |
| 32 | + ttg::SharedMemorySpaceAttr::get(ctx), |
| 33 | + /*mutableMemory=*/true, |
| 34 | + /*allocShape=*/allocShape); |
| 35 | + }) |
| 36 | + .def("get_blocked_layout", |
| 37 | + [](GluonOpBuilder &self, std::vector<unsigned> &sizePerThread, |
| 38 | + std::vector<unsigned> &threadsPerWarp, |
| 39 | + std::vector<unsigned> &warpsPerCta, std::vector<unsigned> &order, |
| 40 | + std::vector<unsigned> &ctasPerCga, |
| 41 | + std::vector<unsigned> &ctaSplitNum, |
| 42 | + std::vector<unsigned> &ctaOrder) -> Attribute { |
| 43 | + auto ctx = self.getContext(); |
| 44 | + auto ctaLayout = ttg::CTALayoutAttr::get(ctx, ctasPerCga, |
| 45 | + ctaSplitNum, ctaOrder); |
| 46 | + return ttg::BlockedEncodingAttr::get(ctx, sizePerThread, |
| 47 | + threadsPerWarp, warpsPerCta, |
| 48 | + order, ctaLayout); |
| 49 | + }) |
| 50 | + .def("get_slice_layout", |
| 51 | + [](GluonOpBuilder &self, unsigned dim, |
| 52 | + Attribute parent) -> Attribute { |
| 53 | + auto ctx = self.getContext(); |
| 54 | + auto dist = cast<ttg::DistributedEncodingTrait>(parent); |
| 55 | + return ttg::SliceEncodingAttr::get(ctx, dim, dist); |
| 56 | + }) |
| 57 | + .def("get_nvmma_shared_layout", |
| 58 | + [](GluonOpBuilder &self, unsigned swizzleByteWidth, |
| 59 | + unsigned elementBitwidth, bool transposed, bool fp4Padded, |
| 60 | + std::vector<unsigned> &ctasPerCga, |
| 61 | + std::vector<unsigned> &ctaSplitNum, |
| 62 | + std::vector<unsigned> &ctaOrder) -> Attribute { |
| 63 | + auto ctx = self.getContext(); |
| 64 | + auto ctaLayout = ttg::CTALayoutAttr::get(ctx, ctasPerCga, |
| 65 | + ctaSplitNum, ctaOrder); |
| 66 | + return ttg::NVMMASharedEncodingAttr::get( |
| 67 | + ctx, swizzleByteWidth, transposed, elementBitwidth, fp4Padded, |
| 68 | + ctaLayout); |
| 69 | + }) |
| 70 | + .def("create_convert_layout", |
| 71 | + [](GluonOpBuilder &self, Type resultTy, Value value) -> Value { |
| 72 | + return self.create<ttg::ConvertLayoutOp>(resultTy, value); |
| 73 | + }) |
| 74 | + .def("create_local_alloc", |
| 75 | + [](GluonOpBuilder &self, Type resultTy, Value value) -> Value { |
| 76 | + return self.create<ttg::LocalAllocOp>(resultTy, value); |
| 77 | + }) |
| 78 | + .def("create_local_store", |
| 79 | + [](GluonOpBuilder &self, Value memDesc, Value value) { |
| 80 | + self.create<ttg::LocalStoreOp>(value, memDesc); |
| 81 | + }) |
| 82 | + .def("create_local_load", |
| 83 | + [](GluonOpBuilder &self, Type resultTy, Value memDesc) -> Value { |
| 84 | + return self.create<ttg::LocalLoadOp>(resultTy, memDesc); |
| 85 | + }); |
| 86 | +} |
0 commit comments