T5 with its language-modelling head — the model every T5 task trains.
Wraps T5Model with a bias-free projection to the vocabulary.
With config.tie_word_embeddings (the paper's setting) the
projection is the shared token table, and the decoder output is
scaled by first: the table is drawn at
unit variance for the input side, so the tied output would otherwise
be too large.
Pass labels alone and the decoder input is built from them —
decoder_start_token_id followed by the labels shifted right, with
-100 replaced by the pad id — as the paper's teacher forcing does.
Parameters
configT5Configtie_word_embeddings and
decoder_start_token_id shape the head.Attributes
Notes
Reference: Raffel et al., JMLR 2020 (arXiv:1910.10683), §2.1 and §3.1.4.
With labels the loss is the mean per-token cross-entropy over
positions not labelled -100:
Examples
>>> import lucid
>>> from lucid.models.text.t5 import T5Config, T5ForSeq2SeqLM
>>> 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 = T5ForSeq2SeqLM(cfg).eval()
>>> src = lucid.tensor([[13, 5, 42, 1]])
>>> labels = lucid.tensor([[7, 9, 1]])
>>> out = model(src, labels=labels)
>>> out.logits.shape, out.loss.shape
((1, 3, 100), ())Used by 2
Constructors
1Instance methods
4forward(input_ids: Tensor, decoder_input_ids: Tensor | None = None, attention_mask: Tensor | None = None, decoder_attention_mask: Tensor | None = None, labels: Tensor | None = None)Teacher-forced pass, with the loss when labels are given.
Parameters
input_ idsTensor(B, S) source token ids.(B, T) target ids, shifted right. Built from labels
when omitted.(B, S) source padding mask.(B, T) target padding mask.(B, T) target ids; -100 marks a position the loss skips.Returns
Seq2SeqLMOutputlogits of shape (B, T, vocab_size), the loss when
labels were given, and the encoder output.
Raises
ValueErrordecoder_input_ids nor labels is given.generate(input_ids: Tensor, max_length: int = 20, max_new_tokens: int | None = None, attention_mask: Tensor | None = None, decoder_start_token_id: int | None = None, eos_token_id: int | None = None, pad_token_id: int | None = None, do_sample: bool = False, temperature: float = 1.0, top_k: int | None = None, top_p: float | None = None, repetition_penalty: float = 1.0, use_cache: bool = True, cache_implementation: str = 'dynamic', max_cache_len: int | None = None)Decode an answer for each source — greedy unless do_sample.
Parameters
input_ idsTensor(B, S) source ids, task prefix included ("translate English to German: …").max_ lengthint= 20, keyword-onlymax_ new_ tokens(int or None, optional, keyword - only)= None>= 0); wins over max_length.(B, S) source padding mask.use_ cachebool= True, keyword-onlycache_ implementationstr= "dynamic", keyword-only"dynamic" → DynamicCache; "static" → fixed-size StaticCache;
"compiled" → StaticCache + compiled_decode_step (one executable).max_ cache_ len(int or None, optional, keyword - only)= None"static"/"compiled"; default: the length cap.do_ samplebool= False, keyword-onlyargmax.temperaturefloat= 1.0, keyword-onlydo_sample.top_ k(int or None, optional, keyword - only)= Nonetop_k most likely tokens only; None or 0 disables.top_ p(float or None, optional, keyword - only)= Nonetop_p, in [0, 1]; None/1.0 is off.repetition_ penaltyfloat= 1.0, keyword-only1.0 is off.decoder_ start_ token_ id(int or None, optional, keyword - only)= Noneconfig.decoder_start_token_id (the pad id) by default.eos_ token_ id(int or None, optional, keyword - only)= Noneconfig.eos_token_id by default.pad_ token_ id(int or None, optional, keyword - only)= Noneconfig.pad_token_id by default.Returns
Tensor(B, T_out) int64 target ids beginning with the start token.
Raises
ValueErrorcache_implementation is out of range, before encoding.The trunk's decoder stack.
The trunk's encoder stack.