【新智元导读】
如何无痛玩转Llama 3,这个手把手教程一看就会!80亿参数推理单卡半分钟速成,微调700亿参数仅用4卡近半小时训完,还有100元代金券免费薅。
就在昨天,许多网友纷纷表示,自己收到了OpenAI暂用API的邮件。
邮件中,OpenAI宣布自7月9日起,将不再对中国等不支持国家的用户提供API服务。
许多开发者听到之后,犹如晴天霹雳,在AI圈里可是吵翻了天。
这意味着,不仅是国内开发者,就连企业,都需要寻找新的解决方案。
现在看来,能与GPT-4分庭抗礼的Llama 3大模型,无疑是一个不错的选择。
Llama作为一个完全开源的模型,只要我们有机器,就相当于有了可以不限使用次数的大语言模型帮忙处理任务。
甚至,可以微调这个模型来实现适合自己需求的独特业务场景!
不过,在使用Llama 3之前,还需先在Meta这边填一个表格,并签一个长长的英文协议;然后会被指引到一个GitHub地址,并收到一个邮件链接去下载模型。
此外,如果想体验最大、效果最好的700亿参数模型,下载所需的时间也十分「令人酸爽」,尤其是在没有科学加速的情况下。
因为我们一直没有下好Llama 3的模型权重,
所以今天没法带大家体验了,此贴到此结束。
(不是)
开玩笑的,我们最近发现了一家宝藏公司,所有的下载和配置都已经提前帮我们完成了!
不但700亿参数需要耗费132GB存储的模型已经放在了公开数据中,而且还配置好了Llama 3的推理和训练微调环境。
换句话说就是,完全不用操心代码库的依赖安装问题,直接上手用就完事了。
潞晨云平台:https://cloud.luchentech.com/
话不多说,在完成潞晨云账户注册充钱等一系列流程后,我们就开了个机器试了一波。
Llama 3 80亿参数的模型
根据Llama 3官方网址用80亿参数的模型推理只需1卡,而700亿的模型需要8卡。
接下来,我们先来试试用单卡跑较小的80亿模型进行推理。
Llama 3推理:https://github.com/meta-Llama/Llama3#inference
选一块A800的卡,计费方式已经默认选择了更省钱的潮汐计费。
在公共镜像中找到Llama 3的推理镜像,然后选择1张显卡,勾上「允许SSH连接」,「存储持久化」,「挂载公开数据」,最后点击右下角的创建。
诚不欺我,两个Llama 3的权重都已经在公开数据盘中,无需下载。
先试一个简单的推理。模型权重的初始化十分快捷,大约半分钟。
似乎写了一半被截断了,应该是max_length给太短,我们调整到200,再来一次。
这次生成结果变长了不少,不过看起来Llama 3 80亿的模型能力还是一般,比如突然生成了「3. The United Kingdom」这个部分,前后文不是很严谨。
我们还注意到,在没有给prompt的情况下,模型两次都生成同样的开头。这引起了我们的好奇,于是我们决定从代码层面,看看到底是怎么一回事。
把ColossalAI的代码下载到了本地,看一下ColossalAI/examples/inference/llama/llama_generation.py这个文件:
ColossalAI代码地址:https://github.com/hpcaitech/ColossalAI
原来是之前跑这个脚本用了默认的prompt,现在,我们换成一个自定义的文本提示试试。
从结果来看,虽然可以成功继续生成,但是有点越扯越远了,除了前两句,后面并没有像我们期待的那样讲一个故事。
下面,让我们来体验一下Meta-Llama-3-8B-Instruct有对话能力的模型的效果。
首先,参考Llama 3官网中对话模型指令的格式。
Llama 3 Model Cards:https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/
因为在bash里面传这么多特殊符号的指令已经不太方便了,我们用了潞晨云的jupyter链接,在里面直接对ColossalAI/examples/inference/llama_generation.py进行了修改,让它可以读取一个指令的txt文件(如dialog.txt),对每一行进行生成并保存到txt文件中(如dialog_resp.txt)。
非常好,这次模型终于真的在讲故事了!感觉指令模型会比基础的模型更好用一些。
完成推理后,赶紧关闭潞晨云机器,毕竟是计时收费的,省钱要紧。
Llama 3 700亿参数的模型
在单卡上尝试过80亿参数模型的效果以后,我们再来试试更大的700亿参数模型。
毕竟,这可是一个和Gemini Pro 1.5打成平手的模型呢!
此时我们发现,其实Llama-Factory的代码既支持推理也支持微调,所以干脆直接拿这个镜像来试试好了。
Llama-Factory项目地址:https://github.com/hiyouga/LLaMA-Factory
和之前一样的流程启动云主机,这次选择Llama-Factory镜像,卡数选4即可。
教程地址:https://cloud.luchentech.com/doc/docs/image/llama-factory/
可以看到,效果拔群!由于完全不用修改代码提示,可以直接输入对话内容,使用体验感直接拉满。
模型写了一个名叫Aria的蓝鲸的故事,讲她在一次海底探险中遇到了海底秘密守护者。守护者告诉她她的歌声有特殊的力量。最终她成功联合了海底生物并打败了因人类污染而出现的黑暗力量。
这个故事中出现了一个名字「Abyssaria」,于是我们继续针对这个名字进行提问。
很快,模型就进一步对于这个海底城市进行了描述。不愧是700亿参数的模型,效果真的不赖。
针对微调,Llama-Factory已经给出了资源使用的预估。
那接下来,我们就来尝试一下先用Lora微调Llama 3的小模型。
Llama 3 80亿参数的模型
根据上图中的资源预估,我们用Lora训练80亿参数的模型仅需一卡,而700亿参数的模型4卡A800即可。
打开Jupyter链接转到机器后,一样是把模型路径改到了本地,节省时间免下载。
可以看到这边默认用两个数据集,「identify」和「alpaca_en_demo」进行微调。我们很快就找到了这两个数据集的位置,放在/root/LLaMA-Factory/data下。
这个格式是真的很简单了,想要用自己的数据来微调这个模型,只需要把训练数据整理成一样的格式,再上传到云机器即可。
# identity.json,
[
{
"instruction": "hi",
"input": "",
"output": "Hello! I am {{name}}, an AI assistant developed by {{author}}. How can I assist you today?"
},
...
]
# alpaca_en_demo.json
[
{
"instruction": "Describe a process of making crepes.",
"input": "",
"output": "Making crepes is an easy and delicious process! Here are step-by-step instructions on how to make them:\n\n1. Assemble your ingredients. For basic crepes, you'll need: 1 cup all-purpose flour, 2 eggs, 1/2 cup milk, 1/2 cup water, 1/4 teaspoon salt, and 2 tablespoons melted butter.\n\n2. Mix the batter: In a large mixing bowl, whisk together the flour and the eggs. Gradually add the milk and water, stirring constantly to ensure that there are no lumps. Add salt and melted butter, and mix well.\n\n3. Let the batter rest: If you can, let the batter sit for an hour or so. This will help the flour to absorb the liquid and make the crepes more tender.\n\n4. Heat your pan: Preheat a non-stick pan over medium heat. Lightly butter the pan or use cooking spray to prevent the crepes from sticking.\n\n5. Pour the batter: Using a ladle or a measuring cup, pour a small amount of batter (about 1/4 cup) onto the center of the pan. Immediately tilt the pan in a circular motion to spread the batter evenly and thinly over the bottom of the pan.\n\n6. Cook the crepe: Cook the crepe for 1-2 minutes until the bottom is lightly golden. Carefully loosen the edges with a spatula and flip the crepe over to cook the other side for another minute.\n\n7. Remove and repeat: Gently slide the crepe onto a plate, and then repeat the process with the remaining batter. Remember to re-butter the pan between each crepe if necessary.\n\n8. Fill and serve: Fill your cooked crepes with your desired filling, such as fresh fruit, whipped cream, Nutella, or ham and cheese. Roll or fold, and serve immediately. Enjoy!"