vLLM Ascend Worker 无法打开 NPU 设备

问题描述

在 Ascend NPU 上运行的 vLLM 推理服务在启动期间失败。Pod 已调度并获得 NPU,但服务容器退出并反复重启。InferenceService 状态可能会报告 ModelLoadFailed,退出代码为 1。

容器日志中包含以下来自 Ascend 软件栈的消息:

Failed to obtain the console log level. The possible causes are as follows:
1. Different containers share the same device;
2. Failed to open the character device when interacting with the OS kernel;
3. Failed to obtain the number of devices.

随后,vLLM 报告 worker 进程失败,且引擎无法启动:

Exception: WorkerProc initialization failed due to an exception in a background process. See stack trace for root cause.
RuntimeError: Engine core initialization failed. See root cause above. Failed core proc(s): {}

worker 进程通常会在记录自身的 Python 回溯之前退出,因此日志中可能不包含 aclrt 错误代码或 NPU out of memory 错误。

根本原因

vLLM worker 进程无法打开 NPU 设备文件。消息中列出的三种原因对应 Kubernetes 集群中的以下情况:

消息集群中的情况
不同容器共享同一设备另一个容器已占用该 NPU,例如,前一个容器中的进程尚未释放 NPU。
无法打开字符设备容器进程没有 /dev/davinci* 的权限。常见情况是,容器以 UID 0 运行且已移除所有 capabilities,而设备文件由 HwHiAiUser 所有,权限模式为 crw-rw----。
无法获取设备数量NPU 设备未注入容器。

以 UID 0 运行的容器只有在具备 CAP_DAC_OVERRIDE capability 时,才能绕过文件权限。Ascend 运行时示例会设置 capabilities.drop: [ALL],从而移除该 capability。因此,UID 0 既不是设备文件的所有者,也不属于其所属组,因而无法打开设备文件。以下任一配置都可能导致容器处于这种状态:

  • 运行时未设置 runAsUser。容器会以镜像的默认用户运行;许多 vLLM-ascend 镜像的默认用户是 root,即使运行时并非旨在以 root 身份运行。非 root 运行时示例通过设置 runAsUser: 1000 和 runAsNonRoot: true 来避免这种情况。
  • 运行时有意以 root 身份运行,如根用户模式,但 InferenceService 未添加设备组。

诊断原因

按顺序运行以下检查。节点级检查要求对运行 Pod 的节点具有 root 访问权限,并且该节点上可用 npu-smi 和 crictl。

1. 检查是否有其他进程占用 NPU

在节点上运行:

npu-smi info

故障容器会反复重启,因此其自身的 worker 进程通常不存在。如果进程表列出了运行在分配给故障 Pod 的 NPU 上的进程,请查找拥有该进程的 Pod:

grep -oE 'pod[0-9a-f]{8}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{4}[-_][0-9a-f]{12}' /proc/<pid>/cgroup | head -1
# Replace underscores with hyphens in the output to get the Pod UID, then:
kubectl get pod -A -o custom-columns=NS:.metadata.namespace,NAME:.metadata.name,UID:.metadata.uid | grep <pod-uid>

如果每个 NPU 都报告 No running processes found,则没有其他容器占用这些 NPU。

2. 检查是否已注入 NPU

在节点上检查故障 Pod 最近的 serving 容器:

POD_ID=$(crictl pods --namespace <namespace> --name <pod-name> --latest -q)
CONTAINER_ID=$(crictl ps -a --pod "$POD_ID" --name kserve-container -q | head -1)
crictl inspect "$CONTAINER_ID" | grep -A1 'ASCEND_VISIBLE_DEVICES'

必须设置 ASCEND_VISIBLE_DEVICES,并且其中列出的 NPU 数量必须与 Pod 请求的数量相同。

3. 排除内存 Cgroup OOM kill

如果 worker 进程因超出容器内存限制而被内核终止,也会在没有回溯的情况下退出。在节点上运行:

dmesg -T | grep -iE 'oom-kill|killed process' | tail -10

如果在故障发生时出现 serving 容器的 python 进程,请改为增加容器的内存限制。

4. 将设备文件权限与容器身份进行比较

在节点上检查设备文件的所有权和设备组:

ls -l /dev/davinci* /dev/davinci_manager /dev/devmm_svm /dev/hisi_hdc
id HwHiAiUser

然后检查 serving 容器的身份和 capabilities:

crictl inspect "$CONTAINER_ID" | grep -A8 '"user":'
crictl inspect "$CONTAINER_ID" | grep -A3 '"capabilities"'

如果无法访问节点,请改为从 Pod 中读取有效的安全上下文:

kubectl get pod <pod-name> -n <namespace> \
  -o jsonpath='{.spec.securityContext}{"\n"}{.spec.containers[?(@.name=="kserve-container")].securityContext}{"\n"}'

满足以下所有条件时,容器无法打开设备:

  • 设备文件未向其他用户授予访问权限,例如 crw-rw---- HwHiAiUser HwHiAiUser。
  • 容器 UID 不是设备文件的所有者,并且其组(gid 和 additionalGids)不包含设备组。
  • 容器没有 CAP_DAC_OVERRIDE。例如,容器配置显示 drop_capabilities: ["ALL"] 和有效的 capabilities 均为空。

解决方案

修复权限不匹配

当步骤 4 显示权限不匹配时,请选择与运行时相匹配的修复方法。

  • 运行时不需要 root:在运行时容器中设置 runAsUser: 1000 和 runAsNonRoot: true,如非 root 示例所示。此更改会应用于使用该运行时的每个服务。
  • 运行时按预期以 root 身份运行:将设备组添加到 InferenceService,如授予对 Ascend 设备文件的访问权限中所述。

在修正运行时期间,如需立即修复单个服务,可使用 merge patch 添加设备组,同时保留现有的 seccompProfile 和其他安全上下文字段。例如,当 HwHiAiUser 的组 ID 为 1000 时:

kubectl patch inferenceservice <inference-service-name> -n <namespace> --type merge \
  -p '{"spec":{"predictor":{"securityContext":{"supplementalGroups":[1000]}}}}'

释放被其他容器占用的 NPU

当步骤 1 显示进程来自另一个 Pod 时,请与该 Pod 的所有者协调解决冲突。如果进程属于已退出的容器,请等待 NPU 释放;或者在确认其容器不再存在后停止该进程。

修复设备注入问题

当步骤 2 显示缺少 ASCEND_VISIBLE_DEVICES 或其中列出的 NPU 数量不正确时,请检查 Pod 是否请求了在节点上运行的设备插件提供的 Ascend 资源、Pod 是否使用了集群配置的 Ascend runtime class,以及设备插件日志是否显示已成功为该 Pod 分配资源。

验证修复

InferenceService 会滚动更新并创建一个新的 Pod。使用 RollingUpdate 策略时,旧 Pod 会一直保留,直到新 Pod 就绪,因此请检查最近创建的 Pod:

kubectl get pod -n <namespace> -l serving.kserve.io/inferenceservice=<inference-service-name> -o wide

确认以下所有情况均成立:

  • serving 容器以设备文件所有者的 UID 运行,或者其组包含设备组。在容器中检查 id,或者在 crictl inspect 中检查 uid 和 additionalGids。
  • 日志中不再包含 Failed to obtain the console log level,且 vLLM 会继续加载模型权重。
  • InferenceService 报告 Ready,并能处理测试请求。

相关文档