XingangPan/DragGAN · error · RuntimeError

Cannot infer type name from input

Error message

Cannot infer type name from input

What it means

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').

Source

Thrown at dnnlib/util.py:212

    "int32": ctypes.c_int32,
    "int64": ctypes.c_int64,
    "float32": ctypes.c_float,
    "float64": ctypes.c_double
}


def get_dtype_and_ctype(type_obj: Any) -> Tuple[np.dtype, Any]:
    """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."""
    type_str = None

    if isinstance(type_obj, str):
        type_str = type_obj
    elif hasattr(type_obj, "__name__"):
        type_str = type_obj.__name__
    elif hasattr(type_obj, "name"):
        type_str = type_obj.name
    else:
        raise RuntimeError("Cannot infer type name from input")

    assert type_str in _str_to_ctype.keys()

    my_dtype = np.dtype(type_str)
    my_ctype = _str_to_ctype[type_str]

    assert my_dtype.itemsize == ctypes.sizeof(my_ctype)

    return my_dtype, my_ctype


def is_pickleable(obj: Any) -> bool:
    try:
        with io.BytesIO() as stream:
            pickle.dump(obj, stream)
        return True
    except:
        return False

View on GitHub (pinned to 336f120ce1)

Solutions

  1. Pass a canonical type-name string such as 'float32', 'uint8', or 'int32' instead of an object
  2. If passing np.dtype, convert first: get_dtype_and_ctype(np.dtype(x).name)
  3. If a new dtype is genuinely needed, add its name string to _str_to_ctype in dnnlib/util.py
  4. Avoid passing torch dtypes (torch.float32); map them manually via str(x).split('.')[-1]

Example fix

// before
get_dtype_and_ctype(torch.float32)  # RuntimeError

// after
get_dtype_and_ctype('float32')
# or
get_dtype_and_ctype(str(torch.float32).split('.')[-1])
Defensive patterns

Strategy: validation

Validate before calling

from dnnlib.util import _str_to_ctype
name = t if isinstance(t, str) else getattr(t, '__name__', None) or getattr(t, 'name', None)
assert name in _str_to_ctype, f'unsupported dtype name: {t!r}'

Type guard

def is_supported_type_name(t) -> bool:
    from dnnlib.util import _str_to_ctype
    s = t if isinstance(t, str) else getattr(t, '__name__', None) or getattr(t, 'name', None)
    return isinstance(s, str) and s in _str_to_ctype

Try / catch

try:
    dtype, ctype = dnnlib.util.get_dtype_and_ctype(x)
except (RuntimeError, AssertionError) as e:
    raise ValueError(f'Pass a dtype name like "float32", got {x!r}') from e

Prevention

When it happens

Trigger: 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'.

Common situations: 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.

Related errors


AI-assisted analysis of XingangPan/DragGAN@336f120ce1 (2026-08-27). Data as JSON: /api/errors/8ad62f41e9ef1571. Report an issue: GitHub.