Commit c9671d72 authored by guobj's avatar guobj
Browse files

Update README.md

parent b5f55dad
<!-- # Gemma-2
* @Author: zhuww
* @email: zhuww@sugon.com
* @Date: 2024-04-25 10:38:07
* @LastEditTime: 2024-12-11 17:18:01
-->
# LLAMA
## 论文 ## 论文
- [https://arxiv.org/pdf/2302.13971.pdf](https://arxiv.org/pdf/2302.13971.pdf) - [Gemma 2: Improving Open Language Models at a Practical Size](https://storage.googleapis.com/deepmind-media/gemma/gemma-2-report.pdf)
## 模型结构 ## 模型结构
LLAMA网络基于 Transformer 架构。提出了各种改进,并用于不同的模型,例如 PaLM。以下是与原始架构的主要区别: Gemma 2 与第一代有许多相似之处,使用transformer decoder结构进行训练。它有 8192 Tokens 的上下文长度,并使用旋转位置嵌入 (RoPE)。在7B模型中,Gemma 2使用了multi-head attention;而在2B模型中,它使用了multi-query attention(具有num_kv_heads = 1)。这种注意力机制有助于模型在处理文本数据时更好地捕捉相关信息。
预归一化。为了提高训练稳定性,对每个transformer 子层的输入进行归一化,而不是对输出进行归一化。使用 RMSNorm 归一化函数 Gemma 2不使用绝对位置编码,而是在每一层前加入RoPE Embedding,并共享输入与输出层的embedding权重。这种方法提高了模型的性能,并减少了计算量
SwiGLU 激活函数 [PaLM]。使用 SwiGLU 激活函数替换 ReLU 非线性以提高性能。使用 2 /3 4d 的维度而不是 PaLM 中的 4d Gemma 2将ReLU激活替换为GeGLU激活。GeGLU是一种基于门控线性单元(GLU)的改进技术,具有更好的性能表现
旋转嵌入。移除了绝对位置嵌入,而是添加了旋转位置嵌入 (RoPE),在网络的每一层 在transformer的每一层layer的前后都进行规一化,Gemma 2使用RMSNorm作为规一化层。这种规一化策略有助于提高模型的稳定性和性能
![img](./docs/llama_str.png) <div align=center>
<img src="./docs/gemma2.jpg"/>
</div>
## 算法原理 ## 算法原理
LLama是一个基础语言模型的集合,参数范围从7B到65B。在数万亿的tokens上训练出的模型,并表明可以专门使用公开可用的数据集来训练最先进的模型,而不依赖于专有的和不可访问的数据集。 Gemma 2是由Google推出的一系列轻量级、最先进的开放模型,旨在推动负责任的AI创新。它基于与创建Gemini模型相同的研究和技术进行构建,提供了27B(270亿)参数大小的版本,具有出色的性能和效率。与原始 Gemma 相比,Gemma 2 的主要进展有四点:
![img](./docs/llama_pri.png) + 滑动窗口注意力: 交替使用滑动窗口和全二次注意力以提高生成质量。
+ Logit 软上限: 通过将 logits 缩放到固定范围来防止其过度增长,从而改进训练。
+ 知识蒸馏: 利用较大的教师模型来训练较小的模型(适用于 90 亿模型)。
+ 模型合并: 将两个或多个大语言模型合并成一个新的模型。
Gemma 2 Instruct 已针对对话应用进行了优化,并使用监督微调 (SFT)、大模型蒸馏、人类反馈强化学习 (RLHF) 和模型合并 (WARP) 来提高整体性能。
## 环境配置 ## 环境配置
...@@ -33,7 +34,7 @@ docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.3.0-py3.10-dtk24.0 ...@@ -33,7 +34,7 @@ docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.3.0-py3.10-dtk24.0
# <Host Path>主机端路径 # <Host Path>主机端路径
# <Container Path>容器映射路径 # <Container Path>容器映射路径
# 若要在主机端和容器端映射端口需要删除--network host参数 # 若要在主机端和容器端映射端口需要删除--network host参数
docker run -it --name llama_vllm --privileged --shm-size=64G --device=/dev/kfd --device=/dev/dri/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --ulimit memlock=-1:-1 --ipc=host --network host --group-add video -v /opt/hyhal:/opt/hyhal -v <Host Path>:<Container Path> <Image ID> /bin/bash docker run -it --name gemma2_vllm --privileged --shm-size=64G --device=/dev/kfd --device=/dev/dri/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --ulimit memlock=-1:-1 --ipc=host --network host --group-add video -v /opt/hyhal:/opt/hyhal -v <Host Path>:<Container Path> <Image ID> /bin/bash
``` ```
`Tips:若在K100/Z100L上使用,使用定制镜像docker pull image.sourcefind.cn:5000/dcu/admin/base/custom:vllm0.5.0-dtk24.04.1-ubuntu20.04-py310-zk-v1,K100/Z100L不支持awq量化` `Tips:若在K100/Z100L上使用,使用定制镜像docker pull image.sourcefind.cn:5000/dcu/admin/base/custom:vllm0.5.0-dtk24.04.1-ubuntu20.04-py310-zk-v1,K100/Z100L不支持awq量化`
...@@ -41,13 +42,13 @@ docker run -it --name llama_vllm --privileged --shm-size=64G --device=/dev/kfd ...@@ -41,13 +42,13 @@ docker run -it --name llama_vllm --privileged --shm-size=64G --device=/dev/kfd
``` ```
# <Host Path>主机端路径 # <Host Path>主机端路径
# <Container Path>容器映射路径 # <Container Path>容器映射路径
docker build -t llama:latest . docker build -t gemma2:latest .
docker run -it --name llama_vllm --privileged --shm-size=64G --device=/dev/kfd --device=/dev/dri/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --ulimit memlock=-1:-1 --ipc=host --network host --group-add video -v /opt/hyhal:/opt/hyhal -v <Host Path>:<Container Path> llama:latest /bin/bash docker run -it --name gemma2_vllm --privileged --shm-size=64G --device=/dev/kfd --device=/dev/dri/ --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --ulimit memlock=-1:-1 --ipc=host --network host --group-add video -v /opt/hyhal:/opt/hyhal -v <Host Path>:<Container Path> gemma2:latest /bin/bash
``` ```
### Anaconda(方法三) ### Anaconda(方法三)
``` ```
conda create -n llama_vllm python=3.10 conda create -n gemma2_vllm python=3.10
``` ```
关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装。 关于本项目DCU显卡所需的特殊深度学习库可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装。
* DTK驱动:dtk24.04.3 * DTK驱动:dtk24.04.3
...@@ -68,15 +69,16 @@ conda create -n llama_vllm python=3.10 ...@@ -68,15 +69,16 @@ conda create -n llama_vllm python=3.10
### 模型下载 ### 模型下载
**快速下载通道:** **快速下载通道:**
| 基座模型 | chat模型 | GPTQ模型 | AWQ模型 | | 基座模型 | SCnet快速下载链接 |
| ------- | ------- | ------- | ------- | | ------- | ------- |
| [Llama-2-7b-hf](http://113.200.138.88:18080/aimodels/Llama-2-7b-hf) | [Llama-2-7b-chat-hf](http://113.200.138.88:18080/aimodels/Llama-2-7b-chat-hf) | [Llama-2-7B-Chat-GPTQ](http://113.200.138.88:18080/aimodels/Llama-2-7B-Chat-GPTQ) | [Llama-2-7B-Chat-AWQ](http://113.200.138.88:18080/aimodels/thebloke/Llama-2-7B-AWQ) | | [gemma-2-2b]| (http://113.200.138.88:18080/aimodels/gemma-2-2b)|
| [Llama-2-13b-hf](http://113.200.138.88:18080/aimodels/Llama-2-13b-hf) | [Llama-2-13b-chat-hf](http://113.200.138.88:18080/aimodels/meta-llama/Llama-2-13b-chat-hf) | [Llama-2-13B-GPTQ](http://113.200.138.88:18080/aimodels/Llama-2-13B-chat-GPTQ) | [Llama-2-13B-AWQ](http://113.200.138.88:18080/aimodels/thebloke/Llama-2-13B-AWQ) | | [gemma-2-2b-it]| (http://113.200.138.88:18080/aimodels/gemma-2-2b-it)|
| [Llama-2-70b-hf](http://113.200.138.88:18080/aimodels/Llama-2-70b-hf) | [Llama-2-70b-chat-hf](http://113.200.138.88:18080/aimodels/meta-llama/Llama-2-70b-chat-hf) | [Llama-2-70B-Chat-GPTQ](http://113.200.138.88:18080/aimodels/Llama-2-70B-Chat-GPTQ) | [Llama-2-70B-Chat-AWQ](http://113.200.138.88:18080/aimodels/thebloke/Llama-2-70B-AWQ) | | [gemma-2-9b]| (http://113.200.138.88:18080/aimodels/gemma-2-9b)|
| [Meta-Llama-3-8B](http://113.200.138.88:18080/aimodels/Meta-Llama-3-8B) | [Meta-Llama-3-8B-Instruct](http://113.200.138.88:18080/aimodels/Meta-Llama-3-8B-Instruct) | [Meta-Llama-3-8B-Instruct-AWQ](http://113.200.138.88:18080/aimodels/solidrust/Meta-Llama-3-8B-Instruct-hf-AWQ) | | [gemma-2-9b-it]| (http://113.200.138.88:18080/aimodels/gemma-2-9b-it)|
| [Meta-Llama-3-70B](http://113.200.138.88:18080/aimodels/Meta-Llama-3-70B) | [Meta-Llama-3-70B-Instruct](http://113.200.138.88:18080/aimodels/Meta-Llama-3-70B-Instruct) | [Meta-Llama-3-70B-Instruct-AWQ](http://113.200.138.88:18080/aimodels/techxgenus/Meta-Llama-3-70B-Instruct-AWQ) | | [gemma-2-27b]| (http://113.200.138.88:18080/aimodels/google/gemma-2-27b)|
| [gemma-2-27b-it]| (http://113.200.138.88:18080/aimodels/gemma-2-27b-it)|
| 注 | 需要修改相应模型的config.json中"attn_logit_softcapping": null ;"final_logit_softcapping": null |
### 离线批量推理 ### 离线批量推理
```bash ```bash
python examples/offline_inference.py python examples/offline_inference.py
...@@ -88,7 +90,7 @@ python examples/offline_inference.py ...@@ -88,7 +90,7 @@ python examples/offline_inference.py
### 离线批量推理性能测试 ### 离线批量推理性能测试
1、指定输入输出 1、指定输入输出
```bash ```bash
python benchmarks/benchmark_throughput.py --num-prompts 1 --input-len 32 --output-len 128 --model meta-llama/Llama-2-7b-chat-hf -tp 1 --trust-remote-code --enforce-eager --dtype float16 python benchmarks/benchmark_throughput.py --num-prompts 1 --input-len 32 --output-len 128 --model gemma-2-9b-it -tp 1 --trust-remote-code --enforce-eager --dtype float16
``` ```
其中`--num-prompts`是batch数,`--input-len`是输入seqlen,`--output-len`是输出token长度,`--model`为模型路径,`-tp`为使用卡数,`dtype="float16"`为推理数据类型,如果模型权重是bfloat16,需要修改为float16推理。若指定`--output-len 1`即为首字延迟。`-q gptq`为使用gptq量化模型进行推理。 其中`--num-prompts`是batch数,`--input-len`是输入seqlen,`--output-len`是输出token长度,`--model`为模型路径,`-tp`为使用卡数,`dtype="float16"`为推理数据类型,如果模型权重是bfloat16,需要修改为float16推理。若指定`--output-len 1`即为首字延迟。`-q gptq`为使用gptq量化模型进行推理。
...@@ -100,7 +102,7 @@ wget http://113.200.138.88:18080/aidatasets/anon8231489123/ShareGPT_Vicuna_unfil ...@@ -100,7 +102,7 @@ wget http://113.200.138.88:18080/aidatasets/anon8231489123/ShareGPT_Vicuna_unfil
``` ```
```bash ```bash
python benchmarks/benchmark_throughput.py --num-prompts 1 --model meta-llama/Llama-2-7b-chat-hf --dataset ShareGPT_V3_unfiltered_cleaned_split.json -tp 1 --trust-remote-code --enforce-eager --dtype float16 python benchmarks/benchmark_throughput.py --num-prompts 1 --model gemma-2-9b-it --dataset ShareGPT_V3_unfiltered_cleaned_split.json -tp 1 --trust-remote-code --enforce-eager --dtype float16
``` ```
其中`--num-prompts`是batch数,`--model`为模型路径,`--dataset`为使用的数据集,`-tp`为使用卡数,`dtype="float16"`为推理数据类型,如果模型权重是bfloat16,需要修改为float16推理。`-q gptq`为使用gptq量化模型进行推理。 其中`--num-prompts`是batch数,`--model`为模型路径,`--dataset`为使用的数据集,`-tp`为使用卡数,`dtype="float16"`为推理数据类型,如果模型权重是bfloat16,需要修改为float16推理。`-q gptq`为使用gptq量化模型进行推理。
...@@ -108,12 +110,12 @@ python benchmarks/benchmark_throughput.py --num-prompts 1 --model meta-llama/Lla ...@@ -108,12 +110,12 @@ python benchmarks/benchmark_throughput.py --num-prompts 1 --model meta-llama/Lla
### openAI api服务推理性能测试 ### openAI api服务推理性能测试
1、启动服务端: 1、启动服务端:
```bash ```bash
python -m vllm.entrypoints.openai.api_server --model meta-llama/Llama-2-7b-chat-hf --dtype float16 --enforce-eager -tp 1 python -m vllm.entrypoints.openai.api_server --model gemma-2-9b-it --dtype float16 --enforce-eager -tp 1
``` ```
2、启动客户端: 2、启动客户端:
```bash ```bash
python benchmarks/benchmark_serving.py --model meta-llama/Llama-2-7b-chat-hf --dataset ShareGPT_V3_unfiltered_cleaned_split.json --num-prompts 1 --trust-remote-code python benchmarks/benchmark_serving.py --model gemma-2-9b-it --dataset ShareGPT_V3_unfiltered_cleaned_split.json --num-prompts 1 --trust-remote-code
``` ```
参数同使用数据集,离线批量推理性能测试,具体参考[benchmarks/benchmark_serving.py](benchmarks/benchmark_serving.py) 参数同使用数据集,离线批量推理性能测试,具体参考[benchmarks/benchmark_serving.py](benchmarks/benchmark_serving.py)
...@@ -121,7 +123,7 @@ python benchmarks/benchmark_serving.py --model meta-llama/Llama-2-7b-chat-hf --d ...@@ -121,7 +123,7 @@ python benchmarks/benchmark_serving.py --model meta-llama/Llama-2-7b-chat-hf --d
### OpenAI兼容服务 ### OpenAI兼容服务
启动服务: 启动服务:
```bash ```bash
vllm serve meta-llama/Llama-2-7b-chat-hf --enforce-eager --dtype float16 --trust-remote-code vllm serve gemma-2-9b-it --enforce-eager --dtype float16 --trust-remote-code --chat-template examples/template_gemma-it.jinja
``` ```
这里serve之后为加载模型路径,`--dtype`为数据类型:float16,默认情况使用tokenizer中的预定义聊天模板,`--chat-template`可以添加新模板覆盖默认模板,`-q gptq`为使用gptq量化模型进行推理。 这里serve之后为加载模型路径,`--dtype`为数据类型:float16,默认情况使用tokenizer中的预定义聊天模板,`--chat-template`可以添加新模板覆盖默认模板,`-q gptq`为使用gptq量化模型进行推理。
...@@ -135,13 +137,12 @@ curl http://localhost:8000/v1/models ...@@ -135,13 +137,12 @@ curl http://localhost:8000/v1/models
curl http://localhost:8000/v1/completions \ curl http://localhost:8000/v1/completions \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d '{ -d '{
"model": "meta-llama/Llama-2-7b-hf", "model": "gemma-2-9b-it",
"prompt": "I believe the meaning of life is", "prompt": "I believe the meaning of life is",
"max_tokens": 7, "max_tokens": 20,
"temperature": 0 "temperature": 0
}' }'
``` ```
或者使用[examples/openai_completion_client.py](examples/openai_completion_client.py)
### OpenAI Chat API和vllm结合使用 ### OpenAI Chat API和vllm结合使用
...@@ -149,14 +150,13 @@ curl http://localhost:8000/v1/completions \ ...@@ -149,14 +150,13 @@ curl http://localhost:8000/v1/completions \
curl http://localhost:8000/v1/chat/completions \ curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d '{ -d '{
"model": "meta-llama/Llama-2-7b-chat-hf", "model": "gemma-2-9b-it",
"messages": [ "messages": [
{"role": "system", "content": "I believe the meaning of life is"}, {"role": "system", "content": "I believe the meaning of life is"},
{"role": "user", "content": "I believe the meaning of life is"} {"role": "user", "content": "I believe the meaning of life is"}
] ]
}' }'
``` ```
或者使用[examples/openai_chatcompletion_client.py](examples/openai_chatcompletion_client.py)
### **gradio和vllm结合使用** ### **gradio和vllm结合使用**
1.安装gradio 1.安装gradio
...@@ -170,7 +170,7 @@ pip install gradio ...@@ -170,7 +170,7 @@ pip install gradio
2.1 启动gradio服务,根据提示操作 2.1 启动gradio服务,根据提示操作
``` ```
python gradio_openai_chatbot_webserver.py --model "meta-llama/Llama-2-7b-chat-hf" --model-url http://localhost:8000/v1 --temp 0.8 --stop-token-ids "" python examples/gradio_openai_chatbot_webserver.py --model "gemma-2-9b-it" --model-url http://localhost:8000/v1 --temp 0.8 --stop-token-ids ""
``` ```
2.2 更改文件权限 2.2 更改文件权限
...@@ -189,13 +189,13 @@ ssh -L 8000:计算节点IP:8000 -L 8001:计算节点IP:8001 用户名@登录节 ...@@ -189,13 +189,13 @@ ssh -L 8000:计算节点IP:8000 -L 8001:计算节点IP:8001 用户名@登录节
3.启动OpenAI兼容服务 3.启动OpenAI兼容服务
``` ```
vllm serve meta-llama/Llama-2-7b-chat-hf --enforce-eager --dtype float16 --trust-remote-code --port 8000 --host "0.0.0.0" vllm serve gemma-2-9b-it --enforce-eager --dtype float16 --trust-remote-code --port 8000 --host "0.0.0.0"
``` ```
4.启动gradio服务 4.启动gradio服务
``` ```
python gradio_openai_chatbot_webserver.py --model "meta-llama/Llama-2-7b-chat-hf" --model-url http://localhost:8000/v1 --temp 0.8 --stop-token-ids --host "0.0.0.0" --port 8001" python examples/gradio_openai_chatbot_webserver.py --model "gemma-2-9b-it" --model-url http://localhost:8000/v1 --temp 0.8 --stop-token-ids --host "0.0.0.0" --port 8001
``` ```
5.使用对话服务 5.使用对话服务
...@@ -204,7 +204,7 @@ python gradio_openai_chatbot_webserver.py --model "meta-llama/Llama-2-7b-chat-h ...@@ -204,7 +204,7 @@ python gradio_openai_chatbot_webserver.py --model "meta-llama/Llama-2-7b-chat-h
## result ## result
使用的加速卡:1张 DCU-K100_AI-64G 使用的加速卡:1张 DCU-K100_AI-64G
``` ```
Prompt: 'I believe the meaning of life is', Generated text: ' to find purpose, happiness, and fulfillment. Here are some reasons why:\n\n1. Purpose: Having a sense of purpose gives life meaning and direction. It helps individuals set goals and work towards achieving them, which can lead to a sense of accomplishment and fulfillment.\n2. Happiness: Happiness is a fundamental aspect of life that brings joy and satisfaction. Prompt: 'I believe the meaning of life is', Generated text: ' to find your purpose and live it to the fullest.\n\nThis purpose can be anything that brings you '
``` ```
### 精度 ### 精度
...@@ -219,7 +219,8 @@ Prompt: 'I believe the meaning of life is', Generated text: ' to find purpose, h ...@@ -219,7 +219,8 @@ Prompt: 'I believe the meaning of life is', Generated text: ' to find purpose, h
金融,科研,教育 金融,科研,教育
## 源码仓库及问题反馈 ## 源码仓库及问题反馈
* [https://developer.hpccube.com/codes/modelzoo/llama_vllm](https://developer.hpccube.com/codes/modelzoo/llama_vllm) * [https://developer.hpccube.com/codes/modelzoo/gemma2_vllm](https://developer.hpccube.com/codes/modelzoo/gemma2_vllm)
## 参考资料 ## 参考资料
* [https://github.com/vllm-project/vllm](https://github.com/vllm-project/vllm) * [https://github.com/vllm-project/vllm](https://github.com/vllm-project/vllm)
* [https://github.com/chujiezheng/chat_templates/blob/main/chat_templates](https://github.com/chujiezheng/chat_templates/blob/main/chat_templates)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment