{"record":{"id":"defdbf6f4a9f7a7c","repo":"sgl-project/sglang","slug":"unknown-type-device-maybe-uuid","errorCode":null,"errorMessage":"Unknown type: {device_maybe_uuid=}","messagePattern":"Unknown type: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/utils/patch_torch.py","lineNumber":99,"sourceCode":"    args = _modify_tuple(args, _REDUCE_TENSOR_ARG_DEVICE_INDEX, _device_from_maybe_uuid)\n    return reductions._rebuild_cuda_tensor_original(*args)\n\n\ndef _device_to_uuid(device: int) -> str:\n    return str(torch.cuda.get_device_properties(device).uuid)\n\n\ndef _device_from_maybe_uuid(device_maybe_uuid: Union[int, str]) -> int:\n    if isinstance(device_maybe_uuid, int):\n        return device_maybe_uuid\n\n    if isinstance(device_maybe_uuid, str):\n        for device in range(torch.cuda.device_count()):\n            if str(torch.cuda.get_device_properties(device).uuid) == device_maybe_uuid:\n                return device\n        raise Exception(\"Invalid device_uuid=\" + device_maybe_uuid)\n\n    raise Exception(f\"Unknown type: {device_maybe_uuid=}\")\n\n\ndef _modify_tuple(t, index: int, modifier: Callable):\n    return *t[:index], modifier(t[index]), *t[index + 1 :]\n\n\ndef monkey_patch_torch_compile():\n    if torch_release < (2, 8):\n        # These things are cacheable by torch.compile. torch.compile just doesn't know it.\n        # This was fixed in PyTorch 2.8, but until then, we monkey patch.\n        import torch._higher_order_ops.auto_functionalize as af\n\n        af.auto_functionalized_v2._cacheable = True\n        af.auto_functionalized._cacheable = True\n\n\ndef register_fake_if_exists(op_name):\n    \"\"\"","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/patch_torch.py#L81-L117","documentation":"The device-patching helper received a value that is neither a torch.device/int-like device nor a str, so it cannot resolve it to a CUDA device index and raises a generic Exception.","triggerScenarios":"Passing None, a bytes object, a custom enum, or any non-str/non-device type into a device argument that flows through the patched torch API (_device_from_maybe_uuid).","commonSituations":"Config parsing that yields None instead of a default device, or serialization layers turning UUID strings into bytes.","solutions":["Coerce the value to str or torch.device before passing it downstream","Default None device values to a concrete device ('cuda', 'cuda:0') at config load","Add a type check/assert where the device value originates"],"exampleFix":"# before\nset_device(cfg.device)  # cfg.device is None\n# after\nset_device(cfg.device or 'cuda:0')","handlingStrategy":"type-guard","validationCode":"assert device_spec is None or isinstance(device_spec, (str, int)), f'bad device {device_spec!r}'","typeGuard":"def is_resolvable_device(v) -> bool:\n    return v is None or isinstance(v, (str, int))","tryCatchPattern":"try:\n    _device_from_maybe_uuid(v)\nexcept Exception as e:\n    if 'Unknown type' in str(e):\n        v = str(v); retry()","preventionTips":["Coerce device config to str/torch.device at load time","Default None devices explicitly","Validate deserialized config types (bytes vs str)"],"tags":["cuda","device","type-error","torch-patch","sglang"],"backgroundTag":"invalid-device-identifier","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}