{"record":{"id":"72d058fd3be5eb93","repo":"sgl-project/sglang","slug":"unknown-device-module-device","errorCode":null,"errorMessage":"Unknown device module: {device}","messagePattern":"Unknown device module: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/utils/common.py","lineNumber":254,"sourceCode":"\ndef get_cuda_version():\n    if torch.version.cuda:\n        return tuple(map(int, torch.version.cuda.split(\".\")))\n    return (0, 0)\n\n\n@contextmanager\ndef device_context(device: torch.device):\n    if device.type == \"cpu\" and is_cpu():\n        with torch.device(\"cpu\"):\n            yield\n    else:\n        module = torch.get_device_module(device)\n        if module is not None:\n            with module.device(device.index):\n                yield\n        else:\n            raise ValueError(f\"Unknown device module: {device}\")\n\n\ndef _check_cuda_device_version(\n    device_capability_majors: List[int], cuda_version: Tuple[int, int]\n):\n    if not is_cuda():\n        return False\n    return (\n        torch.cuda.get_device_capability()[0] in device_capability_majors\n        and tuple(map(int, torch.version.cuda.split(\".\")[:2])) >= cuda_version\n    )\n\n\nis_ampere_with_cuda_12_3 = lru_cache(maxsize=1)(\n    partial(\n        _check_cuda_device_version, device_capability_majors=[8], cuda_version=(12, 3)\n    )\n)","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/common.py#L236-L272","documentation":"device_context yields a context manager for the correct torch device module. After torch.get_device_module(device) returns None for an unrecognized device string, it raises ValueError('Unknown device module').","triggerScenarios":"Passing a device string torch doesn't map to a module (e.g. 'npu' without torch_npu, a typo like 'cude:0', or an OOT platform string) into device_context, reached e.g. via _layer_norm_fwd.","commonSituations":"Custom/OOT platforms whose device module isn't registered with torch; malformed device strings from configs; version drift where get_device_module returns None instead of raising.","solutions":["Normalize/validate the device string before calling (torch.device('cuda:0'), not 'cude:0')","For OOT platforms, ensure the platform plugin patches torch device modules before use","Pass an explicit supported device ('cuda', 'cpu', 'npu', 'mps', 'musa')"],"exampleFix":"# before\nwith device_context('cude:0'):\n# after\nwith device_context(torch.device('cuda:0')):","handlingStrategy":"type-guard","validationCode":"dev = torch.device(device_str)  # raises early on malformed strings\nassert torch.get_device_module(dev) is not None, f'no device module for {dev}'","typeGuard":"def has_device_module(device) -> bool:\n    try:\n        return torch.get_device_module(device) is not None\n    except Exception:\n        return False","tryCatchPattern":null,"preventionTips":["Pass torch.device objects, not ad-hoc strings","Register OOT platform device modules with torch before use"],"tags":["device","torch","validation"],"backgroundTag":"unsupported-device-type","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}