# LLaMA ## 论文 - [https://arxiv.org/pdf/2302.13971.pdf](https://arxiv.org/pdf/2302.13971.pdf) ## 模型结构 LLAMA网络基于 Transformer 架构。提出了各种改进,并用于不同的模型,例如 PaLM。以下是与原始架构的主要区别: 预归一化。为了提高训练稳定性,对每个transformer 子层的输入进行归一化,而不是对输出进行归一化。使用 RMSNorm 归一化函数。 SwiGLU 激活函数 [PaLM]。使用 SwiGLU 激活函数替换 ReLU 非线性以提高性能。使用 2 /3 4d 的维度而不是 PaLM 中的 4d。 旋转嵌入。移除了绝对位置嵌入,而是添加了旋转位置嵌入 (RoPE),在网络的每一层。 ![img](./docs/llama.png) ## 算法原理 LLama是一个基础语言模型的集合,参数范围从7B到65B。在数万亿的tokens上训练出的模型,并表明可以专门使用公开可用的数据集来训练最先进的模型,而不依赖于专有的和不可访问的数据集。 ![img](./docs/llama_1.png) ## 环境配置 提供光源拉取推理的docker镜像: ```bash docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10 # 用上面拉取docker镜像的ID替换 # 主机端路径 # 容器映射路径 docker run -it --network=host --name=llama_lmdeploy --privileged --device=/dev/kfd --device=/dev/dri --ipc=host --shm-size=1024G --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -u root --ulimit stack=-1:-1 --ulimit memlock=-1:-1 -v /opt/hyhal:/opt/hyhal:ro -v : /bin/bash #起容器之后安装软件依赖 pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 镜像版本依赖: * DTK驱动:dtk24.04.1 * Pytorch: 2.1.0 * python: 3.10 ## 数据集 无 ## 推理 ### 源码编译安装 ```bash # 若使用光源的镜像,可以跳过源码编译安装,镜像里面安装好了lmdeploy git clone http://developer.sourcefind.cn/codes/modelzoo/llama_lmdeploy.git cd llama_lmdeploy git submodule init && git submodule updat cd lmdeploy mkdir build && cd build sh ../generate.sh make -j 32 make install cd .. && python3 setup.py install ``` ### 模型下载 [LLama](https://huggingface.co/meta-llama) [LLama-7B](https://huggingface.co/meta-llama/Llama-2-7b-hf) [LLama-13B](https://huggingface.co/meta-llama/Llama-2-13b-hf) [LLama-33B](https://huggingface.co/pinkmanlove/llama-33b-hf) [LLama-65B](https://huggingface.co/Enoch/llama-65b-hf) [LLama2-7B](https://huggingface.co/meta-llama/Llama-2-7b-hf) [LLama2-13B](https://huggingface.co/meta-llama/Llama-2-13b) [LLama2-70B](https://huggingface.co/meta-llama/Llama-2-70b-hf) 支持模型包括:LLama-7B、LLama-13B、LLama-30B、LLama-65B、LLama2-7B、LLama2-13B、LLama2-70B > [!CAUTION] > > 最新lmdepoly推理llama1: > > 1.LLama-13B:需要在tokenizer_config.json中添加“unk_token”对应的值为"\“ > > 2.LLama-65B:config.json文件中“architectures”对应的[LlAmaForCausalLM]改成[LlamaForCausalLM] ### 运行 LLama-7b ```bash # 用于张量并行的GPU数量应该是2^n # bash界面运行 lmdeploy chat turbomind ./path_to_llama7b --tp 1 # 输入问题后执行2次回车进行推理 # 服务器网页端运行 在bash端运行: # gradio服务器的ip地址 # gradio服务器的ip的端口 # 用于张量并行的GPU数量应该是2^n (和模型转换的时候保持一致) lmdeploy serve gradio ./path_to_llama7b --server-name {ip} --server-port {port} --tp 1 在网页上输入{ip}:{port}即可进行对话 ``` ### 运行 LLama-13b ```bash # bash界面运行 lmdeploy chat turbomind ./path_to_llama13b --tp 1 # 服务器网页端运行 在bash端运行: lmdeploy serve gradio ./path_to_llama13b --server-name {ip} --server-port {port} --tp 1 在网页上输入{ip}:{port}即可进行对话 ``` ### 运行 LLama-33b ```bash # bash界面运行 lmdeploy chat turbomind ./path_to_llama33b --tp 4 # 服务器网页端运行 在bash端运行: lmdeploy serve gradio ./path_to_llama33b --server-name {ip} --server-port {port} --tp 4 在网页上输入{ip}:{port}即可进行对话 ``` ### 运行 LLama-65b ```bash # bash界面运行 lmdeploy chat turbomind ./path_to_llama65b --tp 8 # 服务器网页端运行 在bash端运行: lmdeploy serve gradio ./path_to_llama65b --server-name {ip} --server-port {port} --tp 8 在网页上输入{ip}:{port}即可进行对话 ``` ### 运行 LLama2-7b ```bash # bash界面运行 lmdeploy chat turbomind ./path_to_llama2-7b --tp 1 # 服务器网页端运行 在bash端运行: lmdeploy serve gradio ./path_to_llama2-7b --server-name {ip} --server-port {port} --tp 1 在网页上输入{ip}:{port}即可进行对话 ``` ### 运行 LLama2-13b ```bash # bash界面运行 lmdeploy chat turbomind ./path_to_llama2-13b --tp 1 # 服务器网页端运行 在bash端运行: lmdeploy serve gradio ./path_to_llama2-13b --server-name {ip} --server-port {port} --tp 1 在网页上输入{ip}:{port}即可进行对话 ``` ### 运行 LLama2-70b ```bash # bash界面运行 lmdeploy chat turbomind ./path_to_llama2-70b --tp 8 # 服务器网页端运行 在bash端运行: lmdeploy serve gradio ./path_to_llama2-70b --server-name {ip} --server-port {port} --tp 8 在网页上输入{ip}:{port}即可进行对话 ``` ## result ![llama](docs/llama.gif) ### 精度 无 ## 应用场景 ### 算法类别 `对话问答` ### 热点应用行业 `金融,科研,教育` ## 预训练权重 [LLama](https://huggingface.co/meta-llama) [LLama-7B](https://huggingface.co/meta-llama/Llama-2-7b-hf) [LLama-13B](https://huggingface.co/meta-llama/Llama-2-13b-hf) [LLama-33B](https://huggingface.co/pinkmanlove/llama-33b-hf) [LLama-65B](https://huggingface.co/Enoch/llama-65b-hf) [LLama2-7B](https://huggingface.co/meta-llama/Llama-2-7b-hf) [LLama2-13B](https://huggingface.co/meta-llama/Llama-2-13b) [LLama2-70B](https://huggingface.co/meta-llama/Llama-2-70b-hf) ## 源码仓库及问题反馈 https://developer.sourcefind.cn/codes/modelzoo/llama_lmdeploy ## 参考资料 https://github.com/InternLM/LMDeploy