【AI实战】开源中文 llama2 来了,30 分钟搭建 130 亿参数大模型 Llama2-Chinese-13b-Chat

本文详细介绍了如何在Ubuntu环境中配置和搭建Llama2-Chinese-13b-Chat模型,包括拉取docker镜像、安装依赖、下载模型权重,以及使用gradio搭建交互页面。同时提供了国内的下载链接作为备选。
部署运行你感兴趣的模型镜像

简介

环境配置

环境搭建

  • 系统环境

    • Ubuntu 20.04LTS
    • CUDA 11.8
    • Docker 18.09.5
  • 创建docker容器

拉取docker镜像

docker pull nvcr.io/nvidia/pytorch:21.08-py3

创建docker


nvidia-docker run -it -d \
    --name llama2_chinese \
    -p 15550:15550 \
    -p 15551:15551 \
    -v /xx/xx/llm:/notebooks \
    -e TZ='Asia/Shanghai' \
    --shm-size 16G \
    nvcr.io/nvidia/pytorch:21.08-py3
    
# docker exec -it llama2_chinese env LANG=C.UTF-8 /bin/bash

修改 /xx/xx/llm 为自己的路径

进入容器内:

docker exec -it llama2_chinese env LANG=C.UTF-8 /bin/bash
  • 安装conda

下载:

cd /notebooks
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

安装:

bash Miniconda3-latest-Linux-x86_64.sh

安装提升安装即可

将miniconda加入PATH路径:

export PATH="/root/miniconda3/bin:$PATH"

创建 conda 环境:

conda create -n llama2 python=3.10.9
  • 安装依赖库
conda activate llama2
conda init

exit退出docker,重新进入docker

docker exec -it llama2_chinese  env LANG=C.UTF-8 /bin/bash
cd /notebooks
conda activate llama2

依赖安装

安装依赖库:

pip install -r requirements-2.txt  -i https://pypi.tuna.tsinghua.edu.cn/simple

代码及模型权重拉取

拉取 Llama2-Chinese

git clone https://github.com/FlagAlpha/Llama2-Chinese.git

github网站偶尔会抽风,需要耐心等待,如果失败了,执行 rm -rf Llama2-Chinese,再重新拉取

拉取 Llama2-Chinese-13b-Chat 模型权重及代码

cd Llama2-Chinese
git clone git clone https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat

由于权重文件特别大,如果失败了,执行 rm -rf Llama2-Chinese-13b-Chat,再重新拉取。
建议中午时间拉取,速度比较快,大概 1 小时(和你的网络带宽有很大关系!)。

文件大小查看:

du -sh Llama2-Chinese-13b-Chat

输出:

25G    Llama2-Chinese-13b-Chat

如果文件大小不一致,或者太小,说明权重文件下载识别,
请执行: rm -rf Llama2-Chinese-13b-Chat, 再重新拉取
或者 按照后面的方法去下载权重文件

查看文件列表:

ls -l Llama2-Chinese-13b-Chat/

输出:

# ls -l Llama2-Chinese-13b-Chat/
total 25421840
-rw-r--r-- 1 root root       1514 Jul 27 06:33 README.md
-rw-r--r-- 1 root root        683 Jul 27 06:33 config.json
-rw-r--r-- 1 root root        327 Jul 27 07:12 down.sh
-rw-r--r-- 1 root root        175 Jul 27 06:33 generation_config.json
-rw-r--r-- 1 root root 9948728430 Jul 24 23:12 pytorch_model-00001-of-00003.bin
-rw-r--r-- 1 root root 9904165024 Jul 24 12:44 pytorch_model-00002-of-00003.bin
-rw-r--r-- 1 root root 6178983625 Jul 24 12:14 pytorch_model-00003-of-00003.bin
-rw-r--r-- 1 root root      33444 Jul 27 06:33 pytorch_model.bin.index.json
-rw-r--r-- 1 root root        414 Jul 27 06:33 special_tokens_map.json
-rw-r--r-- 1 root root        131 Jul 27 06:33 tokenizer.model
-rw-r--r-- 1 root root        749 Jul 27 06:33 tokenizer_config.json

【】如果大文件下载识别,按照下面的方法下载模型文件;

wget https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat/resolve/main/pytorch_model-00001-of-00003.bin
wget https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat/resolve/main/pytorch_model-00002-of-00003.bin
wget https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat/resolve/main/pytorch_model-00003-of-00003.bin

如果还是没法下载,参考下面的“国内 Llama2 最新下载地址” 进行下载

终端测试

进入python环境:

python3

输入代码:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained('Llama2-Chinese-13b-Chat',device_map='auto',torch_dtype=torch.float16,load_in_8bit=True)
model =model.eval()
tokenizer = AutoTokenizer.from_pretrained('Llama2-Chinese-13b-Chat',use_fast=False)
tokenizer.pad_token = tokenizer.eos_token
input_ids = tokenizer(['<s>Human: 介绍一下深圳\n</s><s>Assistant: '], return_tensors="pt",add_special_tokens=False).input_ids.to('cuda')        
generate_input = {
    "input_ids":input_ids,
    "max_new_tokens":512,
    "do_sample":True,
    "top_k":50,
    "top_p":0.95,
    "temperature":0.3,
    "repetition_penalty":1.3,
    "eos_token_id":tokenizer.eos_token_id,
    "bos_token_id":tokenizer.bos_token_id,
    "pad_token_id":tokenizer.pad_token_id
}
generate_ids  = model.generate(**generate_input)
text = tokenizer.decode(generate_ids[0])
print(text)

页面测试

使用 gradio 搭建页面

安装 gradio

pip install gradio -i https://pypi.tuna.tsinghua.edu.cn/simple

加载模型并启动服务

修改端口;

vi /notebooks/Llama2-Chinese/examples/chat_gradio.py

到94行:

    demo.queue().launch(share=False, debug=True, server_name="0.0.0.0")

修改为:

    demo.queue().launch(share=False, debug=True, server_name="0.0.0.0", server_port=15550)

启动脚本:

python examples/chat_gradio.py --model_name_or_path Llama2-Chinese-13b-Chat

如果出现下面的错误:

  File "/notebooks/Llama2-Chinese/examples/chat_gradio.py", line 94
    demo.queue().launch(share=False, debug=True, server_name="0.0.0.0")
                                               ^
SyntaxError: invalid character ',' (U+FF0C)

则按照下面的步骤修改代码:

vi /notebooks/Llama2-Chinese/examples/chat_gradio.py
:94 
修改中文逗号,为英文逗号,

94    demo.queue().launch(share=False, debug=True, server_name="0.0.0.0")
=>
94    demo.queue().launch(share=False, debug=True, server_name="0.0.0.0")
  • 测试

    浏览器打开地址:http://10.192.x.x:15550/

国内 Llama2 最新下载地址

Llama2-7B官网版本:https://pan.xunlei.com/s/VN_kR2fwuJdG1F3CoF33rwpIA1?pwd=z9kf

Llama2-7B-Chat官网版本:https://pan.xunlei.com/s/VN_kQa1_HBvV-X9QVI6jV2kOA1?pwd=xmra

Llama2-13B官网版本:https://pan.xunlei.com/s/VN_izibaMDoptluWodzJw4cRA1?pwd=2qqb

Llama2-13B-Chat官网版本:https://pan.xunlei.com/s/VN_iyyponyapjIDLXJCNfqy7A1?pwd=t3xw

Llama2-7B Hugging Face版本:https://pan.xunlei.com/s/VN_t0dUikZqOwt-5DZWHuMvqA1?pwd=66ep

Llama2-7B-Chat Hugging Face版本:https://pan.xunlei.com/s/VN_oaV4BpKFgKLto4KgOhBcaA1?pwd=ufir

Llama2-13B Hugging Face版本:https://pan.xunlei.com/s/VN_yT_9G8xNOz0SDWQ7Mb_GZA1?pwd=yvgf

Llama2-13B-Chat Hugging Face版本:https://pan.xunlei.com/s/VN_yA-9G34NGL9B79b3OQZZGA1?pwd=xqrg

参考

1.https://github.com/FlagAlpha/Llama2-Chinese
2.https://huggingface.co/FlagAlpha/Llama2-Chinese-13b-Chat/tree/main
3.https://github.com/facebookresearch/llama
4.https://huggingface.co/meta-llama

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

### Chinese LLaMA2 13B Chat 模型下载与使用教程 #### 模型概述 Chinese LLaMA2 13B Chat 是基于 Meta 发布的 LLaMA2 模型进行改进的一个版本,专注于中英文双语支持。该模型通过扩展中文词表并利用大量中英文预训练语料进行增量训练,显著提升了其在中文场景下的表现能力[^1]。 #### 下载资源 目前,类似的开源项目可以参考 **Llama2-Chinese-13b-Chat** 的公开实现。以下是该项目的相关信息: - **项目地址**: [https://gitcode.com/hf_mirrors/ai-gitcode/Llama2-Chinese-13b-Chat](https://gitcode.com/hf_mirrors/ai-gitcode/Llama2-Chinese-13b-Chat)[^2] 可以通过访问上述链接获取模型权重文件以及相关文档说明。如果目标模型的具体名称为 `Chinese-LLaMA2-13B-chat`,建议进一步确认是否有独立仓库或官方发布渠道。 #### 安装依赖库 为了顺利运行此模型,需先安装必要的 Python 库。以下是一个推荐的环境配置方法: ```bash %pip install transformers accelerate torch datasets safetensors bitsandbytes ``` 对于更高级的功能需求(如索引构建),可额外安装如下工具包: ```bash !pip install llama-index llms-replicate ``` 以上命令用于安装 LlamaIndex 和 Replicate 支持模块[^5]。 #### 加载模型实例代码 加载模型通常借助 Hugging Face Transformers 工具完成。下面提供一段简单的加载脚本供参考: ```python from transformers import AutoTokenizer, AutoModelForCausalLM model_name_or_path = "path/to/chinese_llama2_13b_chat" tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) model = AutoModelForCausalLM.from_pretrained( model_name_or_path, device_map="auto", # 自动分配到 GPU 或 CPU load_in_8bit=True # 启用量化减少显存占用 ) def generate_text(prompt, max_length=100): inputs = tokenizer(prompt, return_tensors="pt").to('cuda') outputs = model.generate(**inputs, max_new_tokens=max_length) result = tokenizer.decode(outputs[0], skip_special_tokens=True) return result prompt = "你好,世界!" response = generate_text(prompt) print(response) ``` 注意:实际路径应替换为本地解压后的模型目录或者远程存储位置。 #### 技术优势与局限性分析 虽然 Chinese LLaMA2 13B Chat 在自然语言理解和生成上具备较强的能力,但在某些特殊领域仍可能存在不足之处。例如,在面对非常规的语言结构或是高度专业化术语时,模型的表现可能受限于原始训练数据的质量和覆盖范围[^3]。 此外,由于此类大型语言模型对计算资源的需求较高,部署过程中也需要考虑硬件条件是否满足要求。 --- ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szZack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值