Transformers 文档

GraniteMoe

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

GraniteMoe

PyTorch FlashAttention SDPA

概述

GraniteMoe 模型在 Power Scheduler: A Batch Size and Token Number Agnostic Learning Rate Scheduler 中被提出,作者是 Yikang Shen、Matthew Stallone、Mayank Mishra、Gaoyuan Zhang、Shawn Tan、Aditya Prasad、Adriana Meza Soria、David D. Cox 和 Rameswar Panda。

PowerMoE-3B 是一个 3B 稀疏混合专家 (sMoE) 语言模型,使用 Power 学习率调度器进行训练。它为每个 token 稀疏激活 8 亿个参数。它在开源和专有数据集的混合数据上进行训练。PowerMoE-3B 在各种基准测试中,包括自然语言多项选择、代码生成和数学推理,都显示出与激活参数为 2 倍的其他密集模型相比,具有令人鼓舞的结果。

以下是论文的摘要

为语言模型预训练找到最佳学习率是一项具有挑战性的任务。这不仅是因为学习率、批次大小、训练 token 数量、模型大小和其他超参数之间存在复杂的关联,还因为对具有数十亿或数万亿参数的大型语言模型执行超参数搜索的成本过高。最近的研究提出使用小型代理模型和小语料库来执行超参数搜索,并将最佳参数转移到大型模型和大型语料库。虽然对于模型大小相关的超参数(如深度和宽度),零样本可迁移性在理论和经验上都得到了证明,但从小语料库到大语料库的零样本迁移性仍未得到充分探索。在本文中,我们研究了最近提出的 WSD 调度器的最佳学习率、批次大小和训练 token 数量之间的相关性。经过数千次小型实验,我们发现了变量之间的幂律关系,并证明了其在不同模型大小之间的可迁移性。基于此观察,我们提出了一种新的学习率调度器 Power 调度器,它与训练 token 数量和批次大小无关。实验表明,将 Power 调度器与最大更新参数化 (\mup) 相结合,无论训练 token 数量、批次大小、模型大小,甚至模型架构如何,都可以始终如一地以一组超参数实现令人印象深刻的性能。我们使用 Power 调度器训练的 3B 密集和 MoE 模型实现了与最先进的小型语言模型相当的性能。我们开源了这些预训练模型。

提示

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_path = "ibm/PowerMoE-3b"
tokenizer = AutoTokenizer.from_pretrained(model_path)

# drop device_map if running on CPU
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
model.eval()

# change input text as desired
prompt = "Write a code to find the maximum value in a list of numbers."

# tokenize the text
input_tokens = tokenizer(prompt, return_tensors="pt")
# generate output tokens
output = model.generate(**input_tokens, max_new_tokens=100)
# decode output tokens into text
output = tokenizer.batch_decode(output)
# loop over the batch to print, in this example the batch size is 1
for i in output:
    print(i)

此模型由 mayank-mishra 贡献。

GraniteMoeConfig

class transformers.GraniteMoeConfig

< >

( vocab_size = 32000 hidden_size = 4096 intermediate_size = 11008 num_hidden_layers = 32 num_attention_heads = 32 num_key_value_heads = None hidden_act = 'silu' max_position_embeddings = 2048 initializer_range = 0.02 rms_norm_eps = 1e-06 use_cache = True pad_token_id = None bos_token_id = 1 eos_token_id = 2 tie_word_embeddings = False rope_theta = 10000.0 rope_scaling = None attention_bias = False attention_dropout = 0.0 embedding_multiplier = 1.0 logits_scaling = 1.0 residual_multiplier = 1.0 attention_multiplier = 1.0 num_local_experts = 8 num_experts_per_tok = 2 output_router_logits = False router_aux_loss_coef = 0.001 **kwargs )

参数

  • vocab_size (int, optional, 默认值为 32000) — GraniteMoe 模型的词汇表大小。 定义了在调用 GraniteMoeModel 时传递的 inputs_ids 可以表示的不同 tokens 的数量。
  • hidden_size (int, optional, 默认值为 4096) — 隐藏层表示的维度。
  • intermediate_size (int, optional, 默认值为 11008) — MLP 表示的维度。
  • num_hidden_layers (int, optional, 默认值为 32) — Transformer 解码器中隐藏层的数量。
  • num_attention_heads (int, optional, 默认值为 32) — Transformer 解码器中每个注意力层的注意力头数。
  • num_key_value_heads (int, optional) — 这是用于实现分组查询注意力(Grouped Query Attention)的 key_value 头的数量。如果 num_key_value_heads=num_attention_heads,模型将使用多头注意力(Multi Head Attention,MHA);如果 num_key_value_heads=1,模型将使用多查询注意力(Multi Query Attention,MQA);否则将使用 GQA。当将多头检查点转换为 GQA 检查点时,每个组 key 和 value 头应通过平均池化该组内的所有原始头来构建。有关更多详细信息,请查看这篇论文。如果未指定,则默认为 num_attention_heads
  • hidden_act (strfunction, optional, 默认值为 "silu") — 解码器中的非线性激活函数(函数或字符串)。
  • max_position_embeddings (int, optional, 默认值为 2048) — 此模型可能使用的最大序列长度。
  • initializer_range (float, optional, 默认值为 0.02) — 用于初始化所有权重矩阵的 truncated_normal_initializer 的标准差。
  • rms_norm_eps (float, optional, 默认值为 1e-06) — rms 归一化层使用的 epsilon 值。
  • use_cache (bool, optional, 默认值为 True) — 模型是否应返回最后一次的 key/values 注意力(并非所有模型都使用)。 仅当 config.is_decoder=True 时相关。
  • pad_token_id (int, optional) — 填充 token id。
  • bos_token_id (int, optional, 默认值为 1) — 流开始 token id。
  • eos_token_id (int, optional, 默认值为 2) — 流结束 token id。
  • tie_word_embeddings (bool, optional, 默认值为 False) — 是否绑定词嵌入权重
  • rope_theta (float, optional, 默认值为 10000.0) — RoPE 嵌入的基础周期。
  • rope_scaling (Dict, optional) — 包含 RoPE 嵌入缩放配置的字典。 目前支持两种缩放策略:linear 和 dynamic。 它们的缩放因子必须是大于 1 的浮点数。 预期格式为 {"type": strategy name, "factor": scaling factor}。 使用此标志时,请勿将 max_position_embeddings 更新为预期的新最大值。 有关这些缩放策略如何运作的更多信息,请参见以下 thread:https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/。 这是一个实验性功能,未来版本中可能会有破坏性的 API 更改。
  • attention_bias (bool, optional, 默认值为 False) — 是否在自注意力期间在 query、key、value 和输出投影层中使用 bias。
  • attention_dropout (float, optional, 默认值为 0.0) — 注意力概率的 dropout 比率。
  • embedding_multiplier (float, optional, 默认值为 1.0) — 嵌入乘数
  • logits_scaling (float, optional, 默认值为 1.0) — 输出 logits 的除数
  • residual_multiplier (float, optional, 默认值为 1.0) — 残差乘数
  • attention_multiplier (float, optional, 默认值为 1.0) — 注意力乘数
  • num_local_experts (int, optional, 默认值为 8) — experts 总数
  • num_experts_per_tok (int, optional, defaults to 2) — 每个 token 的专家数量 (int, 可选,默认为 2)
  • output_router_logits (bool, optional, defaults to False) — 是否模型应返回路由器 logits (bool, 可选,默认为 False) — 是否模型应返回路由器 logits。启用此选项还将允许模型输出辅助损失。
  • router_aux_loss_coef (float, optional, defaults to 0.001) — 路由器辅助损失系数 (float, 可选,默认为 0.001)

This is the configuration class to store the configuration of a GraniteMoeModel. 这是用于存储 GraniteMoeModel 配置的配置类。It is used to instantiate an GraniteMoe model according to the specified arguments, defining the model architecture. 它用于根据指定的参数实例化 GraniteMoe 模型,定义模型架构。Instantiating a configuration with the defaults will yield a similar configuration to that of the GraniteMoe-3B. 使用默认值实例化配置将产生与 GraniteMoe-3B 类似的配置。

Configuration objects inherit from PretrainedConfig and can be used to control the model outputs. 配置对象继承自 PretrainedConfig,可用于控制模型输出。Read the documentation from PretrainedConfig for more information. 有关更多信息,请阅读 PretrainedConfig 的文档。

>>> from transformers import GraniteMoeModel, GraniteMoeConfig

>>> # Initializing a GraniteMoe granitemoe-3b style configuration
>>> configuration = GraniteMoeConfig()

>>> # Initializing a model from the granitemoe-7b style configuration
>>> model = GraniteMoeModel(configuration)

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

GraniteMoeModel

class transformers.GraniteMoeModel

< >

( config: GraniteMoeConfig )

参数

  • config (GraniteMoeConfig) — 具有模型所有参数的模型配置类。 (GraniteMoeConfig) — 具有模型所有参数的模型配置类。Initializing with a config file does not load the weights associated with the model, only the configuration. 使用配置文件初始化不会加载与模型关联的权重,仅加载配置。Check out the from_pretrained() method to load the model weights. 查看 from_pretrained() 方法以加载模型权重。
  • config — GraniteMoeConfig

The bare GraniteMoe Model outputting raw hidden-states without any specific head on top. 裸 GraniteMoe 模型输出原始隐藏状态,顶部没有任何特定的头。This model inherits from PreTrainedModel. 此模型继承自 PreTrainedModel。Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.) 查看超类文档,了解库为所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、剪枝头等)。

This model is also a PyTorch torch.nn.Module subclass. 此模型也是 PyTorch torch.nn.Module 子类。Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior. 将其用作常规 PyTorch 模块,并参阅 PyTorch 文档,了解与一般用法和行为相关的所有事项。

Transformer decoder consisting of config.num_hidden_layers layers. Transformer 解码器由 config.num_hidden_layers 层组成。Each layer is a GraniteMoeDecoderLayer 每层都是一个 GraniteMoeDecoderLayer

forward

< >

( input_ids: LongTensor = None attention_mask: typing.Optional[torch.Tensor] = None position_ids: typing.Optional[torch.LongTensor] = None past_key_values: typing.Union[transformers.cache_utils.Cache, typing.List[torch.FloatTensor], NoneType] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None use_cache: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None output_router_logits: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None cache_position: typing.Optional[torch.LongTensor] = None )

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — 词汇表中输入序列 tokens 的索引。 (torch.LongTensor,形状为 (batch_size, sequence_length)) — 词汇表中输入序列 tokens 的索引。如果您提供填充,默认情况下将忽略填充。

    Indices can be obtained using AutoTokenizer. 索引可以使用 AutoTokenizer 获得。See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details. 有关详细信息,请参阅 PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    What are input IDs? 什么是输入 ID?

  • attention_mask (torch.Tensor of shape (batch_size, sequence_length), optional) — 掩码,以避免在填充 token 索引上执行注意力。 (torch.Tensor,形状为 (batch_size, sequence_length), 可选) — 掩码,以避免在填充 token 索引上执行注意力。Mask values selected in [0, 1]: 掩码值在 [0, 1] 中选择:

    • 1 for tokens that are not masked,
    • 1 表示 未被掩码 的 tokens,
    • 0 for tokens that are masked.
    • 0 表示 被掩码 的 tokens。

    What are attention masks?什么是注意力掩码?

    Indices can be obtained using AutoTokenizer. 索引可以使用 AutoTokenizer 获得。See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details. 有关详细信息,请参阅 PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    If past_key_values is used, optionally only the last input_ids have to be input (see past_key_values). 如果使用 past_key_values,则可以选择仅输入最后一个 input_ids(请参阅 past_key_values)。

    If you want to change padding behavior, you should read modeling_opt._prepare_decoder_attention_mask and modify to your needs. 如果您想更改填充行为,您应该阅读 modeling_opt._prepare_decoder_attention_mask 并根据您的需要进行修改。See diagram 1 in the paper for more information on the default strategy. 有关默认策略的更多信息,请参见 论文 中的图 1。

    • 1 indicates the head is not masked,
    • 1 表示 head 是 未被掩码 的,
    • 0 indicates the head is masked.
    • 0 表示 head 是 被掩码 的。
  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — 位置嵌入中每个输入序列 tokens 的位置索引。 (torch.LongTensor,形状为 (batch_size, sequence_length), 可选) — 位置嵌入中每个输入序列 tokens 的位置索引。Selected in the range [0, config.n_positions - 1]. 在范围 [0, config.n_positions - 1] 中选择。

    What are position IDs? 什么是位置 ID?

  • past_key_values (Cache or tuple(tuple(torch.FloatTensor)), optional) — 预计算的隐藏状态(自注意力模块和交叉注意力模块中的键和值),可用于加速顺序解码。 (Cachetuple(tuple(torch.FloatTensor)), 可选) — 预计算的隐藏状态(自注意力模块和交叉注意力模块中的键和值),可用于加速顺序解码。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. 这通常包括模型在解码的先前阶段返回的 past_key_values,当 use_cache=Trueconfig.use_cache=True 时。

    Two formats are allowed: 允许两种格式:

    • a Cache instance;
    • Cache 实例;
    • Tuple of tuple(torch.FloatTensor) of length config.n_layers, with each tuple having 2 tensors of shape (batch_size, num_heads, sequence_length, embed_size_per_head)).
    • 长度为 config.n_layerstuple(torch.FloatTensor) 元组,其中每个元组都有 2 个形状为 (batch_size, num_heads, sequence_length, embed_size_per_head) 的张量)。

    This is also known as the legacy cache format. 这也称为旧版缓存格式。The model will output the same cache format that is fed as input. 模型将输出与输入相同的缓存格式。If no past_key_values are passed, the legacy cache format will be returned. 如果未传递 past_key_values,则将返回旧版缓存格式。

    If past_key_values are used, the user can optionally input only the last input_ids (those that don’t have their past key value states given to this model) of shape (batch_size, 1) instead of all input_ids of shape (batch_size, sequence_length). 如果使用 past_key_values,用户可以选择仅输入最后一个 input_ids(那些没有将其过去键值状态提供给此模型的 input_ids),形状为 (batch_size, 1),而不是所有形状为 (batch_size, sequence_length)input_ids

  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递 input_ids。 (torch.FloatTensor,形状为 (batch_size, sequence_length, hidden_size), 可选) — 可选地,您可以选择直接传递嵌入表示,而不是传递 input_ids。如果您希望比模型的内部嵌入查找矩阵更精细地控制如何将 input_ids 索引转换为关联向量,这将非常有用。
  • use_cache (bool, optional) — 如果设置为 True,则返回 past_key_values 键值状态,并且可以用于加速解码(请参阅 past_key_values)。 (bool, 可选) — 如果设置为 True,则返回 past_key_values 键值状态,并且可以用于加速解码(请参阅 past_key_values)。
  • output_attentions (bool, optional) — 是否返回所有注意力层的注意力张量。 (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的 attentions
  • output_hidden_states (bool, optional) — 是否返回所有层的隐藏状态。 (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的 hidden_states
  • return_dict (bool, optional) — 是否返回 ModelOutput 而不是普通元组。 (bool, 可选) — 是否返回 ModelOutput 而不是普通元组。
  • cache_position (torch.LongTensor of shape (sequence_length), optional) — 索引,描述输入序列 tokens 在序列中的位置。 (torch.LongTensor,形状为 (sequence_length), 可选) — 索引,描述输入序列 tokens 在序列中的位置。Contrarily to position_ids, this tensor is not affected by padding. 与 position_ids 相反,此张量不受填充的影响。It is used to update the cache in the correct position and to infer the complete sequence length. 它用于在正确的位置更新缓存并推断完整的序列长度。

The GraniteMoeModel forward method, overrides the __call__ special method. GraniteMoeModel 的 forward 方法,覆盖了 __call__ 特殊方法。

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them. 虽然 forward 传递的配方需要在该函数中定义,但是应该在之后调用 Module 实例而不是此函数,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

GraniteMoeForCausalLM

class transformers.GraniteMoeForCausalLM

< >

( config: GraniteMoeConfig )

forward

< >

( input_ids: LongTensor = None attention_mask: typing.Optional[torch.Tensor] = None position_ids: typing.Optional[torch.LongTensor] = None past_key_values: typing.Union[transformers.cache_utils.Cache, typing.List[torch.FloatTensor], NoneType] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None labels: typing.Optional[torch.LongTensor] = None use_cache: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None output_router_logits: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None cache_position: typing.Optional[torch.LongTensor] = None **kwargs ) transformers.modeling_outputs.MoeCausalLMOutputWithPast or tuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — 词汇表中输入序列 tokens 的索引。默认情况下,如果您提供 padding,则会被忽略。

    索引可以使用 AutoTokenizer 获得。 有关详细信息,请参阅 PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入 IDs?

  • attention_mask (torch.Tensor of shape (batch_size, sequence_length), optional) — 用于避免在 padding token 索引上执行 attention 的掩码。掩码值在 [0, 1] 中选择:

    • 1 表示 未被掩码 的 tokens,
    • 0 表示 被掩码 的 tokens。

    什么是 attention 掩码?

    索引可以使用 AutoTokenizer 获得。 有关详细信息,请参阅 PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    如果使用 past_key_values,则可以选择仅输入最后一个 input_ids(请参阅 past_key_values)。

    如果您想更改 padding 行为,您应该阅读 modeling_opt._prepare_decoder_attention_mask 并根据您的需要进行修改。 有关默认策略的更多信息,请参见 论文 中的图 1。

    • 1 表示 head 未被掩码
    • 0 表示 head 被掩码
  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — 位置嵌入中每个输入序列 tokens 的位置索引。在范围 [0, config.n_positions - 1] 中选择。

    什么是位置 IDs?

  • past_key_values (Cache or tuple(tuple(torch.FloatTensor)), optional) — 预先计算的 hidden-states(self-attention 块和 cross-attention 块中的 key 和 values),可用于加速顺序解码。 这通常包括模型在先前解码阶段返回的 past_key_values,当 use_cache=Trueconfig.use_cache=True 时。

    允许两种格式:

    • Cache 实例;
    • 长度为 config.n_layerstuple(torch.FloatTensor) 元组,每个元组包含 2 个形状为 (batch_size, num_heads, sequence_length, embed_size_per_head)) 的 tensors。 这也称为传统缓存格式。

    模型将输出与作为输入馈送的缓存格式相同的格式。 如果未传递 past_key_values,则将返回传统缓存格式。

    如果使用 past_key_values,用户可以选择仅输入最后一个 input_ids(那些没有将其 past key value states 提供给此模型的 input_ids),形状为 (batch_size, 1),而不是所有形状为 (batch_size, sequence_length)input_ids

  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递 input_ids。 如果您希望比模型的内部嵌入查找矩阵更精细地控制如何将 input_ids 索引转换为关联的向量,这将非常有用。
  • use_cache (bool, optional) — 如果设置为 True,则返回 past_key_values key value states,并可用于加速解码(请参阅 past_key_values)。
  • output_attentions (bool, optional) — 是否返回所有 attention 层的 attentions tensors。 有关更多详细信息,请参见返回的 tensors 下的 attentions
  • output_hidden_states (bool, optional) — 是否返回所有层的 hidden states。 有关更多详细信息,请参见返回的 tensors 下的 hidden_states
  • return_dict (bool, optional) — 是否返回 ModelOutput 而不是纯 tuple。
  • cache_position (torch.LongTensor of shape (sequence_length), optional) — 描述输入序列 tokens 在序列中位置的索引。 与 position_ids 相反,此 tensor 不受 padding 的影响。 它用于在正确的位置更新缓存并推断完整的序列长度。
  • labels (torch.LongTensor of shape (batch_size, sequence_length), optional) — 用于计算 masked language modeling loss 的标签。 索引应为 [0, ..., config.vocab_size] 或 -100(请参阅 input_ids 文档字符串)。 索引设置为 -100 的 tokens 将被忽略(掩码),loss 仅针对标签在 [0, ..., config.vocab_size] 中的 tokens 计算。

返回

transformers.modeling_outputs.MoeCausalLMOutputWithPasttuple(torch.FloatTensor)

一个 transformers.modeling_outputs.MoeCausalLMOutputWithPasttorch.FloatTensor 的 tuple (如果传递 return_dict=False 或当 config.return_dict=False 时),其中包含各种元素,具体取决于配置 (GraniteMoeConfig) 和输入。

  • loss (torch.FloatTensor of shape (1,), optional, 当提供 labels 时返回) — 语言建模 loss(用于下一个 token 预测)。

  • logits (torch.FloatTensor of shape (batch_size, sequence_length, config.vocab_size)) — 语言建模 head 的预测分数(SoftMax 之前每个词汇表 token 的分数)。

  • aux_loss (torch.FloatTensor, optional, 当提供 labels 时返回) — 稀疏模块的 aux_loss。

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

    MoE routers 计算的原始 router logits (post-softmax),这些术语用于计算 Mixture of Experts 模型的辅助 loss。

  • past_key_values (tuple(tuple(torch.FloatTensor)), optional, 当传递 use_cache=True 或当 config.use_cache=True 时返回) — 长度为 config.n_layerstuple(torch.FloatTensor) 元组,每个元组包含 2 个形状为 (batch_size, num_heads, sequence_length, embed_size_per_head)) 的 tensors

    包含预先计算的 hidden-states(self-attention 块中的 key 和 values),这些 hidden-states 可以使用(请参阅 past_key_values 输入)来加速顺序解码。

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

    模型在每一层输出端的 Hidden-states,以及可选的初始 embedding 输出。

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

    attention softmax 之后的 Attentions weights,用于计算 self-attention heads 中的加权平均值。

GraniteMoeForCausalLM forward 方法,覆盖了 __call__ 特殊方法。

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them. 虽然 forward 传递的配方需要在该函数中定义,但是应该在之后调用 Module 实例而不是此函数,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

示例

>>> from transformers import AutoTokenizer, GraniteMoeForCausalLM

>>> model = GraniteMoeForCausalLM.from_pretrained("ibm/PowerMoE-3b")
>>> tokenizer = AutoTokenizer.from_pretrained("ibm/PowerMoE-3b")

>>> prompt = "Hey, are you conscious? Can you talk to me?"
>>> inputs = tokenizer(prompt, return_tensors="pt")

>>> # Generate
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
< > 在 GitHub 上更新