{"record":{"id":"03fbe3b9e2ab0933","repo":"hiyouga/LlamaFactory","slug":"torch-accelerator-is-not-available-please-upgrade","errorCode":null,"errorMessage":"torch.accelerator is not available, please upgrade torch to 2.7.0 or higher.","messagePattern":"torch\\.accelerator is not available, please upgrade torch to 2\\.7\\.0 or higher\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/accelerator/helper.py","lineNumber":68,"sourceCode":"\n@unique\nclass ReduceOp(StrEnum):\n    SUM = \"sum\"\n    MEAN = \"mean\"\n    MAX = \"max\"\n    MIN = \"min\"\n\n\ndef requires_accelerator(fn):\n    \"\"\"Decorator to check if torch.accelerator is available.\n\n    Note: this api requires torch>=2.7.0, otherwise it will raise an AttributeError or RuntimeError\n    \"\"\"\n\n    @wraps(fn)\n    def wrapper(*args, **kwargs):\n        if not hasattr(torch, \"accelerator\"):\n            raise RuntimeError(\"torch.accelerator is not available, please upgrade torch to 2.7.0 or higher.\")\n\n        return fn(*args, **kwargs)\n\n    return wrapper\n\n\ndef is_distributed() -> bool:\n    \"\"\"Check if distributed environment is available.\"\"\"\n    return os.getenv(\"RANK\") is not None\n\n\ndef get_rank() -> int:\n    \"\"\"Get rank.\"\"\"\n    return int(os.getenv(\"RANK\", \"0\"))\n\n\ndef get_world_size() -> int:\n    \"\"\"Get world size.\"\"\"","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/accelerator/helper.py#L50-L86","documentation":"The `requires_accelerator` decorator in the v1 accelerator helper raises this RuntimeError when `torch.accelerator` does not exist on the installed PyTorch. `torch.accelerator` is the unified device API introduced in PyTorch 2.7.0, so its absence means the v1 architecture is running on an older torch build. The check is a plain `hasattr(torch, 'accelerator')` before any decorated function (device selection, barrier, etc.) runs.","triggerScenarios":"Setting `USE_V1=1` (or using the v1 launcher) with torch < 2.7.0 installed, then touching any decorated helper function such as `get_accelerator()`, `current_device()`, or device transfer utilities in `src/llamafactory/v1/accelerator/helper.py`.","commonSituations":"Upgrading LlamaFactory but keeping an old torch wheel for CUDA compatibility; a fresh environment resolved to torch 2.4-2.6; mixing v0-era requirements files with the v1 code path.","solutions":["Upgrade torch: `uv pip install -U 'torch>=2.7.0'` (pick the wheel matching your local CUDA runtime)","Verify with `python -c \"import torch; print(torch.__version__, hasattr(torch, 'accelerator'))\"` before rerunning","If you cannot upgrade torch, unset `USE_V1` to fall back to the v0 architecture which does not use this API"],"exampleFix":"# before\npip install torch==2.5.1  # later: USE_V1=1 llamafactory-cli train ... -> RuntimeError\n\n# after\npip install -U 'torch>=2.7.0'","handlingStrategy":"type-guard","validationCode":"import torch\n\nif not hasattr(torch, \"accelerator\"):\n    raise SystemExit(f\"torch {torch.__version__} lacks torch.accelerator; v1 requires torch>=2.7.0\")","typeGuard":"def supports_v1_accelerator() -> bool:\n    \"\"\"True when torch exposes the unified accelerator API (torch>=2.7.0).\"\"\"\n    import torch\n    return hasattr(torch, \"accelerator\")","tryCatchPattern":"try:\n    from llamafactory.v1.accelerator import helper\n    helper.get_current_device()\nexcept RuntimeError as e:\n    if \"torch.accelerator\" in str(e):\n        raise SystemExit(\"Upgrade torch to >=2.7.0 or unset USE_V1\") from e\n    raise","preventionTips":["Pin `torch>=2.7.0` in requirements when adopting USE_V1","Run a smoke `python -c \"import torch; assert hasattr(torch, 'accelerator')\"` in Dockerfile/CI","Keep separate environments for v0 (older torch) and v1 workloads"],"tags":["torch","version-mismatch","v1","environment"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}