Repository navigation
perf(bitcannon): add RVV literal matching - #2650
Merged
Merged
Conversation
xiazhuozhao
marked this pull request as ready for review
November 4, 2026 17:57
SBrandeis
reviewed
Nov 6, 2026
SBrandeis
left a comment
Collaborator
There was a problem hiding this comment.
Thanks a lot for your contribution!
Add runtime-dispatched RVV eq64 and any64 kernels on RV64 Linux. Process 64-byte blocks with e8/m4 and retain the portable path for partial blocks and systems without the V extension. Co-authored-by: Fei Zhang <zhangfei@iscas.ac.cn>
Centralize has_rvv in lib.rs and cache its result with AtomicU8, following has_ssse3. Co-authored-by: Fei Zhang <zhangfei@iscas.ac.cn>
Query PR_RISCV_V_GET_CONTROL before using RVV and fall back to HWCAP when the interface is unavailable. Cache enabled status per thread and recheck disabled threads to observe later enablement. Co-authored-by: Fei Zhang <zhangfei@iscas.ac.cn>
xiazhuozhao
force-pushed
the
perf/rvv-literal
branch
from
November 7, 2026 12:06
0f51fcf to
07fc707
Compare
SBrandeis
reviewed
Nov 10, 2026
Cache both enabled and disabled RVV states to avoid repeated prctl() calls for every 64-byte block when vector support is disabled. Threads that enable RVV after the first check continue using the correct scalar fallback. Co-authored-by: Simon Brandeis <33657802+SBrandeis@users.noreply.github.com>
Co-authored-by: Fei Zhang <zhangfei@iscas.ac.cn>
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Contributor
Author
Thank you for the approval and helpful feedback! |
Closed
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 Linux riscv64 RVV implementations of
eq64andany64for literal matching. RISC-V currently compares each byte in these 64-byte blocks individually; the new kernels usevmseq.vxwithvsm.vto produce the match bitmap andvfirst.mto skip blocks without a match.Linux
AT_HWCAPdetection is cached withOnceLock; CPUs without V and partial blocks use the existing scalar path. The change adds no dependencies and preserves the existing matching algorithm and offsets.RVV configuration
RV64 is the 64-bit RISC-V architecture; RVV is its vector extension. VLEN is the hardware width of one vector register and can differ between processors. LMUL is RVV's vector register grouping multiplier:
m1uses one register, whilem4groups four registers for a vector operand. With 8-bit elements (e8), an m4 group holds4 × VLEN / 8bytes. These terms are defined in the RISC-V vector specification.This implementation always uses
e8,m4and requests an active vector length of 64. At VLEN=128, the group holds exactly 64 bytes; wider implementations have more capacity but still load and compare only those 64 bytes. The full V extension guarantees VLEN of at least 128 bits, so the same kernel handles the existing 64-byte block and 64-bit result mask on every supported processor.Fixed m4 keeps this small kernel independent of the hardware vector width without adding VLEN-dependent dispatch or per-iterator configuration state. A smaller group would require multiple chunks on 128-bit hardware.
Benchmarks
Measured with tokbench against tokenizers
fb9f0f83f446db6fc4f05ff795a81d8e1e6f36e5, with and without this patch. Both versions were cross-compiled with Rust 1.99.1 forriscv64gc-unknown-linux-gnu, using the GCC 11 linker and tokbench's release profile. V was not enabled globally.Test systems
The systems cover three RISC-V processor designs and four CPU configurations:
Sophgo SG2044: a 64-core RISC-V server processor, with 128-bit vector registers. It exercises the minimum vector width supported by this kernel.
Muse Pi Pro / SpacemiT K1: a development board with eight X60 CPU cores and 256-bit vector registers.
SpacemiT K3: a heterogeneous processor with eight X100 general-purpose CPU cores and eight A100 compute cores. X100 has 256-bit vector registers; A100 has 1024-bit vector registers. Both groups execute RISC-V code and were measured separately, with the process assigned to the corresponding core group.
All measurements are single-threaded. X100 and A100 are two core groups on the same K3. Within each run, tokbench uses one warm-up slice and five disjoint timed slices, reporting median time per byte. The tables average the two run medians per version. Speedup is baseline / patched; model summaries are equal-weight geometric means across English, Chinese and Japanese.
GPT-2 is included to check performance on the ByteLevel path, as a baseline check, which is not targeted by this optimization.
SG2044: per-case encode results
K1 X60: per-case encode results
K3 X100: per-case encode results
K3 A100: per-case encode results
Validation
hf-tokenizers0.23.1 verified all 12 model/corpus combinations on SG2044, X100 and A100.cargo fmt -p bitcannon --checkandcargo clippy -p bitcannon --lib --target riscv64gc-unknown-linux-gnu -- -D warningspassed.cargo test --workspace --release --target riscv64gc-unknown-linux-gnu --locked --no-fail-fast -- --nocapturewith a remote runner.