TransformerDecoderLayer
ModuleTransformerDecoderLayer(d_model: int, nhead: int, dim_feedforward: int = 2048, dropout: float = 0.1, activation: str | Callable[[Tensor], Tensor] = 'relu', layer_norm_eps: float = 1e-05, batch_first: bool = False, norm_first: bool = False, bias: bool = True, device: DeviceLike = None, dtype: DTypeLike = None, attention_dropout: float | None = None)Single transformer decoder layer: masked self-attention, cross-attention over encoder memory, and a position-wise FFN — each with residual connections and layer normalisation.
The decoder layer introduces a second attention sub-layer that
attends to the encoder output (memory), connecting the
generation process to the source context. The three sub-layers
and their residual connections are:
Post-LN (default, norm_first=False):
Pre-LN (norm_first=True):
where is the memory tensor produced by the encoder.
Feed-forward network (FFN):
Parameters
d_ modelintnheadintdim_ feedforwardint= 20482048.dropoutfloat= 0.10.1.activationstr or callable= 'relu'"relu" (default),
"gelu", or a callable applied as-is. Any other string
raises ValueError.batch_ firstbool= FalseTrue, all tensors use (batch, seq, feature) layout.
Default: False (sequence-first).norm_ firstbool= FalseTrue, uses Pre-LN order. Default: False (Post-LN).deviceDeviceLike= NonedtypeDTypeLike= NoneAttributes
self_ attnMultiheadAttentionmultihead_ attnMultiheadAttentionlinear1Lineard_model → dim_feedforward.linear2Lineardim_feedforward → d_model.norm1LayerNormnorm2LayerNormnorm3LayerNormdropoutDropoutdropout1Dropoutdropout2Dropoutdropout3DropoutNotes
tgt: whenbatch_first=False, or whenbatch_first=True.memory: whenbatch_first=False, or whenbatch_first=True.- Output: same shape as
tgt.
where is the target sequence length, is the
source sequence length, is the batch size, and
= d_model.
The self-attention sub-layer is typically used with
tgt_is_causal=True during autoregressive decoding so that
position cannot attend to any future position
. This prevents information leakage from future
tokens during teacher-forced training.
The cross-attention sub-layer uses the encoder output memory
as both keys and values, while the decoder's current hidden state
provides the queries. This is the mechanism by which the decoder
conditions its generation on the source sequence.
Examples
**Basic decoder layer** (sequence-first layout):
>>> import lucid
>>> import lucid.nn as nn
>>> layer = nn.TransformerDecoderLayer(d_model=512, nhead=8)
>>> tgt = lucid.randn(10, 2, 512) # (tgt_len, batch, d_model)
>>> memory = lucid.randn(20, 2, 512) # (src_len, batch, d_model)
>>> out = layer(tgt, memory)
>>> out.shape
(10, 2, 512)
**Pre-LN decoder layer with causal self-attention**:
>>> layer = nn.TransformerDecoderLayer(
... d_model=256, nhead=4, activation="gelu",
... batch_first=True, norm_first=True,
... )
>>> tgt = lucid.randn(2, 8, 256)
>>> memory = lucid.randn(2, 12, 256)
>>> out = layer(tgt, memory, tgt_is_causal=True)
>>> out.shape
(2, 8, 256)Used by 1
Constructors
1__init__
→None__init__(d_model: int, nhead: int, dim_feedforward: int = 2048, dropout: float = 0.1, activation: str | Callable[[Tensor], Tensor] = 'relu', layer_norm_eps: float = 1e-05, batch_first: bool = False, norm_first: bool = False, bias: bool = True, device: DeviceLike = None, dtype: DTypeLike = None, attention_dropout: float | None = None)Initialise the TransformerDecoderLayer module. See the class docstring for parameter semantics.
Instance methods
2Return a string representation of the layer's configuration.
forward(tgt: Tensor, memory: Tensor, tgt_mask: Tensor | None = None, memory_mask: Tensor | None = None, tgt_key_padding_mask: Tensor | None = None, memory_key_padding_mask: Tensor | None = None, tgt_is_causal: bool = False, memory_is_causal: bool = False, past_key_value: Cache | None = None, layer_idx: int = 0, use_cache: bool = False, cache_position: Tensor | None = None)Run the forward pass of the module.
Parameters
tgtTensormemoryTensormemory.layer_ idx(int, optional, keyword - only)= 0use_ cache(bool, optional, keyword - only)= Falsetgt tokens; accepted for parity.Returns
TensorOutput tensor; refer to the class docstring for the exact shape.