{"record":{"id":"badf310e1d4b1c94","repo":"huggingface/transformers","slug":"you-are-using-from-pretrained-with-a-meta-device","errorCode":null,"errorMessage":"You are using `from_pretrained` with a meta device context manager or `torch.set_default_device('meta')`.\\nThis is an anti-pattern as `from_pretrained` wants to load existing weights.\\nIf you want to initialize an empty model on the meta device, use the context manager or global device with `from_config`, or `ModelClass(config)`","messagePattern":"You are using `from_pretrained` with a meta device context manager or `torch\\.set_default_device\\('meta'\\)`\\.\\\\nThis is an anti-pattern as `from_pretrained` wants to load existing weights\\.\\\\nIf you want to initialize an empty model on the meta device, use the context manager or global device with `from_config`, or `ModelClass\\(config\\)`","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/transformers/integrations/accelerate.py","lineNumber":101,"sourceCode":"    for tied_param in tied_params:\n        tied_module_index = [i for i, (n, _) in enumerate(modules_to_treat) if tied_param.startswith(n + \".\")][0]\n        tied_module_name = modules_to_treat[tied_module_index][0]\n        if tied_module_name not in tied_module_names:\n            tied_module_names.append(tied_module_name)\n            tied_modules.append(modules_to_treat[tied_module_index][1])\n            module_size_with_ties += module_sizes[tied_module_name]\n\n    return module_size_with_ties, tied_module_names, tied_modules\n\n\ndef check_and_set_device_map(device_map: \"torch.device | int | str | dict | None\") -> dict | str | None:\n    from ..modeling_utils import get_torch_context_manager_or_global_device\n\n    # Potentially detect context manager or global device, and use it (only if no device_map was provided)\n    if device_map is None and not is_deepspeed_zero3_enabled():\n        device_in_context = get_torch_context_manager_or_global_device()\n        if device_in_context == torch.device(\"meta\"):\n            raise RuntimeError(\n                \"You are using `from_pretrained` with a meta device context manager or `torch.set_default_device('meta')`.\\n\"\n                \"This is an anti-pattern as `from_pretrained` wants to load existing weights.\\nIf you want to initialize an \"\n                \"empty model on the meta device, use the context manager or global device with `from_config`, or `ModelClass(config)`\"\n            )\n        device_map = device_in_context\n\n    # change device_map into a map if we passed an int, a str or a torch.device\n    if isinstance(device_map, torch.device):\n        device_map = {\"\": device_map}\n    elif isinstance(device_map, str) and device_map not in [\"auto\", \"balanced\", \"balanced_low_0\", \"sequential\"]:\n        try:\n            if device_map == \"cuda\":\n                # setting to the local rank\n                local_rank = int(os.environ.get(\"LOCAL_RANK\", 0))\n                device_map = f\"cuda:{local_rank}\"\n            device_map = {\"\": torch.device(device_map)}\n        except RuntimeError:\n            raise ValueError(","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/integrations/accelerate.py#L83-L119","documentation":"Error \"You are using `from_pretrained` with a meta device context manager or `torch.set_default_device('meta')`.\\nThis is an anti-pattern as `from_pretrained` wants to load existing weights.\\nIf you want to initialize an empty model on the meta device, use the context manager or global device with `from_config`, or `ModelClass(config)`\" thrown in huggingface/transformers.","triggerScenarios":"Raised in from_pretrained device handling when a meta-device context or torch.set_default_device('meta') is active.","commonSituations":"Loading pretrained weights inside init_empty_weights or with default device set to meta; weights cannot load onto meta tensors.","solutions":["Do not wrap `from_pretrained` in a meta-device context or `torch.set_default_device('meta')`.","Use `from_config` or `ModelClass(config)` to create an empty meta-device model."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}