import requests
import json

outputs = []
with open('/mnt/Yuan2.0-M32/vllm/humaneval/human-eval-gpt4-translation-fixed5.jsonl', 'r', encoding='utf-8') as file:
    for line in file:
        data = json.loads(line)
        prompt = data.get('prompt')
        raw_json_data = {
                "model": "/mnt/beegfs2/Yuan2-M32-HF/",
                "prompt": prompt,
                "max_tokens": 256,
                "temperature": 1,
                "use_beam_search": False,
                "top_p": 0,
                "top_k": 1,
                "stop": "<eod>",
                }
        json_data = json.dumps(raw_json_data, ensure_ascii=True)
        headers = {
                "Content-Type": "application/json",
                }
        response = requests.post(f'http://localhost:8000/v1/completions',
                             data=json_data,
                             headers=headers)
        output = response.text
        output = json.loads(output)
        output = output["choices"][0]['text']
        #outputs.append(output0)
        print(output)
        break

GPT-4o

Poe

以下是用 vllm 部署的 Yuan API 代码逐行解释:

python

复制

import requests
import json
  • 说明:导入 requests 库用于发送 HTTP 请求,导入 json 库用于处理 JSON 数据。

python

复制

outputs = []
  • 说明:初始化一个空列表,用于存储输出结果。

python

复制

with open('/mnt/Yuan2.0-M32/vllm/humaneval/human-eval-gpt4-translation-fixed5.jsonl', 'r', encoding='utf-8') as file:
  • 说明:打开一个位于 /mnt/Yuan2.0-M32/vllm/humaneval/ 路径下的 JSONL 文件,文件名为 human-eval-gpt4-translation-fixed5.jsonl。文件以只读方式打开,并指定编码为 UTF-8。

python

复制

    for line in file:
  • 说明:逐行读取文件

python

复制

        data = json.loads(line)
  • 说明:将每行的 JSON 数据解析为 Python 字典。

python

复制

        prompt = data.get('prompt')
  • 说明:从解析后的字典中获取 prompt 字段的值

python

复制

        raw_json_data = {
                "model": "/mnt/beegfs2/Yuan2-M32-HF/",
                "prompt": prompt,
                "max_tokens": 256,
                "temperature": 1,
                "use_beam_search": False,
                "top_p": 0,
                "top_k": 1,
                "stop": "<eod>",
                }
  • 说明:构建一个包含请求参数的字典 raw_json_data,具体参数如下:
    • model: 模型路径 /mnt/beegfs2/Yuan2-M32-HF/
    • prompt: 请求的提示文本。
    • max_tokens: 最大生成的 token 数量,设为 256。
    • temperature: 生成的温度参数,设为 1。
    • use_beam_search: 是否使用束搜索,设为 False
    • top_p: 核心采样的累积概率阈值,设为 0。
    • top_k: 核心采样的最高概率 token 数,设为 1。
    • stop: 生成文本的停止标记,设为 <eod>

python

复制

        json_data = json.dumps(raw_json_data, ensure_ascii=True)
  • 说明:将 raw_json_data 字典转换为 JSON 格式的字符串,并确保所有字符都在 ASCII 范围内。

python

复制

        headers = {
                "Content-Type": "application/json",
                }
  • 说明:定义 HTTP 请求头,指定内容类型为 application/json

python

复制

        response = requests.post(f'http://localhost:8000/v1/completions',
                             data=json_data,
                             headers=headers)
  • 说明:发送 HTTP POST 请求到本地服务器的 /v1/completions 端点,传递 JSON 数据和请求头。

python

复制

        output = response.text
  • 说明:获取 HTTP 响应的文本内容。

python

复制

        output = json.loads(output)
  • 说明:将响应的 JSON 文本解析为 Python 字典。

python

复制

        output = output["choices"][0]['text']
  • 说明:从解析后的字典中提取生成的文本,具体路径是 choices 列表中的第一个元素的 text 字段。

python

复制

        #outputs.append(output)
  • 说明:将生成的文本追加到 outputs 列表中(此行被注释掉,未执行)。

python

复制

        print(output)
  • 说明:打印生成的文本。

python

复制

        break
  • 说明:跳出 for 循环,仅处理文件中的第一行

总结

这段代码读取一个 JSONL 文件中的每一行,提取 prompt 字段,然后使用 vllm 部署的 Yuan API 发送请求,生成文本后打印结果。代码目前设置为仅处理文件中的第一行

Step 4. 基于vllm.entrypoints.openai.api_server部署Yuan2-M32-HF

注意:openai.api_server调用tokenizer方式有所不同,在使用此服务时,先将tokenizer_config.json中的add_eos_token和add_bos_token设置为false

# 修改tokenizer_config.json文件
vim /mnt/beegfs2/Yuan2-M32-HF/tokenizer_config.json
# 以下内容修改为false即可
"add_bos_token": false,
"add_eos_token": false,

基于openai的api_server部署Yuan2.0-2B的步骤和step 3的步骤类似,发起服务和调用服务的方式如下:

发起服务命令:

# 发起服务,--model修改为您的ckpt路径
python -m vllm.entrypoints.openai.api_server --model=/mnt/beegfs2/Yuan2-M32-HF/ --trust-remote-code --disable-custom-all-reduce --tensor-parallel-size=8 --max-num-seqs=1 --gpu-memory-utilization=0.8

# 发起服务后,服务端显示如下:
INFO 05-17 09:38:43 ray_gpu_executor.py:228] # GPU blocks: 8073, # CPU blocks: 682
INFO 05-17 09:38:44 model_runner.py:970] Capturing the model for CUDA graphs. This may lead to unexpected consequences if the model is not static. To run the model in eager mode, set 'enforce_eager=True' or use '--enforce-eager' in the CLI.
INFO 05-17 09:38:44 model_runner.py:974] CUDA graphs can take additional 1~3 GiB memory per GPU. If you are running out of memory, consider decreasing `gpu_memory_utilization` or enforcing eager mode. You can also reduce the `max_num_seqs` as needed to decrease memory usage.
(RayWorkerVllm pid=42632) INFO 05-17 09:38:44 model_runner.py:970] Capturing the model for CUDA graphs. This may lead to unexpected consequences if the model is not static. To run the model in eager mode, set 'enforce_eager=True' or use '--enforce-eager' in the CLI.
(RayWorkerVllm pid=42632) INFO 05-17 09:38:44 model_runner.py:974] CUDA graphs can take additional 1~3 GiB memory per GPU. If you are running out of memory, consider decreasing `gpu_memory_utilization` or enforcing eager mode. You can also reduce the `max_num_seqs` as needed to decrease memory usage.
(RayWorkerVllm pid=43401) INFO 05-17 09:38:30 model_runner.py:169] Loading model weights took 9.7006 GB [repeated 6x across cluster]
(RayWorkerVllm pid=42632) INFO 05-17 09:38:44 model_runner.py:1061] Graph capturing finished in 0 secs.
INFO 05-17 09:38:44 model_runner.py:1061] Graph capturing finished in 0 secs.
WARNING 05-17 09:38:48 serving_chat.py:340] No chat template provided. Chat API will not work.
INFO:     Started server process [30051]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

调用服务命令:

# 使用curl命令行调用服务的指令如下
curl http://localhost:8000/v1/completions -H "Content-Type: application/json" -d '{"model": "/mnt/beegfs2/Yuan2-M32-HF/", "prompt": "写一篇春游作文", "max_tokens": 300, "temperature": 1, "top_p": 0, "top_k": 1, "stop": "<eod>"}'

openAI api

python -m vllm.entrypoints.openai.api_server --model=/mnt/Yuan2-M32-hf/ --trust-remote-code --disable-custom-all-reduce --tensor-parallel-size=8 --max-num-seqs=1 --gpu-memory-utilization=0.8
2024-07-11 10:17:45,445	INFO worker.py:1749 -- Started a local Ray instance.

GPT-4o

Poe

以下是该 vllm 启动命令逐个参数的详解:

python -m vllm.entrypoints.openai.api_server \
    --model=/mnt/Yuan2-M32-hf/ \
    --trust-remote-code \
    --disable-custom-all-reduce \
    --tensor-parallel-size=8 \
    --max-num-seqs=1 \
    --gpu-memory-utilization=0.8

参数详解

python -m vllm.entrypoints.openai.api_server
  • 说明:使用 python -m 命令启动 vllm.entrypoints.openai.api_server 模块。这个模块是 vllm 的一个入口点,用于启动 OpenAI 风格的 API 服务器。
--model=/mnt/Yuan2-M32-hf/
  • 说明:指定模型的路径,这里是 /mnt/Yuan2-M32-hf/。这个路径指向了存储预训练模型的目录。
--trust-remote-code
  • 说明:允许信任远程代码。这通常用于加载可能包含自定义代码的模型。
--disable-custom-all-reduce
  • 说明:禁用自定义的 all-reduce 操作all-reduce 是一种分布式计算操作,通常用于在多个 GPU 之间汇总数据。禁用这个选项可能是为了使用默认的 all-reduce 实现。
--tensor-parallel-size=8
  • 说明:设置张量并行的大小,这里是 8。张量并行意味着模型的某些部分将在多个 GPU 上并行计算。大小为 8 表示使用 8 个 GPU 进行张量并行计算。
--max-num-seqs=1
  • 说明:设置最大序列数量,这里是 1。这个参数限制了服务器一次可以处理的最大序列数量。
--gpu-memory-utilization=0.8
  • 说明:设置 GPU 内存的利用率,这里是 0.8,表示最多使用 80% 的 GPU 内存。这有助于防止因内存不足而导致的错误。

其他信息

2024-07-11 10:17:45,445 INFO worker.py:1749 -- Started a local Ray instance.
  • 说明:启动命令的输出信息,表示 Ray 实例已在本地启动。Ray 是一个用于分布式计算的框架,vllm 可能利用 Ray 来管理分布式 GPU 资源和任务调度。
Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐