南阳商都网站做网站腾讯企点app下载安装
最近这一两周看到不少互联网公司都已经开始秋招提前批了。不同以往的是,当前职场环境已不再是那个双向奔赴时代了。求职者在变多,HC 在变少,岗位要求还更高了。
最近,我们又陆续整理了很多大厂的面试题,帮助一些球友解惑答疑,分享技术面试中的那些弯弯绕绕。
《大模型面试宝典》(2024版) 发布!
《AIGC 面试宝典》圈粉无数!
喜欢本文记得收藏、关注、点赞。更多实战和面试交流,欢迎交流
文章目录
- 模型推理
- 模型微调
- 模型部署
- Llama3.1 工具调用服务实战
近日,Meta正式发布Llama 3.1,包含8B、70B 和405B三个规模,最大上下文提升到了128k。Llama系列模型是目前开源领域中用户最多、性能最强的大型模型系列之一。
本次Llama 3.1的要点有:
1.共有8B、70B及405B三种版本,其中405B版本是目前最大的开源模型之一;
2.该模型最大参数规模达到4050亿参数,在性能上超越了现有的顶级AI模型;
3.模型引入了更长的上下文窗口(最长可达128K tokens),能够处理更复杂的任务和对话;
4. 支持多语言输入和输出,增强了模型的通用性和适用范围;
5.提高了推理能力,特别是在解决复杂数学问题和即时生成内容方面表现突出。
为大家带来的一站式模型体验、下载、推理、微调、部署实战教程!
模型推理
以Llama-3.1-8B-Instruct为例:
import transformers
import torch
from modelscope import snapshot_downloadmodel_id = snapshot_download("LLM-Research/Meta-Llama-3.1-8B-Instruct")pipeline = transformers.pipeline("text-generation",model=model_id,model_kwargs={"torch_dtype": torch.bfloat16},device_map="auto",
)messages = [{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},{"role": "user", "content": "Who are you?"},
]outputs = pipeline(messages,max_new_tokens=256,
)
print(outputs[0]["generated_text"][-1])
模型微调
我们介绍使用ms-swift对llama3_1-8b-instruct进行古文翻译腔微调,并对微调前后模型进行推理。swift是魔搭社区官方提供的LLM工具箱,支持300+大语言模型和50+多模态大模型的微调、推理、量化、评估和部署。
在开始微调之前,请确保您的环境已正确安装
# 安装ms-swift
git clone https://github.com/modelscope/swift.git
cd swift
pip install -e .[llm]
微调脚本:(如果出现OOM,请降低max_length)
# 实验环境: 3090/A10
# 显存占用: 24GB
CUDA_VISIBLE_DEVICES=0 \
swift sft \--model_type llama3_1-8b-instruct \--sft_type lora \--output_dir output \--dataset classical-chinese-translate \--num_train_epochs 1 \--max_length 2048 \--gradient_checkpointing true \--batch_size 1 \--gradient_accumulation_steps 16 \--warmup_ratio 0.1 \--eval_steps 100 \--save_steps 100 \--save_total_limit -1 \--logging_steps 10# 实验环境: 4 * 3090/A10
# 显存占用: 4 * 24GB
# DDP + ZeRO2
nproc_per_node=4NPROC_PER_NODE=$nproc_per_node \
CUDA_VISIBLE_DEVICES=0,1,2,3 \
swift sft \--model_type llama3_1-8b-instruct \--sft_type lora \--output_dir output \--dataset classical-chinese-translate \--num_train_epochs 1 \--max_length 2048 \--gradient_checkpointing true \--batch_size 1 \--gradient_accumulation_steps $(expr 16 / $nproc_per_node) \--warmup_ratio 0.1 \--eval_steps 100 \--save_steps 100 \--save_total_limit -1 \--logging_steps 10 \--deepspeed default-zero2
微调显存消耗:
微调过程的loss可视化:
微调后推理脚本如下,这里的ckpt_dir需要修改为训练生成的last checkpoint文件夹。我们可以使用vLLM对merge后的checkpoint进行推理加速。
pip install vllm -U # vllm>=0.5.3.post1# Experimental environment: A10, 3090, V100, ...
CUDA_VISIBLE_DEVICES=0 swift export \--ckpt_dir output/llama3_1-8b-instruct/vx-xxx/checkpoint-xxx \--merge_lora true# 使用vLLM进行推理加速
CUDA_VISIBLE_DEVICES=0 swift infer \--ckpt_dir output/llama3_1-8b-instruct/vx-xxx/checkpoint-xxx-merged \--infer_backend vllm --max_model_len 4096
微调后模型对验证集进行推理的示例:
模型部署
使用vLLM部署Llama3.1-70B-Instruct
部署Llama3.1-70B-Instruct需要至少2卡80GiB A100 GPU,部署方式如下:
服务端:
# 请确保已经安装了git-lfs
git lfs installGIT_LFS_SKIP_SMUDGE=1 git clone https://www.modelscope.cn/LLM-Research/Meta-Llama-3.1-70B-Instruct.git
cd Meta-Llama-3.1-70B-Instruct
git lfs pull# 实验环境:2 * A100
# <local_path>传入本地路径
CUDA_VISIBLE_DEVICES=0,1 vllm serve <local_path> \--dtype bfloat16 --served-model-name llama3_1-70b-instruct \--gpu_memory_utilization 0.96 --tensor_parallel_size 2 \--max_model_len 50000# or 实验环境:4 * A100
CUDA_VISIBLE_DEVICES=0,1,2,3 vllm serve <local_path> \--dtype bfloat16 --served-model-name llama3_1-70b-instruct \--tensor_parallel_size 4
客户端:
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama3_1-70b-instruct",
"messages": [{"role": "user", "content": "晚上睡不着觉怎么办?"}],
"max_tokens": 1024,
"temperature": 0
}'
模型输出:
{"id":"chat-d1b12066eedf445bbee4257a8c3a1b30","object":"chat.completion","created":1721809149,"model":"llama3_1-70b-instruct","choices":[{"index":0,"message":{"role":"assistant","content":"答:如果你晚上睡不着觉,可以尝试以下方法:1. 保持卧室安静、黑暗和凉爽。2. 避免在睡前使用电子设备。3. 不要在睡前饮用含有咖啡因的饮料。4. 尝试放松技巧,如深呼吸、冥想或瑜伽。5. 如果问题持续,可以咨询医生或睡眠专家。","tool_calls":[]},"logprobs":null,"finish_reason":"stop","stop_reason":null}],"usage":{"prompt_tokens":19,"total_tokens":128,"completion_tokens":109}}
Llama3.1 工具调用服务实战
环境准备
Llama3.1部署依赖vllm 最新补丁版本 0.5.3.post1
# speed up if needed
# pip config set global.index-url https://mirrors.cloud.aliyuncs.com/pypi/simple
# pip config set install.trusted-host mirrors.cloud.aliyuncs.com
pip install https://github.com/vllm-project/vllm/releases/download/v0.5.3.post1/vllm-0.5.3.post1+cu118-cp310-cp310-manylinux1_x86_64.whl
依赖modelscope-agent项目下的modelscope-agent-server进行tool calling能力调用
git clone https://github.com/modelscope/modelscope-agent.git
cd modelscope-agent
服务调用
利用modelscope-agent-server的能力,允许用户在本地拉起一个支持openai SDK调用的chat/completions服务,并且赋予该模型tool calling 的能力。这样子可以让原本仅支持prompt调用的模型,可以通过modelscope的服务快速进行tool calling的调用。
服务curl调用
于此同时, 服务启动以后,可以通过以下方式curl 使用带有tool的信息调用服务。
curl -X POST 'http://localhost:31512/v1/chat/completions' \
-H 'Content-Type: application/json' \
-d '{"tools": [{"type": "function","function": {"name": "amap_weather","description": "amap weather tool","parameters": [{"name": "location","type": "string","description": "城市/区具体名称,如`北京市海淀区`请描述为`海淀区`","required": true}]}}],"tool_choice": "auto","model": "meta-llama/Meta-Llama-3.1-8B-Instruct","messages": [{"content": "海淀区天气", "role": "user"}]
}'
返回如下结果:
{"request_id": "chatcmpl_84a66af2-4021-4ae6-822d-8e3f42ca9f43","message": "","output": null,"id": "chatcmpl_84a66af2-4021-4ae6-822d-8e3f42ca9f43","choices": [{"index": 0,"message": {"role": "assistant","content": "工具调用\nAction: amap_weather\nAction Input: {\"location\": \"北京市\"}\n","tool_calls": [{"type": "function","function": {"name": "amap_weather","arguments": "{\"location\": \"北京市\"}"}}]},"finish_reason": "tool_calls"}],"created": 1721803228,"model": "meta-llama/Meta-Llama-3.1-8B-Instruct","system_fingerprint": "chatcmpl_84a66af2-4021-4ae6-822d-8e3f42ca9f43","object": "chat.completion","usage": {"prompt_tokens": -1,"completion_tokens": -1,"total_tokens": -1}
}