[MLAS] Add RISC-V RVV backend for MLAS QNBitGemm (MatMulNBits)#29537
Conversation
|
Hi, @hariharans29 |
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. |
Review: PR #29537 —
|
|
If you have resolved the Copilot comments in the code - can you please manually resolve the comments visible on the PR ? |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Okay, I've resolved. Thank you. |
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.