{"record":{"id":"0ab5e128f6f10b36","repo":"hankcs/HanLP","slug":"only-supports-floating-point-dtypes","errorCode":null,"errorMessage":"Only supports floating point dtypes.","messagePattern":"Only supports floating point dtypes\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"hanlp/components/parsers/ud/udify_util.py","lineNumber":292,"sourceCode":"                weights_batch_sum + tiny_value_of_dtype(negative_log_likelihood.dtype)\n        )\n        return per_batch_loss\n\n\ndef tiny_value_of_dtype(dtype: torch.dtype):\n    \"\"\"Returns a moderately tiny value for a given PyTorch data type that is used to avoid numerical\n    issues such as division by zero.\n    This is different from `info_value_of_dtype(dtype).tiny` because it causes some NaN bugs.\n    Only supports floating point dtypes.\n\n    Args:\n      dtype: torch.dtype: \n\n    Returns:\n\n    \"\"\"\n    if not dtype.is_floating_point:\n        raise TypeError(\"Only supports floating point dtypes.\")\n    if dtype == torch.float or dtype == torch.double:\n        return 1e-13\n    elif dtype == torch.half:\n        return 1e-4\n    else:\n        raise TypeError(\"Does not support dtype \" + str(dtype))\n\n\ndef combine_initial_dims_to_1d_or_2d(tensor: torch.Tensor) -> torch.Tensor:\n    \"\"\"Given a (possibly higher order) tensor of ids with shape\n    (d1, ..., dn, sequence_length)\n\n    Args:\n      tensor: torch.Tensor: \n\n    Returns:\n      If original tensor is 1-d or 2-d, return it as is.\n","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/components/parsers/ud/udify_util.py#L274-L310","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nlogits = scores.long()  # accidentally cast\nloss = sequence_cross_entropy_with_logits(logits, targets, weights)\n# after\nloss = sequence_cross_entropy_with_logits(scores.float(), targets, weights)","handlingStrategy":"type-guard","validationCode":"assert logits.is_floating_point(), f'logits dtype {logits.dtype} is not floating point'","typeGuard":"def is_float_tensor(t: torch.Tensor) -> bool:\n    return t.is_floating_point()","tryCatchPattern":null,"preventionTips":["Check tensor.is_floating_point() before any loss call","Keep label tensors separate from logit tensors to avoid dtype cross-contamination"],"tags":["python","pytorch","dtype","loss-function"],"backgroundTag":"wrong-tensor-dtype","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}