​ 本博客以 Llama3.2 的 1B-Instruct 版本为例,在远程服务器上部署模型(和在本地部署类似)。服务器为 X86 Ubuntu 系统

​ 首先安装必要的环境如 torch 和 transformers 等

  1. Llama3 版本的模型权重和分词器需要申请,因此首先去 HuggingFace 的 meta 官方处申请使用模型,也可以直接下载第三方模型;
  2. 申请通过后,在 “Files and versions” 中下载模型文件.safetensor 和配置文件.json 到本地,这一步也可以直接通过 git 或代码中访问远程仓库下载,但需要远程服务器附魔;
  3. 文件下载完成后,本地打包上传至服务器目录 /home/user/.cache/huggingface/hub/ 下;
  4. 写 python 脚本:
python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
model_path = "/home/user/.cache/huggingface/hub/Llama3/"
pipe = pipeline(
"text-generation",
model=model_path,
torch_dtype=torch.bfloat16,
device_map="auto",
)

while True:
prompt = input("Please input:")
messages = [{"role": "system", "content": "You are a linguist who is good at English, now help me to fix the words in the sentence."},{"role": "user", "content": "{}".format(prompt)},]

outputs = pipe(
messages,
max_new_tokens=256,
)
print(outputs[0]["generated_text"][-1]['content'])
  1. 运行脚本即可循环调用模型,实现和大模型的对话