Transformers 文档

JetMoe

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

该模型于 2023-06-07 发布,并于 2024-05-14 添加到 Hugging Face Transformers。

JetMoe

PyTorch FlashAttention SDPA

概述

JetMoe-8B 是由 Yikang ShenMyShell 开发的 80 亿参数混合专家(MoE)语言模型。JetMoe 项目旨在以有限的预算提供 LLaMA2 级别的性能和高效的语言模型。为了实现这一目标,JetMoe 采用了受 ModuleFormer 启发的稀疏激活架构。每个 JetMoe 块包含两个 MoE 层:注意力头混合(Mixture of Attention Heads)和 MLP 专家混合(Mixture of MLP Experts)。给定输入 token,它会激活一部分专家来处理它们。这种稀疏激活机制使得 JetMoe 能够实现比同等大小的密集模型更高的训练吞吐量。JetMoe-8B 的训练吞吐量在拥有 96 个 H100 GPU 的集群上,采用简单的三向流水线并行策略,每秒可处理约 1000 亿个 token。

此模型由 Yikang Shen 贡献。

JetMoeConfig

class transformers.JetMoeConfig

< >

( vocab_size: int | None = 32000 hidden_size: int | None = 2048 num_hidden_layers: int | None = 12 num_key_value_heads: int | None = 16 kv_channels: int | None = 128 intermediate_size: int | None = 5632 max_position_embeddings: int | None = 4096 activation_function: str | None = 'silu' num_local_experts: int | None = 8 num_experts_per_tok: int | None = 2 output_router_logits: bool | None = False aux_loss_coef: float | None = 0.01 use_cache: bool | None = True bos_token_id: int | None = 1 eos_token_id: int | None = 2 pad_token_id: int | None = None tie_word_embeddings: bool | None = True rope_parameters: transformers.modeling_rope_utils.RopeParameters | dict[str, transformers.modeling_rope_utils.RopeParameters] | None = None rms_norm_eps: int | None = 1e-06 initializer_range: float | None = 0.01 attention_dropout: float | None = 0.0 **kwargs )

参数

  • vocab_size (int可选,默认为 32000) — JetMoe 模型词汇量大小。定义了调用 JetMoeModel 时传入的 inputs_ids 可以表示的不同 token 的数量。
  • hidden_size (int可选,默认为 2048) — 隐藏表示的维度。
  • num_hidden_layers (int可选,默认为 12) — Transformer 编码器中的隐藏层数量。
  • num_key_value_heads (int可选,默认为 16) — Transformer 编码器中每个键值对的注意力头数量。
  • kv_channels (int可选,默认为 128) — 定义键和值张量的通道数。
  • intermediate_size (int可选,默认为 5632) — MLP 表示的维度。
  • max_position_embeddings (int可选,默认为 4096) — 此模型可能使用的最大序列长度。JetMoe 的注意力允许最长 4096 个 token 的序列。
  • activation_function (string可选,默认为 "silu") — 定义 MLP 专家的激活函数。
  • num_local_experts (int可选,默认为 8) — 定义 MoE 和 MoA 中的专家数量。
  • num_experts_per_tok (`int`,可选,默认为 2) — 为 MoE 和 MoA 中的每个 token 和 MoE 以及 MoA 指定的专家数量。
  • output_router_logits (bool可选,默认为 False) — 模型是否应返回路由器 logits。启用此项也将允许模型输出辅助损失。
  • aux_loss_coef (float可选,默认为 0.01) — 辅助损失的系数。
  • use_cache (bool可选,默认为 True) — 模型是否应返回最后的键/值注意力(并非所有模型都使用)。仅在 config.is_decoder=True 时相关。
  • bos_token_id (int可选,默认为 1) — “开始序列” token 的 ID。
  • eos_token_id (int可选,默认为 2) — “结束序列” token 的 ID。
  • pad_token_id (int可选) — padding token 的 ID。
  • tie_word_embeddings (bool可选,默认为 True) — 模型输入和输出词嵌入是否应该绑定。
  • rope_parameters (RopeParameters可选) — 包含 RoPE 嵌入配置参数的字典。该字典应包含 rope_theta 的值,如果想在更长的 max_position_embeddings 中使用 RoPE 进行缩放,还可以包含用于缩放的参数。
  • rms_norm_eps (float可选,默认为 1e-06) — RMS normalization 层使用的 epsilon。
  • initializer_range (float可选,默认为 0.01) — 用于初始化所有权重矩阵的 truncated_normal_initializer 的标准差。
  • attention_dropout (float可选,默认为 0.0) — 注意力概率的 dropout 比率。

这是用于存储 JetMoeModel 配置的配置类。它用于根据指定的参数实例化 JetMoe 模型,定义模型架构。使用默认值实例化配置将得到 JetMoe-4B 的配置。

jetmoe/jetmoe-8b

配置对象继承自 PreTrainedConfig,可用于控制模型输出。有关更多信息,请阅读 PreTrainedConfig 的文档。

>>> from transformers import JetMoeModel, JetMoeConfig

>>> # Initializing a JetMoe 4B style configuration
>>> configuration = JetMoeConfig()

>>> # Initializing a model from the JetMoe 4B style configuration
>>> model = JetMoeModel(configuration)

>>> # Accessing the model configuration
>>> configuration = model.config

JetMoeModel

class transformers.JetMoeModel

< >

( config: JetMoeConfig )

参数

  • config (JetMoeConfig) — 模型配置类,包含模型的所有参数。使用配置文件初始化不会加载与模型相关的权重,只加载配置。请查看 from_pretrained() 方法来加载模型权重。

The bare Jetmoe Model outputting raw hidden-states without any specific head on top.

此模型继承自 PreTrainedModel。查看其父类文档,了解库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、修剪头等)。

此模型也是一个 PyTorch torch.nn.Module 子类。像普通的 PyTorch Module 一样使用它,并参考 PyTorch 文档了解一般用法和行为的所有相关信息。

forward

< >

( input_ids: torch.LongTensor | None = None attention_mask: torch.Tensor | None = None position_ids: torch.LongTensor | None = None past_key_values: transformers.cache_utils.Cache | None = None inputs_embeds: torch.FloatTensor | None = None use_cache: bool | None = None cache_position: torch.LongTensor | None = None **kwargs: typing_extensions.Unpack[transformers.utils.generic.TransformersKwargs] ) transformers.modeling_outputs.MoeModelOutputWithPast or tuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Input sequence tokens的索引。默认情况下将忽略填充。可以使用 AutoTokenizer 获取索引。有关详细信息,请参阅 PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.Tensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:

    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.n_positions - 1].

    什么是位置ID?

  • past_key_values (~cache_utils.Cache, optional) — Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention blocks) that can be used to speed up sequential decoding. This typically consists in the past_key_values returned by the model at a previous stage of decoding, when use_cache=True or config.use_cache=True.

    Only Cache instance is allowed as input, see our kv cache guide. If no past_key_values are passed, DynamicCache will be initialized by default.

    The model will output the same cache format that is fed as input.

    If past_key_values are used, the user is expected to input only unprocessed input_ids (those that don’t have their past key value states given to this model) of shape (batch_size, unprocessed_length) instead of all input_ids of shape (batch_size, sequence_length).

  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — Optionally, instead of passing input_ids you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert input_ids indices into associated vectors than the model’s internal embedding lookup matrix.
  • use_cache (bool, optional) — If set to True, past_key_values key value states are returned and can be used to speed up decoding (see past_key_values).
  • cache_position (torch.LongTensor of shape (sequence_length), optional) — Indices depicting the position of the input sequence tokens in the sequence. Contrarily to position_ids, this tensor is not affected by padding. It is used to update the cache in the correct position and to infer the complete sequence length.

返回

transformers.modeling_outputs.MoeModelOutputWithPasttuple(torch.FloatTensor)

A transformers.modeling_outputs.MoeModelOutputWithPast or a tuple of torch.FloatTensor (if return_dict=False is passed or when config.return_dict=False) comprising various elements depending on the configuration (JetMoeConfig) and inputs.

  • last_hidden_state (torch.FloatTensor, 形状为 (batch_size, sequence_length, hidden_size)) — 模型最后一层输出的隐藏状态序列。

  • past_key_values (Cache, optional, 当传递 use_cache=True 或当 config.use_cache=True 时返回) — 它是 Cache 实例。更多详情,请参阅我们的 kv cache 指南

    Contains pre-computed hidden-states (key and values in the self-attention blocks and optionally if config.is_encoder_decoder=True in the cross-attention blocks) that can be used (see past_key_values input) to speed up sequential decoding.

  • hidden_states (tuple(torch.FloatTensor), optional, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — torch.FloatTensor 的元组(一个用于嵌入层的输出,如果模型有嵌入层;+一个用于每个层的输出),形状为 (batch_size, sequence_length, hidden_size)

    模型在每个层输出的隐藏状态以及可选的初始嵌入输出。

  • attentions (tuple(torch.FloatTensor), optional, 当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — torch.FloatTensor 的元组(每个层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力 softmax 后的注意力权重,用于计算自注意力头中的加权平均值。

  • router_logits (tuple(torch.FloatTensor), 可选, 当传递 output_router_probs=Trueconfig.add_router_probs=True 时,或 config.output_router_probs=True 时返回) — 形状为 (batch_size, sequence_length, num_experts)torch.FloatTensor 元组(每一层一个)。

    由 MoE 路由器计算的原始路由器对数(softmax 后),这些术语用于计算专家混合模型的辅助损失。

The JetMoeModel forward method, overrides the __call__ special method.

虽然 forward pass 的实现需要在此函数中定义,但你应该在之后调用 Module 实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

JetMoeForCausalLM

class transformers.JetMoeForCausalLM

< >

( config )

forward

< >

( input_ids: torch.LongTensor | None = None attention_mask: torch.Tensor | None = None position_ids: torch.LongTensor | None = None past_key_values: transformers.cache_utils.Cache | None = None inputs_embeds: torch.FloatTensor | None = None labels: torch.LongTensor | None = None use_cache: bool | None = None cache_position: torch.LongTensor | None = None logits_to_keep: int | torch.Tensor = 0 output_router_logits: bool | None = False **kwargs ) transformers.modeling_outputs.MoeCausalLMOutputWithPast or tuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Input sequence tokens的索引。默认情况下将忽略填充。可以使用 AutoTokenizer 获取索引。有关详细信息,请参阅 PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.Tensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:

    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.n_positions - 1].

    什么是位置ID?

  • past_key_values (~cache_utils.Cache, optional) — Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention blocks) that can be used to speed up sequential decoding. This typically consists in the past_key_values returned by the model at a previous stage of decoding, when use_cache=True or config.use_cache=True.

    Only Cache instance is allowed as input, see our kv cache guide. If no past_key_values are passed, DynamicCache will be initialized by default.

    The model will output the same cache format that is fed as input.

    If past_key_values are used, the user is expected to input only unprocessed input_ids (those that don’t have their past key value states given to this model) of shape (batch_size, unprocessed_length) instead of all input_ids of shape (batch_size, sequence_length).

  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — Optionally, instead of passing input_ids you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert input_ids indices into associated vectors than the model’s internal embedding lookup matrix.
  • labels (torch.LongTensor of shape (batch_size, sequence_length), optional) — Labels for computing the masked language modeling loss. Indices should either be in [0, ..., config.vocab_size] or -100 (see input_ids docstring). Tokens with indices set to -100 are ignored (masked), the loss is only computed for the tokens with labels in [0, ..., config.vocab_size].
  • use_cache (bool, optional) — If set to True, past_key_values key value states are returned and can be used to speed up decoding (see past_key_values).
  • cache_position (torch.LongTensor, 形状为 (sequence_length), 可选) — 表示输入序列 token 在序列中的位置的索引。与 `position_ids` 不同,此张量不受填充的影响。它用于在正确的位置更新缓存并推断完整的序列长度。
  • logits_to_keep (Union[int, torch.Tensor], 可选, 默认为 0) — 如果是 int,则计算最后 logits_to_keep 个 token 的 logits。如果为 0,则计算所有 input_ids 的 logits(特殊情况)。仅生成需要最后一个 token 的 logits,并且仅为该 token 计算 logits 可以节省内存,这对于长序列或大词汇量来说非常可观。如果是一个 torch.Tensor,则必须是 1D 的,对应于序列长度维度中需要保留的索引。当使用打包张量格式(批次和序列长度的单维)时,这非常有用。
  • output_router_logits (bool, 可选, 默认为 False) — 是否返回所有路由器的 logits。它们对于计算路由器损失很有用,并且在推理过程中不应返回。

返回

transformers.modeling_outputs.MoeCausalLMOutputWithPasttuple(torch.FloatTensor)

一个 transformers.modeling_outputs.MoeCausalLMOutputWithPast 或一个元组 torch.FloatTensor(如果传入 return_dict=False 或当 config.return_dict=False 时),包含各种元素,具体取决于配置 (JetMoeConfig) 和输入。

  • loss (torch.FloatTensor 形状为 (1,)可选,当提供 labels 时返回) — 语言建模损失(用于下一个 token 预测)。

  • logits (形状为 (batch_size, sequence_length, config.vocab_size)torch.FloatTensor) — 语言建模头部的预测分数(SoftMax 之前的每个词汇标记的分数)。

  • aux_loss (torch.FloatTensor可选,当提供 labels 时返回) — 稀疏模块的辅助损失。

  • router_logits (tuple(torch.FloatTensor), 可选, 当传递 output_router_probs=Trueconfig.add_router_probs=True 时,或 config.output_router_probs=True 时返回) — 形状为 (batch_size, sequence_length, num_experts)torch.FloatTensor 元组(每一层一个)。

    由 MoE 路由器计算的原始路由器对数(softmax 后),这些术语用于计算专家混合模型的辅助损失。

  • past_key_values (Cache, optional, 当传递 use_cache=True 或当 config.use_cache=True 时返回) — 它是 Cache 实例。更多详情,请参阅我们的 kv cache 指南

    包含预计算的隐藏状态(自注意力块中的键和值),可用于(参见 past_key_values 输入)加速顺序解码。

  • hidden_states (tuple(torch.FloatTensor), optional, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — torch.FloatTensor 的元组(一个用于嵌入层的输出,如果模型有嵌入层;+一个用于每个层的输出),形状为 (batch_size, sequence_length, hidden_size)

    模型在每个层输出的隐藏状态以及可选的初始嵌入输出。

  • attentions (tuple(torch.FloatTensor), optional, 当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — torch.FloatTensor 的元组(每个层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力 softmax 后的注意力权重,用于计算自注意力头中的加权平均值。

JetMoeForCausalLM 的 forward 方法会覆盖 __call__ 特殊方法。

虽然 forward pass 的实现需要在此函数中定义,但你应该在之后调用 Module 实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

示例

JetMoeForSequenceClassification

class transformers.JetMoeForSequenceClassification

< >

( config )

forward

< >

( input_ids: torch.LongTensor | None = None attention_mask: torch.Tensor | None = None position_ids: torch.LongTensor | None = None past_key_values: transformers.cache_utils.Cache | None = None inputs_embeds: torch.FloatTensor | None = None labels: torch.LongTensor | None = None use_cache: bool | None = None **kwargs: typing_extensions.Unpack[transformers.utils.generic.TransformersKwargs] ) transformers.modeling_outputs.SequenceClassifierOutputWithPasttuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor, 形状为 (batch_size, sequence_length), 可选) — 词汇表中输入序列 token 的索引。填充将被忽略。

    索引可以通过 AutoTokenizer 获取。有关详细信息,请参阅 PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    输入 ID 是什么?

  • attention_mask (torch.Tensor, 形状为 (batch_size, sequence_length), 可选) — 用于避免在填充 token 索引上执行 attention 的掩码。掩码值选择在 [0, 1] 中:

    • 1 表示未掩码的 token,
    • 0 表示已掩码的 token。

    attention masks 是什么?

  • position_ids (torch.LongTensor, 形状为 (batch_size, sequence_length), 可选) — 每个输入序列 token 在位置嵌入中的位置索引。选择范围为 [0, config.n_positions - 1]

    位置 ID 是什么?

  • past_key_values (~cache_utils.Cache, 可选) — 预计算的隐藏状态(自注意力块和交叉注意力块中的键和值),可用于加速顺序解码。这通常包括模型在先前解码阶段返回的 past_key_values,当 use_cache=Trueconfig.use_cache=True 时。

    只有 Cache 实例允许作为输入,请参阅我们的 kv 缓存指南。如果未传入 past_key_values,将默认初始化 DynamicCache

    模型将输出与输入相同的缓存格式。

    如果使用 past_key_values,用户需要仅输入未处理的 input_ids(即其 past key value 状态未提供给此模型的那些),形状为 (batch_size, unprocessed_length),而不是所有 input_ids,形状为 (batch_size, sequence_length)

  • inputs_embeds (torch.FloatTensor, 形状为 (batch_size, sequence_length, hidden_size), 可选) — 可选地,您可以通过直接传入嵌入表示来代替 input_ids。如果您希望比模型内部的嵌入查找矩阵更精细地控制如何将 input_ids 索引转换为相关的向量,则这非常有用。
  • labels (torch.LongTensor, 形状为 (batch_size, sequence_length), 可选) — 用于计算掩码语言模型损失的标签。索引应在 [0, ..., config.vocab_size] 或 -100 之间(请参阅 input_ids 文档字符串)。值为 -100 的 token 将被忽略(掩码),损失仅为标签在 [0, ..., config.vocab_size] 范围内的 token 计算。
  • use_cache (bool, 可选) — 如果设置为 True,则返回 past_key_values 键值状态,可用于加速解码(请参阅 past_key_values)。

返回

transformers.modeling_outputs.SequenceClassifierOutputWithPasttuple(torch.FloatTensor)

A transformers.modeling_outputs.SequenceClassifierOutputWithPast or a tuple of torch.FloatTensor (if return_dict=False is passed or when config.return_dict=False) comprising various elements depending on the configuration (None) and inputs.

  • loss (形状为 (1,)torch.FloatTensor可选,当提供 labels 时返回) — 分类损失(如果 config.num_labels==1,则为回归损失)。

  • logits (形状为 (batch_size, config.num_labels)torch.FloatTensor) — 分类(如果 config.num_labels==1,则为回归)分数(SoftMax 之前)。

  • past_key_values (Cache, optional, 当传递 use_cache=True 或当 config.use_cache=True 时返回) — 它是 Cache 实例。更多详情,请参阅我们的 kv cache 指南

    包含预计算的隐藏状态(自注意力块中的键和值),可用于(参见 past_key_values 输入)加速顺序解码。

  • hidden_states (tuple(torch.FloatTensor), optional, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — torch.FloatTensor 的元组(一个用于嵌入层的输出,如果模型有嵌入层;+一个用于每个层的输出),形状为 (batch_size, sequence_length, hidden_size)

    模型在每个层输出的隐藏状态以及可选的初始嵌入输出。

  • attentions (tuple(torch.FloatTensor), optional, 当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — torch.FloatTensor 的元组(每个层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力 softmax 后的注意力权重,用于计算自注意力头中的加权平均值。

The GenericForSequenceClassification forward method, overrides the __call__ special method.

虽然 forward pass 的实现需要在此函数中定义,但你应该在之后调用 Module 实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.