Bitsandbytes 文档

嵌入

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

嵌入

embedding 类用于存储和从索引中检索词嵌入。bitsandbytes 中有两种类型的嵌入,标准的 PyTorch Embedding 类和 StableEmbedding 类。

StableEmbedding 类在通过分块量化的 8 位优化器论文中引入,旨在减少由于输入 tokens 的非均匀分布而导致的梯度方差。此类旨在支持量化。

嵌入

class bitsandbytes.nn.Embedding

< >

( num_embeddings: int embedding_dim: int padding_idx: typing.Optional[int] = None max_norm: typing.Optional[float] = None norm_type: float = 2.0 scale_grad_by_freq: bool = False sparse: bool = False _weight: typing.Optional[torch.Tensor] = None device: typing.Optional[torch.device] = None )

Embedding 类用于存储和从索引中检索词嵌入。

__init__

< >

( num_embeddings: int embedding_dim: int padding_idx: typing.Optional[int] = None max_norm: typing.Optional[float] = None norm_type: float = 2.0 scale_grad_by_freq: bool = False sparse: bool = False _weight: typing.Optional[torch.Tensor] = None device: typing.Optional[torch.device] = None )

参数

  • num_embeddings (int) — 唯一嵌入的数量(词汇表大小)。
  • embedding_dim (int) — 嵌入的维度。
  • padding_idx (Optional[int]) — 在给定索引处用零填充输出。
  • max_norm (Optional[float]) — 重新归一化嵌入,使其具有最大 L2 范数。
  • norm_type (float, 默认为 2.0) — 为 max_norm 选项计算的 p 范数。
  • scale_grad_by_freq (bool, 默认为 False) — 在反向传播期间按频率缩放梯度。
  • sparse (bool, 默认为 False) — 计算密集梯度。设置为 True 以计算稀疏梯度。
  • _weight (Optional[Tensor]) — 预训练的嵌入。

StableEmbedding

class bitsandbytes.nn.StableEmbedding

< >

( num_embeddings: int embedding_dim: int padding_idx: typing.Optional[int] = None max_norm: typing.Optional[float] = None norm_type: float = 2.0 scale_grad_by_freq: bool = False sparse: bool = False _weight: typing.Optional[torch.Tensor] = None device = None dtype = None )

参数

  • norm (torch.nn.LayerNorm) — 在嵌入后应用的层归一化。

自定义嵌入层,旨在通过使用 32 位优化器状态来提高 NLP 任务训练期间的稳定性。它旨在减少可能由量化导致的梯度变化。此嵌入层使用 Xavier 均匀初始化进行初始化,然后进行层归一化。

示例

# Initialize StableEmbedding layer with vocabulary size 1000, embedding dimension 300
embedding_layer = StableEmbedding(num_embeddings=1000, embedding_dim=300)

# Reset embedding parameters
embedding_layer.reset_parameters()

# Perform a forward pass with input tensor
input_tensor = torch.tensor([1, 2, 3])
output_embedding = embedding_layer(input_tensor)

方法:reset_parameters(): 使用 Xavier 均匀初始化重置嵌入参数。forward(input: Tensor) -> Tensor: 通过 stable embedding 层的前向传播。

__init__

< >

( num_embeddings: int embedding_dim: int padding_idx: typing.Optional[int] = None max_norm: typing.Optional[float] = None norm_type: float = 2.0 scale_grad_by_freq: bool = False sparse: bool = False _weight: typing.Optional[torch.Tensor] = None device = None dtype = None )

参数

  • num_embeddings (int) — 唯一嵌入的数量(词汇表大小)。
  • embedding_dim (int) — 嵌入的维度。
  • padding_idx (Optional[int]) — 在给定索引处用零填充输出。
  • max_norm (Optional[float]) — 重新归一化嵌入,使其具有最大 L2 范数。
  • norm_type (float, 默认为 2.0) — 为 max_norm 选项计算的 p 范数。
  • scale_grad_by_freq (bool, defaults to False) — 在反向传播过程中,根据频率缩放梯度。
  • sparse (bool, defaults to False) — 计算密集梯度。 设置为 True 以计算稀疏梯度。
  • _weight (Optional[Tensor]) — 预训练的嵌入。
< > 在 GitHub 上更新