人人玩轉(zhuǎn)Llama 2!Meta正式官宣免費(fèi)用,微調(diào)羊駝指南大全集
今天,Llama 2宣布正式開源,免費(fèi)用于研究和商用。
下載地址:https://ai.meta.com/resources/models-and-libraries/llama-downloads/?utm_source=twitter&utm_medium=organic_social&utm_campaign=llama2&utm_cnotallow=card
發(fā)布不到一周的Llama 2,已經(jīng)在研究社區(qū)爆火,一系列性能評(píng)測(cè)、在線試用的demo紛紛出爐。
就連OpenAI聯(lián)合創(chuàng)始人Karpathy用C語(yǔ)言實(shí)現(xiàn)了對(duì)Llama 2嬰兒模型的推理。
既然Llama 2現(xiàn)已人人可用,那么如何去微調(diào)實(shí)現(xiàn)更多可能的應(yīng)用呢?
Llama 2微調(diào)指南
這兩天,來自Brev的創(chuàng)始工程師Sam L'Huillier,就做了一個(gè)簡(jiǎn)易版的Llama 2微調(diào)指南。
甚至還一度沖進(jìn)了Hacker News榜單的前五。
圖片
為了制作這個(gè)「對(duì)話摘要生成器」,作者利用samsum對(duì)話摘要數(shù)據(jù)集對(duì)Llama 2進(jìn)行了微調(diào)。
記得準(zhǔn)備一個(gè)A10、A10G、A100(或其他顯存大于24GB的GPU)。如果沒有的話,也可以選擇線上的開發(fā)環(huán)境。
圖片
數(shù)據(jù)集地址:https://huggingface.co/datasets/samsum
1. 下載模型
克隆Meta的Llama推理存儲(chǔ)庫(kù)(包含下載腳本):
git clone https://github.com/facebookresearch/llama.git
然后運(yùn)行下載腳本:
bash download.sh
在這里,你只需要下載7B模型就可以了。
2. 將模型轉(zhuǎn)換為Hugging Face支持的格式
pip install git+https://github.com/huggingface/transformerscd transformerspython convert_llama_weights_to_hf.py \ --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir models_hf/7B
現(xiàn)在,我們得到了一個(gè)Hugging Face模型,可以利用Hugging Face庫(kù)進(jìn)行微調(diào)了!
3. 運(yùn)行微調(diào)筆記本:
克隆Llama-recipies存儲(chǔ)庫(kù):
git clone https://github.com/facebookresearch/llama-recipes.git
然后,在你喜歡的notebook界面中打開quickstart.ipynb文件,并運(yùn)行整個(gè)notebook。
(此處,作者使用的是Jupyter lab):
pip install
jupyterlabjupyter lab # in the repo you want to work in
為了適應(yīng)轉(zhuǎn)換后的實(shí)際模型路徑,確保將以下一行更改為:
model_id="./models_hf/7B"
最后,一個(gè)經(jīng)過Lora微調(diào)的模型就完成了。
4. 在微調(diào)的模型上進(jìn)行推理
當(dāng)前,問題在于Hugging Face只保存了適配器權(quán)重,而不是完整的模型。所以我們需要將適配器權(quán)重加載到完整的模型中。
導(dǎo)入庫(kù):
import torch
from transformers import LlamaForCausalLM, LlamaTokenizer
from peft import PeftModel, PeftConfig
加載分詞器和模型:
model_id="./models_hf/7B"
tokenizer = LlamaTokenizer.from_pretrained(model_id)
model =LlamaForCausalLM.from_pretrained(model_id, load_in_8bit=True, device_map='auto', torch_dtype=torch.float16)
從訓(xùn)練后保存的位置加載適配器:
model = PeftModel.from_pretrained(model, "/root/llama-recipes/samsungsumarizercheckpoint")
運(yùn)行推理:
eval_prompt = """
Summarize this dialog:
A: Hi Tom, are you busy tomorrow’s afternoon?
B: I’m pretty sure I am. What’s up?
A: Can you go with me to the animal shelter?.
B: What do you want to do?
A: I want to get a puppy for my son.
B: That will make him so happy.
A: Yeah, we’ve discussed it many times. I think he’s ready now.
B: That’s good. Raising a dog is a tough issue. Like having a baby ;-)
A: I'll get him one of those little dogs.
B: One that won't grow up too big;-)A: And eat too much;-))
B: Do you know which one he would like?
A: Oh, yes, I took him there last Monday. He showed me one that he really liked.
B: I bet you had to drag him away.
A: He wanted to take it home right away ;-).
B: I wonder what he'll name it.
A: He said he’d name it after his dead hamster – Lemmy - he's a great Motorhead fan :-)))
---
Summary:
"""
model_input = tokenizer(eval_prompt, return_tensors="pt").to("cuda")
model.eval()
with torch.no_grad():
print(tokenizer.decode(model.generate(**model_input, max_new_tokens=100)[0], skip_special_tokens=True))
LLM Engine微調(diào)更便捷
如果你想用自己的數(shù)據(jù)對(duì)Llama 2微調(diào),該如何做?
創(chuàng)辦Scale AI初創(chuàng)公司的華人CEO Alexandr Wang表示,自家公司開源的LLM Engine,能夠用最簡(jiǎn)單方法微調(diào)Llama 2。
圖片
Scale AI的團(tuán)隊(duì)在一篇博文中,具體介紹了Llama 2的微調(diào)方法。
from llmengine import FineTune
response = FineTune.create(
model="llama-2-7b",
training_file="s3://my-bucket/path/to/training-file.csv",
)
print(response.json())
數(shù)據(jù)集
在如下示例中,Scale使用了Science QA數(shù)據(jù)集。
這是一個(gè)由多項(xiàng)選擇題組成的流行數(shù)據(jù)集,每個(gè)問題可能有文本上下文和圖像上下文,并包含支持解決方案的詳盡解釋和講解。
Science QA的示例
目前,LLM Engine支持對(duì)「提示完成對(duì)」進(jìn)行微調(diào)。首先,需要將Science QA數(shù)據(jù)集轉(zhuǎn)換為支持的格式,一個(gè)包含兩列的CSV:prompt和response 。
在開始之前,請(qǐng)安裝所需的依賴項(xiàng)。
pip install datasets==2.13.1 smart_open[s3]==5.2.1 pandas==1.4.4
可以從Hugging Face加載數(shù)據(jù)集,并觀察數(shù)據(jù)集的特征。
from datasets import load_dataset
from smart_open import smart_open
import pandas as pd
dataset = load_dataset('derek-thomas/ScienceQA')
dataset['train'].features
提供Science QA示例的常用格式是:
Context: A baby wants to know what is inside of a cabinet. Her hand applies a force to the door, and the door opens.
Question: Which type of force from the baby's hand opens the cabinet door?
Options: (A) pull (B) push
Answer: A.
由于Hugging Face數(shù)據(jù)集中options的格式是「可能答案的列表」,需要通過添加枚舉前綴,將此列表轉(zhuǎn)換為上面的示例格式。
choice_prefixes = [chr(ord('A') + i) for i in range(26)] # A-Z
def format_options(options, choice_prefixes):
return ' '.join([f'({c}) {o}' for c, o in zip(choice_prefixes, options)])
現(xiàn)在,編寫格式化函數(shù),將這個(gè)數(shù)據(jù)集中的單個(gè)樣本轉(zhuǎn)換為輸入模型的prompt和response 。
def format_prompt(r, choice_prefixes):
options = format_options(r['choices'], choice_prefixes)
return f'''Context: {r["hint"]}\nQuestion: {r["question"]}\nOptions:{options}\nAnswer:'''
def format_response(r, choice_prefixes):
return choice_prefixes[r['answer']]
最后,構(gòu)建數(shù)據(jù)集。
請(qǐng)注意,Science QA中的某些示例只有上下文圖像。(如下演示中會(huì)跳過這些示例,因?yàn)長(zhǎng)lama-2純粹是一種語(yǔ)言模型,并且不能接受圖像輸入。)
def convert_dataset(ds):
prompts = [format_prompt(i, choice_prefixes) for i in ds if i['hint'] != '']
labels = [format_response(i, choice_prefixes) for i in ds if i['hint'] != '']
df = pd.DataFrame.from_dict({'prompt': prompts, 'response': labels})
return df
LLM Engine支持使用「預(yù)訓(xùn)練和驗(yàn)證數(shù)據(jù)集」來進(jìn)行訓(xùn)練。假如你只提供訓(xùn)練集,LLM Engine會(huì)從數(shù)據(jù)集中隨機(jī)拆分10%內(nèi)容進(jìn)行驗(yàn)證。
因?yàn)椴鸱謹(jǐn)?shù)據(jù)集可以防止模型過度擬合訓(xùn)練數(shù)據(jù),不會(huì)導(dǎo)致在推理期間實(shí)時(shí)數(shù)據(jù)泛化效果不佳。
另外,這些數(shù)據(jù)集文件必須存儲(chǔ)在可公開訪問的URL中,以便LLM Engine可以讀取。對(duì)于此示例,Scale將數(shù)據(jù)集保存到s3。
并且,還在Github Gist中公開了預(yù)處理訓(xùn)練數(shù)據(jù)集和驗(yàn)證數(shù)據(jù)集。你可以直接用這些鏈接替換train_url和val_url 。
train_url = 's3://...'
val_url = 's3://...'
df_train = convert_dataset(dataset['train'])
with smart_open(train_url, 'wb') as f:
df_train.to_csv(f)
df_val = convert_dataset(dataset['validation'])
with smart_open(val_url, 'wb') as f:
df_val.to_csv(f)
現(xiàn)在,可以通過LLM Engine API開始微調(diào)。
微調(diào)
首先,需要安裝LLM Engine。
pip install scale-llm-engine
接下來,你需要設(shè)置Scale API密鑰。按照README的說明獲你唯一的API密鑰。
高級(jí)用戶還可以按照自托管LLM Engine指南進(jìn)行操作,由此就不需要Scale API密鑰。
import os
os.environ['SCALE_API_KEY'] = 'xxx'
一旦你設(shè)置好一切,微調(diào)模型只需要一個(gè)API的調(diào)用。
在此,Scale選擇了Llama-2的70億參數(shù)版本,因?yàn)樗鼘?duì)大多數(shù)用例來說已經(jīng)足夠強(qiáng)大了。
from llmengine import FineTune
response = FineTune.create(
model="llama-2-7b",
training_file=train_url,
validation_file=val_url,
hyperparameters={
'lr':2e-4,
},
suffix='science-qa-llama'
)
run_id = response.fine_tune_id
通過run_id ,你可以監(jiān)控工作狀態(tài),并獲取每個(gè)epoch的實(shí)時(shí)更新指標(biāo),比如訓(xùn)練和驗(yàn)證損失。
Science QA是一個(gè)大型數(shù)據(jù)集,因此訓(xùn)練可能需要一兩個(gè)小時(shí)才能完成。
while True:
job_status = FineTune.get(run_id).status
# Returns one of `PENDING`, `STARTED`, `SUCCESS`, `RUNNING`,
# `FAILURE`, `CANCELLED`, `UNDEFINED` or `TIMEOUT`
print(job_status)
if job_status == 'SUCCESS':
break
time.sleep(60)
#Logs for completed or running jobs can be fetched with
logs = FineTune.get_events(run_id)
推理與評(píng)估
完成微調(diào)后,你可以開始對(duì)任何輸入生成響應(yīng)。但是,在此之前,確保模型存在,并準(zhǔn)備好接受輸入。
ft_model = FineTune.get(run_id).fine_tuned_model
不過,你的第一個(gè)推理結(jié)果可能需要幾分鐘才能輸出。之后,推理過程就會(huì)加快。
一起評(píng)估下在Science QA上微調(diào)的Llama-2模型的性能。
import pandas as pd
#Helper a function to get outputs for fine-tuned model with retries
def get_output(prompt: str, num_retry: int = 5):
for _ in range(num_retry):
try:
response = Completion.create(
model=ft_model,
prompt=prompt,
max_new_tokens=1,
temperature=0.01
)
return response.output.text.strip()
except Exception as e:
print(e)
return ""
#Read the test data
test = pd.read_csv(val_url)
test["prediction"] = test["prompt"].apply(get_output)
print(f"Accuracy: {(test['response'] == test['prediction']).mean() * 100:.2f}%")
微調(diào)后的Llama-2能夠達(dá)到82.15%的準(zhǔn)確率,已經(jīng)相當(dāng)不錯(cuò)了。
那么,這個(gè)結(jié)果與Llama-2基礎(chǔ)模型相比如何?
由于預(yù)訓(xùn)練模型沒有在這些數(shù)據(jù)集上進(jìn)行微調(diào),因此需要在提示中提供一個(gè)示例,以便模型學(xué)會(huì)遵從我們期望的回復(fù)格式。
另外,我們還可以看到與微調(diào)類似大小的模型MPT-7B相比的情況。
圖片
在Science QA上微調(diào)Llama-2,其性能增益有26.59%的絕對(duì)差異!
此外,由于提示長(zhǎng)度較短,使用微調(diào)模型進(jìn)行推理比使用少樣本提示更便宜。這種微調(diào)Llama-27B模型也優(yōu)于1750億參數(shù)模型GPT-3.5。
可以看到,Llama-2模型在微調(diào)和少樣本提示設(shè)置中表現(xiàn)都優(yōu)于MPT,充分展示了它作為基礎(chǔ)模型和可微調(diào)模型的優(yōu)勢(shì)。
此外,Scale還使用LLM Engine微調(diào)和評(píng)估LLAMA-2在GLUE(一組常用的NLP基準(zhǔn)數(shù)據(jù)集)的幾個(gè)任務(wù)上的性能。
圖片
現(xiàn)在,任何人都可以釋放微調(diào)模型的真正潛力,并見證強(qiáng)大的AI生成回復(fù)的魔力。
我發(fā)現(xiàn)雖然Huggingface在transformers方面構(gòu)建了一個(gè)出色的庫(kù),但他們的指南對(duì)于普通用戶來說往往過于復(fù)雜。