Comfy-Org/ComfyUI · error · ValueError
Unsupported dtype
Error message
Unsupported dtype
What it means
Raised by manual_stochastic_round_to_float8 when the requested dtype is neither torch.float8_e4m3fn nor torch.float8_e5m2. This manual fallback implements mantissa/exponent arithmetic only for the two FP8 formats PyTorch defines for inference; any other dtype string (including float8_e4m3fnuz or a bogus value) hits the else branch.
Source
Thrown at comfy/float.py:35
def calc_mantissa(abs_x, exponent, normal_mask, MANTISSA_BITS, EXPONENT_BIAS, generator=None):
mantissa_scaled = torch.where(
normal_mask,
(abs_x / (2.0 ** (exponent - EXPONENT_BIAS)) - 1.0) * (2**MANTISSA_BITS),
(abs_x / (2.0 ** (-EXPONENT_BIAS + 1 - MANTISSA_BITS)))
)
mantissa_scaled += torch.rand(mantissa_scaled.size(), dtype=mantissa_scaled.dtype, layout=mantissa_scaled.layout, device=mantissa_scaled.device, generator=generator)
return mantissa_scaled.floor() / (2**MANTISSA_BITS)
#Not 100% sure about this
def manual_stochastic_round_to_float8(x, dtype, generator=None):
if dtype == torch.float8_e4m3fn:
EXPONENT_BITS, MANTISSA_BITS, EXPONENT_BIAS = 4, 3, 7
elif dtype == torch.float8_e5m2:
EXPONENT_BITS, MANTISSA_BITS, EXPONENT_BIAS = 5, 2, 15
else:
raise ValueError("Unsupported dtype")
x = x.half()
sign = torch.sign(x)
abs_x = x.abs()
sign = torch.where(abs_x == 0, 0, sign)
# Combine exponent calculation and clamping
exponent = torch.clamp(
torch.floor(torch.log2(abs_x)) + EXPONENT_BIAS,
0, 2**EXPONENT_BITS - 1
)
# Combine mantissa calculation and rounding
normal_mask = ~(exponent == 0)
abs_x[:] = calc_mantissa(abs_x, exponent, normal_mask, MANTISSA_BITS, EXPONENT_BIAS, generator=generator)
sign *= torch.where(View on GitHub (pinned to 1c6d8d45b3)
Solutions
- Map the unsupported dtype to the nearest supported FP8 format before calling (e4m3fnuz -> float8_e4m3fn) if the precision loss is acceptable for your use
- Use the comfy_kitchen _ck_stochastic_rounding_fp8 path, which may support a broader dtype set
- Reject fnuz early in your own config parsing with a clear message instead of reaching this helper
Example fix
# before
manual_stochastic_round_to_float8(x, torch.float8_e4m3fnuz) # ValueError
# after
if dtype == torch.float8_e4m3fnuz:
dtype = torch.float8_e4m3fn
manual_stochastic_round_to_float8(x, dtype) Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED_FP8 = (torch.float8_e4m3fn, torch.float8_e5m2)
if dtype not in SUPPORTED_FP8:
raise ValueError(f'manual_stochastic_round_to_float8 supports only {SUPPORTED_FP8}') Type guard
def is_supported_fp8(dtype: torch.dtype) -> bool:
return dtype in (torch.float8_e4m3fn, torch.float8_e5m2) Prevention
- Map fnuz dtypes to e4m3fn at your config boundary
- Never forward raw quantization-config dtype strings into rounding helpers
When it happens
Trigger: Calling manual_stochastic_round_to_float8(x, dtype) with dtype=torch.float8_e4m3fnuz, a float16/bfloat16 dtype, or a raw string/int. Code that derives dtype from checkpoint quantization config (e.g. 'fp8_e4m3fnuz' on older AMD/NPU paths) and forwards it verbatim.
Common situations: Custom quantization nodes supporting the fnuz variant that assume the manual rounding helper accepts it; passing a fake dtype placeholder before real FP8 support is wired; copy-paste from code that assumed torch.float8_e4m3 only.
Related errors
AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14).
Data as JSON: /api/errors/871b311e54fc1a9c.
Report an issue: GitHub.