MCPcopy Create free account
hub / github.com/bitsandbytes-foundation/bitsandbytes / StableEmbedding

Class StableEmbedding

bitsandbytes/nn/modules.py:28–131  ·  view source on GitHub ↗

Custom embedding layer designed to improve stability during training for NLP tasks by using 32-bit optimizer states. It is designed to reduce gradient variations that can result from quantization. This embedding layer is initialized with Xavier uniform initialization followed by layer normaliza

Source from the content-addressed store, hash-verified

26
27
28class StableEmbedding(torch.nn.Embedding):
29 """
30 Custom embedding layer designed to improve stability during training for NLP tasks by using 32-bit optimizer states. It is designed to reduce gradient variations that can result from quantization. This embedding layer is initialized with Xavier uniform initialization followed by layer normalization.
31
32 Example:
33
34 ```
35 # Initialize StableEmbedding layer with vocabulary size 1000, embedding dimension 300
36 embedding_layer = StableEmbedding(num_embeddings=1000, embedding_dim=300)
37
38 # Reset embedding parameters
39 embedding_layer.reset_parameters()
40
41 # Perform a forward pass with input tensor
42 input_tensor = torch.tensor([1, 2, 3])
43 output_embedding = embedding_layer(input_tensor)
44 ```
45
46 Attributes:
47 norm (`torch.nn.LayerNorm`): Layer normalization applied after the embedding.
48
49 Methods:
50 reset_parameters(): Reset embedding parameters using Xavier uniform initialization.
51 forward(input: Tensor) -> Tensor: Forward pass through the stable embedding layer.
52 """
53
54 def __init__(
55 self,
56 num_embeddings: int,
57 embedding_dim: int,
58 padding_idx: Optional[int] = None,
59 max_norm: Optional[float] = None,
60 norm_type: float = 2.0,
61 scale_grad_by_freq: bool = False,
62 sparse: bool = False,
63 _weight: Optional[Tensor] = None,
64 device=None,
65 dtype=None,
66 ) -> None:
67 """
68 Args:
69 num_embeddings (`int`):
70 The number of unique embeddings (vocabulary size).
71 embedding_dim (`int`):
72 The dimensionality of the embedding.
73 padding_idx (`Optional[int]`):
74 Pads the output with zeros at the given index.
75 max_norm (`Optional[float]`):
76 Renormalizes embeddings to have a maximum L2 norm.
77 norm_type (`float`, defaults to `2.0`):
78 The p-norm to compute for the `max_norm` option.
79 scale_grad_by_freq (`bool`, defaults to `False`):
80 Scale gradient by frequency during backpropagation.
81 sparse (`bool`, defaults to `False`):
82 Computes dense gradients. Set to `True` to compute sparse gradients instead.
83 _weight (`Optional[Tensor]`):
84 Pretrained embeddings.
85 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected