Repository navigation
Add attention op to linalg_ext - #11928
Conversation
|
Here is what the IR looks like when lowering this op |
| OpPassManager &pm, bool useFuseTensorPadWithConsumerPass = true) { | ||
| pm.addPass(createTileAndDistributeToWorkgroupsPass()); | ||
| auto &nestedModulePM = pm.nest<ModuleOp>(); | ||
| nestedModulePM.addNestedPass<func::FuncOp>( |
There was a problem hiding this comment.
Nice! I have a suggestion here that is not necessary for this PR.
It seems like we can combine the "DecomposeAttentionPass" and "DecomposeSoftmaxPass" into a single pass with a new interface. A simple interface that just adds a single method, i.e. "decomposeOperation" that decomposes the operation into the required sequence. Then the pass is just walking over the IR and decomposing the ops as needed?
WDYT?
Also FYI @ftynse (since Alex seems to be interested)
There was a problem hiding this comment.
Sounds great! Where would I add such an interface?
There was a problem hiding this comment.
+1 for the interface. IMO, it can go to LinalgExtInterfaces.h/cpp. This sounds specific to Linalg at this point, at the ops it applies to are in LianlgExt.
07c3c22 to
9d49540
Compare
| OpPassManager &pm, bool useFuseTensorPadWithConsumerPass = true) { | ||
| pm.addPass(createTileAndDistributeToWorkgroupsPass()); | ||
| auto &nestedModulePM = pm.nest<ModuleOp>(); | ||
| nestedModulePM.addNestedPass<func::FuncOp>( |
There was a problem hiding this comment.
+1 for the interface. IMO, it can go to LinalgExtInterfaces.h/cpp. This sounds specific to Linalg at this point, at the ops it applies to are in LianlgExt.
| // Attention | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| class CheckShapedOperandOfRank3<int i> : And<[ |
There was a problem hiding this comment.
Document this in English please :)
There was a problem hiding this comment.
Sure will add a line of documentation.
| Value query() { | ||
| return getInputOperand(0)->get(); | ||
| } | ||
| Value key() { | ||
| return getInputOperand(1)->get(); | ||
| } | ||
| Value value() { | ||
| return getInputOperand(2)->get(); | ||
| } | ||
| Value output() { | ||
| return getOutputOperand(0)->get(); | ||
| } |
There was a problem hiding this comment.
Naming nit: these should be using the prefixed form, i.e. getQuery, getKey, etc.
There was a problem hiding this comment.
Sounds good, will change.
| ArrayRef<int64_t> keyShape = keyType.getShape(); | ||
| ArrayRef<int64_t> valueShape = valueType.getShape(); | ||
| ArrayRef<int64_t> outputShape = outputType.getShape(); | ||
| if (!areShapesCompatible(queryShape, keyShape)) { |
There was a problem hiding this comment.
Annoying nit: MLIR (and thus external-projects/iree-dialects) elides braces around single-line bodies.
| queryOutputOffsets, queryOutputSizes, | ||
| queryOutputStrides)); | ||
|
|
||
| SmallVector<Type, 4> resultTypes; |
There was a problem hiding this comment.
Nit: drop the explicit number of stack elements.
There was a problem hiding this comment.
Sure will remove.
4a99397 to
997a75c
Compare
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Ok, this looks fine to me. I think its fine to do the tiling and decomposition in a combined way for now. We probably need to refactor this into a generate reduction tiling algorithm, but that should just be a refactoring....
|
|
||
| static Value computeSoftmax(Value qkTranspose, Value currentMax, Value output, | ||
| Location loc, OpBuilder &builder) { | ||
| AffineMap identityMap = |
There was a problem hiding this comment.
Cant we use the iree_linalg_ext.softmax here?
There was a problem hiding this comment.
This computes a partial softmax, so just exp(x - max(x)), so that's why we cant use the iree_linalg_ext.softmax op. If you would like, I can rename this function?
There was a problem hiding this comment.
yeah renaming would be good.... Thats a bummer, you had to do all the work for adding softmax op, but wasnt really able to use it here.
There was a problem hiding this comment.
I know but maybe in the future we can figure out how to leverage that here.
This patch adds the attention op to the linalg ext dialect. It also adds a lowering that decomposes the attention into a sequence of linalg generic ops (batch matmul + softmax + batch matmul). Finally, we add the pass into the CPU and CUDA pipeline along with an e2e test to validate functional accuracy.
Rather than try to fit this into the PartialTileReductionInterface, we do a special lowering of the attention op that tiles and decomposes it to a sequence of linalg ops.
Also add further simplifications and bug fixes.
This patch fuses the last two generics into one, so now we generate a total of 5 generics. Also adds additional output to linalg generic for proper compute.
Also keep last two generics separate as their dimensions may not always be the same.
|
I have made the changes. Please merge when you get a chance. Thanks! |
| NAME | ||
| check_llvm-cpu_local-task | ||
| SRCS | ||
| "attention.mlir" |
There was a problem hiding this comment.
This test is failing on emscripten with --iree-llvm-target-triple=wasm32-unknown-emscripten (tested on post-submit only, best-effort to maintain): https://github.com/iree-org/iree/actions/runs/4026898563/jobs/6922078074
FAILED: tests/e2e/linalg_ext_ops/check_llvm-cpu_local-task_attention.mlir_module.vmfb /work/build-emscripten/tests/e2e/linalg_ext_ops/check_llvm-cpu_local-task_attention.mlir_module.vmfb
cd /work/build-emscripten/tests/e2e/linalg_ext_ops && /work/full-build-dir/install/bin/iree-compile --output-format=vm-bytecode --mlir-print-op-on-diagnostic=false --iree-hal-target-backends=llvm-cpu /work/tests/e2e/linalg_ext_ops/attention.mlir -o check_llvm-cpu_local-task_attention.mlir_module.vmfb --iree-hal-executable-object-search-path=\"/work/build-emscripten\" --iree-llvm-target-triple=wasm32-unknown-emscripten
/work/tests/e2e/linalg_ext_ops/attention.mlir:6:8: error: 'memref.alloca' op all stack allocations need to be hoisted to the entry block of the function
%1 = iree_linalg_ext.attention ins(%query, %key, %value : tensor<1x4x4xf32>,
^
Does that error make sense to you?
There was a problem hiding this comment.
Is this happening only in the wasm backend... or is it on all backends?
There was a problem hiding this comment.
Just the wasm compilation (which is just the CPU path but using --iree-llvm-target-triple=wasm32-unknown-emscripten). We might see similar errors with other target triples or LLVM/codegen options, but all of our other CI builds and tests are passing.
There was a problem hiding this comment.
Surprising that this is happening on wasm, and not outside of it... This should be using the CPU compilation pipeline directly right? If so, then I can look into it.
This patch adds the attention op to the linalg ext dialect. It also adds a lowering that decomposes the attention into a sequence of linalg generic ops (batch matmul + softmax + batch matmul). Finally, we add the pass into the CPU and CUDA pipeline along with an e2e test to validate functional accuracy.
This patch adds the attention op to the linalg ext dialect. It also adds a lowering that decomposes the attention into a sequence of linalg generic ops (batch matmul + softmax + batch matmul). Finally, we add the pass into the CPU and CUDA pipeline along with an e2e test to validate functional accuracy.
This patch adds the attention op to the linalg ext dialect. It also adds a lowering that decomposes the attention into a sequence of linalg generic ops (batch matmul + softmax + batch matmul). Finally, we add the pass into the CPU and CUDA pipeline along with an e2e test to validate functional accuracy.
This patch adds the attention op to the linalg ext dialect. It also adds a lowering that decomposes the attention into a sequence of linalg generic ops (batch matmul + softmax + batch matmul). Finally, we add the pass into the CPU and CUDA pipeline along with an e2e test to validate functional accuracy.