You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Huggingface EncoderDecoderModel的generate()方法仅输出bos-token的配置问题求助

Huggingface EncoderDecoderModel的generate()方法仅输出bos-token的配置问题求助

我目前在基于Huggingface Transformers搭建一个EncoderDecoderModel,结构是冻结的ModernBERT编码器(使用answerdotai-ModernBERT-base预训练模型)搭配可训练的GPT2解码器。因为编码器和解码器架构不同,我分别给它们配了对应的tokenizer:编码器用ModernBERT自带的tokenizer,解码器则是我自己训练的自定义tokenizer。

训练阶段的情况(一切正常)

训练过程中,我会在评估环节用forward()方法做样本解码,监控训练进度,核心代码如下:

outputs = self.model.forward(
    input_ids=x, 
    attention_mask=m, 
    labels=y, 
    decoder_input_ids=y
)
example_logits = outputs.logits[0]
predicted_token_ids = torch.argmax(example_logits, dim=-1)
decoded_prediction = self.decoder_tokenizer.decode(predicted_token_ids)

能明显看到生成的序列效果越来越好,说明模型训练是有效的。

推理阶段的问题(generate()方法失效)

但到了推理环节,当我改用generate()方法生成结果时,输出全是bos-token,就像模型完全没训练过一样。我的推理代码是这样的:

output = model.generate(
    inputs=input_ids, 
    attention_mask=attention_mask, 
    decoder_start_token_id=decoder_tokenizer.bos_token_id,
    streamer=text_streamer,
)
# 要是我换回forward()方法,结果又回到训练时的优秀水平:
# outputs = model.forward(
#     input_ids=input_ids,
#     attention_mask=attention_mask,
#     labels=decoder_input_ids,
#     decoder_input_ids=decoder_input_ids,
# )
# output = outputs.logits.argmax(dim=-1)
print("Output: ", output)
output = decoder_tokenizer.decode(output.squeeze())

最奇怪的是,如果我在推理时不用generate(),而是继续用forward()方法解码,结果就和训练时一样好——这说明模型权重确实训练到位了,问题应该出在generate()方法的配置上。

我已经排查过的点

  • 确认了两个tokenizer的特殊token(bos、eos等)都设置正确,并且已经把这些特殊token同步到了模型的Config中,但没有效果;
  • 测试过未训练的初始模型,发现forward()和generate()的输出也存在差异,说明不是训练过程的问题;
  • 怀疑过是不是teacher forcing的机制差异导致的,但不确定该怎么调整generate()的配置来匹配训练时的行为。

附上我的EncoderDecoderConfig示例供参考:

{
  "architectures": [ "EncoderDecoderModel" ],
  "decoder": {
    "activation_function": "gelu_new",
    "add_cross_attention": true,
    "attn_pdrop": 0.1,
    "bos_token_id": 1,
    "embd_pdrop": 0.1,
    "eos_token_id": 2,
    "initializer_range": 0.02,
    "is_decoder": true,
    "layer_norm_epsilon": 1e-05,
    "model_type": "gpt2",
    "n_embd": 768,
    "n_head": 4,
    "n_inner": null,
    "n_layer": 24,
    "n_positions": 4048,
    "reorder_and_upcast_attn": false,
    "resid_pdrop": 0.1,
    "scale_attn_by_inverse_layer_idx": false,
    "scale_attn_weights": true,
    "summary_activation": null,
    "summary_first_dropout": 0.1,
    "summary_proj_to_labels": true,
    "summary_type": "cls_index",
    "summary_use_proj": true,
    "torch_dtype": "float32",
    "use_cache": true,
    "vocab_size": 1024
  },
  "encoder": {
    "_name_or_path": "/mnt/models/pretrained/answerdotai-ModernBERT-base",
    "architectures": [ "ModernBertModel" ],
    "attention_bias": false,
    "attention_dropout": 0.0,
    "classifier_activation": "gelu",
    "classifier_bias": false,
    "classifier_dropout": 0.0,
    "classifier_pooling": "mean",
    "cls_token_id": 50281,
    "decoder_bias": true,
    "deterministic_flash_attn": false,
    "embedding_dropout": 0.0,
    "global_attn_every_n_layers": 3,
    "global_rope_theta": 160000.0,
    "gradient_checkpointing": false,
    "hidden_activation": "gelu",
    "hidden_size": 768,
    "initializer_cutoff_factor": 2.0,
    "initializer_range": 0.02,
    "intermediate_size": 1152,
    "layer_norm_eps": 1e-05,
    "local_attention": 128,
    "local_rope_theta": 10000.0,
    "max_position_embeddings": 8192,
    "mlp_bias": false,
    "mlp_dropout": 0.0,
    "model_type": "modernbert",
    "norm_bias": false,
    "norm_eps": 1e-05,
    "num_attention_heads": 12,
    "num_hidden_layers": 22,
    "position_embedding_type": "absolute",
    "repad_logits_with_grad": false,
    "sparse_pred_ignore_index": -100,
    "sparse_prediction": false,
    "torch_dtype": "float32",
    "vocab_size": 50368
  },
  "is_encoder_decoder": true,
  "model_type": "encoder-decoder",
  "torch_dtype": "float32",
  "transformers_version": "4.52.4"
}

有没有朋友遇到过类似的问题?是不是我在generate()方法里漏掉了什么关键参数,或者模型Config还有没配置对的地方?


内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 03:13:06