{"record":{"id":"8ad62f41e9ef1571","repo":"XingangPan/DragGAN","slug":"cannot-infer-type-name-from-input","errorCode":null,"errorMessage":"Cannot infer type name from input","messagePattern":"Cannot infer type name from input","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"dnnlib/util.py","lineNumber":212,"sourceCode":"    \"int32\": ctypes.c_int32,\n    \"int64\": ctypes.c_int64,\n    \"float32\": ctypes.c_float,\n    \"float64\": ctypes.c_double\n}\n\n\ndef get_dtype_and_ctype(type_obj: Any) -> Tuple[np.dtype, Any]:\n    \"\"\"Given a type name string (or an object having a __name__ attribute), return matching Numpy and ctypes types that have the same size in bytes.\"\"\"\n    type_str = None\n\n    if isinstance(type_obj, str):\n        type_str = type_obj\n    elif hasattr(type_obj, \"__name__\"):\n        type_str = type_obj.__name__\n    elif hasattr(type_obj, \"name\"):\n        type_str = type_obj.name\n    else:\n        raise RuntimeError(\"Cannot infer type name from input\")\n\n    assert type_str in _str_to_ctype.keys()\n\n    my_dtype = np.dtype(type_str)\n    my_ctype = _str_to_ctype[type_str]\n\n    assert my_dtype.itemsize == ctypes.sizeof(my_ctype)\n\n    return my_dtype, my_ctype\n\n\ndef is_pickleable(obj: Any) -> bool:\n    try:\n        with io.BytesIO() as stream:\n            pickle.dump(obj, stream)\n        return True\n    except:\n        return False","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/XingangPan/DragGAN/blob/336f120ce126aca6f55dc58537e76c10d19eabd0/dnnlib/util.py#L194-L230","documentation":"Raised by dnnlib.util.get_dtype_and_ctype when converting a type object to a NumPy dtype and C type. The function accepts a type, a type name string, or an np.dtype; it extracts a string via __name__ or name attributes, and if the object has neither (and is not itself a string), it cannot map the input to a known C type. The subsequent assert requires the string to be one of the keys of _str_to_ctype (e.g. 'float32', 'uint8', 'int32').","triggerScenarios":"Calling get_dtype_and_ctype with an object that is not a str, not a type with __name__, has no .name attribute, or a np.dtype whose str/char (like '<f4' or 'float64' variants) is not a key in _str_to_ctype. Common when users pass torch dtypes (torch.float32) or numpy dtype objects directly instead of canonical names like 'float32'.","commonSituations":"Writing custom network pickles or custom ops where params are exposed with non-standard dtype names; passing np.dtype('float64') or torch dtype objects; minor stylegan2-ada forks that add new dtypes without updating _str_to_ctype.","solutions":["Pass a canonical type-name string such as 'float32', 'uint8', or 'int32' instead of an object","If passing np.dtype, convert first: get_dtype_and_ctype(np.dtype(x).name)","If a new dtype is genuinely needed, add its name string to _str_to_ctype in dnnlib/util.py","Avoid passing torch dtypes (torch.float32); map them manually via str(x).split('.')[-1]"],"exampleFix":"// before\nget_dtype_and_ctype(torch.float32)  # RuntimeError\n\n// after\nget_dtype_and_ctype('float32')\n# or\nget_dtype_and_ctype(str(torch.float32).split('.')[-1])","handlingStrategy":"validation","validationCode":"from dnnlib.util import _str_to_ctype\nname = t if isinstance(t, str) else getattr(t, '__name__', None) or getattr(t, 'name', None)\nassert name in _str_to_ctype, f'unsupported dtype name: {t!r}'","typeGuard":"def is_supported_type_name(t) -> bool:\n    from dnnlib.util import _str_to_ctype\n    s = t if isinstance(t, str) else getattr(t, '__name__', None) or getattr(t, 'name', None)\n    return isinstance(s, str) and s in _str_to_ctype","tryCatchPattern":"try:\n    dtype, ctype = dnnlib.util.get_dtype_and_ctype(x)\nexcept (RuntimeError, AssertionError) as e:\n    raise ValueError(f'Pass a dtype name like \"float32\", got {x!r}') from e","preventionTips":["Always pass canonical dtype-name strings ('float32', 'uint8') instead of objects","Centralize dtype constants in one module and reuse them","Extend _str_to_ctype when adding new dtypes rather than bypassing the helper"],"tags":["python","numpy","dtype","type-conversion","stylegan2"],"backgroundTag":"dtype-lookup-failed","analyzedSha":"336f120ce126aca6f55dc58537e76c10d19eabd0","analyzedAt":"2026-08-27T06:29:00.251Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}