TransformerForSeq2SeqLM
LanguageModelingModelTransformerForSeq2SeqLM(config: TransformerConfig)Encoder-decoder Transformer with a tied seq2seq language-modeling head.
Wraps TransformerModel with a linear LM head projecting decoder
hidden states to the target vocabulary. The head weight is tied to
tgt_tok_emb.weight when config.tie_word_embeddings is True,
matching the Vaswani 2017 §3.4 convention that halves the parameter cost
of the output softmax. Standard interface for translation and
summarisation — pass input_ids (the source) and
decoder_input_ids (the right-shifted target); supply labels to
also compute the shift-loss for training. Greedy inference is exposed
through generate.
Parameters
configTransformerConfigconfig.tie_word_embeddings (default True)
controls the LM-head weight tying.Attributes
transformerTransformerModellm_ headnn.Linear(d_model, effective_decoder_vocab_size)
mapping decoder hidden states to target-vocabulary logits.Notes
Reference: Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, and Polosukhin, "Attention Is All You Need", NeurIPS, 2017 (arXiv:1706.03762).
Loss when labels is supplied is the per-token cross-entropy over
the right-shifted target sequence:
with positions labelled -100 excluded.
Examples
>>> import lucid
>>> from lucid.models.text.transformer import TransformerConfig, TransformerForSeq2SeqLM
>>> cfg = TransformerConfig(num_hidden_layers=2, num_decoder_layers=2,
... hidden_size=64, num_attention_heads=2,
... intermediate_size=128, vocab_size=1000)
>>> model = TransformerForSeq2SeqLM(cfg).eval()
>>> src = lucid.tensor([[1, 234, 567, 2]])
>>> tgt = lucid.tensor([[1, 100, 200]])
>>> out = model(src, decoder_input_ids=tgt)
>>> out.logits.shape # (B=1, T_tgt=3, vocab=1000)
(1, 3, 1000)Used by 2
Constructors
1Instance methods
2forward(input_ids: Tensor, decoder_input_ids: Tensor, attention_mask: Tensor | None = None, decoder_attention_mask: Tensor | None = None, labels: Tensor | None = None)Override in subclasses to define the computation.
generate(input_ids: Tensor, max_length: int = 32, max_new_tokens: int | None = None, bos_token_id: int | None = None, eos_token_id: int | None = None, pad_token_id: int | None = None, attention_mask: Tensor | None = None, use_cache: bool = True, cache_implementation: str = 'dynamic', max_cache_len: 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)Seq2seq decoding — greedy (default) or stochastic sampling.
Encodes input_ids once, then unrolls the decoder one token at a
time (BOS-seeded) until max_length (or every batch row hits
eos_token_id). Token selection routes through the shared sampling
primitives, so do_sample / temperature / top_k / top_p /
repetition_penalty behave exactly as for decoder-only models.
Parameters
input_ idsTensor(B, S) source token ids.max_ lengthint= 32max_ new_ tokensint | None= None>= 0); wins over max_length.bos_ token_ idint | None= Noneconfig.bos_token_id then 0.eos_ token_ idint | None= Noneconfig.eos_token_id.pad_ token_ idint | None= Noneconfig.pad_token_id then 0.use_ cachebool= TrueEncoderDecoderCache so each step only
decodes the new token (default True).cache_ implementationstr= 'dynamic'"dynamic" (default), "static"
(pre-allocated self-attention StaticCache),
or "compiled" (compiled single-token decode via
compiled_decode_step).max_ cache_ lenint | None= None"static" or
"compiled" (defaults to the length cap).do_ samplebool= Falseargmax (default False).temperaturefloat= 1.0top_ kint | None= Nonetop_k likeliest tokens; None or 0 disables.top_ pfloat | None= Nonetop_p, in [0, 1]; None or 1.0 disables.repetition_ penaltyfloat= 1.0Returns
Tensor(B, T_out) int Tensor of generated target tokens.
Raises
ValueErrorcache_implementation is out of range.