{"record":{"id":"4f2ab2f26fa96e51","repo":"deepset-ai/haystack","slug":"only-single-devices-can-be-converted-to-pytorch-fo","errorCode":null,"errorMessage":"Only single devices can be converted to PyTorch format","messagePattern":"Only single devices can be converted to PyTorch format","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/utils/device.py","lineNumber":323,"sourceCode":"        \"\"\"\n        if not (self._single_device is not None) ^ (self._multiple_devices is not None):\n            raise ValueError(\n                \"The component device can neither be empty nor contain both a single device and a device map\"\n            )\n\n    def to_torch(self) -> \"torch.device\":\n        \"\"\"\n        Convert the component device representation to PyTorch format.\n\n        Device maps are not supported.\n\n        :returns:\n            The PyTorch device representation.\n        \"\"\"\n        self._validate()\n\n        if self._single_device is None:\n            raise ValueError(\"Only single devices can be converted to PyTorch format\")\n\n        torch_import.check()\n        assert self._single_device is not None\n        return torch.device(str(self._single_device))\n\n    def to_torch_str(self) -> str:\n        \"\"\"\n        Convert the component device representation to PyTorch string format.\n\n        Device maps are not supported.\n\n        :returns:\n            The PyTorch device string representation.\n        \"\"\"\n        self._validate()\n\n        if self._single_device is None:\n            raise ValueError(\"Only single devices can be converted to PyTorch format\")","sourceCodeStart":305,"sourceCodeEnd":341,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/utils/device.py#L305-L341","documentation":"ComponentDevice.to_torch raises ValueError when the component holds a multi-device map rather than a single device, since a multi-device setup cannot be expressed as one torch.device.","triggerScenarios":"Calling to_torch() on a ComponentDevice built from a DeviceMap (from_multiple/from_hf with multiple devices).","commonSituations":"Pipeline configured with multi-GPU device maps; component-level utility assumes single device but the model was sharded across devices (e.g. via accelerate).","solutions":["Use to_torch only on single-device components; inspect has_multiple_devices() first","For multi-device setups, access ComponentDevice._multiple_devices/DeviceMap and handle per-device entries or rely on the HF/accelerate integration instead of to_torch","Reconfigure the component to a single device if a single torch.device is required"],"exampleFix":"// before\ncomp = ComponentDevice.from_multiple(DeviceMap({\"0\": dev0, \"1\": dev1}))\ntorch_dev = comp.to_torch()  # raises\n// after\nif comp.has_multiple_devices():\n    # handle the map explicitly\n    devices = comp._multiple_devices.devices\nelse:\n    torch_dev = comp.to_torch()","handlingStrategy":"validation","validationCode":"def torch_device_or_none(comp: ComponentDevice):\n    return comp.to_torch() if not comp.has_multiple_devices() else None","typeGuard":"def has_single_device(comp: ComponentDevice) -> bool:\n    return comp._single_device is not None","tryCatchPattern":"try:\n    dev = comp.to_torch()\nexcept ValueError:\n    # multi-device: handle per-device or delegate to HF/accelerate\n    dev = None","preventionTips":["Check has_multiple_devices() before to_torch/to_torch_str","Keep single-device and multi-device code paths separate","For sharded models, rely on accelerate rather than a single torch.device"],"tags":["device","pytorch","multi-gpu","value-error"],"backgroundTag":"invalid-device-configuration","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}