Files

68 lines
2.7 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 包在 vllm 前面的 wrapper:只拦 "serve <model_dir>",其它子命令原样透传。
# 构建时会把 @@VLLM_REAL@@ / @@FIX_PYTHON@@ 替换成镜像里的真实路径。
#
# 行为:
# * 调 /usr/local/bin/fix_tokenizer.py 拿到修复目录(没有就什么都不输出);
# * 拿到目录才追加 --tokenizer <dir>,否则原样启动 —— 与原始镜像完全一致;
# * 修复脚本挂了/超时/输出非法路径,一律按原样启动,绝不因此把容器搞挂;
# * 调用方已经显式给了 --tokenizer 时尊重调用方,不再覆盖。
#
# 可覆盖的环境变量(默认值就是构建时烘焙进来的路径,测试时才需要改):
# FIX_TOKENIZER_PYTHON 解释器,默认取原 vllm 脚本的 shebang
# FIX_TOKENIZER_SCRIPT 修复脚本,默认 /usr/local/bin/fix_tokenizer.py
# FIX_TOKENIZER_VLLM 原 vllm 二进制,默认构建期烘焙的路径(不可用时按 wrapper 自身路径推断 *_real)
# FIX_TOKENIZER_TIMEOUT 默认 180(秒)
set -u
SELF="$(readlink -f "$0" 2>/dev/null || echo "$0")"
VLLM_BIN="${FIX_TOKENIZER_VLLM:-${VLLM_REAL:-@@VLLM_REAL@@}}"
PYTHON_BIN="${FIX_TOKENIZER_PYTHON:-@@FIX_PYTHON@@}"
FIX_SCRIPT="${FIX_TOKENIZER_SCRIPT:-/usr/local/bin/fix_tokenizer.py}"
FIX_TIMEOUT="${FIX_TOKENIZER_TIMEOUT:-180}"
# 兜底:构建期烘焙的路径不可用时,按 wrapper 自身的路径推断出 *_real
if [ ! -x "$VLLM_BIN" ] && [ -x "${SELF}_real" ]; then
VLLM_BIN="${SELF}_real"
fi
if [ ! -x "$VLLM_BIN" ]; then
echo "[fix_tokenizer] real vllm binary not found, exec fallback failed" >&2
exit 127
fi
if [ "$#" -ge 2 ] && [ "$1" = "serve" ]; then
case "$2" in
-*) ;; # 形如 "serve --help",不是模型目录,透传
*)
MODEL_DIR="$2"
shift 2
for arg in "$@"; do
case "$arg" in
--tokenizer|--tokenizer=*)
exec "$VLLM_BIN" serve "$MODEL_DIR" "$@"
;;
esac
done
FIXED_DIR=""
if [ -x "$PYTHON_BIN" ] && [ -f "$FIX_SCRIPT" ]; then
if command -v timeout >/dev/null 2>&1; then
FIXED_DIR="$(timeout "$FIX_TIMEOUT" "$PYTHON_BIN" "$FIX_SCRIPT" "$MODEL_DIR" || true)"
else
FIXED_DIR="$("$PYTHON_BIN" "$FIX_SCRIPT" "$MODEL_DIR" || true)"
fi
fi
# 只接受「存在且是目录」的路径;其余一律忽略
if [ -n "$FIXED_DIR" ] && [ -d "$FIXED_DIR" ]; then
exec "$VLLM_BIN" serve "$MODEL_DIR" --tokenizer "$FIXED_DIR" "$@"
fi
exec "$VLLM_BIN" serve "$MODEL_DIR" "$@"
;;
esac
fi
exec "$VLLM_BIN" "$@"