Skip to content
data

CausalLMOutput

extendsModelOutput
CausalLMOutput(logits: Tensor, loss: Tensor | None = None, past_key_values: Cache | tuple[tuple[Tensor, Tensor], ...] | None = None, hidden_states: tuple[Tensor, ...] | None = None, attentions: tuple[Tensor, ...] | None = None)

Output of any causal (left-to-right) language model head.

Attributes

logitsTensor
Per-position vocabulary logits, shape (B, T, vocab_size).
loss(Tensor or None, optional)
Scalar cross-entropy loss when labels were supplied. Labels are typically shifted internally so loss reflects next-token prediction.
past_key_values(tuple[tuple[Tensor, Tensor], ...] or None, optional)
Per-layer (key, value) cache for incremental decoding. Each tensor shape (B, num_heads, T, head_dim).
hidden_states(tuple[Tensor, ...] or None, optional)
Per-layer hidden states.
attentions(tuple[Tensor, ...] or None, optional)
Per-layer attention weights.

Notes

Produced by GPT-1, GPT-2 (GPT2LMHeadModel), and any future decoder-only LM head. CausalLMMixin consumes the logits field; future cache-aware sampling will plumb through past_key_values.

Examples

>>> import lucid
>>> from lucid.models import CausalLMOutput
>>> out = CausalLMOutput(logits=lucid.zeros(1, 128, 50257))
>>> out.logits.shape              # (B, T, vocab)
(1, 128, 50257)
past_key_values holds the cache that makes the next step cheap,
and is filled only when generation asks for it.
>>> out.past_key_values is None
True

Used by 4