{
  "id": "llm/tokenizer-cache-corruption-multiprocessing",
  "signature": "ValueError: Could not load tokenizer cache from /home/user/.cache/huggingface/hub — file is corrupted or truncated",
  "signature_zh": "ValueError: 无法从 /home/user/.cache/huggingface/hub 加载分词器缓存 — 文件已损坏或截断",
  "regex": "ValueError: Could not load tokenizer cache from .*corrupted or truncated",
  "domain": "llm",
  "category": "resource_error",
  "subcategory": null,
  "root_cause": "Multiple processes (e.g., DataLoader workers with num_workers > 1) concurrently download or write to the Hugging Face tokenizer cache, causing race conditions that corrupt the cached tokenizer files.",
  "root_cause_type": "generic",
  "root_cause_zh": "多个进程（例如，num_workers > 1 的 DataLoader 工作进程）同时下载或写入 Hugging Face 分词器缓存，导致竞态条件，损坏缓存的分词器文件。",
  "versions": [
    {
      "version": "transformers==4.38.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tokenizers==0.15.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torch==2.2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Setting TOKENIZERS_PARALLELISM=false in environment",
      "why_fails": "This only disables tokenizer parallelism within a single process, it does not prevent cache corruption from multiple processes writing to the same cache directory.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Manually deleting the cache file and re-running",
      "why_fails": "The corruption will recur if the root cause (concurrent writes) is not addressed. The cache will be corrupted again on the next run with multiple workers.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Increasing HF_HUB_DOWNLOAD_TIMEOUT",
      "why_fails": "Timeout is not the issue; the issue is concurrent writes. Increasing timeout does not prevent race conditions.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Set the environment variable HF_HUB_ENABLE_HF_TRANSFER=1 and use a local lock file to serialize cache writes. Example:\n\nimport os\nos.environ['HF_HUB_ENABLE_HF_TRANSFER'] = '1'\n# Use a file lock for cache directory\nimport filelock\nlock = filelock.FileLock('/tmp/hf_cache.lock')\nwith lock:\n    tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')",
      "success_rate": 0.95,
      "how": "Set the environment variable HF_HUB_ENABLE_HF_TRANSFER=1 and use a local lock file to serialize cache writes. Example:\n\nimport os\nos.environ['HF_HUB_ENABLE_HF_TRANSFER'] = '1'\n# Use a file lock for cache directory\nimport filelock\nlock = filelock.FileLock('/tmp/hf_cache.lock')\nwith lock:\n    tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')",
      "condition": "",
      "sources": []
    },
    {
      "action": "Set num_workers=0 in DataLoader to disable multiprocessing for tokenization, forcing all tokenization to happen in the main process.",
      "success_rate": 0.85,
      "how": "Set num_workers=0 in DataLoader to disable multiprocessing for tokenization, forcing all tokenization to happen in the main process.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Pre-download the tokenizer before spawning workers: run AutoTokenizer.from_pretrained() once in the main process before creating the DataLoader. This ensures the cache is populated before workers access it.",
      "success_rate": 0.9,
      "how": "Pre-download the tokenizer before spawning workers: run AutoTokenizer.from_pretrained() once in the main process before creating the DataLoader. This ensures the cache is populated before workers access it.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "设置环境变量 HF_HUB_ENABLE_HF_TRANSFER=1 并使用本地锁文件序列化缓存写入。示例：\n\nimport os\nos.environ['HF_HUB_ENABLE_HF_TRANSFER'] = '1'\n# 使用文件锁保护缓存目录\nimport filelock\nlock = filelock.FileLock('/tmp/hf_cache.lock')\nwith lock:\n    tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')",
    "在 DataLoader 中设置 num_workers=0 以禁用分词的多进程处理，强制所有分词在主进程中完成。",
    "在生成工作进程之前预下载分词器：在创建 DataLoader 之前，在主进程中运行一次 AutoTokenizer.from_pretrained()。这确保在工作进程访问之前缓存已填充。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://huggingface.co/docs/huggingface_hub/en/guides/cache#cache-corruption",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2024-01-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}