hankcs/HanLP · error · TypeError
Does not support dtype " + str(dtype)
Error message
Does not support dtype " + str(dtype)
What it means
tiny_value_of_dtype handles torch.float, torch.double, and torch.half but no other floating dtype. With torch.bfloat16 (or an exotic float dtype), neither branch matches and it raises TypeError naming the dtype.
Solutions
- Cast logits to float32 before the loss: logits.float()
- Disable bf16 / use fp16 AMP so loss computation stays in half or float
- Patch tiny_value_of_dtype to add a bfloat16 branch (e.g. return 1e-4) if you control the vendored copy
Example fix
# before loss = sequence_cross_entropy_with_logits(model(input_ids).logits, targets, weights) # bf16 # after loss = sequence_cross_entropy_with_logits(model(input_ids).logits.float(), targets, weights)
Defensive patterns
Strategy: fallback
Validate before calling
if logits.dtype == torch.bfloat16:
logits = logits.float() # tiny_value_of_dtype doesn't support bf16 Type guard
def is_supported_float_dtype(t: torch.Tensor) -> bool:
return t.dtype in (torch.float32, torch.float64, torch.float16) Try / catch
try:
loss = sequence_cross_entropy_with_logits(logits, ...)
except TypeError as e:
if 'Does not support dtype' in str(e):
loss = sequence_cross_entropy_with_logits(logits.float(), ...)
else:
raise Prevention
- Cast model outputs to float32 before loss computation in mixed-precision training
- Pin supported dtypes in training config tests
When it happens
Trigger: Calling sequence_cross_entropy_with_logits with logits in bfloat16 (common when loading models with torch_dtype=torch.bfloat16 or bf16 AMP training), which propagates to tiny_value_of_dtype via norm.dtype.
Common situations: Training udify-style parsers under bf16 mixed precision or on Ampere+ GPUs where bfloat16 is the default; upgrading PyTorch and switching model dtype for memory savings.
Related errors
- activation must be callable: type=
- alpha must be float, list of float, or torch.FloatTensor
- DataParallel not supported when CRF is used
- emissions must have dimension of 3, got
- expected last dimension of emissions is
AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27).
Data as JSON: /api/errors/fc7893d5c8d91d1e.
Report an issue: GitHub.
Appendix: source
Thrown at hanlp/components/parsers/ud/udify_util.py:298
"""Returns a moderately tiny value for a given PyTorch data type that is used to avoid numerical
issues such as division by zero.
This is different from `info_value_of_dtype(dtype).tiny` because it causes some NaN bugs.
Only supports floating point dtypes.
Args:
dtype: torch.dtype:
Returns:
"""
if not dtype.is_floating_point:
raise TypeError("Only supports floating point dtypes.")
if dtype == torch.float or dtype == torch.double:
return 1e-13
elif dtype == torch.half:
return 1e-4
else:
raise TypeError("Does not support dtype " + str(dtype))
def combine_initial_dims_to_1d_or_2d(tensor: torch.Tensor) -> torch.Tensor:
"""Given a (possibly higher order) tensor of ids with shape
(d1, ..., dn, sequence_length)
Args:
tensor: torch.Tensor:
Returns:
If original tensor is 1-d or 2-d, return it as is.
"""
if tensor.dim() <= 2:
return tensor
else:
return tensor.view(-1, tensor.size(-1))
View on GitHub (pinned to ddb1299bdd)