Knowledge Distillation of Large Language Models

MINILLM是一种新的知识蒸馏方法,用于从大型语言模型(LLM)中提取知识,以训练更小的模型。它通过反向Kullback-Leibler散度(KLD)来优化,避免学生模型高估教师分布的低概率区域。MINILLM通过策略梯度优化、教师混合采样和长度归一化来提高训练效果,适用于不同大小的模型。实验表明,MINILLM在指令跟随任务中优于标准KD方法,具有更低的曝光偏差、更好的校准和长文本生成性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是大模型系列模型的文章,针对《Knowledge Distillation of Large Language Models》的翻译。

摘要

知识蒸馏(KD)是一种很有前途的技术,可以减少大型语言模型(LLM)的高计算需求。然而,以前的KD方法主要应用于白盒分类模型或训练小模型来模仿像ChatGPT这样的黑盒模型API。如何有效地从白盒生成LLM中提取知识仍有待探索,随着LLM的蓬勃发展,这一点变得越来越重要。在这项工作中,我们提出了MINILLM,它从生成的较大语言模型中提取较小的语言模型。我们首先将标准KD方法中的前向Kullback-Leibler散度(KLD)目标替换为更适合生成语言模型上的KD的反向KLD,以防止学生模型高估教师分布的低概率区域。然后,我们推导出一种有效的优化方法来学习这个目标。在指令跟随设置中的大量实验表明,MINILLM模型生成更精确的响应,具有更高的整体质量、更低的曝光偏差、更好的校准和更高的长文本生成性能。我们的方法也适用于具有120M到13B参数的不同模型族。我们将在https://aka.ms/MiniLLM发布我们的代码和模型检查点。

1 引言

### 关于Vision Transformer (ViT) 的伪代码实现 为了更好地理解如何构建和训练 Vision Transformer (ViT),以下是基于现有研究[^1]的一个简化版伪代码实现: ```python import torch.nn as nn class PatchEmbedding(nn.Module): """ 将图像分割成多个patch并嵌入 """ def __init__(self, img_size=224, patch_size=16, embed_dim=768): super().__init__() self.proj = nn.Conv2d(3, embed_dim, kernel_size=patch_size, stride=patch_size) def forward(self, x): x = self.proj(x).flatten(2).transpose(1, 2) return x class MultiHeadSelfAttention(nn.Module): """ 多头自注意力机制 """ def __init__(self, dim, num_heads=12): super().__init__() assert dim % num_heads == 0, "dim should be divisible by num_heads" self.num_heads = num_heads head_dim = dim // num_heads self.scale = head_dim ** -0.5 self.qkv = nn.Linear(dim, dim * 3) self.proj = nn.Linear(dim, dim) def forward(self, x): B, N, C = x.shape qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k, v = qkv.unbind(0) attn = (q @ k.transpose(-2, -1)) * self.scale attn = attn.softmax(dim=-1) out = (attn @ v).transpose(1, 2).reshape(B, N, C) out = self.proj(out) return out class MLPBlock(nn.Module): """ 多层感知机模块 """ def __init__(self, in_features, hidden_features=None, out_features=None): super().__init__() hidden_features = hidden_features or in_features out_features = out_features or in_features self.fc1 = nn.Linear(in_features, hidden_features) self.act = nn.GELU() self.fc2 = nn.Linear(hidden_features, out_features) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.fc2(x) return x class EncoderLayer(nn.Module): """ 编码器层,由多头自注意机制和MLP组成 """ def __init__(self, dim, mlp_ratio=4., drop_rate=0.1, num_heads=12): super().__init__() self.norm1 = nn.LayerNorm(dim) self.attn = MultiHeadSelfAttention(dim, num_heads=num_heads) self.drop_path = DropPath(drop_rate) if drop_rate > 0. else nn.Identity() self.norm2 = nn.LayerNorm(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = MLPBlock(in_features=dim, hidden_features=mlp_hidden_dim) def forward(self, x): residual = x x = self.norm1(x) x = self.attn(x) x = residual + self.drop_path(x) y = self.norm2(x) y = self.mlp(y) x = x + self.drop_path(y) return x class VisionTransformer(nn.Module): """ 整合上述组件形成完整的ViT结构 """ def __init__(self, img_size=224, patch_size=16, embed_dim=768, depth=12, num_heads=12, mlp_ratio=4., class_token=True, distillation=False): super().__init__() self.patch_embed = PatchEmbedding(img_size=img_size, patch_size=patch_size, embed_dim=embed_dim) num_patches = (img_size // patch_size)**2 self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) self.pos_embed = nn.Parameter(torch.randn(1, num_patches + 1, embed_dim)) self.blocks = nn.Sequential(*[ EncoderLayer( dim=embed_dim, mlp_ratio=mlp_ratio, drop_rate=drop_rate, num_heads=num_heads ) for _ in range(depth)]) self.class_token = class_token self.distillation = distillation def forward(self, x): batch_size = x.size(0) x = self.patch_embed(x) cls_tokens = self.cls_token.expand(batch_size, -1, -1) x = torch.cat((cls_tokens, x), dim=1) x += self.pos_embed[:, :x.size(1)] x = self.blocks(x) if not self.distillation: x = x[:, 0] else: x = x.mean(dim=1) return x ``` 此段代码展示了如何创建一个基本版本的 Vision Transformer 架构。具体来说,这段代码定义了几种核心组件——`PatchEmbedding`, `MultiHeadSelfAttention`, `MLPBlock` 和 `EncoderLayer` —— 并最终通过组合这些部分来建立整个网络架构。 值得注意的是,在实际应用中可能还需要考虑更多细节,比如位置编码的具体形式、正则化方法的选择以及优化策略等。此外,不同论文可能会对某些超参数做出特定调整以适应不同的任务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UnknownBody

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值