Add files using upload-large-folder tool
Browse files- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_COCO_HuatuoGPT_V_34B.sh +27 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_COCO_LLaVA_7B.sh +24 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_COCO_MedGemma_27B_IT.sh +27 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_COCO_Qwen25VL_3B.sh +24 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_HuatuoGPT_V_34B.sh +24 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_HuatuoGPT_V_7B.sh +21 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_LLaVA_7B.sh +21 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_LLaVA_Med_7B.sh +21 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_MedGemma_27B_IT.sh +24 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_MedGemma_4B_IT.sh +21 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_Qwen25VL_32B.sh +24 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_Qwen25VL_7B.sh +21 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/eval/table/results/test_sqa_llava_13b_v0.json +0 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/peft/utils/save_and_load.py +79 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/__init__.py +0 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/cli.py +126 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/gradio_web_server.py +479 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/register_worker.py +26 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/sglang_worker.py +244 -0
- 之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/test_message.py +62 -0
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_COCO_HuatuoGPT_V_34B.sh
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
export CUDA_VISIBLE_DEVICES=0,2,3
|
| 6 |
+
|
| 7 |
+
MODEL_PATH="FreedomIntelligence/HuatuoGPT-Vision-34B"
|
| 8 |
+
COCO_ROOT="../benchmarks/COCO/val2014"
|
| 9 |
+
COCO_ANNOTATION_ROOT="../benchmarks/COCO"
|
| 10 |
+
OUTPUT_ROOT="attention_results/HuatuoGPT-V-34B"
|
| 11 |
+
|
| 12 |
+
python measure_attention.py \
|
| 13 |
+
--dataset COCO \
|
| 14 |
+
--q_type localization \
|
| 15 |
+
--model_path "$MODEL_PATH" \
|
| 16 |
+
--coco_root "$COCO_ROOT" \
|
| 17 |
+
--coco_annotation_root "$COCO_ANNOTATION_ROOT" \
|
| 18 |
+
--output_root "$OUTPUT_ROOT" \
|
| 19 |
+
--device auto
|
| 20 |
+
|
| 21 |
+
# python measure_attention.py \
|
| 22 |
+
# --dataset COCO \
|
| 23 |
+
# --q_type attribute \
|
| 24 |
+
# --model_path "$MODEL_PATH" \
|
| 25 |
+
# --coco_root "$COCO_ROOT" \
|
| 26 |
+
# --coco_annotation_root "$COCO_ANNOTATION_ROOT" \
|
| 27 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_COCO_LLaVA_7B.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
MODEL_PATH="llava-hf/llava-1.5-7b-hf"
|
| 6 |
+
COCO_ROOT="../benchmarks/COCO/val2014"
|
| 7 |
+
COCO_ANNOTATION_ROOT="../benchmarks/COCO"
|
| 8 |
+
OUTPUT_ROOT="attention_results/LLaVA-7B"
|
| 9 |
+
|
| 10 |
+
python measure_attention.py \
|
| 11 |
+
--dataset COCO \
|
| 12 |
+
--q_type localization \
|
| 13 |
+
--model_path "$MODEL_PATH" \
|
| 14 |
+
--coco_root "$COCO_ROOT" \
|
| 15 |
+
--coco_annotation_root "$COCO_ANNOTATION_ROOT" \
|
| 16 |
+
--output_root "$OUTPUT_ROOT"
|
| 17 |
+
|
| 18 |
+
# python measure_attention.py \
|
| 19 |
+
# --dataset COCO \
|
| 20 |
+
# --q_type attribute \
|
| 21 |
+
# --model_path "$MODEL_PATH" \
|
| 22 |
+
# --coco_root "$COCO_ROOT" \
|
| 23 |
+
# --coco_annotation_root "$COCO_ANNOTATION_ROOT" \
|
| 24 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_COCO_MedGemma_27B_IT.sh
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
export CUDA_VISIBLE_DEVICES=0,2,3
|
| 6 |
+
|
| 7 |
+
MODEL_PATH="google/medgemma-27b-it"
|
| 8 |
+
COCO_ROOT="../benchmarks/COCO/val2014"
|
| 9 |
+
COCO_ANNOTATION_ROOT="../benchmarks/COCO"
|
| 10 |
+
OUTPUT_ROOT="attention_results/MedGemma-27B-IT"
|
| 11 |
+
|
| 12 |
+
python measure_attention.py \
|
| 13 |
+
--dataset COCO \
|
| 14 |
+
--q_type localization \
|
| 15 |
+
--model_path "$MODEL_PATH" \
|
| 16 |
+
--coco_root "$COCO_ROOT" \
|
| 17 |
+
--coco_annotation_root "$COCO_ANNOTATION_ROOT" \
|
| 18 |
+
--output_root "$OUTPUT_ROOT" \
|
| 19 |
+
--device auto
|
| 20 |
+
|
| 21 |
+
# python measure_attention.py \
|
| 22 |
+
# --dataset COCO \
|
| 23 |
+
# --q_type attribute \
|
| 24 |
+
# --model_path "$MODEL_PATH" \
|
| 25 |
+
# --coco_root "$COCO_ROOT" \
|
| 26 |
+
# --coco_annotation_root "$COCO_ANNOTATION_ROOT" \
|
| 27 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_COCO_Qwen25VL_3B.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
MODEL_PATH="Qwen/Qwen2.5-VL-3B-Instruct"
|
| 6 |
+
COCO_ROOT="../benchmarks/COCO/val2014"
|
| 7 |
+
COCO_ANNOTATION_ROOT="../benchmarks/COCO"
|
| 8 |
+
OUTPUT_ROOT="attention_results/Qwen2.5VL-3B"
|
| 9 |
+
|
| 10 |
+
python measure_attention.py \
|
| 11 |
+
--dataset COCO \
|
| 12 |
+
--q_type localization \
|
| 13 |
+
--model_path "$MODEL_PATH" \
|
| 14 |
+
--coco_root "$COCO_ROOT" \
|
| 15 |
+
--coco_annotation_root "$COCO_ANNOTATION_ROOT" \
|
| 16 |
+
--output_root "$OUTPUT_ROOT"
|
| 17 |
+
|
| 18 |
+
# python measure_attention.py \
|
| 19 |
+
# --dataset COCO \
|
| 20 |
+
# --q_type attribute \
|
| 21 |
+
# --model_path "$MODEL_PATH" \
|
| 22 |
+
# --coco_root "$COCO_ROOT" \
|
| 23 |
+
# --coco_annotation_root "$COCO_ANNOTATION_ROOT" \
|
| 24 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_HuatuoGPT_V_34B.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
export CUDA_VISIBLE_DEVICES=0,2,3
|
| 6 |
+
|
| 7 |
+
MODEL_PATH="FreedomIntelligence/HuatuoGPT-Vision-34B"
|
| 8 |
+
VGMED_ROOT="../benchmarks/VGMED"
|
| 9 |
+
OUTPUT_ROOT="attention_results/HuatuoGPT-V-34B"
|
| 10 |
+
|
| 11 |
+
python measure_attention.py \
|
| 12 |
+
--dataset VGMED \
|
| 13 |
+
--q_type localization \
|
| 14 |
+
--model_path "$MODEL_PATH" \
|
| 15 |
+
--vgmed_root "$VGMED_ROOT" \
|
| 16 |
+
--output_root "$OUTPUT_ROOT" \
|
| 17 |
+
--device auto
|
| 18 |
+
|
| 19 |
+
# python measure_attention.py \
|
| 20 |
+
# --dataset VGMED \
|
| 21 |
+
# --q_type attribute \
|
| 22 |
+
# --model_path "$MODEL_PATH" \
|
| 23 |
+
# --vgmed_root "$VGMED_ROOT" \
|
| 24 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_HuatuoGPT_V_7B.sh
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
MODEL_PATH="FreedomIntelligence/HuatuoGPT-Vision-7B"
|
| 6 |
+
VGMED_ROOT="../benchmarks/VGMED"
|
| 7 |
+
OUTPUT_ROOT="attention_results/HuatuoGPT-V-7B"
|
| 8 |
+
|
| 9 |
+
python measure_attention.py \
|
| 10 |
+
--dataset VGMED \
|
| 11 |
+
--q_type localization \
|
| 12 |
+
--model_path "$MODEL_PATH" \
|
| 13 |
+
--vgmed_root "$VGMED_ROOT" \
|
| 14 |
+
--output_root "$OUTPUT_ROOT"
|
| 15 |
+
|
| 16 |
+
# python measure_attention.py \
|
| 17 |
+
# --dataset VGMED \
|
| 18 |
+
# --q_type attribute \
|
| 19 |
+
# --model_path "$MODEL_PATH" \
|
| 20 |
+
# --vgmed_root "$VGMED_ROOT" \
|
| 21 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_LLaVA_7B.sh
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
MODEL_PATH="llava-hf/llava-1.5-7b-hf"
|
| 6 |
+
VGMED_ROOT="../benchmarks/VGMED"
|
| 7 |
+
OUTPUT_ROOT="attention_results/LLaVA-7B"
|
| 8 |
+
|
| 9 |
+
python measure_attention.py \
|
| 10 |
+
--dataset VGMED \
|
| 11 |
+
--q_type localization \
|
| 12 |
+
--model_path "$MODEL_PATH" \
|
| 13 |
+
--vgmed_root "$VGMED_ROOT" \
|
| 14 |
+
--output_root "$OUTPUT_ROOT"
|
| 15 |
+
|
| 16 |
+
# python measure_attention.py \
|
| 17 |
+
# --dataset VGMED \
|
| 18 |
+
# --q_type attribute \
|
| 19 |
+
# --model_path "$MODEL_PATH" \
|
| 20 |
+
# --vgmed_root "$VGMED_ROOT" \
|
| 21 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_LLaVA_Med_7B.sh
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
MODEL_PATH="microsoft/llava-med-v1.5-mistral-7b"
|
| 6 |
+
VGMED_ROOT="../benchmarks/VGMED"
|
| 7 |
+
OUTPUT_ROOT="attention_results/LLaVA-Med-7B"
|
| 8 |
+
|
| 9 |
+
python measure_attention.py \
|
| 10 |
+
--dataset VGMED \
|
| 11 |
+
--q_type localization \
|
| 12 |
+
--model_path "$MODEL_PATH" \
|
| 13 |
+
--vgmed_root "$VGMED_ROOT" \
|
| 14 |
+
--output_root "$OUTPUT_ROOT"
|
| 15 |
+
|
| 16 |
+
# python measure_attention.py \
|
| 17 |
+
# --dataset VGMED \
|
| 18 |
+
# --q_type attribute \
|
| 19 |
+
# --model_path "$MODEL_PATH" \
|
| 20 |
+
# --vgmed_root "$VGMED_ROOT" \
|
| 21 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_MedGemma_27B_IT.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
export CUDA_VISIBLE_DEVICES=0,2,3
|
| 6 |
+
|
| 7 |
+
MODEL_PATH="google/medgemma-27b-it"
|
| 8 |
+
VGMED_ROOT="../benchmarks/VGMED"
|
| 9 |
+
OUTPUT_ROOT="attention_results/MedGemma-27B-IT"
|
| 10 |
+
|
| 11 |
+
python measure_attention.py \
|
| 12 |
+
--dataset VGMED \
|
| 13 |
+
--q_type localization \
|
| 14 |
+
--model_path "$MODEL_PATH" \
|
| 15 |
+
--vgmed_root "$VGMED_ROOT" \
|
| 16 |
+
--output_root "$OUTPUT_ROOT" \
|
| 17 |
+
--device auto
|
| 18 |
+
|
| 19 |
+
# python measure_attention.py \
|
| 20 |
+
# --dataset VGMED \
|
| 21 |
+
# --q_type attribute \
|
| 22 |
+
# --model_path "$MODEL_PATH" \
|
| 23 |
+
# --vgmed_root "$VGMED_ROOT" \
|
| 24 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_MedGemma_4B_IT.sh
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
MODEL_PATH="google/medgemma-4b-it"
|
| 6 |
+
VGMED_ROOT="../benchmarks/VGMED"
|
| 7 |
+
OUTPUT_ROOT="attention_results/MedGemma-4B-IT"
|
| 8 |
+
|
| 9 |
+
python measure_attention.py \
|
| 10 |
+
--dataset VGMED \
|
| 11 |
+
--q_type localization \
|
| 12 |
+
--model_path "$MODEL_PATH" \
|
| 13 |
+
--vgmed_root "$VGMED_ROOT" \
|
| 14 |
+
--output_root "$OUTPUT_ROOT"
|
| 15 |
+
|
| 16 |
+
# python measure_attention.py \
|
| 17 |
+
# --dataset VGMED \
|
| 18 |
+
# --q_type attribute \
|
| 19 |
+
# --model_path "$MODEL_PATH" \
|
| 20 |
+
# --vgmed_root "$VGMED_ROOT" \
|
| 21 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_Qwen25VL_32B.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
export CUDA_VISIBLE_DEVICES=0,2,3
|
| 6 |
+
|
| 7 |
+
MODEL_PATH="Qwen/Qwen2.5-VL-32B-Instruct"
|
| 8 |
+
VGMED_ROOT="../benchmarks/VGMED"
|
| 9 |
+
OUTPUT_ROOT="attention_results/Qwen2.5VL-32B"
|
| 10 |
+
|
| 11 |
+
python measure_attention.py \
|
| 12 |
+
--dataset VGMED \
|
| 13 |
+
--q_type localization \
|
| 14 |
+
--model_path "$MODEL_PATH" \
|
| 15 |
+
--vgmed_root "$VGMED_ROOT" \
|
| 16 |
+
--output_root "$OUTPUT_ROOT" \
|
| 17 |
+
--device auto
|
| 18 |
+
|
| 19 |
+
# python measure_attention.py \
|
| 20 |
+
# --dataset VGMED \
|
| 21 |
+
# --q_type attribute \
|
| 22 |
+
# --model_path "$MODEL_PATH" \
|
| 23 |
+
# --vgmed_root "$VGMED_ROOT" \
|
| 24 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/eval_scripts/measure_attention/measure_attention_VGMED_Qwen25VL_7B.sh
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# export HF_ENDPOINT=https://hf-mirror.com
|
| 3 |
+
cd "$(dirname "$0")/../.."
|
| 4 |
+
|
| 5 |
+
MODEL_PATH="Qwen/Qwen2.5-VL-7B-Instruct"
|
| 6 |
+
VGMED_ROOT="../benchmarks/VGMED"
|
| 7 |
+
OUTPUT_ROOT="attention_results/Qwen2.5VL-7B"
|
| 8 |
+
|
| 9 |
+
python measure_attention.py \
|
| 10 |
+
--dataset VGMED \
|
| 11 |
+
--q_type localization \
|
| 12 |
+
--model_path "$MODEL_PATH" \
|
| 13 |
+
--vgmed_root "$VGMED_ROOT" \
|
| 14 |
+
--output_root "$OUTPUT_ROOT"
|
| 15 |
+
|
| 16 |
+
# python measure_attention.py \
|
| 17 |
+
# --dataset VGMED \
|
| 18 |
+
# --q_type attribute \
|
| 19 |
+
# --model_path "$MODEL_PATH" \
|
| 20 |
+
# --vgmed_root "$VGMED_ROOT" \
|
| 21 |
+
# --output_root "$OUTPUT_ROOT"
|
之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/eval/table/results/test_sqa_llava_13b_v0.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/peft/utils/save_and_load.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2023-present the HuggingFace Inc. team.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
from .config import PeftType
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def get_peft_model_state_dict(model, state_dict=None):
|
| 20 |
+
"""
|
| 21 |
+
Get the state dict of the Peft model.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
model ([`PeftModel`]): The Peft model. When using torch.nn.DistributedDataParallel, DeepSpeed or FSDP,
|
| 25 |
+
the model should be the underlying model/unwrapped model (i.e. model.module).
|
| 26 |
+
state_dict (`dict`, *optional*, defaults to `None`):
|
| 27 |
+
The state dict of the model. If not provided, the state dict of the model
|
| 28 |
+
will be used.
|
| 29 |
+
"""
|
| 30 |
+
if state_dict is None:
|
| 31 |
+
state_dict = model.state_dict()
|
| 32 |
+
if model.peft_config.peft_type == PeftType.LORA:
|
| 33 |
+
# to_return = lora_state_dict(model, bias=model.peft_config.bias)
|
| 34 |
+
# adapted from `https://github.com/microsoft/LoRA/blob/main/loralib/utils.py`
|
| 35 |
+
# to directly with the state dict which is necessary when using DeepSpeed or FSDP
|
| 36 |
+
bias = model.peft_config.bias
|
| 37 |
+
if bias == "none":
|
| 38 |
+
to_return = {k: state_dict[k] for k in state_dict if "lora_" in k}
|
| 39 |
+
elif bias == "all":
|
| 40 |
+
to_return = {k: state_dict[k] for k in state_dict if "lora_" in k or "bias" in k}
|
| 41 |
+
elif bias == "lora_only":
|
| 42 |
+
to_return = {}
|
| 43 |
+
for k in state_dict:
|
| 44 |
+
if "lora_" in k:
|
| 45 |
+
to_return[k] = state_dict[k]
|
| 46 |
+
bias_name = k.split("lora_")[0] + "bias"
|
| 47 |
+
if bias_name in state_dict:
|
| 48 |
+
to_return[bias_name] = state_dict[bias_name]
|
| 49 |
+
else:
|
| 50 |
+
raise NotImplementedError
|
| 51 |
+
else:
|
| 52 |
+
to_return = {}
|
| 53 |
+
if model.peft_config.inference_mode:
|
| 54 |
+
prompt_embeddings = model.prompt_encoder.embedding.weight
|
| 55 |
+
else:
|
| 56 |
+
prompt_embeddings = model.get_prompt_embedding_to_save()
|
| 57 |
+
to_return["prompt_embeddings"] = prompt_embeddings
|
| 58 |
+
if model.modules_to_save is not None:
|
| 59 |
+
for key, value in state_dict.items():
|
| 60 |
+
if any(module_name in key for module_name in model.modules_to_save):
|
| 61 |
+
to_return[key] = value
|
| 62 |
+
return to_return
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def set_peft_model_state_dict(model, peft_model_state_dict):
|
| 66 |
+
"""
|
| 67 |
+
Set the state dict of the Peft model.
|
| 68 |
+
|
| 69 |
+
Args:
|
| 70 |
+
model ([`PeftModel`]): The Peft model.
|
| 71 |
+
peft_model_state_dict (`dict`): The state dict of the Peft model.
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
for name, param in model.named_parameters():
|
| 75 |
+
if name in peft_model_state_dict.keys():
|
| 76 |
+
print(f"Loading LoRA in lora_path, {name}...")
|
| 77 |
+
|
| 78 |
+
model.load_state_dict(peft_model_state_dict, strict=False)
|
| 79 |
+
return model
|
之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/__init__.py
ADDED
|
File without changes
|
之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/cli.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
|
| 5 |
+
from llava.conversation import conv_templates, SeparatorStyle
|
| 6 |
+
from llava.model.builder import load_pretrained_model
|
| 7 |
+
from llava.utils import disable_torch_init
|
| 8 |
+
from llava.mm_utils import process_images, tokenizer_image_token, get_model_name_from_path
|
| 9 |
+
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
import requests
|
| 13 |
+
from PIL import Image
|
| 14 |
+
from io import BytesIO
|
| 15 |
+
from transformers import TextStreamer
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def load_image(image_file):
|
| 19 |
+
if image_file.startswith('http://') or image_file.startswith('https://'):
|
| 20 |
+
response = requests.get(image_file)
|
| 21 |
+
image = Image.open(BytesIO(response.content)).convert('RGB')
|
| 22 |
+
else:
|
| 23 |
+
image = Image.open(image_file).convert('RGB')
|
| 24 |
+
return image
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def main(args):
|
| 28 |
+
# Model
|
| 29 |
+
disable_torch_init()
|
| 30 |
+
|
| 31 |
+
model_name = get_model_name_from_path(args.model_path)
|
| 32 |
+
tokenizer, model, image_processor, context_len = load_pretrained_model(args.model_path, args.model_base, model_name, args.load_8bit, args.load_4bit, device=args.device)
|
| 33 |
+
|
| 34 |
+
if "llama-2" in model_name.lower():
|
| 35 |
+
conv_mode = "llava_llama_2"
|
| 36 |
+
elif "mistral" in model_name.lower():
|
| 37 |
+
conv_mode = "mistral_instruct"
|
| 38 |
+
elif "v1.6-34b" in model_name.lower():
|
| 39 |
+
conv_mode = "chatml_direct"
|
| 40 |
+
elif "v1" in model_name.lower():
|
| 41 |
+
conv_mode = "llava_v1"
|
| 42 |
+
elif "mpt" in model_name.lower():
|
| 43 |
+
conv_mode = "mpt"
|
| 44 |
+
else:
|
| 45 |
+
conv_mode = "llava_v0"
|
| 46 |
+
|
| 47 |
+
if args.conv_mode is not None and conv_mode != args.conv_mode:
|
| 48 |
+
print('[WARNING] the auto inferred conversation mode is {}, while `--conv-mode` is {}, using {}'.format(conv_mode, args.conv_mode, args.conv_mode))
|
| 49 |
+
else:
|
| 50 |
+
args.conv_mode = conv_mode
|
| 51 |
+
|
| 52 |
+
conv = conv_templates[args.conv_mode].copy()
|
| 53 |
+
if "mpt" in model_name.lower():
|
| 54 |
+
roles = ('user', 'assistant')
|
| 55 |
+
else:
|
| 56 |
+
roles = conv.roles
|
| 57 |
+
|
| 58 |
+
image = load_image(args.image_file)
|
| 59 |
+
image_size = image.size
|
| 60 |
+
# Similar operation in model_worker.py
|
| 61 |
+
image_tensor = process_images([image], image_processor, model.config)
|
| 62 |
+
if type(image_tensor) is list:
|
| 63 |
+
image_tensor = [image.to(model.device, dtype=torch.float16) for image in image_tensor]
|
| 64 |
+
else:
|
| 65 |
+
image_tensor = image_tensor.to(model.device, dtype=torch.float16)
|
| 66 |
+
|
| 67 |
+
while True:
|
| 68 |
+
try:
|
| 69 |
+
inp = input(f"{roles[0]}: ")
|
| 70 |
+
except EOFError:
|
| 71 |
+
inp = ""
|
| 72 |
+
if not inp:
|
| 73 |
+
print("exit...")
|
| 74 |
+
break
|
| 75 |
+
|
| 76 |
+
print(f"{roles[1]}: ", end="")
|
| 77 |
+
|
| 78 |
+
if image is not None:
|
| 79 |
+
# first message
|
| 80 |
+
if model.config.mm_use_im_start_end:
|
| 81 |
+
inp = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + inp
|
| 82 |
+
else:
|
| 83 |
+
inp = DEFAULT_IMAGE_TOKEN + '\n' + inp
|
| 84 |
+
image = None
|
| 85 |
+
|
| 86 |
+
conv.append_message(conv.roles[0], inp)
|
| 87 |
+
conv.append_message(conv.roles[1], None)
|
| 88 |
+
prompt = conv.get_prompt()
|
| 89 |
+
|
| 90 |
+
input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).to(model.device)
|
| 91 |
+
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
|
| 92 |
+
keywords = [stop_str]
|
| 93 |
+
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 94 |
+
|
| 95 |
+
with torch.inference_mode():
|
| 96 |
+
output_ids = model.generate(
|
| 97 |
+
input_ids,
|
| 98 |
+
images=image_tensor,
|
| 99 |
+
image_sizes=[image_size],
|
| 100 |
+
do_sample=True if args.temperature > 0 else False,
|
| 101 |
+
temperature=args.temperature,
|
| 102 |
+
max_new_tokens=args.max_new_tokens,
|
| 103 |
+
streamer=streamer,
|
| 104 |
+
use_cache=True)
|
| 105 |
+
|
| 106 |
+
outputs = tokenizer.decode(output_ids[0]).strip()
|
| 107 |
+
conv.messages[-1][-1] = outputs
|
| 108 |
+
|
| 109 |
+
if args.debug:
|
| 110 |
+
print("\n", {"prompt": prompt, "outputs": outputs}, "\n")
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
parser = argparse.ArgumentParser()
|
| 115 |
+
parser.add_argument("--model-path", type=str, default="facebook/opt-350m")
|
| 116 |
+
parser.add_argument("--model-base", type=str, default=None)
|
| 117 |
+
parser.add_argument("--image-file", type=str, required=True)
|
| 118 |
+
parser.add_argument("--device", type=str, default="cuda")
|
| 119 |
+
parser.add_argument("--conv-mode", type=str, default=None)
|
| 120 |
+
parser.add_argument("--temperature", type=float, default=0.2)
|
| 121 |
+
parser.add_argument("--max-new-tokens", type=int, default=512)
|
| 122 |
+
parser.add_argument("--load-8bit", action="store_true")
|
| 123 |
+
parser.add_argument("--load-4bit", action="store_true")
|
| 124 |
+
parser.add_argument("--debug", action="store_true")
|
| 125 |
+
args = parser.parse_args()
|
| 126 |
+
main(args)
|
之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/gradio_web_server.py
ADDED
|
@@ -0,0 +1,479 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import datetime
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
from llava.conversation import (default_conversation, conv_templates,
|
| 11 |
+
SeparatorStyle)
|
| 12 |
+
from llava.constants import LOGDIR
|
| 13 |
+
from llava.utils import (build_logger, server_error_msg,
|
| 14 |
+
violates_moderation, moderation_msg)
|
| 15 |
+
import hashlib
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
logger = build_logger("gradio_web_server", "gradio_web_server.log")
|
| 19 |
+
|
| 20 |
+
headers = {"User-Agent": "LLaVA Client"}
|
| 21 |
+
|
| 22 |
+
no_change_btn = gr.Button()
|
| 23 |
+
enable_btn = gr.Button(interactive=True)
|
| 24 |
+
disable_btn = gr.Button(interactive=False)
|
| 25 |
+
|
| 26 |
+
priority = {
|
| 27 |
+
"vicuna-13b": "aaaaaaa",
|
| 28 |
+
"koala-13b": "aaaaaab",
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def get_conv_log_filename():
|
| 33 |
+
t = datetime.datetime.now()
|
| 34 |
+
name = os.path.join(LOGDIR, f"{t.year}-{t.month:02d}-{t.day:02d}-conv.json")
|
| 35 |
+
return name
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def get_model_list():
|
| 39 |
+
ret = requests.post(args.controller_url + "/refresh_all_workers")
|
| 40 |
+
assert ret.status_code == 200
|
| 41 |
+
ret = requests.post(args.controller_url + "/list_models")
|
| 42 |
+
models = ret.json()["models"]
|
| 43 |
+
models.sort(key=lambda x: priority.get(x, x))
|
| 44 |
+
logger.info(f"Models: {models}")
|
| 45 |
+
return models
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
get_window_url_params = """
|
| 49 |
+
function() {
|
| 50 |
+
const params = new URLSearchParams(window.location.search);
|
| 51 |
+
url_params = Object.fromEntries(params);
|
| 52 |
+
console.log(url_params);
|
| 53 |
+
return url_params;
|
| 54 |
+
}
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def load_demo(url_params, request: gr.Request):
|
| 59 |
+
logger.info(f"load_demo. ip: {request.client.host}. params: {url_params}")
|
| 60 |
+
|
| 61 |
+
dropdown_update = gr.Dropdown(visible=True)
|
| 62 |
+
if "model" in url_params:
|
| 63 |
+
model = url_params["model"]
|
| 64 |
+
if model in models:
|
| 65 |
+
dropdown_update = gr.Dropdown(value=model, visible=True)
|
| 66 |
+
|
| 67 |
+
state = default_conversation.copy()
|
| 68 |
+
return state, dropdown_update
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def load_demo_refresh_model_list(request: gr.Request):
|
| 72 |
+
logger.info(f"load_demo. ip: {request.client.host}")
|
| 73 |
+
models = get_model_list()
|
| 74 |
+
state = default_conversation.copy()
|
| 75 |
+
dropdown_update = gr.Dropdown(
|
| 76 |
+
choices=models,
|
| 77 |
+
value=models[0] if len(models) > 0 else ""
|
| 78 |
+
)
|
| 79 |
+
return state, dropdown_update
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def vote_last_response(state, vote_type, model_selector, request: gr.Request):
|
| 83 |
+
with open(get_conv_log_filename(), "a") as fout:
|
| 84 |
+
data = {
|
| 85 |
+
"tstamp": round(time.time(), 4),
|
| 86 |
+
"type": vote_type,
|
| 87 |
+
"model": model_selector,
|
| 88 |
+
"state": state.dict(),
|
| 89 |
+
"ip": request.client.host,
|
| 90 |
+
}
|
| 91 |
+
fout.write(json.dumps(data) + "\n")
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def upvote_last_response(state, model_selector, request: gr.Request):
|
| 95 |
+
logger.info(f"upvote. ip: {request.client.host}")
|
| 96 |
+
vote_last_response(state, "upvote", model_selector, request)
|
| 97 |
+
return ("",) + (disable_btn,) * 3
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def downvote_last_response(state, model_selector, request: gr.Request):
|
| 101 |
+
logger.info(f"downvote. ip: {request.client.host}")
|
| 102 |
+
vote_last_response(state, "downvote", model_selector, request)
|
| 103 |
+
return ("",) + (disable_btn,) * 3
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def flag_last_response(state, model_selector, request: gr.Request):
|
| 107 |
+
logger.info(f"flag. ip: {request.client.host}")
|
| 108 |
+
vote_last_response(state, "flag", model_selector, request)
|
| 109 |
+
return ("",) + (disable_btn,) * 3
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def regenerate(state, image_process_mode, request: gr.Request):
|
| 113 |
+
logger.info(f"regenerate. ip: {request.client.host}")
|
| 114 |
+
state.messages[-1][-1] = None
|
| 115 |
+
prev_human_msg = state.messages[-2]
|
| 116 |
+
if type(prev_human_msg[1]) in (tuple, list):
|
| 117 |
+
prev_human_msg[1] = (*prev_human_msg[1][:2], image_process_mode)
|
| 118 |
+
state.skip_next = False
|
| 119 |
+
return (state, state.to_gradio_chatbot(), "", None) + (disable_btn,) * 5
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def clear_history(request: gr.Request):
|
| 123 |
+
logger.info(f"clear_history. ip: {request.client.host}")
|
| 124 |
+
state = default_conversation.copy()
|
| 125 |
+
return (state, state.to_gradio_chatbot(), "", None) + (disable_btn,) * 5
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def add_text(state, text, image, image_process_mode, request: gr.Request):
|
| 129 |
+
logger.info(f"add_text. ip: {request.client.host}. len: {len(text)}")
|
| 130 |
+
if len(text) <= 0 and image is None:
|
| 131 |
+
state.skip_next = True
|
| 132 |
+
return (state, state.to_gradio_chatbot(), "", None) + (no_change_btn,) * 5
|
| 133 |
+
if args.moderate:
|
| 134 |
+
flagged = violates_moderation(text)
|
| 135 |
+
if flagged:
|
| 136 |
+
state.skip_next = True
|
| 137 |
+
return (state, state.to_gradio_chatbot(), moderation_msg, None) + (
|
| 138 |
+
no_change_btn,) * 5
|
| 139 |
+
|
| 140 |
+
text = text[:1536] # Hard cut-off
|
| 141 |
+
if image is not None:
|
| 142 |
+
text = text[:1200] # Hard cut-off for images
|
| 143 |
+
if '<image>' not in text:
|
| 144 |
+
# text = '<Image><image></Image>' + text
|
| 145 |
+
text = text + '\n<image>'
|
| 146 |
+
text = (text, image, image_process_mode)
|
| 147 |
+
state = default_conversation.copy()
|
| 148 |
+
state.append_message(state.roles[0], text)
|
| 149 |
+
state.append_message(state.roles[1], None)
|
| 150 |
+
state.skip_next = False
|
| 151 |
+
return (state, state.to_gradio_chatbot(), "", None) + (disable_btn,) * 5
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def http_bot(state, model_selector, temperature, top_p, max_new_tokens, request: gr.Request):
|
| 155 |
+
logger.info(f"http_bot. ip: {request.client.host}")
|
| 156 |
+
start_tstamp = time.time()
|
| 157 |
+
model_name = model_selector
|
| 158 |
+
|
| 159 |
+
if state.skip_next:
|
| 160 |
+
# This generate call is skipped due to invalid inputs
|
| 161 |
+
yield (state, state.to_gradio_chatbot()) + (no_change_btn,) * 5
|
| 162 |
+
return
|
| 163 |
+
|
| 164 |
+
if len(state.messages) == state.offset + 2:
|
| 165 |
+
# First round of conversation
|
| 166 |
+
if "llava" in model_name.lower():
|
| 167 |
+
if 'llama-2' in model_name.lower():
|
| 168 |
+
template_name = "llava_llama_2"
|
| 169 |
+
elif "mistral" in model_name.lower() or "mixtral" in model_name.lower():
|
| 170 |
+
if 'orca' in model_name.lower():
|
| 171 |
+
template_name = "mistral_orca"
|
| 172 |
+
elif 'hermes' in model_name.lower():
|
| 173 |
+
template_name = "chatml_direct"
|
| 174 |
+
else:
|
| 175 |
+
template_name = "mistral_instruct"
|
| 176 |
+
elif 'llava-v1.6-34b' in model_name.lower():
|
| 177 |
+
template_name = "chatml_direct"
|
| 178 |
+
elif "v1" in model_name.lower():
|
| 179 |
+
if 'mmtag' in model_name.lower():
|
| 180 |
+
template_name = "v1_mmtag"
|
| 181 |
+
elif 'plain' in model_name.lower() and 'finetune' not in model_name.lower():
|
| 182 |
+
template_name = "v1_mmtag"
|
| 183 |
+
else:
|
| 184 |
+
template_name = "llava_v1"
|
| 185 |
+
elif "mpt" in model_name.lower():
|
| 186 |
+
template_name = "mpt"
|
| 187 |
+
else:
|
| 188 |
+
if 'mmtag' in model_name.lower():
|
| 189 |
+
template_name = "v0_mmtag"
|
| 190 |
+
elif 'plain' in model_name.lower() and 'finetune' not in model_name.lower():
|
| 191 |
+
template_name = "v0_mmtag"
|
| 192 |
+
else:
|
| 193 |
+
template_name = "llava_v0"
|
| 194 |
+
elif "mpt" in model_name:
|
| 195 |
+
template_name = "mpt_text"
|
| 196 |
+
elif "llama-2" in model_name:
|
| 197 |
+
template_name = "llama_2"
|
| 198 |
+
else:
|
| 199 |
+
template_name = "vicuna_v1"
|
| 200 |
+
new_state = conv_templates[template_name].copy()
|
| 201 |
+
new_state.append_message(new_state.roles[0], state.messages[-2][1])
|
| 202 |
+
new_state.append_message(new_state.roles[1], None)
|
| 203 |
+
state = new_state
|
| 204 |
+
|
| 205 |
+
# Query worker address
|
| 206 |
+
controller_url = args.controller_url
|
| 207 |
+
ret = requests.post(controller_url + "/get_worker_address",
|
| 208 |
+
json={"model": model_name})
|
| 209 |
+
worker_addr = ret.json()["address"]
|
| 210 |
+
logger.info(f"model_name: {model_name}, worker_addr: {worker_addr}")
|
| 211 |
+
|
| 212 |
+
# No available worker
|
| 213 |
+
if worker_addr == "":
|
| 214 |
+
state.messages[-1][-1] = server_error_msg
|
| 215 |
+
yield (state, state.to_gradio_chatbot(), disable_btn, disable_btn, disable_btn, enable_btn, enable_btn)
|
| 216 |
+
return
|
| 217 |
+
|
| 218 |
+
# Construct prompt
|
| 219 |
+
prompt = state.get_prompt()
|
| 220 |
+
|
| 221 |
+
all_images = state.get_images(return_pil=True)
|
| 222 |
+
all_image_hash = [hashlib.md5(image.tobytes()).hexdigest() for image in all_images]
|
| 223 |
+
for image, hash in zip(all_images, all_image_hash):
|
| 224 |
+
t = datetime.datetime.now()
|
| 225 |
+
filename = os.path.join(LOGDIR, "serve_images", f"{t.year}-{t.month:02d}-{t.day:02d}", f"{hash}.jpg")
|
| 226 |
+
if not os.path.isfile(filename):
|
| 227 |
+
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
| 228 |
+
image.save(filename)
|
| 229 |
+
|
| 230 |
+
# Make requests
|
| 231 |
+
pload = {
|
| 232 |
+
"model": model_name,
|
| 233 |
+
"prompt": prompt,
|
| 234 |
+
"temperature": float(temperature),
|
| 235 |
+
"top_p": float(top_p),
|
| 236 |
+
"max_new_tokens": min(int(max_new_tokens), 1536),
|
| 237 |
+
"stop": state.sep if state.sep_style in [SeparatorStyle.SINGLE, SeparatorStyle.MPT] else state.sep2,
|
| 238 |
+
"images": f'List of {len(state.get_images())} images: {all_image_hash}',
|
| 239 |
+
}
|
| 240 |
+
logger.info(f"==== request ====\n{pload}")
|
| 241 |
+
|
| 242 |
+
pload['images'] = state.get_images()
|
| 243 |
+
|
| 244 |
+
state.messages[-1][-1] = "▌"
|
| 245 |
+
yield (state, state.to_gradio_chatbot()) + (disable_btn,) * 5
|
| 246 |
+
|
| 247 |
+
try:
|
| 248 |
+
# Stream output
|
| 249 |
+
response = requests.post(worker_addr + "/worker_generate_stream",
|
| 250 |
+
headers=headers, json=pload, stream=True, timeout=10)
|
| 251 |
+
for chunk in response.iter_lines(decode_unicode=False, delimiter=b"\0"):
|
| 252 |
+
if chunk:
|
| 253 |
+
data = json.loads(chunk.decode())
|
| 254 |
+
if data["error_code"] == 0:
|
| 255 |
+
output = data["text"][len(prompt):].strip()
|
| 256 |
+
state.messages[-1][-1] = output + "▌"
|
| 257 |
+
yield (state, state.to_gradio_chatbot()) + (disable_btn,) * 5
|
| 258 |
+
else:
|
| 259 |
+
output = data["text"] + f" (error_code: {data['error_code']})"
|
| 260 |
+
state.messages[-1][-1] = output
|
| 261 |
+
yield (state, state.to_gradio_chatbot()) + (disable_btn, disable_btn, disable_btn, enable_btn, enable_btn)
|
| 262 |
+
return
|
| 263 |
+
time.sleep(0.03)
|
| 264 |
+
except requests.exceptions.RequestException as e:
|
| 265 |
+
state.messages[-1][-1] = server_error_msg
|
| 266 |
+
yield (state, state.to_gradio_chatbot()) + (disable_btn, disable_btn, disable_btn, enable_btn, enable_btn)
|
| 267 |
+
return
|
| 268 |
+
|
| 269 |
+
state.messages[-1][-1] = state.messages[-1][-1][:-1]
|
| 270 |
+
yield (state, state.to_gradio_chatbot()) + (enable_btn,) * 5
|
| 271 |
+
|
| 272 |
+
finish_tstamp = time.time()
|
| 273 |
+
logger.info(f"{output}")
|
| 274 |
+
|
| 275 |
+
with open(get_conv_log_filename(), "a") as fout:
|
| 276 |
+
data = {
|
| 277 |
+
"tstamp": round(finish_tstamp, 4),
|
| 278 |
+
"type": "chat",
|
| 279 |
+
"model": model_name,
|
| 280 |
+
"start": round(start_tstamp, 4),
|
| 281 |
+
"finish": round(finish_tstamp, 4),
|
| 282 |
+
"state": state.dict(),
|
| 283 |
+
"images": all_image_hash,
|
| 284 |
+
"ip": request.client.host,
|
| 285 |
+
}
|
| 286 |
+
fout.write(json.dumps(data) + "\n")
|
| 287 |
+
|
| 288 |
+
title_markdown = ("""
|
| 289 |
+
# 🌋 LLaVA: Large Language and Vision Assistant
|
| 290 |
+
[[Project Page](https://llava-vl.github.io)] [[Code](https://github.com/haotian-liu/LLaVA)] [[Model](https://github.com/haotian-liu/LLaVA/blob/main/docs/MODEL_ZOO.md)] | 📚 [[LLaVA](https://arxiv.org/abs/2304.08485)] [[LLaVA-v1.5](https://arxiv.org/abs/2310.03744)] [[LLaVA-v1.6](https://llava-vl.github.io/blog/2024-01-30-llava-1-6/)]
|
| 291 |
+
""")
|
| 292 |
+
|
| 293 |
+
tos_markdown = ("""
|
| 294 |
+
### Terms of use
|
| 295 |
+
By using this service, users are required to agree to the following terms:
|
| 296 |
+
The service is a research preview intended for non-commercial use only. It only provides limited safety measures and may generate offensive content. It must not be used for any illegal, harmful, violent, racist, or sexual purposes. The service may collect user dialogue data for future research.
|
| 297 |
+
Please click the "Flag" button if you get any inappropriate answer! We will collect those to keep improving our moderator.
|
| 298 |
+
For an optimal experience, please use desktop computers for this demo, as mobile devices may compromise its quality.
|
| 299 |
+
""")
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
learn_more_markdown = ("""
|
| 303 |
+
### License
|
| 304 |
+
The service is a research preview intended for non-commercial use only, subject to the model [License](https://github.com/facebookresearch/llama/blob/main/MODEL_CARD.md) of LLaMA, [Terms of Use](https://openai.com/policies/terms-of-use) of the data generated by OpenAI, and [Privacy Practices](https://chrome.google.com/webstore/detail/sharegpt-share-your-chatg/daiacboceoaocpibfodeljbdfacokfjb) of ShareGPT. Please contact us if you find any potential violation.
|
| 305 |
+
""")
|
| 306 |
+
|
| 307 |
+
block_css = """
|
| 308 |
+
|
| 309 |
+
#buttons button {
|
| 310 |
+
min-width: min(120px,100%);
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
"""
|
| 314 |
+
|
| 315 |
+
def build_demo(embed_mode, cur_dir=None, concurrency_count=10):
|
| 316 |
+
textbox = gr.Textbox(show_label=False, placeholder="Enter text and press ENTER", container=False)
|
| 317 |
+
with gr.Blocks(title="LLaVA", theme=gr.themes.Default(), css=block_css) as demo:
|
| 318 |
+
state = gr.State()
|
| 319 |
+
|
| 320 |
+
if not embed_mode:
|
| 321 |
+
gr.Markdown(title_markdown)
|
| 322 |
+
|
| 323 |
+
with gr.Row():
|
| 324 |
+
with gr.Column(scale=3):
|
| 325 |
+
with gr.Row(elem_id="model_selector_row"):
|
| 326 |
+
model_selector = gr.Dropdown(
|
| 327 |
+
choices=models,
|
| 328 |
+
value=models[0] if len(models) > 0 else "",
|
| 329 |
+
interactive=True,
|
| 330 |
+
show_label=False,
|
| 331 |
+
container=False)
|
| 332 |
+
|
| 333 |
+
imagebox = gr.Image(type="pil")
|
| 334 |
+
image_process_mode = gr.Radio(
|
| 335 |
+
["Crop", "Resize", "Pad", "Default"],
|
| 336 |
+
value="Default",
|
| 337 |
+
label="Preprocess for non-square image", visible=False)
|
| 338 |
+
|
| 339 |
+
if cur_dir is None:
|
| 340 |
+
cur_dir = os.path.dirname(os.path.abspath(__file__))
|
| 341 |
+
gr.Examples(examples=[
|
| 342 |
+
[f"{cur_dir}/examples/extreme_ironing.jpg", "What is unusual about this image?"],
|
| 343 |
+
[f"{cur_dir}/examples/waterview.jpg", "What are the things I should be cautious about when I visit here?"],
|
| 344 |
+
], inputs=[imagebox, textbox])
|
| 345 |
+
|
| 346 |
+
with gr.Accordion("Parameters", open=False) as parameter_row:
|
| 347 |
+
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, step=0.1, interactive=True, label="Temperature",)
|
| 348 |
+
top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, step=0.1, interactive=True, label="Top P",)
|
| 349 |
+
max_output_tokens = gr.Slider(minimum=0, maximum=1024, value=512, step=64, interactive=True, label="Max output tokens",)
|
| 350 |
+
|
| 351 |
+
with gr.Column(scale=8):
|
| 352 |
+
chatbot = gr.Chatbot(
|
| 353 |
+
elem_id="chatbot",
|
| 354 |
+
label="LLaVA Chatbot",
|
| 355 |
+
height=650,
|
| 356 |
+
layout="panel",
|
| 357 |
+
)
|
| 358 |
+
with gr.Row():
|
| 359 |
+
with gr.Column(scale=8):
|
| 360 |
+
textbox.render()
|
| 361 |
+
with gr.Column(scale=1, min_width=50):
|
| 362 |
+
submit_btn = gr.Button(value="Send", variant="primary")
|
| 363 |
+
with gr.Row(elem_id="buttons") as button_row:
|
| 364 |
+
upvote_btn = gr.Button(value="👍 Upvote", interactive=False)
|
| 365 |
+
downvote_btn = gr.Button(value="👎 Downvote", interactive=False)
|
| 366 |
+
flag_btn = gr.Button(value="⚠️ Flag", interactive=False)
|
| 367 |
+
#stop_btn = gr.Button(value="⏹️ Stop Generation", interactive=False)
|
| 368 |
+
regenerate_btn = gr.Button(value="🔄 Regenerate", interactive=False)
|
| 369 |
+
clear_btn = gr.Button(value="🗑️ Clear", interactive=False)
|
| 370 |
+
|
| 371 |
+
if not embed_mode:
|
| 372 |
+
gr.Markdown(tos_markdown)
|
| 373 |
+
gr.Markdown(learn_more_markdown)
|
| 374 |
+
url_params = gr.JSON(visible=False)
|
| 375 |
+
|
| 376 |
+
# Register listeners
|
| 377 |
+
btn_list = [upvote_btn, downvote_btn, flag_btn, regenerate_btn, clear_btn]
|
| 378 |
+
upvote_btn.click(
|
| 379 |
+
upvote_last_response,
|
| 380 |
+
[state, model_selector],
|
| 381 |
+
[textbox, upvote_btn, downvote_btn, flag_btn]
|
| 382 |
+
)
|
| 383 |
+
downvote_btn.click(
|
| 384 |
+
downvote_last_response,
|
| 385 |
+
[state, model_selector],
|
| 386 |
+
[textbox, upvote_btn, downvote_btn, flag_btn]
|
| 387 |
+
)
|
| 388 |
+
flag_btn.click(
|
| 389 |
+
flag_last_response,
|
| 390 |
+
[state, model_selector],
|
| 391 |
+
[textbox, upvote_btn, downvote_btn, flag_btn]
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
regenerate_btn.click(
|
| 395 |
+
regenerate,
|
| 396 |
+
[state, image_process_mode],
|
| 397 |
+
[state, chatbot, textbox, imagebox] + btn_list
|
| 398 |
+
).then(
|
| 399 |
+
http_bot,
|
| 400 |
+
[state, model_selector, temperature, top_p, max_output_tokens],
|
| 401 |
+
[state, chatbot] + btn_list,
|
| 402 |
+
concurrency_limit=concurrency_count
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
clear_btn.click(
|
| 406 |
+
clear_history,
|
| 407 |
+
None,
|
| 408 |
+
[state, chatbot, textbox, imagebox] + btn_list,
|
| 409 |
+
queue=False
|
| 410 |
+
)
|
| 411 |
+
|
| 412 |
+
textbox.submit(
|
| 413 |
+
add_text,
|
| 414 |
+
[state, textbox, imagebox, image_process_mode],
|
| 415 |
+
[state, chatbot, textbox, imagebox] + btn_list,
|
| 416 |
+
queue=False
|
| 417 |
+
).then(
|
| 418 |
+
http_bot,
|
| 419 |
+
[state, model_selector, temperature, top_p, max_output_tokens],
|
| 420 |
+
[state, chatbot] + btn_list,
|
| 421 |
+
concurrency_limit=concurrency_count
|
| 422 |
+
)
|
| 423 |
+
|
| 424 |
+
submit_btn.click(
|
| 425 |
+
add_text,
|
| 426 |
+
[state, textbox, imagebox, image_process_mode],
|
| 427 |
+
[state, chatbot, textbox, imagebox] + btn_list
|
| 428 |
+
).then(
|
| 429 |
+
http_bot,
|
| 430 |
+
[state, model_selector, temperature, top_p, max_output_tokens],
|
| 431 |
+
[state, chatbot] + btn_list,
|
| 432 |
+
concurrency_limit=concurrency_count
|
| 433 |
+
)
|
| 434 |
+
|
| 435 |
+
if args.model_list_mode == "once":
|
| 436 |
+
demo.load(
|
| 437 |
+
load_demo,
|
| 438 |
+
[url_params],
|
| 439 |
+
[state, model_selector],
|
| 440 |
+
js=get_window_url_params
|
| 441 |
+
)
|
| 442 |
+
elif args.model_list_mode == "reload":
|
| 443 |
+
demo.load(
|
| 444 |
+
load_demo_refresh_model_list,
|
| 445 |
+
None,
|
| 446 |
+
[state, model_selector],
|
| 447 |
+
queue=False
|
| 448 |
+
)
|
| 449 |
+
else:
|
| 450 |
+
raise ValueError(f"Unknown model list mode: {args.model_list_mode}")
|
| 451 |
+
|
| 452 |
+
return demo
|
| 453 |
+
|
| 454 |
+
|
| 455 |
+
if __name__ == "__main__":
|
| 456 |
+
parser = argparse.ArgumentParser()
|
| 457 |
+
parser.add_argument("--host", type=str, default="0.0.0.0")
|
| 458 |
+
parser.add_argument("--port", type=int)
|
| 459 |
+
parser.add_argument("--controller-url", type=str, default="http://localhost:21001")
|
| 460 |
+
parser.add_argument("--concurrency-count", type=int, default=16)
|
| 461 |
+
parser.add_argument("--model-list-mode", type=str, default="once",
|
| 462 |
+
choices=["once", "reload"])
|
| 463 |
+
parser.add_argument("--share", action="store_true")
|
| 464 |
+
parser.add_argument("--moderate", action="store_true")
|
| 465 |
+
parser.add_argument("--embed", action="store_true")
|
| 466 |
+
args = parser.parse_args()
|
| 467 |
+
logger.info(f"args: {args}")
|
| 468 |
+
|
| 469 |
+
models = get_model_list()
|
| 470 |
+
|
| 471 |
+
logger.info(args)
|
| 472 |
+
demo = build_demo(args.embed, concurrency_count=args.concurrency_count)
|
| 473 |
+
demo.queue(
|
| 474 |
+
api_open=False
|
| 475 |
+
).launch(
|
| 476 |
+
server_name=args.host,
|
| 477 |
+
server_port=args.port,
|
| 478 |
+
share=args.share
|
| 479 |
+
)
|
之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/register_worker.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Manually register workers.
|
| 3 |
+
|
| 4 |
+
Usage:
|
| 5 |
+
python3 -m fastchat.serve.register_worker --controller http://localhost:21001 --worker-name http://localhost:21002
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import argparse
|
| 9 |
+
|
| 10 |
+
import requests
|
| 11 |
+
|
| 12 |
+
if __name__ == "__main__":
|
| 13 |
+
parser = argparse.ArgumentParser()
|
| 14 |
+
parser.add_argument("--controller-address", type=str)
|
| 15 |
+
parser.add_argument("--worker-name", type=str)
|
| 16 |
+
parser.add_argument("--check-heart-beat", action="store_true")
|
| 17 |
+
args = parser.parse_args()
|
| 18 |
+
|
| 19 |
+
url = args.controller_address + "/register_worker"
|
| 20 |
+
data = {
|
| 21 |
+
"worker_name": args.worker_name,
|
| 22 |
+
"check_heart_beat": args.check_heart_beat,
|
| 23 |
+
"worker_status": None,
|
| 24 |
+
}
|
| 25 |
+
r = requests.post(url, json=data)
|
| 26 |
+
assert r.status_code == 200
|
之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/sglang_worker.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A model worker executes the model.
|
| 3 |
+
"""
|
| 4 |
+
import argparse
|
| 5 |
+
import asyncio
|
| 6 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 7 |
+
import json
|
| 8 |
+
import time
|
| 9 |
+
import threading
|
| 10 |
+
import uuid
|
| 11 |
+
|
| 12 |
+
from fastapi import FastAPI, Request, BackgroundTasks
|
| 13 |
+
from fastapi.responses import StreamingResponse
|
| 14 |
+
import requests
|
| 15 |
+
import re
|
| 16 |
+
import uvicorn
|
| 17 |
+
from functools import partial
|
| 18 |
+
|
| 19 |
+
from llava.constants import WORKER_HEART_BEAT_INTERVAL
|
| 20 |
+
from llava.utils import (build_logger, server_error_msg,
|
| 21 |
+
pretty_print_semaphore)
|
| 22 |
+
from llava.mm_utils import process_images, load_image_from_base64, tokenizer_image_token, expand2square
|
| 23 |
+
from llava.constants import DEFAULT_IMAGE_TOKEN
|
| 24 |
+
|
| 25 |
+
import sglang as sgl
|
| 26 |
+
from sglang.backend.runtime_endpoint import RuntimeEndpoint
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
GB = 1 << 30
|
| 30 |
+
|
| 31 |
+
worker_id = str(uuid.uuid4())[:6]
|
| 32 |
+
logger = build_logger("model_worker", f"model_worker_{worker_id}.log")
|
| 33 |
+
global_counter = 0
|
| 34 |
+
|
| 35 |
+
model_semaphore = None
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def heart_beat_worker(controller):
|
| 39 |
+
while True:
|
| 40 |
+
time.sleep(WORKER_HEART_BEAT_INTERVAL)
|
| 41 |
+
controller.send_heart_beat()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@sgl.function
|
| 45 |
+
def pipeline(s, prompt, max_tokens):
|
| 46 |
+
for p in prompt:
|
| 47 |
+
if type(p) is str:
|
| 48 |
+
s += p
|
| 49 |
+
else:
|
| 50 |
+
s += sgl.image(p)
|
| 51 |
+
s += sgl.gen("response", max_tokens=max_tokens)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class ModelWorker:
|
| 55 |
+
def __init__(self, controller_addr, worker_addr, sgl_endpoint,
|
| 56 |
+
worker_id, no_register, model_name):
|
| 57 |
+
self.controller_addr = controller_addr
|
| 58 |
+
self.worker_addr = worker_addr
|
| 59 |
+
self.worker_id = worker_id
|
| 60 |
+
|
| 61 |
+
# Select backend
|
| 62 |
+
backend = RuntimeEndpoint(sgl_endpoint)
|
| 63 |
+
sgl.set_default_backend(backend)
|
| 64 |
+
model_path = backend.model_info["model_path"]
|
| 65 |
+
|
| 66 |
+
if model_path.endswith("/"):
|
| 67 |
+
model_path = model_path[:-1]
|
| 68 |
+
if model_name is None:
|
| 69 |
+
model_paths = model_path.split("/")
|
| 70 |
+
if model_paths[-1].startswith('checkpoint-'):
|
| 71 |
+
self.model_name = model_paths[-2] + "_" + model_paths[-1]
|
| 72 |
+
else:
|
| 73 |
+
self.model_name = model_paths[-1]
|
| 74 |
+
else:
|
| 75 |
+
self.model_name = model_name
|
| 76 |
+
|
| 77 |
+
logger.info(f"Loading the SGLANG model {self.model_name} on worker {worker_id} ...")
|
| 78 |
+
|
| 79 |
+
if not no_register:
|
| 80 |
+
self.register_to_controller()
|
| 81 |
+
self.heart_beat_thread = threading.Thread(
|
| 82 |
+
target=heart_beat_worker, args=(self,), daemon=True)
|
| 83 |
+
self.heart_beat_thread.start()
|
| 84 |
+
|
| 85 |
+
def register_to_controller(self):
|
| 86 |
+
logger.info("Register to controller")
|
| 87 |
+
|
| 88 |
+
url = self.controller_addr + "/register_worker"
|
| 89 |
+
data = {
|
| 90 |
+
"worker_name": self.worker_addr,
|
| 91 |
+
"check_heart_beat": True,
|
| 92 |
+
"worker_status": self.get_status()
|
| 93 |
+
}
|
| 94 |
+
r = requests.post(url, json=data)
|
| 95 |
+
assert r.status_code == 200
|
| 96 |
+
|
| 97 |
+
def send_heart_beat(self):
|
| 98 |
+
logger.info(f"Send heart beat. Models: {[self.model_name]}. "
|
| 99 |
+
f"Semaphore: {pretty_print_semaphore(model_semaphore)}. "
|
| 100 |
+
f"global_counter: {global_counter}")
|
| 101 |
+
|
| 102 |
+
url = self.controller_addr + "/receive_heart_beat"
|
| 103 |
+
|
| 104 |
+
while True:
|
| 105 |
+
try:
|
| 106 |
+
ret = requests.post(url, json={
|
| 107 |
+
"worker_name": self.worker_addr,
|
| 108 |
+
"queue_length": self.get_queue_length()}, timeout=5)
|
| 109 |
+
exist = ret.json()["exist"]
|
| 110 |
+
break
|
| 111 |
+
except requests.exceptions.RequestException as e:
|
| 112 |
+
logger.error(f"heart beat error: {e}")
|
| 113 |
+
time.sleep(5)
|
| 114 |
+
|
| 115 |
+
if not exist:
|
| 116 |
+
self.register_to_controller()
|
| 117 |
+
|
| 118 |
+
def get_queue_length(self):
|
| 119 |
+
if model_semaphore is None:
|
| 120 |
+
return 0
|
| 121 |
+
else:
|
| 122 |
+
return args.limit_model_concurrency - model_semaphore._value + (len(
|
| 123 |
+
model_semaphore._waiters) if model_semaphore._waiters is not None else 0)
|
| 124 |
+
|
| 125 |
+
def get_status(self):
|
| 126 |
+
return {
|
| 127 |
+
"model_names": [self.model_name],
|
| 128 |
+
"speed": 1,
|
| 129 |
+
"queue_length": self.get_queue_length(),
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
async def generate_stream(self, params):
|
| 133 |
+
ori_prompt = prompt = params["prompt"]
|
| 134 |
+
images = params.get("images", None)
|
| 135 |
+
if images is not None and len(images) > 0:
|
| 136 |
+
if len(images) > 0:
|
| 137 |
+
if len(images) != prompt.count(DEFAULT_IMAGE_TOKEN):
|
| 138 |
+
raise ValueError("Number of images does not match number of <image> tokens in prompt")
|
| 139 |
+
|
| 140 |
+
images = [load_image_from_base64(image) for image in images]
|
| 141 |
+
|
| 142 |
+
# FIXME: for image-start/end token
|
| 143 |
+
# replace_token = DEFAULT_IMAGE_TOKEN
|
| 144 |
+
# if getattr(self.model.config, 'mm_use_im_start_end', False):
|
| 145 |
+
# replace_token = DEFAULT_IM_START_TOKEN + replace_token + DEFAULT_IM_END_TOKEN
|
| 146 |
+
# prompt = prompt.replace(DEFAULT_IMAGE_TOKEN, replace_token)
|
| 147 |
+
prompt = prompt.replace(' ' + DEFAULT_IMAGE_TOKEN + '\n', DEFAULT_IMAGE_TOKEN)
|
| 148 |
+
prompt_split = prompt.split(DEFAULT_IMAGE_TOKEN)
|
| 149 |
+
prompt = []
|
| 150 |
+
for i in range(len(prompt_split)):
|
| 151 |
+
prompt.append(prompt_split[i])
|
| 152 |
+
if i < len(images):
|
| 153 |
+
prompt.append(images[i])
|
| 154 |
+
else:
|
| 155 |
+
prompt = [prompt]
|
| 156 |
+
|
| 157 |
+
temperature = float(params.get("temperature", 1.0))
|
| 158 |
+
top_p = float(params.get("top_p", 1.0))
|
| 159 |
+
# max_context_length = getattr(model.config, 'max_position_embeddings', 2048)
|
| 160 |
+
max_new_tokens = min(int(params.get("max_new_tokens", 256)), 1024)
|
| 161 |
+
stop_str = params.get("stop", None)
|
| 162 |
+
stop_str = [stop_str] if stop_str is not None else None
|
| 163 |
+
|
| 164 |
+
print({'prompt': prompt, 'max_new_tokens': max_new_tokens, 'temperature': temperature, 'top_p': top_p})
|
| 165 |
+
state = pipeline.run(prompt, max_new_tokens, temperature=temperature, top_p=top_p, stream=True)
|
| 166 |
+
|
| 167 |
+
generated_text = ori_prompt
|
| 168 |
+
async for text_outputs in state.text_async_iter(var_name="response"):
|
| 169 |
+
generated_text += text_outputs
|
| 170 |
+
yield json.dumps({"text": generated_text, "error_code": 0}).encode() + b"\0"
|
| 171 |
+
|
| 172 |
+
async def generate_stream_gate(self, params):
|
| 173 |
+
try:
|
| 174 |
+
async for x in self.generate_stream(params):
|
| 175 |
+
yield x
|
| 176 |
+
except ValueError as e:
|
| 177 |
+
print("Caught ValueError:", e)
|
| 178 |
+
ret = {
|
| 179 |
+
"text": server_error_msg,
|
| 180 |
+
"error_code": 1,
|
| 181 |
+
}
|
| 182 |
+
yield json.dumps(ret).encode() + b"\0"
|
| 183 |
+
except Exception as e:
|
| 184 |
+
print("Caught Unknown Error", e)
|
| 185 |
+
ret = {
|
| 186 |
+
"text": server_error_msg,
|
| 187 |
+
"error_code": 1,
|
| 188 |
+
}
|
| 189 |
+
yield json.dumps(ret).encode() + b"\0"
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
app = FastAPI()
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def release_model_semaphore(fn=None):
|
| 196 |
+
model_semaphore.release()
|
| 197 |
+
if fn is not None:
|
| 198 |
+
fn()
|
| 199 |
+
|
| 200 |
+
|
| 201 |
+
@app.post("/worker_generate_stream")
|
| 202 |
+
async def generate_stream(request: Request):
|
| 203 |
+
global model_semaphore, global_counter
|
| 204 |
+
global_counter += 1
|
| 205 |
+
params = await request.json()
|
| 206 |
+
|
| 207 |
+
if model_semaphore is None:
|
| 208 |
+
model_semaphore = asyncio.Semaphore(args.limit_model_concurrency)
|
| 209 |
+
await model_semaphore.acquire()
|
| 210 |
+
worker.send_heart_beat()
|
| 211 |
+
generator = worker.generate_stream_gate(params)
|
| 212 |
+
background_tasks = BackgroundTasks()
|
| 213 |
+
background_tasks.add_task(partial(release_model_semaphore, fn=worker.send_heart_beat))
|
| 214 |
+
return StreamingResponse(generator, background=background_tasks)
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
@app.post("/worker_get_status")
|
| 218 |
+
async def get_status(request: Request):
|
| 219 |
+
return worker.get_status()
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
if __name__ == "__main__":
|
| 223 |
+
parser = argparse.ArgumentParser()
|
| 224 |
+
parser.add_argument("--host", type=str, default="localhost")
|
| 225 |
+
parser.add_argument("--port", type=int, default=21002)
|
| 226 |
+
parser.add_argument("--worker-address", type=str,
|
| 227 |
+
default="http://localhost:21002")
|
| 228 |
+
parser.add_argument("--controller-address", type=str,
|
| 229 |
+
default="http://localhost:21001")
|
| 230 |
+
parser.add_argument("--model-name", type=str)
|
| 231 |
+
parser.add_argument("--sgl-endpoint", type=str)
|
| 232 |
+
parser.add_argument("--limit-model-concurrency", type=int, default=5)
|
| 233 |
+
parser.add_argument("--stream-interval", type=int, default=1)
|
| 234 |
+
parser.add_argument("--no-register", action="store_true")
|
| 235 |
+
args = parser.parse_args()
|
| 236 |
+
logger.info(f"args: {args}")
|
| 237 |
+
|
| 238 |
+
worker = ModelWorker(args.controller_address,
|
| 239 |
+
args.worker_address,
|
| 240 |
+
args.sgl_endpoint,
|
| 241 |
+
worker_id,
|
| 242 |
+
args.no_register,
|
| 243 |
+
args.model_name)
|
| 244 |
+
uvicorn.run(app, host=args.host, port=args.port, log_level="info")
|
之前的代码版本/这一版注意力有问题/MedEvalKit/models/HealthGPT/llava/serve/test_message.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
from llava.conversation import default_conversation
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def main():
|
| 10 |
+
if args.worker_address:
|
| 11 |
+
worker_addr = args.worker_address
|
| 12 |
+
else:
|
| 13 |
+
controller_addr = args.controller_address
|
| 14 |
+
ret = requests.post(controller_addr + "/refresh_all_workers")
|
| 15 |
+
ret = requests.post(controller_addr + "/list_models")
|
| 16 |
+
models = ret.json()["models"]
|
| 17 |
+
models.sort()
|
| 18 |
+
print(f"Models: {models}")
|
| 19 |
+
|
| 20 |
+
ret = requests.post(controller_addr + "/get_worker_address",
|
| 21 |
+
json={"model": args.model_name})
|
| 22 |
+
worker_addr = ret.json()["address"]
|
| 23 |
+
print(f"worker_addr: {worker_addr}")
|
| 24 |
+
|
| 25 |
+
if worker_addr == "":
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
+
conv = default_conversation.copy()
|
| 29 |
+
conv.append_message(conv.roles[0], args.message)
|
| 30 |
+
prompt = conv.get_prompt()
|
| 31 |
+
|
| 32 |
+
headers = {"User-Agent": "LLaVA Client"}
|
| 33 |
+
pload = {
|
| 34 |
+
"model": args.model_name,
|
| 35 |
+
"prompt": prompt,
|
| 36 |
+
"max_new_tokens": args.max_new_tokens,
|
| 37 |
+
"temperature": 0.7,
|
| 38 |
+
"stop": conv.sep,
|
| 39 |
+
}
|
| 40 |
+
response = requests.post(worker_addr + "/worker_generate_stream", headers=headers,
|
| 41 |
+
json=pload, stream=True)
|
| 42 |
+
|
| 43 |
+
print(prompt.replace(conv.sep, "\n"), end="")
|
| 44 |
+
for chunk in response.iter_lines(chunk_size=8192, decode_unicode=False, delimiter=b"\0"):
|
| 45 |
+
if chunk:
|
| 46 |
+
data = json.loads(chunk.decode("utf-8"))
|
| 47 |
+
output = data["text"].split(conv.sep)[-1]
|
| 48 |
+
print(output, end="\r")
|
| 49 |
+
print("")
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
parser = argparse.ArgumentParser()
|
| 54 |
+
parser.add_argument("--controller-address", type=str, default="http://localhost:21001")
|
| 55 |
+
parser.add_argument("--worker-address", type=str)
|
| 56 |
+
parser.add_argument("--model-name", type=str, default="facebook/opt-350m")
|
| 57 |
+
parser.add_argument("--max-new-tokens", type=int, default=32)
|
| 58 |
+
parser.add_argument("--message", type=str, default=
|
| 59 |
+
"Tell me a story with more than 1000 words.")
|
| 60 |
+
args = parser.parse_args()
|
| 61 |
+
|
| 62 |
+
main()
|