hankcs/HanLP · error · TypeError
Only supports floating point dtypes.
Error message
Only supports floating point dtypes.
What it means
tiny_value_of_dtype returns the smallest useful positive value for a dtype (to avoid division by zero when normalizing logits). It only supports floating-point dtypes; passing an integer or bool torch dtype raises TypeError because tiny values are meaningless for them.
Source
Thrown at hanlp/components/parsers/ud/udify_util.py:292
weights_batch_sum + tiny_value_of_dtype(negative_log_likelihood.dtype)
)
return per_batch_loss
def tiny_value_of_dtype(dtype: torch.dtype):
"""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.
View on GitHub (pinned to ddb1299bdd)
Solutions
- Ensure the logits tensor passed to the loss is float32/float64/half
- Check argument order: logits first, targets (LongTensor) second
- If you cast a tensor with .long() for labels, keep a separate float copy for logits
Example fix
# before logits = scores.long() # accidentally cast loss = sequence_cross_entropy_with_logits(logits, targets, weights) # after loss = sequence_cross_entropy_with_logits(scores.float(), targets, weights)
Defensive patterns
Strategy: type-guard
Validate before calling
assert logits.is_floating_point(), f'logits dtype {logits.dtype} is not floating point' Type guard
def is_float_tensor(t: torch.Tensor) -> bool:
return t.is_floating_point() Prevention
- Check tensor.is_floating_point() before any loss call
- Keep label tensors separate from logit tensors to avoid dtype cross-contamination
When it happens
Trigger: sequence_cross_entropy_with_logits calls this with logits.dtype; if logits (or norm) is an int/long/bool tensor (e.g. targets passed as logits, or a model output cast to long), the error fires.
Common situations: Feeding LongTensor logits into the loss because a tensor was cast for labels earlier; custom models emitting integer scores; mixed up argument order when calling the loss.
Related errors
- Got average f{average}, expected one of None, 'token', or 'b
- alpha must be float, list of float, or torch.FloatTensor, {}
- Does not support dtype " + str(dtype)
- activation must be callable: type={}
- DataParallel not supported when CRF is used
AI-assisted analysis of hankcs/HanLP@ddb1299bdd (2026-08-27).
Data as JSON: /api/errors/0ab5e128f6f10b36.
Report an issue: GitHub.