Skip to content
class

Embedding

extendsHiraLayer
Embedding(base_layer: nn.Module, adapter_name: str, config: C)

Implementing kernel

C++ engine symbols that back this Python API.

An nn.Embedding with a HiRA update: the table EE is looked up as E+E⊙(BA)⊤E + E \odot (BA)^\top.

The factors are A∈Rr×VA \in \mathbb{R}^{r \times V} and B∈Rd×rB \in \mathbb{R}^{d \times r} (hira_embedding_A, hira_embedding_B). They start as the reference starts them, A=0A = 0 and B∼N(0,1)B \sim \mathcal N(0, 1), so the update is zero; init_weights=False leaves both Gaussian. The modulated table is looked up as is: the base's padding_idx and max_norm apply to the base lookup only, and hira_dropout has nothing to act on.

Parameters

base_layernn.Embedding
The layer to adapt; the other parameters are those of HiraLayer.
adapter_namestr
Name of the first adapter.
r, hira_dropout and init_weights (the per-module ranks are the tuner's to resolve).

Examples

>>> import lucid
>>> import lucid.nn as nn
>>> from lucid.peft import HiraConfig
>>> from lucid.peft.tuners.hira import Embedding
>>> base = nn.Embedding(10, 4)
>>> layer = Embedding(base, "default", HiraConfig(r=2, target_modules=["x"]))
>>> tuple(layer.hira_embedding_A["default"].shape), tuple(layer.hira_embedding_B["default"].shape)
((2, 10), (4, 2))
>>> ids = lucid.tensor([[1, 2, 3]])
>>> bool((layer(ids) == base(ids)).all().item())   # A starts at zero
True

Used by 1