論文題目:《LLaMA: Open and Efficient Foundation Language Models》
論文鏈接:https://arxiv.org/pdf/2302.13971.pdf
github鏈接:https://github.com/facebookresearch/llama/tree/main
huggingface鏈接:https://huggingface.co/decapoda-research/llama-7b-hf
1 模型簡介
LLaMA 是 Meta AI 發(fā)布的包含 7B、13B、33B 和 65B 四種參數(shù)規(guī)模的基礎(chǔ)語言模型集合,LLaMA-13B 僅以 1/10 規(guī)模的參數(shù)在多數(shù)的 benchmarks 上性能優(yōu)于 GPT-3(175B),LLaMA-65B 與業(yè)內(nèi)最好的模型 Chinchilla-70B 和 PaLM-540B 比較也具有競爭力。
主要貢獻(xiàn):
- 開源一系列語言模型,可以與SOTA模型競爭
- LLaMA-13B比GPT-3的性能更好,但是模型大小卻是十分之一
- LLaMA-65B與Chinchilla-70B和PaLM-540B的實力相當(dāng)
- 使用公開數(shù)據(jù)集即可部分復(fù)現(xiàn)最先進(jìn)的性能(86%左右的效果)
2 研究背景
在給定預(yù)算的條件下,最好的模型并不一定是最大的模型,在更多的數(shù)據(jù)上訓(xùn)練的較小的模型反而會達(dá)到更好的性能。Hoffmann工作的目的是決定如何確定數(shù)據(jù)集和模型大小的規(guī)模,但是他忽略了推理的成本。所以在這篇文章中,給定一個目標(biāo)的性能等級,更推薦的模型不是最快訓(xùn)練的,但是是最快推理的。產(chǎn)生的模型稱為LLaMA,參數(shù)范圍從7B到65B,與現(xiàn)在最好的LLM相當(dāng)。
LLaMA-13B比GPT-3在大多數(shù)benchmarks上性能都要好,但是模型大小縮減到十分之一。Meta團(tuán)隊相信這個模型有助于LLM的使用和研究的大眾化,因為可以在單個GPU上運行。在更高的規(guī)模量上,65B參數(shù)量模型與當(dāng)前最好的LLM(比如Chinchila或PaLM-540B)相比更具有競爭力。LLaMA的另一個優(yōu)勢是它是使用公開數(shù)據(jù)集進(jìn)行訓(xùn)練。
3 訓(xùn)練方法
這項工作的訓(xùn)練方法相似于Brown的工作,并且受到Hoffmann(Chinchilla scaling laws)的啟發(fā)。模型使用標(biāo)準(zhǔn)優(yōu)化器進(jìn)行優(yōu)化。后面會單獨解讀下《 Scaling Laws for Neural Language Models》,該文主要建模了模型性能與非embedding參數(shù) N,數(shù)據(jù)集大小 D 與計算量 C之間的關(guān)系。最主要的發(fā)現(xiàn):
- 性能主要與模型大小相關(guān),而與模型結(jié)構(gòu)弱相關(guān)
- 性能與上面三個因素有比較貼合的power-law關(guān)系
從實驗來看,模型越大越好,小模型確實達(dá)不到大模型大力出奇跡的效果,而模型結(jié)構(gòu)也并沒有那么重要(雖然有很多工作是在改進(jìn)模型結(jié)構(gòu)本身)。結(jié)論部分更強調(diào)了大模型比大數(shù)據(jù)更重要
3.1 預(yù)訓(xùn)練數(shù)據(jù)
我們的訓(xùn)練數(shù)據(jù)集是多個來源的混合,如表 1 所示,涵蓋了不同的領(lǐng)域。 在大多數(shù)情況下,我們重復(fù)使用已用于培訓(xùn)其他 LLM 的數(shù)據(jù)源,但僅限于使用公開可用且與開源兼容的數(shù)據(jù)。 這導(dǎo)致以下混合數(shù)據(jù)及其在訓(xùn)練集中所占的百分比:
3.2 模型結(jié)構(gòu)
整體架構(gòu)仍然是Transformer的解碼器模塊,該模塊參考論文Attention is all you need。下面是在Transformer架構(gòu)上的進(jìn)一步的3個改進(jìn)。
- [GPT3] 使用RMSNorm(即Root Mean square Layer Normalization)對輸入數(shù)據(jù)進(jìn)行標(biāo)準(zhǔn)化,RMSNorm可以參考論文:Root mean square layer normalization。
a ˉ i = a i RMS ( a ) g i , where??RMS ( a ) = 1 n ∑ i = 1 n a i 2 . \begin{align} \begin{split} & \bar{a}i = \frac{a_i}{\text{RMS}(\mathbf{a})} g_i, \quad \text{where}~~ \text{RMS}(\mathbf{a}) = \sqrt{\frac{1}{n} \sum{i=1}^{n} a_i^2}. \end{split}\nonumber \end{align} ?aˉi=RMS(a)ai??gi?,where??RMS(a)=n1?∑i=1nai2??.??
為了提高訓(xùn)練的穩(wěn)定性,在每個transformer子層的input處進(jìn)行正則化,而不是在output處,使用的正則化方法是RMSNorm。
LLaMA源碼中實現(xiàn)方式為:
class RMSNorm(torch.nn.Module):
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def _norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x):
output = self._norm(x.float()).type_as(x)
return output * self.weight
- [PaLM]使用激活函數(shù)SwiGLU, 該函數(shù)可以參考PALM論文:Glu variants improve transformer。
class FeedForward(nn.Module):
def __init__(
self,
dim: int,
hidden_dim: int,
multiple_of: int,
):
super().__init__()
hidden_dim = int(2 * hidden_dim / 3)
hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)
self.w1 = ColumnParallelLinear(
dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x
)
self.w2 = RowParallelLinear(
hidden_dim, dim, bias=False, input_is_parallel=True, init_method=lambda x: x
)
self.w3 = ColumnParallelLinear(
dim, hidden_dim, bias=False, gather_output=False, init_method=lambda x: x
)
def forward(self, x):
return self.w2(F.silu(self.w1(x)) * self.w3(x))
- [GPTNeo]使用Rotary Embeddings進(jìn)行位置編碼,該編碼可以參考論文 Roformer: Enhanced transformer with rotary position embedding。
3.3 優(yōu)化器
使用了AdamW優(yōu)化器,并使用cosine learning rate schedule,使得最終學(xué)習(xí)率等于最大學(xué)習(xí)率的10%,設(shè)置0.1的權(quán)重衰減和1.0的梯度裁剪。warmup的step為2000,并根據(jù)模型的大小改變學(xué)習(xí)率和批處理大小(詳見表2)。
3.4 高效實現(xiàn)
- 作者做了一些優(yōu)化來提高模型的訓(xùn)練速度。首先,使用因果多頭注意的有效實現(xiàn)來減少內(nèi)存使用和運行時間。該實現(xiàn)可在xformers庫中獲得。
https://github.com/facebookresearch/xformers
- 為了進(jìn)一步提高訓(xùn)練效率,通過檢查點減少了在向后傳遞過程中重新計算的激活量。更準(zhǔn)確地說,節(jié)省了計算成本高的激活,比如線性層的輸出。這是通過手動實現(xiàn)transformer層的backward函數(shù)來實現(xiàn)的,而不是依賴于PyTorch的autograd。
這個指的是gradient checkpointing,這個策略是用時間(重新計算這些值兩次的時間成本)來換空間(提前存儲這些值的內(nèi)存成本)。
-
此外,還盡可能地覆蓋激活的計算和gpu之間通過網(wǎng)絡(luò)的通信(由于all_reduce操作)。
-
訓(xùn)練65b參數(shù)模型時,代碼在2048 A100 GPU和80GB RAM上處理大約380個token/秒/GPU。這意味著在包含1.4T token的數(shù)據(jù)集上進(jìn)行訓(xùn)練大約需要21天。
4 實驗結(jié)果
作者主要對比了在Zero-shot、Few-shot上的結(jié)果。
4.1 常識推理(Common Sense Reasoning)
可以觀察到13B和GPT-3 175B的結(jié)果實際上是非常相近的。
4.2 閉卷問答(Closed-book QA)
LLaMA模型要優(yōu)于PaLM 540B模型
4.3 閱讀理解(Reading Comprehension)
可以看到LLaMA對標(biāo)到540B的PaLM。
4.4 數(shù)學(xué)推理(Mathematical reasoning)
4.5 代碼生成(Code generation)
4.6 大規(guī)模多任務(wù)語言理解(Massive Multitask Language Understanding)
可以觀察到LLaMA-65B在大多數(shù)領(lǐng)域平均落后于Chinchilla70B和PaLM-540B幾個百分點。一種可能的解釋是,預(yù)訓(xùn)練數(shù)據(jù)中使用了有限數(shù)量的書籍和學(xué)術(shù)論文,即ArXiv, Gutenberg和book3,總計只有177GB,而這些模型在高達(dá)2TB的書籍上進(jìn)行了訓(xùn)練。Gopher、Chinchilla和PaLM使用的大量書籍可能也解釋了為什么Gopher在這個基準(zhǔn)測試中表現(xiàn)優(yōu)于GPT-3,而在其他基準(zhǔn)測試中卻不相上下
4.7 訓(xùn)練過程中的性能演變(Evolution of performance during training)
在訓(xùn)練期間,我們在一些問題回答和常識基準(zhǔn)上跟蹤了模型的性能,并在圖2中報告了它們。在大多數(shù)基準(zhǔn)測試中,性能穩(wěn)步提高,并且與模型的訓(xùn)練困惑度相關(guān)(見圖1)。例外是SIQA和WinoGrande。最值得注意的是,在SIQA上,我們觀察到性能上有很多差異,這可能表明這個基準(zhǔn)測試不可靠。在WinoGrande上,性能與訓(xùn)練困惑度不相關(guān):LLaMA-33B和LLaMA-65B在訓(xùn)練過程中表現(xiàn)相似。
5 指令調(diào)優(yōu)
指令模型LLaMA-I在MMLU上的結(jié)果,并與現(xiàn)有中等規(guī)模的指令微調(diào)模型,進(jìn)行了比較。盡管這里使用的指令調(diào)優(yōu)方法很簡單,但在MMLU上達(dá)到了68.9%。LLaMA-I (65B)在現(xiàn)有中等規(guī)模的指令微調(diào)模型上的表現(xiàn)優(yōu)于MMLU,但仍遠(yuǎn)未達(dá)到最先進(jìn)的水平,在MMLU上的GPT code-davincii-002為77.4。
6 模型代碼
https://github.com/facebookresearch/llama/blob/main/llama/model.py
class TransformerBlock(nn.Module):
def __init__(self, layer_id: int, args: ModelArgs):
super().__init__()
self.n_heads = args.n_heads
self.dim = args.dim
self.head_dim = args.dim // args.n_heads
self.attention = Attention(args)
self.feed_forward = FeedForward(
dim=args.dim, hidden_dim=4 * args.dim, multiple_of=args.multiple_of
)
self.layer_id = layer_id
self.attention_norm = RMSNorm(args.dim, eps=args.norm_eps)
self.ffn_norm = RMSNorm(args.dim, eps=args.norm_eps)
def forward(self, x: torch.Tensor, start_pos: int, freqs_cis: torch.Tensor, mask: Optional[torch.Tensor]):
h = x + self.attention.forward(self.attention_norm(x), start_pos, freqs_cis, mask)
out = h + self.feed_forward.forward(self.ffn_norm(h))
return out
作者對每個Transformer子層的輸入進(jìn)行歸一化,而不是對輸出進(jìn)行歸一化。注意看Transformer中黃色方塊(Add & Norm)部分,都是在輸出部分的,現(xiàn)在把這個操作調(diào)整到前面對輸入進(jìn)行Norm操作。
class Transformer(nn.Module):
def __init__(self, params: ModelArgs):
super().__init__()
self.params = params
self.vocab_size = params.vocab_size
self.n_layers = params.n_layers
self.tok_embeddings = ParallelEmbedding(
params.vocab_size, params.dim, init_method=lambda x: x
)
self.layers = torch.nn.ModuleList()
for layer_id in range(params.n_layers):
self.layers.append(TransformerBlock(layer_id, params))
self.norm = RMSNorm(params.dim, eps=params.norm_eps)
self.output = ColumnParallelLinear(
params.dim, params.vocab_size, bias=False, init_method=lambda x: x
)
self.freqs_cis = precompute_freqs_cis(
self.params.dim // self.params.n_heads, self.params.max_seq_len * 2
)
@torch.inference_mode()
def forward(self, tokens: torch.Tensor, start_pos: int):
_bsz, seqlen = tokens.shape
h = self.tok_embeddings(tokens)
self.freqs_cis = self.freqs_cis.to(h.device)
freqs_cis = self.freqs_cis[start_pos : start_pos + seqlen]
mask = None
if seqlen > 1:
mask = torch.full((1, 1, seqlen, seqlen), float("-inf"), device=tokens.device)
mask = torch.triu(mask, diagonal=start_pos + 1).type_as(h)
for layer in self.layers:
h = layer(h, start_pos, freqs_cis, mask)
h = self.norm(h)
output = self.output(h[:, -1, :]) # only compute last logits
return output.float()
7 論文結(jié)論
本文中提出了一系列公開發(fā)布的語言模型,并實現(xiàn)與最先進(jìn)的基礎(chǔ)模型相競爭的結(jié)果。最值得注意的是,LLaMA-13B的性能優(yōu)于GPT-3,但體積比GPT-3小10倍以上,LLaMA-65B與Chinchilla-70B和PaLM-540B競爭。
與之前的研究不同,論文的研究表明,不使用專有數(shù)據(jù)集,而只使用公開可用的數(shù)據(jù)集進(jìn)行訓(xùn)練,可以達(dá)到最先進(jìn)的性能。作者希望向研究界發(fā)布這些模型將加速大型語言模型的發(fā)展,并有助于提高它們的魯棒性,減輕已知的問題,如毒性和偏見。
此外,作者像Chung等人一樣觀察到,根據(jù)指令對這些模型進(jìn)行微調(diào)會產(chǎn)生有希望的結(jié)果計劃在未來的工作中進(jìn)一步研究這一點。文章來源:http://www.zghlxwxcb.cn/news/detail-495304.html
最后,作者計劃在未來發(fā)布在更大的預(yù)訓(xùn)練語料庫上訓(xùn)練的更大的模型,因為作者在擴(kuò)展語料時已經(jīng)看到了性能的不斷提高文章來源地址http://www.zghlxwxcb.cn/news/detail-495304.html
到了這里,關(guān)于【LLM系列之LLaMA】LLaMA: Open and Efficient Foundation Language Models的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!