Skip to content
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

Codegen for sparse-dense cross-entropy #919

Closed
wants to merge 17 commits into from

Conversation

AlexRTer
Copy link
Collaborator

This PR adds a codegen pass to lower the following expression to a fused operator that exploits sparsity
sum(CSRMat * ln(denseLhs @ t(denseRhs))).
It can be run simply by enabling the codegen pipeline (--mlir-codegen) and ensuring the lhs of the elementwise multiplication is a CSRMatrix (currently --select-matrix-repr) with the corresponding cli flags.
By computing the sum directly, the pass not only avoids materializing potentially large dense matrices in the dense, right matrix multiplication, it also only computes the necessary dot products corresponding to non-zero entries in the CSRMatrix. Thus, it uses constant memory and reduces runtime significantly.

An example script to test the results (--explain mlir_codegen is optional to show the generated IR):

// RUN: ./bin/daphne --select-matrix-repr --mlir-codegen --explain mlir_codegen ./fileName.daphne

seed = 1;
sparsity = 1e-6;
sparseRows = 10_000;
sparseCols = 10_000;
hiddenDim = 20;

startGeneratingMatrices = now();
sparseLhs = rand(sparseRows, sparseCols, 0.0, 1.0, sparsity, seed);

DenseU = rand(sparseRows, hiddenDim, 0.0, 1.0, 1.0, seed + 1); // sparsity: 1.0
DenseV = rand(sparseCols, hiddenDim, 0.0, 1.0, 1.0, seed + 2); // sparsity: 1.0
endGeneratingMatrices = now();

startCalc = now();
res = sum(sparseLhs * ln(DenseU @ t(DenseV)));
endCalc = now();

print(res);
print("sparse dim: " + sparseRows + "x" + sparseCols + " (sparsity: " + sparsity + "), dense dim: " + sparseRows + "x" + hiddenDim + "@" + hiddenDim + "x" + sparseCols + "->" + sparseRows + "x" + sparseCols);
print("time to generate matrices: " + as.f64(endGeneratingMatrices - startGeneratingMatrices) * 1e-9 + ", comp. time: " + as.f64(endCalc - startCalc) * 1e-9);

A more thorough description will be given once some tests have been added.

- add linalg transposeOp
- rewrite some EwUnary/Binary Ops to handle matrix and scalar types
- use specialized Ops in EwUnary/Binary (e.g. ipowi, fpowi)
- add canonicalizer pattern for ceil/floor/round for integer types
- rework aggAll codegen
- implement aggDim codegen
- rework ewUnary/ewBinary codegen
- add tests for new codegen (scripts and filecheck)
  - rename some existing tests for better overview
- add some needed kernel instantiations (kernels.json)
- minor refactoring/formatting
- fix some typos
- general minor polishing
… specific pattern

- the pattern sum(sparse * ln(dense @ dense)) with a CSRMatrix and two DenseMatrices is replaced entirely by codegen. This avoids materializing potentially large dense intermediates and fuses the whole pattern.
- Added CSRMatrix -> MemRef helper functions/kernels to enable (one-way) interop with MLIR
  - small changes to CSRMatrix.h to enable helper functions by returning shared pointers to its underlying data arrays
  - slightly modified rewriteToCallKernelOp, kernelCatalogParser, and compilerUtils to handle single rank MemRefs
  - Added needed instantiations to kernels.json
- Modified existing codegen passes to handle inputs that are not DenseMatrices/scalars (corresponding Ops are now marked as legal if input does not meet requirements)
- Todo: add tests
- revert leftover duplicate code from merge
- remove use of format for compatibility with docker containers
- minor formatting/tidying
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant