Skip to content
class

TransformerForSeq2SeqLM

extendsLanguageModelingModel
TransformerForSeq2SeqLM(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

Hyperparameters. config.tie_word_embeddings (default True) controls the LM-head weight tying.

Attributes

transformerTransformerModel
Underlying encoder-decoder trunk.
lm_headnn.Linear
Output projection of shape (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:

LS2S=−1BT∑b,tlog⁡pθ(yb,t∣xb,yb,<t),\mathcal{L}_{\mathrm{S2S}} = -\frac{1}{BT} \sum_{b, t} \log p_\theta(y_{b, t} \mid x_b, y_{b, < t}),

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

1
dunder

__init__

→None
__init__(config: TransformerConfig)

Instance methods

2
fn

forward

→Seq2SeqLMOutput
forward(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.

fn

generate

→Tensor
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= 32
Cap on generated target length (incl. BOS).
max_new_tokensint | None= None
Cap on the tokens after BOS (>= 0); wins over max_length.
bos_token_idint | None= None
Start-of-sequence id; defaults to config.bos_token_id then 0.
eos_token_idint | None= None
End-of-sequence id; defaults to config.eos_token_id.
pad_token_idint | None= None
Right-padding for finished rows; defaults to config.pad_token_id then 0.
attention_maskTensor | None= None
Optional source padding mask.
use_cachebool= True
Use an EncoderDecoderCache 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
Self-attention buffer capacity for "static" or "compiled" (defaults to the length cap).
do_samplebool= False
Sample from the (filtered) distribution instead of greedy argmax (default False).
temperaturefloat= 1.0
Logit temperature applied before sampling; strictly positive.
top_kint | None= None
Keep only the top_k likeliest tokens; None or 0 disables.
top_pfloat | None= None
Nucleus threshold — keep the smallest set whose cumulative probability reaches top_p, in [0, 1]; None or 1.0 disables.
repetition_penaltyfloat= 1.0
Down-weight logits of already-generated tokens; strictly positive.

Returns

Tensor

(B, T_out) int Tensor of generated target tokens.

Raises

ValueError
A sampling argument or cache_implementation is out of range.