This Transformer layer implements the original Transformer architecture described
in the Attention Is All You Need paper. The
intent of this layer is as a reference implementation for foundational understanding
and thus it contains only limited features relative to newer Transformer architectures.
Given the fast pace of innovation in transformer-like architectures, we recommend
exploring this tutorial
to build an efficient transformer layer from building blocks in core or using higher
level libraries from the PyTorch Ecosystem.
Parameters:
d_model (int) – the number of expected features in the encoder/decoder inputs (default=512).
nhead (int) – the number of heads in the multiheadattention models (default=8).
num_encoder_layers (int) – the number of sub-encoder-layers in the encoder (default=6).
num_decoder_layers (int) – the number of sub-decoder-layers in the decoder (default=6).
dim_feedforward (int) – the dimension of the feedforward network model (default=2048).
dropout (float) – the dropout value (default=0.1).
activation (str | Callable[[Tensor], Tensor]) – the activation function of encoder/decoder intermediate layer, can be a string
(“relu” or “gelu”) or a unary callable. Default: relu
layer_norm_eps (float) – the eps value in layer normalization components (default=1e-5).
batch_first (bool) – If True, then the input and output tensors are provided
as (batch, seq, feature). Default: False (seq, batch, feature).
norm_first (bool) – if True, encoder and decoder layers will perform LayerNorms before
other attention and feedforward operations, otherwise after. Default: False (after).
bias (bool) – If set to False, Linear and LayerNorm layers will not learn an additive
bias. Default: True.
Take in and process masked source/target sequences.
Parameters:
src (Tensor) – the sequence to the encoder (required).
tgt (Tensor) – the sequence to the decoder (required).
src_mask (Tensor | None) – the additive mask for the src sequence (optional).
tgt_mask (Tensor | None) – the additive mask for the tgt sequence (optional).
memory_mask (Tensor | None) – the additive mask for the encoder output (optional).
src_key_padding_mask (Tensor | None) – the Tensor mask for src keys per batch (optional).
tgt_key_padding_mask (Tensor | None) – the Tensor mask for tgt keys per batch (optional).
memory_key_padding_mask (Tensor | None) – the Tensor mask for memory keys per batch (optional).
src_is_causal (bool | None) – If specified, applies a causal mask as src_mask.
Default: None; try to detect a causal mask.
Warning:
src_is_causal provides a hint that src_mask is
the causal mask. Providing incorrect hints can result in
incorrect execution, including forward and backward
compatibility.
tgt_is_causal (bool | None) – If specified, applies a causal mask as tgt_mask.
Default: None; try to detect a causal mask.
Warning:
tgt_is_causal provides a hint that tgt_mask is
the causal mask. Providing incorrect hints can result in
incorrect execution, including forward and backward
compatibility.
memory_is_causal (bool) – If specified, applies a causal mask as
memory_mask.
Default: False.
Warning:
memory_is_causal provides a hint that
memory_mask is the causal mask. Providing incorrect
hints can result in incorrect execution, including
forward and backward compatibility.
src_key_padding_mask: (S) for unbatched input otherwise (N,S).
tgt_key_padding_mask: (T) for unbatched input otherwise (N,T).
memory_key_padding_mask: (S) for unbatched input otherwise (N,S).
Note: [src/tgt/memory]_mask ensures that position i is allowed to attend the unmasked
positions. If a BoolTensor is provided, positions with True
are not allowed to attend while False values will be unchanged. If a FloatTensor
is provided, it will be added to the attention weight.
[src/tgt/memory]_key_padding_mask provides specified elements in the key to be ignored by
the attention. If a BoolTensor is provided, the positions with the
value of True will be ignored while the position with the value of False will be unchanged.
output: (T,E) for unbatched input, (T,N,E) if batch_first=False or
(N, T, E) if batch_first=True.
Note: Due to the multi-head attention architecture in the transformer model,
the output sequence length of a transformer is same as the input sequence
(i.e. target) length of the decoder.
where S is the source sequence length, T is the target sequence length, N is the
batch size, E is the feature number