You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义TransformerEncoderLayer遇'is_causal'参数错误,求修复方案

解决TransformerEncoderLayer.forward()收到意外参数'is_causal'的问题

问题核心是:你给自定义层的forward加了**kwargs,但没把这些参数传递给内部调用的nn.MultiheadAttention。PyTorch新版本的MultiheadAttention.forward新增了is_causal参数,TrainAD的上层逻辑会自动传递这个参数,你的自定义层接收到后没转发给注意力模块,导致报错。

直接修改forward方法里的注意力调用部分,把**kwargs传进去即可,顺便补上之前遗漏的掩码参数,让逻辑和PyTorch原生层对齐:

class TransformerEncoderLayer(nn.Module):
    def __init__(self, d_model, nhead, dim_feedforward=16, dropout=0):
        super(TransformerEncoderLayer, self).__init__()
        self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout)
        self.linear1 = nn.Linear(d_model, dim_feedforward)
        self.dropout = nn.Dropout(dropout)
        self.linear2 = nn.Linear(dim_feedforward, d_model)
        self.dropout1 = nn.Dropout(dropout)
        self.dropout2 = nn.Dropout(dropout)

        self.activation = nn.LeakyReLU(True)

    def forward(self, src, src_mask=None, src_key_padding_mask=None, **kwargs):
        # 关键修改:将所有接收的参数转发给注意力模块
        src2 = self.self_attn(src, src, src, attn_mask=src_mask, key_padding_mask=src_key_padding_mask, **kwargs)[0]
        src = src + self.dropout1(src2)
        src2 = self.linear2(self.dropout(self.activation(self.linear1(src))))
        src = src + self.dropout2(src2)
        return src

内容的提问来源于stack exchange,提问作者23_7867

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 22:53:16