Skip to content

Commit 16f8110

Browse files
I3eg1nnerclaude
andauthored
[Bugfix][CPU][RISC-V] Fix VLEN detection for RVV attention path (vllm-project#47532)
Signed-off-by: liutong <liutong@iscas.ac.cn> Co-authored-by: Claude <noreply@anthropic.com>
1 parent d9c1767 commit 16f8110

3 files changed

Lines changed: 26 additions & 18 deletions

File tree

cmake/cpu_extension.cmake

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,24 @@ elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "riscv64")
178178
# Override with -DVLLM_RVV_VLEN=128 or -DVLLM_RVV_VLEN=256 for RVV.
179179
if(NOT DEFINED VLLM_RVV_VLEN)
180180
# Auto-detect: find the largest zvl<N>b in /proc/cpuinfo isa line.
181-
if(EXISTS /proc/cpuinfo)
181+
# Skip when cross-compiling — /proc/cpuinfo describes the build host.
182+
if(CMAKE_CROSSCOMPILING)
183+
message(STATUS "Cross-compiling: skipping VLEN auto-detection from /proc/cpuinfo")
184+
elseif(EXISTS /proc/cpuinfo)
182185
file(READ /proc/cpuinfo _cpuinfo)
183186
set(_best 0)
184187
foreach(_n IN ITEMS 128 256 512 1024)
185188
if(_cpuinfo MATCHES "zvl${_n}b")
186189
set(_best ${_n})
187190
endif()
188191
endforeach()
192+
# Only VLEN=128 and VLEN=256 are supported by the RVV kernels.
193+
if(_best GREATER 256)
194+
message(WARNING
195+
"Detected VLEN=${_best} but only 128/256 are supported; "
196+
"clamping to 256")
197+
set(_best 256)
198+
endif()
189199
if(_best GREATER 0)
190200
set(VLLM_RVV_VLEN ${_best})
191201
endif()

csrc/cpu/cpu_attn.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ static inline cpu_attention::Fp8KVCacheDataType parse_fp8_kv_dtype(
1313

1414
bool cpu_attn_has_isa(const std::string& isa) {
1515
if (isa == "rvv") {
16-
#if defined(__riscv) && defined(__riscv_v_min_vlen) && __riscv_v_min_vlen == 128
16+
#if defined(__riscv) && defined(__riscv_v_min_vlen) && \
17+
(__riscv_v_min_vlen == 128 || __riscv_v_min_vlen == 256)
1718
return true;
1819
#else
1920
return false;

vllm/v1/attention/backends/cpu_attn.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -437,27 +437,24 @@ def _riscv_supports_rvv() -> bool:
437437
The RVV path is compiled whenever __riscv_v_min_vlen is defined, so
438438
we check that at least one supported zvl<N>b is advertised.
439439
"""
440-
try:
441-
with open("/proc/cpuinfo") as f:
442-
cpuinfo = f.read()
443-
except OSError:
444-
return False
445-
# If VLEN >= 512 is detected, the RVV kernel was not compiled.
446-
if any(f"zvl{n}b" in cpuinfo for n in (512, 1024)):
447-
return False
448-
449-
# zvl128b or zvl256b explicitly advertised -> RVV kernel available.
450-
if any(f"zvl{n}b" in cpuinfo for n in (128, 256)):
451-
return True
452-
453-
# No zvl<N>b flag at all (e.g. some hardware reports zve* without
454-
# a VLEN hint). Delegate to the C++ compile-time check instead.
440+
# The C++ compile-time check is the ground truth: it knows which
441+
# VLEN the binary was actually compiled for. The cpuinfo check
442+
# below is only a fast-path shortcut.
455443
try:
456444
import torch
457445

458-
return torch.ops._C.cpu_attn_has_isa("rvv")
446+
if torch.ops._C.cpu_attn_has_isa("rvv"):
447+
return True
459448
except Exception:
449+
pass
450+
451+
# Fallback: check /proc/cpuinfo for zvl128b/zvl256b.
452+
try:
453+
with open("/proc/cpuinfo") as f:
454+
cpuinfo = f.read()
455+
except OSError:
460456
return False
457+
return any(f"zvl{n}b" in cpuinfo for n in (128, 256))
461458

462459

463460
def _get_attn_isa(

0 commit comments

Comments
 (0)