-
Notifications
You must be signed in to change notification settings - Fork 78
[LDE: Project] 'Codegen-only compilation pipeline for LA operations' #935
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
base: main
Are you sure you want to change the base?
Conversation
philipportner
left a comment
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.
Thanks for the PR.
This already looks promising! A few changes are needed and we should add test cases before merging this in.
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.
Remove these changes.
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.
Remove these changes.
scripts/examples/slice.daph
Outdated
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.
Remove this file and add proper testcases. Look at the testcases in test/codegen/ and test/api/cli/codegen, there you will find different kind of tests for our codegen. FileCheck based tests that verify the IR after a certain pass(es), and end-to-end tests that execute daphne and verify the output. You should add a single file, e.g., test/codegen/sliceop.mlir for the IR tests and a single .cpp file for the end-to-end tests that compare the non codegen with the codegen'd execution of daphne.
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.
Combine these into a single pass which has two rewrite patterns.
| SmallVector<AffineMap, 2> indexMaps{AffineMap::getMultiDimIdentityMap(2, rewriter.getContext()), | ||
| AffineMap::getMultiDimIdentityMap(2, rewriter.getContext())}; | ||
|
|
||
| SmallVector<utils::IteratorType, 2> iterTypes{utils::IteratorType::parallel, | ||
| utils::IteratorType::parallel}; | ||
|
|
||
| rewriter.create<linalg::GenericOp>(loc, TypeRange{}, ValueRange{selMemref}, ValueRange{resMemref}, | ||
| indexMaps, iterTypes, | ||
| [&](OpBuilder &OpBuilderNested, Location locNested, ValueRange arg) { | ||
| OpBuilderNested.create<linalg::YieldOp>(locNested, arg[0]); | ||
| }); |
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.
This shouldn't be needed, as we want to create a view of a matrix we don't want to copy the data. The memref::SubViewOp should be enough. You'll need to properly lower it afterwards by adding the following passes to the pipeline:
mlir::memref::createExpandStridedMetadataPass; and mlir::createFinalizeMemRefToLLVMConversionPass.
This reverts commit 04fc40e.
Overview
This is an implementation of Codegen-only compilation pipeline for LA operations #693 as the coursework for 'LDE: Project'
This PR implements lowering passes for linear algebra related operations involving CSR matrices and interactions between dense and CSR matrices using MLIR. Instead of lowering these operations to pre-compiled C++ kernels, these passes offer an alternative option that lowers them to MLIR by enabling the codegen pipeline (
--mlir-codegen).The lowering of
EwUnaryOpandEwBinaryOpon matrix-scalar makes use of linalg GenericOps to perform computations on all elements of the matrix. For other operations, the passes utilizescf.forop andscf.whileop for locating the required elements.Additionally, all of the passes convert the input matrix into
memref, perform computations using operations from thearithdialect, and finally convert thememrefback into a matrix as the output.Changes:
Add codegens for
SliceOpfor warm-up,EwUnaryOpon CSR matrices,EwBinaryOp(Add,Mul) on CSR-Dense/CSR-CSR/CSR-Scalar,MatMulOpon CSR-Dense/CSR-CSR.Add a new constructor for CSR matrix.
Add
convertMemRefToCSRMatrixkernel for convertingMemRefto CSR matrix.Add missing kernels for testing:
EwBinaryMat(Add) on CSR-CSR,EwBinaryObjScaon CSR-Scalar,EwUnaryMaton CSR.Add a script-level test case (GEMM).
Add some necessary instantiations in
kernels.json.Edit related TableGen files.
A Small Example of Lowering A Kernel
%9 = "daphne.matMul"(%7, %8, %3, %3) : (!daphne.Matrix<5x5xf64:sp[5.000000e-02]:rep[sparse]>, !daphne.Matrix<5x5xf64:sp[1.000000e+00]>, i1, i1) -> !daphne.Matrix<5x5xf64:sp[0.22621906250000023]:rep[sparse]>The input CSR matrix is first converted to three
memrefs.In this case, the result is changed to be a dense matrix, so just one result
memrefis allocated and initialized, if the result is in CSR matrix, the number will be three.Create a
scf.forloop for locating the required elements, compute the result byarithoperations, and store it in the resultmemref.Finally, convert the
memrefback to dense matrix.%13 = "daphne.convertMemRefToDenseMatrix"(%intptr, %offset, %sizes#0, %sizes#1, %strides#0, %strides#1) : (index, index, index, index, index, index) -> !daphne.Matrix<5x5xf64:sp[1.000000e+00]>Performance
This is a test case testing the performance of codegen for GEMM operation.
Protocol
Results
Known Limitations:
memrefto store the result. However, in matrix multiplication, the sparsity of the output is not solely determined by the sparsity of the inputs. For instance, the result of aCSR @ CSRcould be either dense or sparse, depending on the input data.