Skip to content
class

EmbeddingBackward

EmbeddingBackward

Python wrappers

Public Python APIs implemented by this engine symbols.

Autograd node for the embedding lookup y=W[idx]y = W[\text{idx}] on a (V,D)(V, D) weight matrix.

Only the weight matrix is a differentiable input. Integer indices have no meaningful gradient and are not stored in the standard saved_inputs_ slot — they live in saved_indices_ instead, alongside their shape and dtype, so the backward can scatter-add grad_out into the correct rows of dW.

Forward dispatches to IBackend::embedding_forward which gathers rows from the weight storage at positions given by indices — every row, padding_idx included. Backward uses IBackend::embedding_backward to scatter-add grad_out into a zero-initialized dW at those same positions, skipping any whose index equals padding_idx.

padding_idx is a training contract, not a lookup mask. The module zeroes that row at initialisation and the backward keeps it from being updated, so a fresh table answers zeros there and a loaded checkpoint answers with the vector it carries — BERT's [PAD] is a trained one. Masking the lookup instead is the mistake this used to make, in eager and compiled alike.

Attributes

schema_v1OpSchema
Registered schema (name "embedding", version 1, AmpPolicy::Promote).
padding_idx_int
Index whose row receives no gradient. -1 disables it. The forward does not apply it.
weight_shape_Shape
(num_embeddings, embed_dim); recovered during backward to size dW.
saved_indices_Storage
Integer index tensor saved from the forward pass. Not subject to AMP because the dtype is integer.
saved_indices_shape_Shape
Shape of the saved index tensor.
saved_indices_dtype_Dtype
Dtype of the saved index tensor (default I64).

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & weight, const int & indices, int padding_idx, bool scale_grad_by_freq)

Look up rows of weight at positions given by indices.

Math

y[…]=W[idx[…]]y[\ldots] = W[\text{idx}[\ldots]] for every index, padding_idx included.

Shape

  • weight : (V,D)(V, D)
  • indices : (∗)(\ast) integer
  • Output : (∗,D)(\ast, D)

Parameters

weightTensorImplPtr
Embedding matrix of shape (num_embeddings, embed_dim). Must be exactly 2-D.
indicesTensorImplPtr
Integer-typed tensor of any shape with values in [0, num_embeddings).
padding_idxint
Row the backward leaves without gradient; pass a negative value to disable. The lookup returns that row as stored.
scale_grad_by_freqbool
When true, divide each row's accumulated gradient by the number of times its index appears in indices.

Returns

TensorImplPtr

Output of shape (∗ indices.shape,embed_dim)(\ast\,\text{indices.shape}, \text{embed\_dim}) — the input index shape with an extra trailing dim of embed_dim.

Raises

ShapeMismatch
If weight is not 2-D.
DeviceMismatch
If weight and indices live on different devices.

Methods

3
fn

apply

→std::vector<Storage>
int apply(int grad_out)

Scatter-add backward.

Returns a single dW storage of shape weight_shape_, zero-initialized except where the saved indices accumulate contributions from grad_out. Rows at padding_idx_ accumulate nothing.

Parameters

grad_outStorage
Incoming gradient of shape (∗,D)(\ast, D).

Returns

std::vector<Storage>

Single-element {dW}.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

The same scatter-add, recorded, so a gradient of the weight gradient reaches the upstream gradient.

fn

release_own_saved

void release_own_saved()