掘金 人工智能 09月17日
LLaMA模型:性能与成本的平衡之道
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

LLaMA模型的核心理念在于,在同等计算预算下,通过增加训练数据量而非模型参数量来提升性能,并特别强调了推理成本的优化。相较于GPT-3,LLaMA-13B在多数基准测试中表现更优,且模型规模更小,允许在单个GPU上运行,从而降低了大型语言模型的研究和使用门槛。其架构沿用Transformer,并引入了RMSNorm、SwiGLU激活函数和Rotary Embeddings(RoPE)等关键改进,以提高训练效率、稳定性和性能。文章还详细介绍了数据集构建、RMSNorm、RoPE、SwiGLU的实现原理以及LLaMA模型的整体网络结构。

💡 **LLaMA的核心理念:数据量胜于参数量** LLaMA模型的设计理念突破了“参数越多越好”的传统观念,强调在有限的计算预算下,通过海量数据的训练能够实现更优的性能。这种策略不仅提升了模型在基准测试中的表现,更重要的是显著降低了实际应用中的推理成本和运行速度,使得大型语言模型的研究和部署更加可行和经济。

🚀 **架构创新与性能优化** LLaMA在Transformer架构的基础上,集成了多项关键技术以提升效率和稳定性。其中包括采用RMSNorm进行预归一化,替代传统的LayerNorm,以加速训练;引入SwiGLU激活函数,增强模型处理信息的能力;以及应用Rotary Embeddings(RoPE),替代绝对位置嵌入,从而更好地处理变长序列和捕捉相对位置信息。

🛠️ **关键组件实现细节** 文章深入剖析了LLaMA模型的核心组件。在数据处理方面,通过构建输入和目标序列来训练自回归模型;在归一化层面,详细介绍了RMSNorm的计算方式及其优势;在位置编码上,阐述了RoPE如何通过旋转嵌入向量来编码位置信息;在激活函数方面,解释了SwiGLU通过门控机制动态控制信息流的原理。这些技术细节共同构成了LLaMA模型高效运作的基础。

🌐 **模型结构与应用拓展** LLaMA模型由词嵌入层、一系列LLaMA块(Transformer层)以及最终的输出层构成。每个LLaMA块内部包含自注意力机制和前馈网络,负责捕捉序列上下文。这种模块化的设计易于扩展,使其能够适应不同规模的需求。文章最后提及了完整的代码实现和进阶学习资源,为读者提供了实践和深入研究的途径。

本文较长,建议点赞收藏。更多AI大模型应用开发学习视频及资料,在智泊AI

LLaMA 的开发基于一个核心理念:在给定计算预算下,通过增加训练数据量而非单纯增加模型参数,可以达到更好的性能。这与之前普遍认为“参数越多性能越好”的观点不同,并特别强调了 推理成本 的重要性。尽管训练一个大型号的模型可能更快达到某个性能水平,但一个参数更少但训练更久的小模型在实际应用中的推理成本会更低、速度更快。

LLaMA-13B 在大多数基准测试中表现优于拥有 175B 参数的 GPT-3,尽管其模型规模小了十倍。这使得 LLaMA-13B 可以在单个 GPU 上运行,从而“民主化”了大型语言模型的研究和使用。

架构与优化 (Architecture and Optimizer)

LLaMA 沿用了 Transformer 架构,但引入了几个关键的改进以提升性能和训练稳定性,这些改进借鉴了其他现有模型:

步骤1:构建数据集

创建输入和目标序列: 对于每一个随机选定的起始索引 i,代码创建两个序列:

def get_batches(data, split, batch_size, context_window, config=DEFAULT_CONFIG):    # Split the dataset into training, validation, and test sets    train=data[:int(.8*len(data))]    val=data[int(.8 * len(data)): int(.9*len(data))]    test=data[int(.9 *len(data)):]        # Determine whcih split to use    batch_data=train    if split=='val':        batch_data=val    if split=='test':        batch_data=test        # Pick random starting points within the data    ix=torch.randint(0,batch_data.size(0)-context_window-1, (batch_size,))        # create input sequences (x) and corrsponding target sequences (y)    x=torch.stack([batch_data[i:i+context_window] for i in ix]).long()    y=torch.stack([batch_data[i+1:i+context_window+1] for i in ix]).long()        return x,yxs, ys=get_batches(dataset, 'train', DEFAULT_CONFIG['batch_size'], DEFAULT_CONFIG['context_window'])

用于让一个预训练的语言模型生成文本。该函数遵循了自回归(autoregressive)生成的核心逻辑:每次生成一个新 token,并将其添加到序列中,然后使用新序列作为输入来预测下一个 token。

def generate(model, config=DEFAULT_CONFIG, max_new_tokens=30):    idx=torch.zeros(5,1).long()    for _ in range(max_new_tokens):        # Call the model        logits=model(idx[:, -config['context_window']:])        # all the batches (1), last time step, all the logits        last_time_step_logits=logits[            :,-1,:        ]        # softmax to get probabilities        p=F.softmax(last_time_step_logits, dim=-1)                # sample from the distribution to get the next token        idx_next=torch.multinomial(            p, num_samples=1        )                # append to the sequence        idx=torch.cat([idx, idx_next], dim=-1)            return [decode(x) for x in idx.tolist()]        

步骤2:RMSNorm

RMSNorm(Root Mean Square Normalization)层,这是一种用于深度学习模型(特别是 Transformer 架构)的归一化技术,旨在替代传统的 LayerNorm,以提高训练效率和稳定性。RMSNorm 的设计灵感来源于 Layer Normalization,但它进行了一定的简化:

    省略均值中心化: RMSNorm 不减去均值。它只对输入张量的每个元素进行缩放,缩放因子是该序列元素的均方根(RMS)。传统的 LayerNorm 既会减去均值,也会除以标准差。计算简单: RMSNorm 的计算只涉及平方、求和、开方和除法,比 LayerNorm 省去了均值计算,因此在计算上更加高效。加速训练: 实践证明,RMSNorm 在 Transformer 模型中能加速训练过程,并保持稳定的性能
class RMSNorm(nn.Module):    def __init__(self, layer_shape, eps=1e-8, bias=False):        super(RMSNorm, self).__init__()        self.register_parameter('scale', nn.Parameter(torch.ones(layer_shape)))        def forward(self,x):        #calculating the Frobenius norm, RMS=1/sqrt(N)* Frobenius norm        ff_rms=torch.linalg.norm(x, dim=(1,2))*x[0].numel() ** -.5                # normalizing the input tensor 'x' with respect to RMS        raw=x/ff_rms.unsqueeze(-1).unsqueeze(-1)                # scaling the normalized tensor using the learnable parameter 'scale'        return self.scale[:x.shape[1],:].unsqueeze(0) * raw

步骤3:RoPE

RoPE 是一种为 Transformer 模型提供序列中 token 位置信息的方法,它通过旋转每个 token 的词嵌入向量来编码其绝对和相对位置。与传统的绝对位置编码不同,RoPE 可以更好地处理可变长度的序列,并能有效地将相对位置信息融入到自注意力机制中。

def get_rotary_matrix(context_window, embedding_dim):    # Initialize a tensor for the rotary matrix with zeros    R=torch.zeros((context_window, embedding_dim, embedding_dim), requires_grad=False)        # Loop thorugh each position in the context window    for position in range(context_window):        # Loop through each dimension in the embedding        for i in range(embedding_dim//2):            # Calculate the rotation angle (theta) based on the position and embedding dimension            theta=10000. ** (-2.*(i-1)/embedding_dim)            # Calculate the rotated matrix elements using sine and cosine functions            m_theta=position*theta            R[position, 2*i, 2*i]=np.cos(m_theta)            R[position, 2*i, 2*i+1]=-np.sin(m_theta)            R[position, 2*i+1, 2*i]=np.sin(m_theta)            R[position, 2*i+1, 2*i+1]=np.cos(m_theta)    return R

步骤4:SwiGLU

SwiGLU 的基本思想是,通过一个可学习的“门控”机制来动态地控制信息流。它包含两个平行的线性变换,一个作为主信息通道,另一个作为门控信号。

SwiGLU 的优势在于其门控机制,它允许模型学习哪些特征是重要的。通过将主分支的输出与门控信号相乘,模型可以:

class SwiGLU(nn.Module):    def __init__(self, size):        super().__init__()        # Configuration information        # Linear transformation for the gating mechanism        self.linear_gate=nn.Linear(size, size)        # Linear transformation for the main branch        self.linear=nn.Linear(size, size)        # Random initialization of the beta parameter        self.beta=torch.randn(1, requires_grad=True)                # Using nn.Parameter for beta to ensure it's recognized as a learnable parameter        self.beta=nn.Parameter(torch.ones(1))        self.register_parameter("beta", self.beta)         def forward(self, x):        # Swish-Gated Linear Unit computation        swish_gate=self.linear_gate(x)* torch.sigmoid(self.beta*self.linear_gate(x))        # Element -wise multiplication of the gate and main branch        out=swish_gate*self.linear(x)        return out

步骤5:搭建网络结构

每个 Llama 块内部都包含了自注意力机制和前馈网络,用于捕捉和处理序列中的上下文信息。整个模型由三个主要部分构成:词嵌入层、一系列 Llama 块(即 Transformer 层)以及一个最终的输出层。

class Llama(nn.Module):    def __init__(self, config):        super().__init__()        self.config=config        #embedding layer for token representations        self.embeddings=nn.Embedding(config['vocab_size'], config['d_model'])        #sequential block of LlamaBlocks based on the specified number of layers        self.llama_blocks=nn.Sequential(            OrderedDict([(f"llama_{i}",LlamaBlock(config)) for i in range(config['n_layers'])])        )                #feedforward network (FFN) for final output        self.ffn=nn.Sequential(            nn.Linear(config['d_model'], config['d_model']),            SwiGLU(config['d_model']),            nn.Linear(config['d_model'], config['vocab_size']),        )                # print total number of parameters in the model        print("model params:", sum([m.numel() for m in self.parameters()]))            def forward(self, idx, targets=None):        # input token indices are passed through the embedding layer        x=self.embeddings(idx)        #process the input through the LlamaBlocks        x=self.llama_blocks(x)        #pass the processed input through the final FFN for output logits        logits=self.ffn(x)                # If targets are not provided, return only the logits        if targets isNone:            return logits        # If targets are provided, compute and return the cross-entropy loss        else:            loss=F.cross_entropy(logits.view(-1, self.config['vocab_size']),targets.view(-1))            return logits, loss

完整代码链接:www.kaggle.com/code/aisuko…

学习资源推荐

如果你想更深入地学习大模型,以下是一些非常有价值的学习资源,这些资源将帮助你从不同角度学习大模型,提升你的实践能力。

本文较长,建议点赞收藏。更多AI大模型应用开发学习视频及资料,在智泊AI

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

LLaMA 大型语言模型 AI Transformer 模型优化 LLaMA Model Large Language Models AI Transformer Model Optimization
相关文章