Python wrappers
Public Python APIs implemented by this engine symbols.- py
lucid.nn.Embeddingclass - py
lucid.nn.qat.Embeddingclass - py
lucid.nn.quantized.Embeddingclass - py
lucid.peft.tuners.hira.Embeddingclass - py
lucid.peft.tuners.lora.Embeddingclass - py
lucid.peft.tuners.tinylora.Embeddingclass - py
lucid.peft.tuners.trainable_tokens.Embeddingclass - py
lucid.nn.functional.embeddingfunction
Autograd node for the embedding lookup on a 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"embedding", version 1, AmpPolicy::Promote).padding_ idx_int-1 disables it. The forward does not apply it.weight_ shape_Shape(num_embeddings, embed_dim); recovered during backward to size dW.saved_ indices_Storagesaved_ indices_ shape_Shapesaved_ indices_ dtype_DtypeI64).Static methods
1forward
→TensorImplPtrint 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
for every index, padding_idx included.
Shape
weight:indices: integer- Output :
Parameters
weightTensorImplPtr(num_embeddings, embed_dim). Must be exactly 2-D.indicesTensorImplPtr[0, num_embeddings).padding_ idxintscale_ grad_ by_ freqbooltrue, divide each row's accumulated gradient by the number of times its index appears in indices.Returns
TensorImplPtrOutput of shape — the input index shape with an extra trailing dim of embed_dim.
Raises
ShapeMismatchweight is not 2-D.DeviceMismatchweight and indices live on different devices.Methods
3Scatter-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_ outStorageReturns
std::vector<Storage>Single-element {dW}.
The same scatter-add, recorded, so a gradient of the weight gradient reaches the upstream gradient.