|
| 1 | +//===- RockGemmGemmWrapperInterface.td - ops that wrap rock.attention |
| 2 | +//---------===// |
| 3 | +// |
| 4 | +// Part of the rocMLIR Project, under the Apache License v2.0 with LLVM |
| 5 | +// Exceptions. See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +// Copyright (c) 2025 Advanced Micro Devices INc. |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | +// |
| 11 | +// This file defines RockGemmGemmWrapperInterface, which abstracts attention and |
| 12 | +// gemm+gemm and friends (conv+gemm, ...) to allow code to operate on them |
| 13 | +// generically. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef ROCK_GEMM_GEMM_WRAPPER_INTERFACE |
| 18 | +#define ROCK_GEMM_GEMM_WRAPPER_INTERFACE |
| 19 | + |
| 20 | +include "mlir/IR/OpBase.td" |
| 21 | + |
| 22 | +def RockGemmGemmWrapperInterface : OpInterface<"RockGemmGemmWrapperInterface"> { |
| 23 | + let description = [{ |
| 24 | + Interface to abstract away gemm+gemm-wrapping operators in the rock dialect, |
| 25 | + which mainly include attention and gemm+gemm and friends that can be implemented |
| 26 | + with flash attention. |
| 27 | + |
| 28 | + This should include functions to get common attributes. |
| 29 | + }]; |
| 30 | + let cppNamespace = "::mlir::rock"; |
| 31 | + |
| 32 | + let methods = [ |
| 33 | + InterfaceMethod< |
| 34 | + /*desc=*/[{ |
| 35 | + Return the KernelType of this op |
| 36 | + }], |
| 37 | + /*retType=*/"::mlir::rock::KernelType", |
| 38 | + /*methodName=*/"getKernelType", |
| 39 | + /*args=*/(ins), |
| 40 | + /*methodBody=*/"", |
| 41 | + /*defaultImplementation=*/"" |
| 42 | + >, |
| 43 | + InterfaceMethod< |
| 44 | + /*desc=*/[{ |
| 45 | + Return the arch string of this op |
| 46 | + }], |
| 47 | + /*retType=*/"StringRef", |
| 48 | + /*methodName=*/"getArch", |
| 49 | + /*args=*/(ins), |
| 50 | + /*methodBody=*/"", |
| 51 | + /*defaultImplementation=*/"" |
| 52 | + >, |
| 53 | + InterfaceMethod< |
| 54 | + /*desc=*/[{ |
| 55 | + Return the OpOperand that corresponds to the operand argument |
| 56 | + that corresponds to the output result of the operation. |
| 57 | + }], |
| 58 | + /*retType=*/"OpOperand *", |
| 59 | + /*methodName=*/"getOutArgument", |
| 60 | + /*args=*/(ins), |
| 61 | + /*methodBody=*/"", |
| 62 | + /*defaultImplementation=*/"" |
| 63 | + >, |
| 64 | + InterfaceMethod< |
| 65 | + /*desc=*/[{ |
| 66 | + Return the size of the matrix multiplication that this op will eventually |
| 67 | + perform. |
| 68 | + }], |
| 69 | + /*retType=*/"::mlir::rock::GemmGemmSize", |
| 70 | + /*methodName=*/"getGemmGemmSize", |
| 71 | + /*args=*/(ins), |
| 72 | + /*methodBody=*/"", |
| 73 | + /*defaultImplementation=*/"" |
| 74 | + >, |
| 75 | + InterfaceMethod< |
| 76 | + /*desc=*/[{ |
| 77 | + Return the element type of [what will become] matrix A for this operation. |
| 78 | + }], |
| 79 | + /*retType=*/"::mlir::Type", |
| 80 | + /*methodName=*/"getAType", |
| 81 | + /*args=*/(ins), |
| 82 | + /*methodBody=*/"", |
| 83 | + /*defaultImplementation=*/"" |
| 84 | + >, |
| 85 | + InterfaceMethod< |
| 86 | + /*desc=*/[{ |
| 87 | + Return the element type of [what will become] matrix B for this operation. |
| 88 | + }], |
| 89 | + /*retType=*/"::mlir::Type", |
| 90 | + /*methodName=*/"getBType", |
| 91 | + /*args=*/(ins), |
| 92 | + /*methodBody=*/"", |
| 93 | + /*defaultImplementation=*/"" |
| 94 | + >, |
| 95 | + InterfaceMethod< |
| 96 | + /*desc=*/[{ |
| 97 | + Return the element type of [what will become] matrix C for this operation. |
| 98 | + }], |
| 99 | + /*retType=*/"::mlir::Type", |
| 100 | + /*methodName=*/"getCType", |
| 101 | + /*args=*/(ins), |
| 102 | + /*methodBody=*/"", |
| 103 | + /*defaultImplementation=*/"" |
| 104 | + >, |
| 105 | + InterfaceMethod< |
| 106 | + /*desc=*/[{ |
| 107 | + Return the element type of [what will become] output matrix for this operation. |
| 108 | + }], |
| 109 | + /*retType=*/"::mlir::Type", |
| 110 | + /*methodName=*/"getOutType", |
| 111 | + /*args=*/(ins), |
| 112 | + /*methodBody=*/"", |
| 113 | + /*defaultImplementation=*/"" |
| 114 | + >, |
| 115 | + InterfaceMethod< |
| 116 | + /*desc=*/[{ |
| 117 | + Return the whether matrix A is transposed. |
| 118 | + }], |
| 119 | + /*retType=*/"bool", |
| 120 | + /*methodName=*/"getTransposedA", |
| 121 | + /*args=*/(ins), |
| 122 | + /*methodBody=*/"", |
| 123 | + /*defaultImplementation=*/"" |
| 124 | + >, |
| 125 | + InterfaceMethod< |
| 126 | + /*desc=*/[{ |
| 127 | + Return the whether matrix B is transposed. |
| 128 | + }], |
| 129 | + /*retType=*/"bool", |
| 130 | + /*methodName=*/"getTransposedB", |
| 131 | + /*args=*/(ins), |
| 132 | + /*methodBody=*/"", |
| 133 | + /*defaultImplementation=*/"" |
| 134 | + >, |
| 135 | + InterfaceMethod< |
| 136 | + /*desc=*/[{ |
| 137 | + Return the whether matrix C is transposed. |
| 138 | + }], |
| 139 | + /*retType=*/"bool", |
| 140 | + /*methodName=*/"getTransposedC", |
| 141 | + /*args=*/(ins), |
| 142 | + /*methodBody=*/"", |
| 143 | + /*defaultImplementation=*/"" |
| 144 | + >, |
| 145 | + InterfaceMethod< |
| 146 | + /*desc=*/[{ |
| 147 | + Return the whether output matrix is transposed. |
| 148 | + }], |
| 149 | + /*retType=*/"bool", |
| 150 | + /*methodName=*/"getTransposedOut", |
| 151 | + /*args=*/(ins), |
| 152 | + /*methodBody=*/"", |
| 153 | + /*defaultImplementation=*/"" |
| 154 | + >, |
| 155 | + InterfaceMethod< |
| 156 | + /*desc=*/[{ |
| 157 | + Return the features attribute of this op. |
| 158 | + }], |
| 159 | + /*retType=*/"::mlir::rock::GemmFeatures", |
| 160 | + /*methodName=*/"getGemmFeatures", |
| 161 | + /*args=*/(ins), |
| 162 | + /*methodBody=*/"", |
| 163 | + /*defaultImplementation=*/[{ |
| 164 | + return $_op.getFeatures(); |
| 165 | + }] |
| 166 | + >, |
| 167 | + InterfaceMethod< |
| 168 | + /*desc=*/[{ |
| 169 | + Return the optional number of Compute Units the GPU provides. |
| 170 | + }], |
| 171 | + /*retType=*/"std::optional<uint32_t>", |
| 172 | + /*methodName=*/"getNumCU", |
| 173 | + /*args=*/(ins), |
| 174 | + /*methodBody=*/"", |
| 175 | + /*defaultImplementation=*/ "" |
| 176 | + >, |
| 177 | + |
| 178 | + InterfaceMethod< |
| 179 | + /*desc=*/[{ |
| 180 | + Set the tuning parameters attribute of the first GEMM |
| 181 | + }], |
| 182 | + /*retType=*/"void", |
| 183 | + /*methodName=*/"setGemm0ParamsAttr", |
| 184 | + /*args=*/(ins "::mlir::Attribute":$params), |
| 185 | + /*methodBody=*/"", |
| 186 | + /*defaultImplementation=*/[{ |
| 187 | + $_op->setAttr($_op.getParams0AttrName(), params); |
| 188 | + }] |
| 189 | + >, |
| 190 | + InterfaceMethod< |
| 191 | + /*desc=*/[{ |
| 192 | + Set the tuning parameters attribute of the second GEMM |
| 193 | + }], |
| 194 | + /*retType=*/"void", |
| 195 | + /*methodName=*/"setGemm1ParamsAttr", |
| 196 | + /*args=*/(ins "::mlir::Attribute":$params), |
| 197 | + /*methodBody=*/"", |
| 198 | + /*defaultImplementation=*/[{ |
| 199 | + $_op->setAttr($_op.getParams1AttrName(), params); |
| 200 | + }] |
| 201 | + >, |
| 202 | + InterfaceMethod< |
| 203 | + /*desc=*/[{ |
| 204 | + Get the tuning parameters attribute of the first GEMM |
| 205 | + }], |
| 206 | + /*retType=*/"std::optional<RockTuningParamAttrInterface>", |
| 207 | + /*methodName=*/"getGemm0Params", |
| 208 | + /*args=*/(ins), |
| 209 | + /*methodBody=*/"", |
| 210 | + /*defaultImplementation=*/[{ |
| 211 | + return $_op.getParams0(); |
| 212 | + }] |
| 213 | + >, |
| 214 | + InterfaceMethod< |
| 215 | + /*desc=*/[{ |
| 216 | + Get the tuning parameters attribute of the second GEMM |
| 217 | + }], |
| 218 | + /*retType=*/"std::optional<RockTuningParamAttrInterface>", |
| 219 | + /*methodName=*/"getGemm1Params", |
| 220 | + /*args=*/(ins), |
| 221 | + /*methodBody=*/"", |
| 222 | + /*defaultImplementation=*/[{ |
| 223 | + return $_op.getParams1(); |
| 224 | + }] |
| 225 | + >, |
| 226 | + InterfaceMethod< |
| 227 | + /*desc=*/[{ |
| 228 | + Return the index of the elementwise region argument that comes from the first GEMM. |
| 229 | + }], |
| 230 | + /*retType=*/"uint32_t", |
| 231 | + /*methodName=*/"getFirstGemmIndex", |
| 232 | + /*args=*/(ins), |
| 233 | + /*methodBody=*/"", |
| 234 | + /*defaultImplementation=*/ "" |
| 235 | + >, |
| 236 | + |
| 237 | + // TODO: more methods here as needed |
| 238 | + ]; |
| 239 | + |
| 240 | + let verify = [{ |
| 241 | + auto concreteOp = ::mlir::cast<ConcreteOp>($_op); |
| 242 | + if ($_op->getNumResults() == 1) { |
| 243 | + if ($_op->getResult(0).getType() != |
| 244 | + concreteOp.getOutArgument()->get().getType()) { |
| 245 | + return $_op->emitOpError("result type must match output argument type"); |
| 246 | + } |
| 247 | + } |
| 248 | + return ::mlir::success(); |
| 249 | + }]; |
| 250 | +} |
| 251 | + |
| 252 | +#endif // ROCK_GEMM_GEMM_WRAPPER_INTERFACE |
0 commit comments