Closed1

hf_hub_download の失敗を回避するtips

bilzardbilzard

大量のファイルからなるデータセットをダウンロードしていると、頻繁に以下のようなエラーが出て失敗する。

Exception: An error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Internet connection is on.

手元で確認した限りでは、100MB x ~700個のファイルをダウンロードした場合、数回に1回の確率で失敗する(つまり、失敗する確率は O(0.001)~O(0.01) 程度)。

etag_timeout=60にする

https://huggingface.co/docs/huggingface_hub/package_reference/file_download#huggingface_hub.hf_hub_download

この設定のみでは問題は解消しなかった。

リトライロジックを実装する

import time

def retry_task(task, max_retries=10, wait_sec=60, **args):
    attempt = 0
    while attempt < max_retries:
        try:
            task(**args)
            return
        except Exception as e:
            attempt += 1
            print(f"Failed {attempt}-th attampt. Wait {wait_sec} sec. Exception: {e}")
            if attempt < max_retries:
                time.sleep(wait_sec)  # 10秒待機

                
if __name__ == "__main__":
    import numpy as np

    def dummy_task():
        failed = np.random.choice([True, False])
        if failed:
            raise Exception("Failed!!!")

    retry_task(dummy_task, wait_sec=1)
def download_single(filename, max_retry=10, wait_sec=60):
    retry_task(
        hf_hub_download,
        repo_id="(repo name)",
        repo_type="dataset",
        filename="(filename)",
        etag_timeout=60,
    )

この方法は割とうまくいっている。1-2回くらいのリトライで再開することを確認ずみ(ただし、おそらくネットワーク距離と通信環境に依存する)。

リトライロジック用の汎用的なツールを使うのも良い。
以下はdecoratorを定義するだけで任意の関数にリトライ機能を付加できる。

@retry(stop=stop_after_attempt(10))
def safe_download():
    hf_hub_download(
        repo_id="(repo name)",
        repo_type="dataset",
        filename="(filename)",
        etag_timeout=60,
    )

https://github.com/jd/tenacity

HF_HUB_ENABLE_HF_TRANSFERを有効化しない

このオプションはデフォルトで無効になっている。有効化するとRustベースのパッケージを使ってファイルをチャンクに分割して分割ダウンロードするらしい。ダウンロードが早くなることは期待できるが、以下のようなエラーが出て失敗することがある。

Exception: An error occurred while downloading using hf_transfer. Consider disabling HF_HUB_ENABLE_HF_TRANSFER for better error handling.

https://huggingface.co/docs/huggingface_hub/package_reference/environment_variables#hfhubenablehftransfer

よくある勘違い

resume_download=Trueにする

このオプションは「自動でダウンロードを再開する」という意味ではなく、「途中までダウンロードした結果を消さずに途中からダウンロードを再開する」という意味。この動作は現行ではデフォルトの動作で、将来的にこのオプションも削除される予定とのこと。

https://huggingface.co/docs/huggingface_hub/v0.23.4/en/package_reference/file_download#huggingface_hub.hf_hub_download

このスクラップは2024/07/05にクローズされました