Skip to content
class

Embedding

extendsModule
Embedding(num_embeddings: int, embedding_dim: int, padding_idx: int | None = None)

Implementing kernel

C++ engine symbols that back this Python API.

Quantized embedding lookup table — per-row int8, dequantize-on-lookup.

The inference-time replacement that lucid.quantization.convert installs for a calibrated float lucid.nn.Embedding. Embedding tables dominate the parameter budget of large-vocabulary language models — a (50000, 768) table is ~150 MB in float32 — so storing the table as int8 is the single largest memory win quantization offers on the text side, and this layer is where it lands.

Representation. The learned table (num_embeddings, embedding_dim) is quantized once, at from_float time, into an int8 code tensor plus a per-row (per-token) scale / zero_point; the float table is then dropped. Because each vocabulary row gets its own scale, tokens with very different embedding magnitudes are all tracked tightly — far more accurately than a single per-tensor scale. Each forward dequantizes the table back to float and runs the ordinary lucid.nn.functional.embedding lookup, so the layer needs no int8 gather kernel and runs unchanged on any device.

Encoding happens once; decode + gather run on every forward. For row ii, column jj:

W^ij=(Wijq−zi) si,out⁡[k]=W^[ xk ]\hat{W}_{ij} = (W^{q}_{ij} - z_i)\, s_i, \qquad \operatorname{out}[k] = \hat{W}[\, x_k\, ]

where si,zis_i, z_i are the row-ii scale / zero-point (symmetric qint8, so zi=0z_i = 0) and xkx_k the kk-th input index. The lookup gathers dequantized rows; only rows actually indexed are touched.

Parameters

num_embeddingsint
Size of the vocabulary (number of rows in the table).
embedding_dimint
Dimensionality of each embedding vector.
padding_idxint or None= None
If given, the embedding at this index is treated as padding (its row is not accumulated into gradients upstream). Defaults to None.

Attributes

weight_int8Tensor
The int8 table codes, shape (num_embeddings, embedding_dim).
weight_scaleTensor
Per-row (per-token) scale, shape (num_embeddings,).
weight_zero_pointTensor
Per-row (per-token) zero-point, shape (num_embeddings,) — all zero under the default symmetric qint8 scheme.

Notes

  • Instances are normally produced by lucid.quantization.convert (or from_float), not constructed directly: a bare instance holds a zeroed table with identity qparams and returns zeros until from_float / load_state_dict populates the buffers.
  • The table is quantized per-row on axis 0 (one scale per token) with a symmetric qint8 grid ([-128, 127]). When the source module carries no qconfig, a default per-channel qint8 observer calibrates the rows during from_float.
  • Memory: the int8 codes plus the per-row float scale shrink the table payload ~3.55x versus float32 — the dominant checkpoint saving for text models, whose weight mass is mostly embeddings.
  • This layer wins on memory, not compute — the gather runs in float after an on-the-fly dequantize.

Examples

>>> import lucid, lucid.nn as nn
>>> import lucid.quantization as Q
>>> emb = nn.Embedding(1000, 64)
>>> qemb = nn.quantized.Embedding.from_float(emb)   # per-row int8 table
>>> qemb.weight_int8.dtype
lucid.int8
>>> qemb(lucid.tensor([1, 5, 999])).shape           # dequantize-on-lookup
(3, 64)
A directly constructed layer is zeroed — a common mistake is to use it
without from_float / load_state_dict:
>>> broken = nn.quantized.Embedding(1000, 64)
>>> bool((broken.weight_int8 == 0).all().item())
True

See Also

Used by 1

Constructors

1
dunder

__init__

→None
__init__(num_embeddings: int, embedding_dim: int, padding_idx: int | None = None)

Class methods

1
cls

from_float

→Embedding
from_float(mod: nn.Module)

Quantize a float lucid.nn.Embedding's table (per-row int8).

Instance methods

2
fn

extra_repr

→str
extra_repr()

Override to add extra repr info (e.g. Linear shows in_features, etc.).

fn

forward

→Tensor
forward(indices: Tensor)

Dequantize the table and look up indices.