{"record":{"id":"fc7893d5c8d91d1e","repo":"hankcs/HanLP","slug":"does-not-support-dtype-str-dtype","errorCode":null,"errorMessage":"Does not support dtype \" + str(dtype)","messagePattern":"Does not support dtype \" \\+ str\\(dtype\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"hanlp/components/parsers/ud/udify_util.py","lineNumber":298,"sourceCode":"    \"\"\"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\n    \"\"\"\n    if tensor.dim() <= 2:\n        return tensor\n    else:\n        return tensor.view(-1, tensor.size(-1))\n","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/components/parsers/ud/udify_util.py#L280-L316","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nloss = sequence_cross_entropy_with_logits(model(input_ids).logits, targets, weights)  # bf16\n# after\nloss = sequence_cross_entropy_with_logits(model(input_ids).logits.float(), targets, weights)","handlingStrategy":"fallback","validationCode":"if logits.dtype == torch.bfloat16:\n    logits = logits.float()  # tiny_value_of_dtype doesn't support bf16","typeGuard":"def is_supported_float_dtype(t: torch.Tensor) -> bool:\n    return t.dtype in (torch.float32, torch.float64, torch.float16)","tryCatchPattern":"try:\n    loss = sequence_cross_entropy_with_logits(logits, ...)\nexcept TypeError as e:\n    if 'Does not support dtype' in str(e):\n        loss = sequence_cross_entropy_with_logits(logits.float(), ...)\n    else:\n        raise","preventionTips":["Cast model outputs to float32 before loss computation in mixed-precision training","Pin supported dtypes in training config tests"],"tags":["python","pytorch","bfloat16","mixed-precision"],"backgroundTag":"unsupported-dtype","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}