Transformers 文件

故障排除

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

故障排除

有時會發生錯誤,但我們隨時提供幫助!本指南涵蓋了我們遇到的一些最常見問題以及如何解決這些問題。然而,本指南並非旨在全面收集所有 🤗 Transformers 問題。如需更多故障排除幫助,請嘗試

  1. 論壇上尋求幫助。您可以將問題釋出到特定類別,例如初學者🤗 Transformers。請務必撰寫一篇描述清晰的論壇帖子,並提供一些可復現的程式碼,以最大程度地解決您的問題!
  1. 如果問題與庫相關,請在 🤗 Transformers 倉庫中建立問題。儘量包含儘可能多的資訊來描述 bug,以幫助我們更好地找出問題所在以及如何修復它。

  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"

當填充標記未被掩碼時輸出不正確

在某些情況下,如果 input_ids 包含填充標記,則輸出的 hidden_state 可能不正確。為了演示,載入一個模型和分詞器。您可以訪問模型的 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

以下示例顯示了未遮蔽填充標記的輸出

>>> 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 以忽略填充標記,從而避免這種靜默錯誤。現在,第二個序列的輸出與其實際輸出匹配了。

預設情況下,分詞器會根據您特定分詞器的預設設定為您建立 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 來遮蔽填充標記,如果提供了填充標記,原因如下:

  • 有些模型沒有填充標記。
  • 對於某些用例,使用者希望模型關注填充標記。

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 上更新

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