[MLAS] Add RISC-V RVV backend for MLAS QNBitGemm (MatMulNBits)#29537
Open
velonica0 wants to merge 3 commits into
Open
[MLAS] Add RISC-V RVV backend for MLAS QNBitGemm (MatMulNBits)#29537velonica0 wants to merge 3 commits into
velonica0 wants to merge 3 commits into
Conversation
Contributor
Author
|
Hi, @hariharans29 |
| @@ -0,0 +1,192 @@ | |||
| /*++ | |||
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a RISC-V Vector (RVV) backend to MLAS’s QNBitGemm (MatMulNBits) so n-bit quantized GEMM can execute on rv64gcv (and optionally Zvfh for fp16), enabling vectorized kernels and associated packing/workspace plumbing.
Changes:
- Introduces RVV implementations for QNBitGemm packing, per-GEMM workspace sizing, and SQ4/SQ8 compute kernels, plus optional Zvfh fp16 dequant + GEMM.
- Wires the new RVV QNBitGemm dispatch into MLAS platform initialization and exports the dispatch symbol.
- Extends MLAS unit tests to validate RISC-V SQ8 prepack layout expectations and adds an RVV fp16 end-to-end test.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| cmake/onnxruntime_mlas.cmake | Adds the new RVV and Zvfh source files to the MLAS build for RISC-V. |
| onnxruntime/core/mlas/lib/mlasi.h | Declares the RVV QNBitGemm dispatch for platform wiring. |
| onnxruntime/core/mlas/lib/platform.cpp | Assigns the RVV QNBitGemm dispatch when MLAS_USE_RVV is enabled on RISC-V. |
| onnxruntime/core/mlas/lib/riscv64/qnbitgemm_kernel_rvv.cpp | Implements RVV QNBitGemm packing/workspace helpers and SQ4/SQ8 kernels; defines the dispatch object. |
| onnxruntime/core/mlas/lib/riscv64/hqnbitgemm_kernel_rvv.cpp | Implements Zvfh fp16 dequant + fp16 GEMM kernel for HQNBIT_CompFp16 paths. |
| onnxruntime/test/mlas/unittest/test_sq8bitgemm.cpp | Adds MLAS_TARGET_RISCV64-specific reference packing/layout checks for SQ8 prepack tests. |
| onnxruntime/test/mlas/unittest/test_qnbitgemm_rvv_fp16.cpp | Adds RISC-V Zvfh fp16 end-to-end tests for HQ4/HQ8 paths. |
Comment on lines
+1008
to
+1012
| // SQNBIT_CompFp32 (4-bit) is wired up: the two kernels above plus the portable | ||
| // packing/workspace helpers make MlasIsQNBitGemmAvailable() return true for | ||
| // that variant. The CompInt8 and 8-bit compute kernels remain null and fall | ||
| // back to the generic path. | ||
| // |
| } | ||
| } | ||
| } | ||
| #else // not MLAS_TARGET_ARM64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
MLAS has hand-optimized QNBitGemm kernels for x86 (AVX2/AVX512), ARM (NEON), and LoongArch, but none for RISC-V — on RISC-V the dispatch was null, so MlasIsQNBitGemmAvailable returned false and MatMulNBits fell back to the generic scalar path, leaving the RISC-V Vector unit idle for the hottest kernel in quantized-LLM inference.
This PR adds a native RISC-V Vector (RVV) implementation of the QNBitGemm dispatch so weight-quantized matmul runs on the vector unit.
What's implemented
All compute modes the operator selects among, for rv64gcv:
plus the packing / quantization / workspace-sizing plumbing each mode needs (including the 3-call PrePack protocol).
Design notes
Files
New:
Modified:
Performance (RVV, VLEN=256, N=256, K=2048)
The K-reduction runs at LMUL=4; profiling showed the kernels are widening-multip), so cutting per-chunk instruction overhead is the lever.
Testing
Built and run on real RISC-V hardware (K3 board, rv64gcv_zvfh, VLEN=256) via onnxruntime_mlas_test:
Build
Enable fp16 with -Donnxruntime_USE_RVV_ZVFH=ON. The 4-/8-bit int8/fp32 paths build with the existing onnxruntime_USE_RVV.