[refact] unified soc_version code (#4359)

### What this PR does / why we need it?

Currently, there are two paths to judge the chip type in code,
`get_ascend_soc_version` use `get_soc_version` api in torch_npu, and
`is_310p` `use _build_info.__soc_version__`, which generate when
install. We need to unify the two paths.

We need to unify these codes based on the following points:

1. We need to ensure consistency in chip type judgment between compiling
and running states;
2. In compiling state, we need chip type to complete op's compilation,
but in running state, we only need device
type(910B/910_93/310P/910_95/etc) to make code branch judgement;
3. In compiling state, torch_npu may not have been installed yet, so we
can't use torch_npu's api.

Based on the above points, we have made the following changes:

1. When user set env `SOC_VERSION`, use it; when not set, query
soc_version by `npu-smi`;
2. generate device_type based on soc_version when compiling, and write
`__device_type__` instead of `__soc_version__` in `_build_info.py`;
3. In running state, use `__device_type__` to judge code branch.

### Does this PR introduce _any_ user-facing change?

When not set env `SOC_VERSION`, it will not be `ASCEND910B1` by default,
we will query soc_version by `npu-smi`. And env `SOC_VERSION` must be in
the list `soc_to_device` in `setup.py`.

- vLLM version: v0.11.0
- vLLM main:
2918c1b49c

Signed-off-by: zzzzwwjj <1183291235@qq.com>
This commit is contained in:
zzzzwwjj
2025-11-26 14:28:55 +08:00
committed by GitHub
parent a91e76cd84
commit 136ea9ff56
42 changed files with 361 additions and 243 deletions

View File

@@ -65,25 +65,103 @@ def check_or_set_default_env(cmake_args,
return cmake_args
def get_value_from_lines(lines: List[str], key: str) -> str:
for line in lines:
line = ' '.join(line.split())
if key in line:
return line.split(':')[-1].strip()
return ""
def get_chip_info() -> str:
try:
npu_info_lines = subprocess.check_output(
['npu-smi', 'info', '-l']).decode().strip().split('\n')
npu_id = int(get_value_from_lines(npu_info_lines, 'NPU ID'))
chip_info_lines = subprocess.check_output(
['npu-smi', 'info', '-t', 'board', '-i',
str(npu_id), '-c', '0']).decode().strip().split('\n')
chip_name = get_value_from_lines(chip_info_lines, 'Chip Name')
chip_type = get_value_from_lines(chip_info_lines, 'Chip Type')
npu_name = get_value_from_lines(chip_info_lines, 'NPU Name')
if "310" in chip_name:
# 310P case
assert chip_type
return (chip_type + chip_name).lower()
elif "910" in chip_name:
if chip_type:
# A2 case
assert not npu_name
return (chip_type + chip_name).lower()
else:
# A3 case
assert npu_name
return (chip_name + '_' + npu_name).lower()
else:
# TODO(zzzzwwjj): Currently, A5's chip name has not determined yet.
raise ValueError(
f"Unable to recognize chip name: {chip_name}, please manually set env SOC_VERSION"
)
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Get chip info failed: {e}")
except FileNotFoundError:
# cpu envir, release code case, return `ascend910b1` by default
return "ascend910b1"
envs = load_module_from_path("envs",
os.path.join(ROOT_DIR, "vllm_ascend", "envs.py"))
soc_version = get_chip_info()
if not envs.SOC_VERSION:
envs.SOC_VERSION = soc_version
else:
if envs.SOC_VERSION != soc_version:
logging.warning(
f"env SOC_VERSION: {envs.SOC_VERSION} is not equal to soc_version from npu-smi: {soc_version}"
)
def gen_build_info():
soc_version = envs.SOC_VERSION
if not soc_version:
raise ValueError(
"SOC version is not set. Please set SOC_VERSION environment variable."
)
if "310" in soc_version and not envs.COMPILE_CUSTOM_KERNELS:
raise ValueError(
"SOC version 310 only supports custom kernels. Please set COMPILE_CUSTOM_KERNELS=1 to enable custom kernels."
)
# TODO(zzzzwwjj): Add A5 case
soc_to_device = {
"ascend910b1": "_910B",
"ascend910b2": "_910B",
"ascend910b2c": "_910B",
"ascend910b3": "_910B",
"ascend910b4": "_910B",
"ascend910b4-1": "_910B",
"ascend910_9391": "_910_93",
"ascend910_9381": "_910_93",
"ascend910_9372": "_910_93",
"ascend910_9392": "_910_93",
"ascend910_9382": "_910_93",
"ascend910_9362": "_910_93",
"ascend310p1": "_310P",
"ascend310p3": "_310P",
"ascend310p5": "_310P",
"ascend310p7": "_310P",
"ascend310p3vir01": "_310P",
"ascend310p3vir02": "_310P",
"ascend310p3vir04": "_310P",
"ascend310p3vir08": "_310P",
}
assert soc_version in soc_to_device, f"Undefined soc_version: {soc_version}. Please file an issue to vllm-ascend."
device_type = soc_to_device[soc_version]
package_dir = os.path.join(ROOT_DIR, "vllm_ascend", "_build_info.py")
with open(package_dir, "w+") as f:
f.write('# Auto-generated file\n')
f.write(f"__soc_version__ = '{soc_version}'\n")
f.write(f"__device_type__ = '{device_type}'\n")
f.write(f"__sleep_mode_enabled__ = {envs.COMPILE_CUSTOM_KERNELS}\n")
logging.info(f"Generated _build_info.py with SOC version: {soc_version}")