Skip to content

perf(bitcannon): add RVV literal matching - #2650

Merged
SBrandeis merged 5 commits into
huggingface:mainfrom
xiazhuozhao:perf/rvv-literal
Nov 12, 2026
Merged

SBrandeis merged 5 commits into
huggingface:mainfrom
xiazhuozhao:perf/rvv-literal

Conversation

@xiazhuozhao

@xiazhuozhao xiazhuozhao commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add Linux riscv64 RVV implementations of eq64 and any64 for literal matching. RISC-V currently compares each byte in these 64-byte blocks individually; the new kernels use vmseq.vx with vsm.v to produce the match bitmap and vfirst.m to skip blocks without a match.

Linux AT_HWCAP detection is cached with OnceLock; 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: m1 uses one register, while m4 groups four registers for a vector operand. With 8-bit elements (e8), an m4 group holds 4 × VLEN / 8 bytes. These terms are defined in the RISC-V vector specification.

This implementation always uses e8,m4 and 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 for riscv64gc-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.

Platform OS / kernel VLEN CPU affinity
Sophgo SG2044 openEuler 24.03 LTS / Linux 6.12.32-0.0.0.0.riscv64 128 bits CPU48
Muse Pi Pro, SpacemiT K1 X60 Bianbu Star 2.1.7 / Linux 6.6.63 256 bits CPU0
SpacemiT K3 X100 Ubuntu 26.04 / Linux 6.18.3-6-spacemit-generic 256 bits CPU0
SpacemiT K3 A100 Ubuntu 26.04 / Linux 6.18.3-6-spacemit-generic 1024 bits CPU8

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.

Model SG2044 K1 X60 K3 X100 K3 A100
gemma-4 1.030× 1.086× 1.034× 1.042×
llama-2 1.041× 1.066× 1.038× 1.056×
t5-base 1.038× 1.035× 1.062× 1.031×
gpt2 1.003× 0.982× 1.015× 0.992×

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
Model Corpus Baseline (ns/byte) Patched (ns/byte) Speedup
gemma-4 chinese 134.984 136.367 0.990×
gemma-4 english 409.481 385.132 1.063×
gemma-4 japanese 136.152 131.189 1.038×
gpt2 chinese 78.131 77.739 1.005×
gpt2 english 34.918 35.113 0.994×
gpt2 japanese 92.643 91.901 1.008×
llama-2 chinese 47.112 43.654 1.079×
llama-2 english 238.620 242.505 0.984×
llama-2 japanese 38.678 36.424 1.062×
t5-base chinese 245.269 238.453 1.029×
t5-base english 481.211 454.592 1.059×
t5-base japanese 251.548 245.155 1.026×
K1 X60: per-case encode results
Model Corpus Baseline (ns/byte) Patched (ns/byte) Speedup
gemma-4 chinese 370.593 328.913 1.127×
gemma-4 english 1036.731 1012.981 1.023×
gemma-4 japanese 355.431 319.952 1.111×
gpt2 chinese 258.719 266.139 0.972×
gpt2 english 121.085 120.460 1.005×
gpt2 japanese 290.514 299.945 0.969×
llama-2 chinese 171.426 159.543 1.074×
llama-2 english 917.792 912.167 1.006×
llama-2 japanese 134.165 119.878 1.119×
t5-base chinese 570.914 547.433 1.043×
t5-base english 1144.898 1109.455 1.032×
t5-base japanese 614.847 597.676 1.029×
K3 X100: per-case encode results
Model Corpus Baseline (ns/byte) Patched (ns/byte) Speedup
gemma-4 chinese 88.659 85.351 1.039×
gemma-4 english 292.269 284.284 1.028×
gemma-4 japanese 88.909 85.981 1.034×
gpt2 chinese 64.149 61.712 1.039×
gpt2 english 29.516 29.490 1.001×
gpt2 japanese 77.882 77.466 1.005×
llama-2 chinese 35.163 33.494 1.050×
llama-2 english 209.843 206.154 1.018×
llama-2 japanese 30.150 28.836 1.046×
t5-base chinese 237.049 219.665 1.079×
t5-base english 378.235 369.888 1.023×
t5-base japanese 245.175 226.183 1.084×
K3 A100: per-case encode results
Model Corpus Baseline (ns/byte) Patched (ns/byte) Speedup
gemma-4 chinese 205.550 194.691 1.056×
gemma-4 english 661.266 650.711 1.016×
gemma-4 japanese 207.858 196.846 1.056×
gpt2 chinese 161.295 163.985 0.984×
gpt2 english 73.315 73.765 0.994×
gpt2 japanese 189.805 190.005 0.999×
llama-2 chinese 100.585 92.326 1.089×
llama-2 english 514.924 511.138 1.007×
llama-2 japanese 83.272 77.700 1.072×
t5-base chinese 418.121 405.072 1.032×
t5-base english 758.258 733.861 1.033×
t5-base japanese 426.069 415.077 1.026×

Validation

  • Validated both kernels against scalar results for all 256 byte values and 64 input alignments, including all-match and no-match bitmaps, using a standalone local test.
  • Ran the bitcannon unit and integration tests on SG2044, X60, X100, A100 and native x86-64.
  • Guard-page checks passed on all four RISC-V CPU configurations.
  • All 48 model/corpus/platform combinations produced identical token ID hashes between baseline and patched runs. A separate tokbench comparison against hf-tokenizers 0.23.1 verified all 12 model/corpus combinations on SG2044, X100 and A100.
  • QEMU 7.2 tests passed with V disabled and with VLEN=128; the VLEN=128 guard-page checks also passed.
  • cargo fmt -p bitcannon --check and cargo clippy -p bitcannon --lib --target riscv64gc-unknown-linux-gnu -- -D warnings passed.
  • The full Rust workspace test suite passed on K3 X100 (333 passed, 0 failed, 3 ignored), using cargo test --workspace --release --target riscv64gc-unknown-linux-gnu --locked --no-fail-fast -- --nocapture with a remote runner.

@xiazhuozhao
xiazhuozhao marked this pull request as ready for review November 4, 2026 17:57

@SBrandeis SBrandeis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your contribution!

Comment thread tokenizers/bitcannon/src/simd/rvv.rs Resolved
Comment thread tokenizers/bitcannon/src/simd/rvv.rs Resolved
xiazhuozhao and others added 3 commits November 7, 2026 20:06
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>
Comment thread tokenizers/bitcannon/src/lib.rs Resolved
xiazhuozhao and others added 2 commits November 11, 2026 14:44
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>
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@SBrandeis SBrandeis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@SBrandeis
SBrandeis merged commit 0a2fd6a into huggingface:main Nov 12, 2026
30 of 34 checks passed
@xiazhuozhao

Copy link
Copy Markdown
Contributor Author

lgtm

Thank you for the approval and helpful feedback!

@SBrandeis SBrandeis mentioned this pull request Nov 19, 2026
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.

3 participants