大模型那么火,教你一键Modelarts玩转开源LlaMA(羊驼)大模型

本文分享自华为云社区《大模型那么火,教你一键Modelarts玩转开源LlaMA(羊驼)大模型》,作者:码上开花_Lancer 。

近日, LlaMA(羊驼)这个大模型再次冲上热搜!

LLaMA(Large Language Model Meta AI),由 Meta AI 发布的一个开放且高效的大型基础语言模型,共有 7B、13B、33B、65B(650 亿)四种版本。其数据集来源都是公开数据集,无任何定制数据集,保证了其工作与开源兼容和可复现,整个训练数据集在 token 化之后大约包含 1.4T 的 token。关于模型性能,LLaMA 的性能非常优异:具有 130 亿参数的 LLaMA 模型「在大多数基准上」可以胜过 GPT-3( 参数量达 1750 亿),而且可以在单块 V100 GPU 上运行;而最大的 650 亿参数的 LLaMA 模型可以媲美谷歌的 Chinchilla-70B 和 PaLM-540B。

上篇文章有介绍了LLaMA 所采用的Transformer 结构和细节,与之前所介绍的Transformer架构不同的地方包括采用了前置层归一化(Pre-normalization)并使用RMSNorm 归一化函数(Normalizing Function)、激活函数更换为SwiGLU,并使用了旋转位置嵌入(RoP),整体Transformer架构与GPT-2 类似,如图1.1所示。

图1.1 GPT-2 模型结构

关于训练集,其来源都是公开数据集,无任何定制数据集,保证了其工作与开源兼容和可复现。整个训练数据集在 token 化之后大约包含 1.4T 的 token。其中,LLaMA-65B 和 LLaMA-33B 是在 1.4万亿个 token 上训练的,而最小的模型 LLaMA-7B 是在 1万亿个 token 上训练的。LLaMA 优势在于其只使用公开可用的数据,这可以保证论文的工作与开源兼容和可复现。之前的大模型要么使用了不公开的数据集去训练从而达到了 state-of-the-art,如 Chinchilla、PaLM 或 GPT-3;要么使用了公开数据集,但模型效果不是最佳无法和 PaLM-62B 或 Chinchilla 相竞争,如 OPT、GPT-NeoX、BLOOM 和 GLM。

和 GPT 系列一样,LLaMA 模型也是 Decoder-only 架构,但结合前人的工作做了一些改进,比如:

  • Pre-normalization GPT3. 为了提高训练稳定性,LLaMA 对每个 transformer 子层的输入进行归一化,使用 RMSNorm 归一化函数,Pre-normalization 由Zhang和Sennrich(2019)引入。
  • SwiGLU 激活函数 PaLM. 将 ReLU 非线性替换为 SwiGLU 激活函数,且使用2/3*4D而不是 PaLM 论文中的 4d,SwiGLU 由 Shazeer(2020)引入以提高性能。
  • Rotary Embeddings GPTNeo. 模型的输入不再使用 positional embeddings,而是在网络的每一层添加了 positional embeddings (RoPE),RoPE 方法由Su等人(2021)引入。

不同模型的超参数详细信息在表2中给出,具体可以去看看我上篇文章,

具体怎么在华为云的ModelArts上玩转LLAMA开源大模型呢?

前期准备:

1.登录华为云官方账号

点击右上角"控制台",搜索栏输入"ModelArts"

点击"AI Gallery",选择"北京四"区域,

点击"资产集市--Notebook",输入"Mindformers应用之LLaMA_7B推理应用"

点击"Run in ModelArts",进入,

1. 安装MindFormers开发套件

复制代码
%cd /home/ma-user/work
!git clone -b r0.6 https://gitee.com/mindspore/mindformers.git
Cloning into 'mindformers'...
 
remote: Enumerating objects: 21732, done.
 
remote: Counting objects: 100% (437/437), done.
 
remote: Compressing objects: 100% (330/330), done.
 
remote: Total 21732 (delta 262), reused 190 (delta 107), pack-reused 21295
 
Receiving objects: 100% (21732/21732), 37.74 MiB | 3.73 MiB/s, done.

编译代码

复制代码
%cd mindformers

!bash build.sh

%cd ..

/home/ma-user/work/mindformers

---------------- MindFormers: build start ----------------

running bdist_wheel

running build

running build_py

creating build/lib/mindformers

copying mindformers/__init__.py -> build/lib/mindformers

copying mindformers/auto_class.py -> build/lib/mindformers

copying mindformers/mindformer_book.py -> build/lib/mindformers

creating build/lib/mindformers/core

copying mindformers/core/__init__.py -> build/lib/mindformers/core

copying mindformers/core/clip_grad.py -> build/lib/mindformers/core

copying mindformers/core/parallel_config.py -> build/lib/mindformers/core

creating build/lib/mindformers/dataset

........

2.下载LLaMA模型和tokenizer

复制代码
%cd /home/ma-user/work/mindformers

import moxing as mox

mox.file.copy_parallel('obs://modelarts-labs-bj4-v2/case_zoo/Mindfomer_LLaMA/', 'checkpoint_download/llama')

3.推理-使用pipeline接口开启快速推理

复制代码
from mindformers.pipeline import pipeline

pipeline_task = pipeline("text_generation", model='llama_7b', max_length=20)

pipeline_result = pipeline_task("I love Beijing, because", top_k=3)

print(pipeline_result)
  • 当我输入提示词:
复制代码
text_generation_text': I love Beijing, because

通过LLaMA_7B模型推理可以快速输出:

复制代码
['I love Beijing, because it is a city that is constantly changing.\nI love the city']

赶紧来点击试一试,体验下自己写代码调用LLAMA_7B开源大模型的魅力吧!!

点击关注,第一时间了解华为云新鲜技术~

相关推荐
Soari20 小时前
llama.cpp更新(b9553):LLM inference in C/C++,本地和云端实现高性能大模型推理
c语言·c++·llama
一叶知秋dong1 天前
llama.cpp 启动脚本
linux·服务器·llama
若苗瞬2 天前
继续提速:Llama.cpp 已经正式支持 Gemma4 MTP
google·llama·gemma·qat·mtp
cv魔法师3 天前
Linux构建编译llama.cpp
llama
Fzuim4 天前
Codex + llama.cpp + Qwen3.6-35B:零成本的本地 AI 编程方案,我把整套流程跑通了
人工智能·llama
元拓数智4 天前
跨库NL2SQL可信落地的核心:用IntaLink破解数据关系“迷雾”
数据库·人工智能·ai·nlp·agent·llama
硅谷茶馆5 天前
Codex+本地Qwen3.5无审查实用案例分享及llama对接踩坑。
llama
Soari5 天前
GitHub 开源项目解析:rk‑llama.cpp —— 基于 llama.cpp 的 Rockchip NPU 加速本地推理引擎
开源·github·llama·llm 推理·npu 本地模型推理·加速 c/c++ 开源项目
王天天(Bennet)5 天前
【从第一性原理来深入理解Transformer-更适合入门的理解(llama-3B模型为例)】
深度学习·transformer·llama
zhiSiBuYu05177 天前
llama.cpp 本地大模型部署与调用实战
llama