Skip to content
class

T5Model

extendsPretrainedModel
T5Model(config: T5Config)

Bare T5 encoder-decoder trunk.

Implements the architecture of Raffel, Shazeer, Roberts, Lee, Narang, Matena, Zhou, Li and Liu, 2020. One token table embeds both the source and the target. The encoder stack reads the source with bidirectional attention; the decoder stack reads the target causally and cross-attends to the encoder output. Neither side adds a position to its tokens: each stack's blocks add one learned per-head bias, indexed by bucketed relative distance, to their self-attention logits.

Parameters

configT5Config
Vocabulary, width, depth on each side, head count and width, relative-position bucketing, dropout and the init scale.

Attributes

The (vocab_size, d_model) table both stacks embed with.
encodernn.Module
Bidirectional stack of num_hidden_layers blocks.
decodernn.Module
Causal stack of num_decoder_layers blocks with cross-attention.

Notes

Reference: Raffel et al., "Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer", JMLR 21(140), 2020 (arXiv:1910.10683), §2.1 and §3.1.

Head hh of every block scores query ii against key jj as

Aij=qi⊤kj+bh, β(j−i),A_{ij} = q_i^{\top} k_j + b_{h,\,\beta(j - i)},

unscaled, and every sub-layer ff updates the stream as x←x+f(x/RMS(x)⊙γ)x \leftarrow x + f\bigl(x / \mathrm{RMS}(x) \odot \gamma\bigr).

Examples

>>> import lucid
>>> from lucid.models.text.t5 import T5Config, T5Model
>>> cfg = T5Config(vocab_size=100, hidden_size=32, num_hidden_layers=2,
...                num_decoder_layers=2, num_attention_heads=4,
...                head_dim=8, intermediate_size=64)
>>> model = T5Model(cfg).eval()
>>> src = lucid.tensor([[13, 5, 42, 1]])
>>> tgt = lucid.tensor([[0, 7, 9]])
>>> out = model(src, decoder_input_ids=tgt)
>>> out.last_hidden_state.shape, out.encoder_last_hidden_state.shape
((1, 3, 32), (1, 4, 32))

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: T5Config)

Initialise the trunk. See the class docstring for parameters.

Instance methods

5
fn

decode

→Tensor
decode(tgt_ids: Tensor, memory: Tensor, tgt_attention_mask: Tensor | None = None, memory_attention_mask: Tensor | None = None, past_key_value: EncoderDecoderCache | None = None, use_cache: bool = False, cache_position: Tensor | None = None)

Run the decoder against an already-computed encoder output.

Parameters

tgt_idsTensor
(B, T) target ids to decode now — the whole prefix, or only the new ones when a cache holds the rest.
memoryTensor
(B, S, d_model) from encode.
tgt_attention_mask(Tensor or None, optional, keyword - only)= None
Target padding mask, as wide as the cached history plus T.
memory_attention_mask(Tensor or None, optional, keyword - only)= None
(B, S) source padding mask, applied in cross-attention.
past_key_value(EncoderDecoderCache or None, optional, keyword - only)= None
Decoder self-attention history and the cross-attention keys and values; read and extended in place.
use_cachebool= False, keyword-only
Read and write past_key_value.
cache_position(Tensor or None, optional, keyword - only)= None
Absolute write positions for a StaticCache.

Returns

Tensor

(B, T, d_model) decoder output for the T new positions.

fn

encode

→Tensor
encode(src_ids: Tensor, src_attention_mask: Tensor | None = None)

Run the encoder alone.

Parameters

src_idsTensor
(B, S) source token ids.
src_attention_maskTensor or None= None
(B, S) mask, 1 for a token and 0 for padding.

Returns

Tensor

(B, S, d_model) encoder output.

fn

forward

→T5ModelOutput
forward(input_ids: Tensor, decoder_input_ids: Tensor, attention_mask: Tensor | None = None, decoder_attention_mask: Tensor | None = None)

Encode the source and decode the target against it.

Parameters

input_idsTensor
(B, S) source token ids.
decoder_input_idsTensor
(B, T) target ids, already shifted right.
attention_maskTensor or None= None
(B, S) source padding mask.
decoder_attention_maskTensor or None= None
(B, T) target padding mask.

Returns

T5ModelOutput

Decoder and encoder outputs; T5ForSeq2SeqLM projects the decoder side to the vocabulary.

fn

get_decoder

→nn.Module
get_decoder()

The decoder stack, which reads the encoder output as memory and an optional cache.

fn

get_encoder

→nn.Module
get_encoder()

The encoder stack: forward(embeddings, attention_mask) to the encoder output.