# LLAMA ## 论文 `LLaMA: Open and Efficient Foundation Language Models` - [https://arxiv.org/abs/2302.13971](https://arxiv.org/abs/2302.13971) ## 模型结构 LLaMA,这是一个基础语言模型的集合,参数范围从7B到65B。在数万亿的tokens上训练出的模型,并表明可以专门使用公开可用的数据集来训练最先进的模型,而不依赖于专有的和不可访问的数据集。特别是,llama 13B在大多数基准测试中优于GPT-3 (175B), LLaMA 65B与最好的模型Chinchilla-70B和PaLM-540B具有竞争力。LLAMA网络基于 Transformer 架构。提出了各种改进,并用于不同的模型,例如 PaLM。 llama模型结构.png 以下是llama-13B的主要网络参数配置: ``` "hidden_act": "silu", "hidden_size": 5120, "intermediate_size": 13824, "initializer_range": 0.02, "max_sequence_length": 2048, "model_type": "llama", "num_attention_heads": 40, "num_hidden_layers": 40, "rms_norm_eps": 1e-06, "torch_dtype": "float16", "vocab_size": 32000 ``` ## 算法原理 llama算法原理.png 以下是与原始 Transformer 架构的主要区别: **预归一化**。为了提高训练稳定性,对每个transformer 子层的输入进行归一化,而不是对输出进行归一化。使用 RMSNorm 归一化函数。 **SwiGLU 激活函数 [PaLM]**。使用 SwiGLU 激活函数替换 ReLU 非线性以提高性能。使用 2 /3 4d 的维度而不是 PaLM 中的 4d。 **旋转嵌入**。移除了绝对位置嵌入,而是添加了旋转位置嵌入 (RoPE),在网络的每一层。 ## 数据集 我们在Fastchat目录下集成了英文对话数据集供用户快速验证: $ tree ./FastChat-main/playground/data ── alpaca-data-conversation.json ## 环境配置 由于多节点环境配置差异较大,因此可按照节点环境修改env.sh,环境变量参考dtk-22.10,python3.8环境正常,网口正常。使用2个8卡Z00L裸金属节点,要求dtk环境正常,mpirun文件夹下包含预编译好的openmpi库mpi4.tar.gz,可直接使用。关于本项目DCU显卡所需torch库等均可从[光合](https://developer.hpccube.com/tool/)开发者社区下载安装: ``` cp -r mpirun/* ./ 根据当前系统更改env.sh中相关路径 cd FastChat-main pip3 install -e . cd ../transformers-main pip3 install -e . cd .. pip3 install torch-1.10.0a0+git2040069.dtk2210-cp38-cp38-manylinux2014_x86_64.whl pip3 install deepspeed-0.6.3+1b2721a.dtk2210-cp38-cp38-manylinux2014_x86_64.whl pip3 install apex-0.1+gitdb7007a.dtk2210-cp38-cp38-manylinux2014_x86_64.whl(可选) pip3 uninstall wandb ``` ## 训练 权重链接 13B:[decapoda-research/llama-13b-hf · Hugging Face](https://huggingface.co/srikanthmalla/decapoda-research-llama-13b-hf) 7B:[decapoda-research/llama-7b-hf · Hugging Face](https://huggingface.co/srikanthmalla/decapoda-research-llama-7b-hf) 该训练脚本需要2节点,每节点8张DCU-Z100L-32G。按需更改mpi_single.sh中模型权重所在路径。 并行配置采用zero3,使用fp16精度微调,如果想使能apex adamw_apex_fused优化器,更改./FastChat-main/fastchat/train/train.py:55行优化器改成adamw_apex_fused。deepspeed config.json如下: ``` { "train_micro_batch_size_per_gpu": 4, "gradient_accumulation_steps":16, "zero_allow_untested_optimizer": true, "fp16": { "enabled": "auto", "loss_scale": 0, "initial_scale_power": 16, "loss_scale_window": 1000, "hysteresis": 2, "min_loss_scale": 1 }, "zero_optimization": { "stage": 3, "cpu_offload": false, "allgather_partitions": true, "allgather_bucket_size": 5e8, "overlap_comm": false, "reduce_scatter": true, "reduce_bucket_size": 5e8, "contiguous_gradients" : true } } ``` 进入节点1,根据环境修改hostfile,保证两节点文件路径一致,配置相同,按需修改mpi_job.sh中--mca btl_tcp_if_include enp97s0f1,enp97s0f1改为ip a命令后对应节点ip的网卡名,numa可以根据当前节点拓扑更改绑定,微调命令: ``` bash mpi_job.sh ``` 如果单节点运行7B的模型出现oom,可以适当减少batch size。 ## result ### input ```plaintext >>>冬天,中国哪座城市最适合避寒?问题描述:能推荐一些国内适合冬天避寒的城市吗?回答用户:旅游爱好者 ``` ### output ```plaintext >>>回答:避寒,当然是去海南呀!海南的冬天,阳光明媚,温度适宜,而且空气清新,没有雾霾,没有沙尘暴,没有雾霾,没有雾霾! ``` ### 精度 训练数据:[./FastChat-main/playground/data/alpaca-data-conversation.json](链接) 使用的GPGPU:16张DCU-Z100L-32G。 模型精度(max_sequence_length: 2048): | 卡数 | 分布式工具 | 收敛性 | | :------: | :------: |:------: | | 16 | deepspeed | total_loss: 0.62/150 steps | ## 应用场景 ### 算法类别 `对话问答` ### 热点应用行业 `医疗,教育,科研,金融` ## 源码仓库及问题反馈 - https://developer.hpccube.com/codes/modelzoo/llama_fastchat_pytorch ## 参考 * https://huggingface.co/decapoda-research/llama-13b-hf * https://github.com/lm-sys/FastChat