fn
sinusoidal_embedding
→Tensorsinusoidal_embedding(num_positions: int, embedding_dim: int, base: float = 10000.0, device: str = 'cpu')Build the 1-D sinusoidal positional encoding table from "Attention Is All You Need".
Returns a fixed (non-learnable) lookup table that injects absolute position information into token embeddings without adding parameters. Successive frequencies form a geometric progression spanning wavelengths from to , so the encoding's components vary at vastly different rates and uniquely identify each position even at large sequence lengths.
Parameters
num_positionsintNumber of distinct positions .
embedding_dimintPer-position embedding size . Must be even — half the
entries hold
sin values and half hold cos values.basefloat= 10000.0Frequency base . Vaswani et al. use
10_000;
larger values give longer effective context windows at the cost of
finer per-step discrimination.devicestr= 'cpu'Target device (
"cpu" or "metal") for the resulting buffer.Returns
Tensor(num_positions, embedding_dim) float tensor.
Raises
ValueErrorIf
embedding_dim is not even.Notes
Equation (5) of Vaswani et al. (2017):
The table is pure (deterministic in its arguments) so callers can
safely share the result across model instances when dimensions match.
For training-time hot paths prefer the module form in
lucid.nn, which caches the table as a buffer.
Examples
>>> import lucid
>>> from lucid.nn.functional import sinusoidal_embedding
>>> pe = sinusoidal_embedding(num_positions=128, embedding_dim=64)
>>> pe.shape
(128, 64)