run.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. # run.sh — DeepSeek-V4-Flash-Vision-Exp native vision mod
  3. #
  4. # Ported from MiaAI-Lab/DeepSeek-v4-Flash-DSpark-2x-DGX-Spark
  5. # (patches/hotfix-dsv4-vision-exp.py + patches/vision_exp/). Adds native image
  6. # input to a vLLM that serves deepseek-ai/DeepSeek-V4-Flash-Vision-Exp.
  7. #
  8. # Target layout (Anemll / eugr-style DeepSeek-V4 fork):
  9. # model.py at /usr/local/lib/python3.12/dist-packages/vllm/models/deepseek_v4/
  10. # nvidia/model.py with DeepseekV4ForCausalLM / DeepseekV4MoE /
  11. # DeepseekV4Model classes (apply.py's monkeypatch anchors), plus
  12. # nvidia/dspark.py and tokenizers/deepseek_v4_encoding.py. If the encoding
  13. # file has no IMAGE_PLACEHOLDER the encoding patch is skipped gracefully.
  14. #
  15. # What it does:
  16. # 1. Stage patches/vision_exp -> /opt/dspark-patches/vision_exp (the injected
  17. # import hook reads from there).
  18. # 2. Run hotfix-dsv4-vision-exp.py: injects a fail-closed import hook into
  19. # nvidia/model.py that builds the ViT+Aligner, maps vision./aligner./
  20. # image_*/bias_vl weights, registers a vLLM multimodal processor, makes
  21. # embed_input_ids scatter image-block embeddings, and remaps the DSpark
  22. # draft ffn.gate.bias_vl -> e_score_correction_bias_vl in dspark.py.
  23. # 3. Clear __pycache__ and the stale $VLLM_CACHE_ROOT/modelinfos/ cache (the
  24. # disk-cached multimodal inspection keyed by module+class — a prior
  25. # text-only boot would leave a stale entry).
  26. #
  27. # Notes: images go in user messages only (official restriction). Max 384 image
  28. # tokens per image. num_nextn_predict_layers=3 on Vision-Exp -> DSpark k must be
  29. # a multiple of 3 (k=6 recommended; k=5 is rejected).
  30. set -euo pipefail
  31. PREFIX="[deepseek-v4-vision-exp]"
  32. MOD_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  33. SITE="/usr/local/lib/python3.12/dist-packages/vllm"
  34. MODEL="$SITE/models/deepseek_v4/nvidia/model.py"
  35. DSPARK="$SITE/models/deepseek_v4/nvidia/dspark.py"
  36. ENCODING="$SITE/tokenizers/deepseek_v4_encoding.py"
  37. PATCHES_DIR="/opt/dspark-patches"
  38. HOTFIX="$MOD_DIR/hotfix-dsv4-vision-exp.py"
  39. echo "=== deepseek-v4-vision-exp native vision mod ==="
  40. if [ ! -d "$SITE" ]; then
  41. echo "$PREFIX vLLM tree not found at $SITE" >&2
  42. exit 1
  43. fi
  44. if [ ! -f "$MODEL" ]; then
  45. echo "$PREFIX nvidia/model.py not found at $MODEL" >&2
  46. exit 1
  47. fi
  48. if [ ! -f "$DSPARK" ]; then
  49. echo "$PREFIX nvidia/dspark.py not found at $DSPARK" >&2
  50. exit 1
  51. fi
  52. if [ ! -f "$HOTFIX" ] || [ ! -d "$MOD_DIR/vision_exp" ]; then
  53. echo "$PREFIX hotfix or vision_exp package missing under $MOD_DIR" >&2
  54. exit 1
  55. fi
  56. # Stage the vision_exp package where the injected import hook looks for it.
  57. mkdir -p "$PATCHES_DIR"
  58. rm -rf "$PATCHES_DIR/vision_exp"
  59. cp -r "$MOD_DIR/vision_exp" "$PATCHES_DIR/vision_exp"
  60. echo "$PREFIX staged vision_exp -> $PATCHES_DIR/vision_exp"
  61. # Run the hotfix. It patches model.py + dspark.py in place (idempotent,
  62. # fail-closed on anchor drift) and clears the encoding.py only if that file
  63. # actually has placeholder logic (no-op on lineages without it).
  64. python3 "$HOTFIX"
  65. # Clear stale bytecode + the disk-cached multimodal inspection.
  66. find "$SITE" -name "__pycache__" -type d -prune -exec rm -rf {} + 2>/dev/null || true
  67. CACHE_ROOT="${VLLM_CACHE_ROOT:-$HOME/.cache/vllm}"
  68. if [ -d "$CACHE_ROOT/modelinfos" ]; then
  69. rm -rf "$CACHE_ROOT/modelinfos"
  70. echo "$PREFIX cleared stale $CACHE_ROOT/modelinfos"
  71. fi
  72. echo "=== OK: DeepSeek-V4-Flash-Vision-Exp native vision installed ==="