浅析GPT2中的autoregressive和BERT的autoencoding源码实现

2023-11-17

经常使用BERT来做研究,因此对Encoder的架构较为熟悉,但是从来没有了解过GPT这样的Decoder架构,尤其对自回归的形式不知道源码是如何实现的。

为了方便对比和讨论,接来下所探讨的源码都是基于HuggingFace这个框架的。

Bert注意力机制

先看一看Bert这个Encoder架构是如何实现autoencoding的。在BertModel这个类中,可以看到其结构是由BertEmbeddings和BertEncoder两个重要的模块构成的。

class BertModel(BertPreTrainedModel):
    """

    The model can behave as an encoder (with only self-attention) as well as a decoder, in which case a layer of
    cross-attention is added between the self-attention layers, following the architecture described in [Attention is
    all you need](https://arxiv.org/abs/1706.03762) by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit,
    Llion Jones, Aidan N. Gomez, Lukasz Kaiser and Illia Polosukhin.

    To behave as an decoder the model needs to be initialized with the `is_decoder` argument of the configuration set
    to `True`. To be used in a Seq2Seq model, the model needs to initialized with both `is_decoder` argument and
    `add_cross_attention` set to `True`; an `encoder_hidden_states` is then expected as an input to the forward pass.
    """

    def __init__(self, config, add_pooling_layer=True):
        super().__init__(config)
        self.config = config

        self.embeddings = BertEmbeddings(config)
        self.encoder = BertEncoder(config)

        self.pooler = BertPooler(config) if add_pooling_layer else None

        # Initialize weights and apply final processing
        self.post_init()

BertEmbeddings相对简单,因此编码过程是在BertEncoder中。那么接下来就是不断地层层拨开BertEncoder。

最终我们定位到了BertSelfAttention类,下面就是其定义的一些参数:

def __init__(self, config, position_embedding_type=None):
        super().__init__()
        if config.hidden_size % config.num_attention_heads != 0 and not hasattr(config, "embedding_size"):
            raise ValueError(
                f"The hidden size ({config.hidden_size}) is not a multiple of the number of attention "
                f"heads ({config.num_attention_heads})"
            )

        self.num_attention_heads = config.num_attention_heads
        self.attention_head_size = int(config.hidden_size / config.num_attention_heads)
        self.all_head_size = self.num_attention_heads * self.attention_head_size

        self.query = nn.Linear(config.hidden_size, self.all_head_size) # all_head_size与hidden_size大小相同
        self.key = nn.Linear(config.hidden_size, self.all_head_size)
        self.value = nn.Linear(config.hidden_size, self.all_head_size)

        self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
        self.position_embedding_type = position_embedding_type or getattr(
            config, "position_embedding_type", "absolute"
        )
        if self.position_embedding_type == "relative_key" or self.position_embedding_type == "relative_key_query":
            self.max_position_embeddings = config.max_position_embeddings
            self.distance_embedding = nn.Embedding(2 * config.max_position_embeddings - 1, self.attention_head_size)

看到了我们耳熟能详的Q、K、V了,从源码来看就是利用了三个线性层,我把代码拿出来:

self.query = nn.Linear(config.hidden_size, self.all_head_size)
self.key = nn.Linear(config.hidden_size, self.all_head_size)
self.value = nn.Linear(config.hidden_size, self.all_head_size)

那么接下来就是对应项相乘就好了,在这里我就只给出Q和K的计算就好了,源码中还有很多细节,这里就不展开了,下面就是计算的代码:

# Take the dot product between "query" and "key" to get the raw attention scores.
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
image-20230611205641538

其中query_layer和key_layer是经过transpose_for_scores这个方法计算得出的,它们的转换关系如下:

def transpose_for_scores(self, x):
        new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.attention_head_size) # self.num_attention_heads * self.attention_head_size = hidden_size
        x = x.view(*new_x_shape)
        return x.permute(0, 2, 1, 3) #  (bs, num_attention_heads, length, attention_head_size)
……
key_layer = self.transpose_for_scores(self.key(hidden_states))
value_layer = self.transpose_for_scores(self.value(hidden_states))
query_layer = self.transpose_for_scores(mixed_query_layer)

可以看出通过矩阵的乘法就实现了BERT的双向注意力机制,attention_scores就是注意力机制的得分。

GPT2注意力机制

直接上源码,GPT2Model类的定义如下:

class GPT2Model(GPT2PreTrainedModel):
    _keys_to_ignore_on_load_unexpected = [r"h\.\d+\.attn\.bias", r"h\.\d+\.attn\.masked_bias"]
    _keys_to_ignore_on_load_missing = [r"attn.masked_bias", r"h\.\d+\.attn\.masked_bias", r"h\.\d+\.attn\.bias"]

    def __init__(self, config):
        super().__init__(config)

        self.embed_dim = config.hidden_size

        self.wte = nn.Embedding(config.vocab_size, self.embed_dim)
        self.wpe = nn.Embedding(config.max_position_embeddings, self.embed_dim)

        self.drop = nn.Dropout(config.embd_pdrop)
        self.h = nn.ModuleList([GPT2Block(config, layer_idx=i) for i in range(config.num_hidden_layers)])
        self.ln_f = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_epsilon)

        # Model parallel
        self.model_parallel = False
        self.device_map = None
        self.gradient_checkpointing = False

        # Initialize weights and apply final processing
        self.post_init()

可以看出,其相对BERT开始,更直接一些。直接的地方在于,其将GPT2Block直接给出,而BERT需要很多层封装。接下来我们看一下GPT2Block中的GPT2Attention是怎么定义的。

GPT2Attention类中定义的部分内容如下(我只放了部分明显直接的代码):

class GPT2Attention(nn.Module):
    def __init__(self, config, is_cross_attention=False, layer_idx=None):
      # 在某些时候,我们可能希望模型中的某些参数参数不更新(从开始到结束均保持不变),但又希望参数保存下来,这是我们就会用到 register_buffer() 。
      self.register_buffer( 
            "bias",
            torch.tril(torch.ones((max_positions, max_positions), dtype=torch.bool)).view(
                1, 1, max_positions, max_positions
            ),
            persistent=False,
        ) # 生成了下三角矩阵,这个就是掩码的生成。
      # Layer-wise attention scaling, reordering, and upcasting
      self.scale_attn_by_inverse_layer_idx = config.scale_attn_by_inverse_layer_idx
      self.layer_idx = layer_idx
      self.reorder_and_upcast_attn = config.reorder_and_upcast_attn

      if self.is_cross_attention: # 这里假设不使用is_cross_attention,即is_cross_attention=False
          self.c_attn = Conv1D(2 * self.embed_dim, self.embed_dim)
          self.q_attn = Conv1D(self.embed_dim, self.embed_dim)
      else:
          self.c_attn = Conv1D(3 * self.embed_dim, self.embed_dim)
        self.c_proj = Conv1D(self.embed_dim, self.embed_dim)

        self.attn_dropout = nn.Dropout(config.attn_pdrop)
        self.resid_dropout = nn.Dropout(config.resid_pdrop)

可以明显看出,GPT的计算是使用了1维卷积来实现QKV权重的生成(但是不清楚为什么,看源码和nn.Linear效果差不多)。

当然,这里的Conv1D并不是使用了Pytorch的nn.Conv1D,而是自己重写的一个,我们来看看其是如何定义的,定义的源码如下:

class Conv1D(nn.Module):
    """
    1D-convolutional layer as defined by Radford et al. for OpenAI GPT (and also used in GPT-2).
    Basically works like a linear layer but the weights are transposed.(自己手写的原因)
    Args:
        nf (`int`): The number of output features.
        nx (`int`): The number of input features.
    """
    def __init__(self, nf, nx): # 假设是 Conv1D(3 * self.embed_dim, self.embed_dim) ,即不考虑is_cross_attention的情况
        super().__init__()
        self.nf = nf
        self.weight = nn.Parameter(torch.empty(nx, nf))
        self.bias = nn.Parameter(torch.zeros(nf))
        nn.init.normal_(self.weight, std=0.02)

    def forward(self, x):
        size_out = x.size()[:-1] + (self.nf,)
        x = torch.addmm(self.bias, x.view(-1, x.size(-1)), self.weight) # 将 (batch_size, seq_len, embed_dim) 变为(batch_size, seq_len, 3 * embed_dim)
        x = x.view(size_out)
        return x

这里使用了一个torch.addmm来实现了卷积计算,计算的方式就如下图所示:

image-20230611210707940

然后通过下面的代码实现了Q,K,V的权重获得。其中hidden_states.shape = (batch_size, seq_len, embed_dim), self.split_size=embed_dim。

query, key, value = self.c_attn(hidden_states).split(self.split_size, dim=2)

通过split方法就实现了上述在_int_()方法中的3 * self.embed_dim切分。

_attn方法给出了QKV的计算:

def _attn(self, query, key, value, attention_mask=None, head_mask=None):
    # Q, K矩阵相乘, 求每个 token 相对当前 sequence 所有 token 的注意力
    # [batch, heads, sequence_len, head_dim] * [batch, heads, head_dim, sequence_len] 变为  [batch, heads, sequence_len, sequence_len]
    attn_weights = torch.matmul(query, key.transpose(-1, -2))
		if self.scale_attn_weights:
       # 缩放计算,除以 sqrt(n_embd)
        attn_weights = attn_weights / torch.full(
            [], value.size(-1) ** 0.5, dtype=attn_weights.dtype, device=attn_weights.device
        )

    # Layer-wise attention scaling
    if self.scale_attn_by_inverse_layer_idx:
        attn_weights = attn_weights / float(self.layer_idx + 1)

    # 掩去 mask 位置的注意力
    # 解码时,每个位置的 token 只能跟自己以及之前位置的 token 计算注意力
    if not self.is_cross_attention:
        # if only "normal" attention layer implements causal mask
        query_length, key_length = query.size(-2), key.size(-2)
        causal_mask = self.bias[:, :, key_length - query_length : key_length, :key_length] # 使用的 self.register_buffer生成的掩码矩阵
        mask_value = torch.finfo(attn_weights.dtype).min # 获得attn_weights.dtype数值类型的最小值
       
      	# Need to be a tensor, otherwise we get error: `RuntimeError: expected scalar type float but found double`.Need to be on the same device, otherwise `RuntimeError: ..., x and y to be on the same device`
        mask_value =  ([], mask_value, dtype=attn_weights.dtype).to(attn_weights.device)
        # torch.where(condition,a,b)其中输入参数condition:条件限制,如果满足条件,则选择a,否则选择b作为输出。
        attn_weights = torch.where(causal_mask, attn_weights.to(attn_weights.dtype), mask_value)

    if attention_mask is not None:
        # Apply the attention mask
        attn_weights = attn_weights + attention_mask

    attn_weights = nn.functional.softmax(attn_weights, dim=-1)

    # Downcast (if necessary) back to V's dtype (if in mixed-precision) -- No-Op otherwise
    attn_weights = attn_weights.type(value.dtype)
    attn_weights = self.attn_dropout(attn_weights)

    # Mask heads if we want to
    if head_mask is not None:
        attn_weights = attn_weights * head_mask

    attn_output = torch.matmul(attn_weights, value)

    return attn_output, attn_weights

至此,我们可以看到causal_mask就是GPT模型的自回归重要的特征。attn_weights就是注意力机制的计算。

相关推荐:

past_key_values在P-TuningV2中的巧用

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

浅析GPT2中的autoregressive和BERT的autoencoding源码实现 的相关文章

随机推荐

  • React学习(JSX+组件+state+表单)

    React JSX 声明元素 渲染元素 组件 练习 this props children PropTypes 默认值 获取真实的DOM节点 this state 表单 组件的生命周期 例子 JSX JSX是一种JavaScript语法的拓
  • 链式前向星存树图和遍历它的两种方法【dfs、bfs】

    目录 一 链式前向星存图 二 两种遍历方法 一 链式前向星存图 n个点 n 1条边 链式前向星把上面的树图存下来 输入 9 代表要存进去n个点 1 2 下面是n 1条边 每条边连接两个点 1 3 1 7 2 4 4 5 4 6 3 8 3
  • json-server在vscode终端运行文件报错

    错误 无法加载文件 D 因为在此系统上禁止运行脚本 json server 无法加载文件 D ruanjian nodejs node global json server ps1 因为在此系 统上禁止运行脚本 有关详细信息 请参阅 htt
  • 关于宝塔面板无法访问的解决方法

    前言 本篇文章主要介绍宝塔面板无法访问的几种情况以及如何解决 正文 1 没有开放相应端口 这种情况比较常见 服务商默认情况下会将所有的端口关闭 你需要使用哪个端口就得手动去打开这个端口 例如 http 47 8888 adminuser 中
  • ubuntu如何开放22端口 ubuntu22端口开启

    ubuntu开放22端口的操作步骤 1 打开终端命令行模式 2 依次输入以下命令进行开放22端口即可 输入以下命令打开22端口 sudo ufw allow 22 重启防火墙使其生效即可 sudo ufw reload 附加 sudo ap
  • 详解混合类型文件(Polyglot文件)的应用生成与检测

    1 引入 混合类型文件 Polyglot文件 是指一个文件 既可以是合法的A类型 也可以是合法的B类型 比如参考3中的文件 是一个html文件 可以用浏览器正常打开 它也是一个一个 jar文件 可以用JVM正常运行 参考4 如下图所示 这样
  • Spring源码:PropertyValues类及属性注入一

    概要 相关类 属性注入 概要 Spring获取Bean的实例时 需要把配置的属性值解析到PropertyValues 然后填充入BeanWrapper中 相关类 MutablePropertyValues类 PropertyValues接口
  • http://www.limodev.cn/blog/archives/category/gtk

    The linux mobile development gt http www limodev cn blog 致力于基于linux的嵌入式系统的学习和研究 包括内核 驱动 GUI MMI 软件设计和优化等 欢迎交换友情链接 代码请到Pr
  • Docker实战-NFS安装

    Docker安装 1 使用官方安装脚本自动安装 curl fsSL https get docker com bash s docker mirror Aliyun 2 启动 Docker 服务 systemctl start docker
  • Struts2框架详解(二)

    Struts2结果页面配置 全局结果页面 场景 多个action 有相同的方法返回值 且都到同一个结果页面 需求 创建两个action 执行默认的方法execute方法 让两个action的方法都返回success 返回success之后
  • Nerf 训练自有数据

    1 拍摄序列图像 放置在文件夹 如duck images 2 使用colmap使用COLMAP获取相机位姿 1 参考官方文档安装colmap 2 使用win 可执行程序 3 点击 File 选择 new project 点击 New 在du
  • 强化学习实现智能城市规划,清华团队最新成果登Nature子刊

    由于城市地理空间的多样性和巨大的动作空间 给城市布局用地和道路是一件非常复杂而困难的任务 长久以来一直依靠人类规划师的经验和直觉 如今 城市规划领域也有了自己的AlphaGo 近日 清华大学电子系城市科学与计算研究中心与建筑学院跨学科合作
  • eip系统服务器,eip系统

    本词条缺少概述图 补充相关内容使词条更完整 还能快速升级 赶紧来编辑吧 eip系统是以数据为基础 应用为核心 以实现业务及业务流程的自动化为目的多功能企业信息平台 中文名 eip系统 基 础 数据性 质 多功能企业信息平台 方 法 把不同系
  • 二货小易有一个W*H的网格盒子 JAVA

    import java util public class Main public static void main String args Scanner sc new Scanner System in int w sc nextInt
  • VS2017调试unity没有解决方案

    1 VS2017之前的版本调试unity 需要下载 Visual Studio Tools for Unity 2 VS2017内置了Visual Studio Tools for Unity 3 首先打开 帮助 gt 关于 已安装程序中查
  • 法研杯参赛总结:“抽取-生成”式长文本摘要

    PaperWeekly 原创 作者 苏剑林 单位 追一科技 研究方向 NLP 神经网络 法研杯 1 算是近年来比较知名的 NLP 赛事之一 今年是第三届 包含四个赛道 其中有一个 司法摘要 赛道引起了我们的兴趣 经过了解 这是面向法律领域裁
  • 单片机IO引脚驱动能力的提高

    这篇文字学到不少东西 包括文中链接 在此感谢坐而论道 现摘录如下 早期的51单片机 驱动能力很低 P1 P2和P3口只能驱动3个LSTTL输入端 P0口可驱动8个 如果想要驱动更多的器件 就要用到 总线驱动芯片 经常用的就是74LS244
  • html登录页面

    效果图 html代码 index html
  • Java多线程练习--取钱

    1 需求 小明和小红是一对夫妻 他们有一个共同的账户 余额是10万元 模拟2人同时去取钱10万 分析 1 需要提供一个账户类 创建一个账户对象代表2个人的共享账户 2 需要定义一个线程类 线程类可以处理账户对象 3 创建2个线程对象 传入同
  • 浅析GPT2中的autoregressive和BERT的autoencoding源码实现

    经常使用BERT来做研究 因此对Encoder的架构较为熟悉 但是从来没有了解过GPT这样的Decoder架构 尤其对自回归的形式不知道源码是如何实现的 为了方便对比和讨论 接来下所探讨的源码都是基于HuggingFace这个框架的 Ber