Conversation
Vectorize BF16-to-FP32 decoding with RVV while preserving the existing bit-level conversion semantics. Load unsigned 16-bit values, zero-extend to unsigned 32-bit integers, shift left by 16 bits, and reinterpret the result as FP32. Use e16m1 loads and widen to u32m2 with vzext.vf2 before shifting and storing FP32 values. Compute the maximum vector length once for the bulk loop and use a runtime vector length for the final partial chunk. Keep inputs shorter than 16 elements on the scalar path to avoid short-vector overhead, including the single-element fast path. Guard the implementation with __riscv_vector inside the existing architecture dispatch. Limit the change to faiss/utils/bf16.h and preserve the existing x86 and non-RVV paths. Validate on native RISC-V hardware with GCC 15.3.0 and -march=rv64gcv -mabi=lp64d, based on Faiss commit b5a1463. Exhaustively check all 65,536 BF16 bit patterns and test 31 input sizes covering empty inputs, short inputs, vector boundaries, tails, and large arrays. Observe no correctness failures or output guard overwrites. The conversion requires no rounding or saturation and preserves every input bit pattern, including NaN payloads and subnormals. Representative microbenchmarks show 1.58x speedup at 16 elements, 3.88x at 128 elements, and 4.30x at 65,536 elements over the scalar implementation on the tested host. Co-authored-by: ihb2032 <hebome@foxmail.com> Co-authored-by: lyd1992 <liuyudong@iscas.ac.cn> Co-authored-by: Yuansheng <yuansheng@isrc.iscas.ac.cn>
Xlawy
force-pushed
the
rvv-bf16-decode
branch
from
September 15, 2026 08:54
486f5c2 to
307bc1e
Compare
Contributor
|
@mnorris11 has imported this pull request. If you are a Meta employee, you can view this in D121445956. |
This branch has not been deployed
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.
Summary
Add a RISC-V Vector (RVV) implementation to
decode_bf16_simd().The RVV path preserves the existing bit-level conversion semantics:
Inputs shorter than 16 elements retain the scalar path to avoid short-vector overhead. Larger inputs use fixed-VL RVV chunks followed by a runtime-VL tail.
Validation
Tested on native RISC-V hardware with GCC 15.3.0 and
-march=rv64gcv -mabi=lp64d, based on Faiss commitb5a14632a6f2bfdc824c86d683b9d6e9205c2604.Correctness:
Representative microbenchmark speedups over the scalar implementation:
Scope
This change only modifies
faiss/utils/bf16.h. It is independent of the active ScalarQuantizer RVV changes in PRs #5535 and #5539.Notes
n == 1fast path and then < 16scalar loop exist because at VLEN=128 ane16m1chunk covers only 8 elements, and for very short inputs thevsetvl/load/extend/store sequence costs more than the equivalent scalar work. The measured crossover is at 16 elements.vsetvlmax_e16m1()sovsetvlis not re-executed per iteration; only the final partial chunk callsvsetvlagain.vuint16m1→vuint32m2usesvzext.vf2(LMUL widening), so the 32-bit shift and the FP32 store run atm2. This keeps register pressure low while doubling the elements per chunk.#elif defined(__riscv_vector)inside the existing arch dispatch, so the AVX2/AVX512 paths are untouched and a build without RVV falls through to the scalar loop unchanged.Co-authored-by: ihb2032 hebome@foxmail.com
Co-authored-by: lyd1992 liuyudong@iscas.ac.cn
Co-authored-by: Yuansheng yuansheng@isrc.iscas.ac.cn