Transformers 文档

故障排除

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

故障排除

有时会发生错误,但我们在这里提供帮助!本指南涵盖了我们遇到的一些最常见问题以及如何解决这些问题。但是,本指南并非旨在全面收集每个 🤗 Transformers 问题。如需获得有关问题故障排除的更多帮助,请尝试

  1. 论坛上寻求帮助。您可以将问题发布到特定类别,例如初学者🤗 Transformers。请确保您撰写一篇描述性强的论坛帖子,并附上一些可重现的代码,以最大限度地提高您的问题得到解决的可能性!
  1. 如果问题与库相关的错误,请在 🤗 Transformers 仓库上创建一个 Issue。请尝试包含尽可能多的信息来描述该错误,以帮助我们更好地找出问题所在以及我们如何修复它。

  2. 如果您使用的是旧版本的 🤗 Transformers,请查看迁移指南,因为版本之间引入了一些重要更改。

有关故障排除和获得帮助的更多详细信息,请查看 Hugging Face 课程的第 8 章

防火墙环境

云端和内网设置上的一些 GPU 实例受到防火墙保护,无法进行外部连接,从而导致连接错误。当您的脚本尝试下载模型权重或数据集时,下载将挂起,然后超时并显示以下消息

ValueError: Connection error, and we cannot find the requested files in the cached path.
Please try again or make sure your Internet connection is on.

在这种情况下,您应该尝试在离线模式下运行 🤗 Transformers,以避免连接错误。

CUDA 内存不足

在没有适当硬件的情况下,训练具有数百万参数的大型模型可能具有挑战性。当 GPU 内存耗尽时,您可能会遇到的常见错误是

CUDA out of memory. Tried to allocate 256.00 MiB (GPU 0; 11.17 GiB total capacity; 9.70 GiB already allocated; 179.81 MiB free; 9.85 GiB reserved in total by PyTorch)

以下是一些您可以尝试的潜在解决方案,以减少内存使用

有关节省内存技术的更多详细信息,请参阅性能指南

无法加载已保存的 TensorFlow 模型

TensorFlow 的 model.save 方法会将整个模型(架构、权重、训练配置)保存在单个文件中。但是,当您再次加载模型文件时,您可能会遇到错误,因为 🤗 Transformers 可能无法加载模型文件中的所有 TensorFlow 相关对象。为避免保存和加载 TensorFlow 模型时出现问题,我们建议您

>>> from transformers import TFPreTrainedModel
>>> from tensorflow import keras

>>> model.save_weights("some_folder/tf_model.h5")
>>> model = TFPreTrainedModel.from_pretrained("some_folder")
  • 使用 ~TFPretrainedModel.save_pretrained 保存模型,然后使用 from_pretrained() 再次加载模型
>>> from transformers import TFPreTrainedModel

>>> model.save_pretrained("path_to/model")
>>> model = TFPreTrainedModel.from_pretrained("path_to/model")

ImportError

您可能会遇到的另一个常见错误,尤其是在模型是新发布的情况下,是 ImportError

ImportError: cannot import name 'ImageGPTImageProcessor' from 'transformers' (unknown location)

对于这些错误类型,请检查以确保您已安装最新版本的 🤗 Transformers,以便访问最新的模型

pip install transformers --upgrade

CUDA 错误:设备端断言被触发

有时您可能会遇到关于设备代码中错误的通用 CUDA 错误。

RuntimeError: CUDA error: device-side assert triggered

您应该首先尝试在 CPU 上运行代码以获得更具描述性的错误消息。在代码开头添加以下环境变量以切换到 CPU

>>> import os

>>> os.environ["CUDA_VISIBLE_DEVICES"] = ""

另一种选择是从 GPU 获取更好的回溯信息。在代码开头添加以下环境变量,以使回溯指向错误源

>>> import os

>>> os.environ["CUDA_LAUNCH_BLOCKING"] = "1"

当填充 token 未被掩码时输出不正确

在某些情况下,如果 input_ids 包含填充 token,则输出 hidden_state 可能不正确。为了演示这一点,加载一个模型和 tokenizer。您可以访问模型的 pad_token_id 以查看其值。对于某些模型,pad_token_id 可能是 None,但您可以随时手动设置它。

>>> from transformers import AutoModelForSequenceClassification
>>> import torch

>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-uncased")
>>> model.config.pad_token_id
0

以下示例显示了在不掩码填充 token 的情况下的输出

>>> input_ids = torch.tensor([[7592, 2057, 2097, 2393, 9611, 2115], [7592, 0, 0, 0, 0, 0]])
>>> output = model(input_ids)
>>> print(output.logits)
tensor([[ 0.0082, -0.2307],
        [ 0.1317, -0.1683]], grad_fn=<AddmmBackward0>)

这是第二个序列的实际输出

>>> input_ids = torch.tensor([[7592]])
>>> output = model(input_ids)
>>> print(output.logits)
tensor([[-0.1008, -0.4061]], grad_fn=<AddmmBackward0>)

大多数情况下,您应该向模型提供 attention_mask 以忽略填充 token,从而避免此静默错误。现在,第二个序列的输出与其真实输出匹配

默认情况下,tokenizer 会根据您特定 tokenizer 的默认值为您创建一个 attention_mask

>>> attention_mask = torch.tensor([[1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0]])
>>> output = model(input_ids, attention_mask=attention_mask)
>>> print(output.logits)
tensor([[ 0.0082, -0.2307],
        [-0.1008, -0.4061]], grad_fn=<AddmmBackward0>)

🤗 Transformers 不会自动创建 attention_mask 来掩码提供的填充 token,因为

  • 某些模型没有填充 token。
  • 对于某些用例,用户希望模型关注填充 token。

ValueError: AutoModel 此类模型无法识别配置类 XYZ

通常,我们建议使用 AutoModel 类来加载模型的预训练实例。此类可以基于配置自动推断和加载给定检查点的正确架构。如果您在从检查点加载模型时看到此 ValueError,则表示 Auto 类无法找到给定检查点中配置到您尝试加载的模型类型的映射。最常见的情况是检查点不支持给定的任务。例如,您将在以下示例中看到此错误,因为没有用于问答的 GPT2

>>> from transformers import AutoProcessor, AutoModelForQuestionAnswering

>>> processor = AutoProcessor.from_pretrained("openai-community/gpt2-medium")
>>> model = AutoModelForQuestionAnswering.from_pretrained("openai-community/gpt2-medium")
ValueError: Unrecognized configuration class <class 'transformers.models.gpt2.configuration_gpt2.GPT2Config'> for this kind of AutoModel: AutoModelForQuestionAnswering.
Model type should be one of AlbertConfig, BartConfig, BertConfig, BigBirdConfig, BigBirdPegasusConfig, BloomConfig, ...
< > 在 GitHub 上更新