Skip to content
class

EmbeddingBag

extendsModule
EmbeddingBag(num_embeddings: int, embedding_dim: int, mode: str = 'mean', padding_idx: int | None = None)

Implementing kernel

C++ engine symbols that back this Python API.

Quantized EmbeddingBag — per-row int8 table, pooled dequant lookup.

The inference-time replacement for a calibrated float lucid.nn.EmbeddingBag, installed by lucid.quantization.convert / from_float. Like lucid.nn.quantized.Embedding it stores the table as int8 with a per-row (per-token) scale — the same dominant memory win for large-vocabulary models — but instead of returning one row per index it pools each bag of indices into a single vector, the fused primitive behind recommendation and bag-of-words text models.

Representation. The table (num_embeddings, embedding_dim) is quantized per-row into int8 codes plus a per-row scale / zero_point at from_float time; the float table is dropped. Each forward dequantizes the table, gathers the rows in each bag, and reduces them by mode — fused so the per-token embedding matrix is never materialised.

Given a flat index sequence partitioned into bags by offsets, the ii-th pooled output is

W^rc=(Wrcq−zr) sr,out⁡[i]=reduce⁡k∈bag⁡iW^[ xk ]\hat{W}_{rc} = (W^{q}_{rc} - z_r)\, s_r, \qquad \operatorname{out}[i] = \operatorname{reduce}_{k \in \operatorname{bag}_i} \hat{W}[\, x_k\, ]

where sr,zrs_r, z_r are the row-rr scale / zero-point (symmetric qint8) and reduce is one of sum, mean, or max selected by mode. The reduction runs on the dequantized (float) rows.

Parameters

num_embeddingsint
Size of the vocabulary (number of rows in the table).
embedding_dimint
Dimensionality of each embedding vector.
mode(sum, mean, max)= "sum"
Pooling reduction applied over each bag of gathered rows. Defaults to "mean".
padding_idxint or None= None
If given, the embedding at this index is treated as padding. 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 pools to 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. When the source module carries no qconfig, a default per-channel qint8 observer calibrates the rows.
  • Memory: the int8 codes plus per-row scale shrink the table payload ~3.55x versus float32 — as with Embedding, the layer wins on memory, not compute (pooling runs in float post-dequantize).
  • offsets marks where each bag begins in a flat index tensor; pass a 2-D index tensor instead to treat every row as its own fixed-length bag.

Examples

>>> import lucid, lucid.nn as nn
>>> bag = nn.EmbeddingBag(1000, 32, mode="mean")
>>> qbag = nn.quantized.EmbeddingBag.from_float(bag)
>>> idx = lucid.tensor([1, 2, 4, 5, 4])
>>> offsets = lucid.tensor([0, 3])              # two bags: [1,2,4] and [5,4]
>>> qbag(idx, offsets).shape                    # one pooled vector per bag
(2, 32)

See Also

Used by 1

Constructors

1
dunder

__init__

→None
__init__(num_embeddings: int, embedding_dim: int, mode: str = 'mean', padding_idx: int | None = None)

Class methods

1
cls

from_float

→EmbeddingBag
from_float(mod: nn.Module)

Quantize a float lucid.nn.EmbeddingBag'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, offsets: Tensor | None = None, per_sample_weights: Tensor | None = None, compressed_indices_mapping: Tensor | None = None)

Dequantize the table and run the pooled bag lookup.