Files

46 lines
2.0 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
# 构建期脚本:把 PATH 上的 vllm 换成 wrapper,原二进制改名成 *_real。
#
# 用法(在 Dockerfile 里):bash install_patch.sh /tmp
# /tmp/fix_tokenizer.py 修复脚本
# /tmp/vllm_wrapper.sh wrapper 模板(含 @@VLLM_REAL@@ / @@FIX_PYTHON@@ 占位)
#
# 为什么要现场解析路径:不同加速卡镜像把 vllm 装在不同位置(有的在 /usr/local/bin,
# 有的在 dist-packages/bin),写死路径会打错对象。wrapper 需要落在 vllm 原来的位置上,
# 这样调用方 `--entrypoint vllm` 才会命中。
set -euo pipefail
SRC_DIR="${1:-/tmp}"
FIXER_DEST=/usr/local/bin/fix_tokenizer.py
# 1) 找到 vllm 的真实路径
# 优先 PATH;有些加速卡镜像把 vllm 装进独立 python 前缀但没加进 PATH,再按常见前缀兜底。
# 两边都找不到就构建期失败,不产出「打了补丁其实没打上」的镜像。
VLLM_BIN="$(command -v vllm || true)"
if [ -z "$VLLM_BIN" ]; then
for cand in /usr/local/python3.11.13/bin/vllm /usr/local/bin/vllm /usr/bin/vllm /opt/conda/bin/vllm; do
if [ -x "$cand" ]; then VLLM_BIN="$cand"; break; fi
done
fi
[ -n "$VLLM_BIN" ] || { echo "ERROR: vllm not found (PATH or known prefixes)" >&2; exit 1; }
# 2) 解释器:优先用原 vllm 脚本的 shebang,保证和 vllm 同一个 python
PY="$(head -n 1 "$VLLM_BIN" \
| sed -e 's/^#!//' -e 's|^/usr/bin/env[[:space:]]*||' -e 's|^/bin/env[[:space:]]*||' \
-e 's/[[:space:]].*//')"
if [ -z "$PY" ] || [ ! -x "$PY" ]; then
PY="$(command -v python3 || true)"
fi
[ -n "$PY" ] || { echo "ERROR: python3 not found" >&2; exit 1; }
# 3) 装修复脚本,原 vllm 改名,wrapper 顶上
install -m 0644 "$SRC_DIR/fix_tokenizer.py" "$FIXER_DEST"
mv "$VLLM_BIN" "${VLLM_BIN}_real"
sed -e "s|@@VLLM_REAL@@|${VLLM_BIN}_real|g" \
-e "s|@@FIX_PYTHON@@|${PY}|g" \
"$SRC_DIR/vllm_wrapper.sh" > "$VLLM_BIN"
chmod 0755 "$VLLM_BIN"
echo "patched: $VLLM_BIN -> ${VLLM_BIN}_real (python: $PY)"