{"record":{"id":"535838bca165a7a3","repo":"invoke-ai/InvokeAI","slug":"out-dtype-must-be-a-float-type-but-got-out-dtype","errorCode":null,"errorMessage":"out_dtype must be a float type, but got {out_dtype}","messagePattern":"out_dtype must be a float type, but got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/util/mask.py","lineNumber":39,"sourceCode":"    return mask\n\n\ndef to_standard_float_mask(mask: torch.Tensor, out_dtype: torch.dtype) -> torch.Tensor:\n    \"\"\"Standardize the format of a mask tensor.\n\n    Args:\n        mask (torch.Tensor): A mask tensor. The dtype can be any bool, float, or int type. The shape must be (1, h, w)\n            or (h, w).\n\n        out_dtype (torch.dtype): The dtype of the output mask tensor. Must be a float type.\n\n    Returns:\n        torch.Tensor: The output mask tensor. The dtype is out_dtype. The shape is (1, h, w). All values are either 0.0\n            or 1.0.\n    \"\"\"\n\n    if not out_dtype.is_floating_point:\n        raise ValueError(f\"out_dtype must be a float type, but got {out_dtype}\")\n\n    mask = to_standard_mask_dim(mask)\n    mask = mask.to(out_dtype)\n\n    # Set masked regions to 1.0.\n    if mask.dtype == torch.bool:\n        mask = mask.to(out_dtype)\n    else:\n        mask = mask.to(out_dtype)\n        mask_region = mask > 0.5\n        mask[mask_region] = 1.0\n        mask[~mask_region] = 0.0\n\n    return mask\n","sourceCodeStart":21,"sourceCodeEnd":54,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/util/mask.py#L21-L54","documentation":"to_standard_float_mask converts a mask to a (1, h, w) float tensor of 0.0/1.0 values in the requested out_dtype. Before doing any work it verifies out_dtype.is_floating_point; integer or bool dtypes (torch.uint8, torch.int64, torch.bool) are rejected with this ValueError because thresholding to 1.0/0.0 requires float arithmetic.","triggerScenarios":"Calling to_standard_float_mask(mask, torch.uint8), torch.int32, torch.int64, torch.bool, or any non-float dtype; tests like test_to_standard_float_mask_wrong_shape exercise this path deliberately.","commonSituations":"Reusing a dtype variable that came from an integer input image, assuming bool masks are allowed, passing the VAE latent dtype when it is an integer type.","solutions":["Pass a float dtype, e.g. to_standard_float_mask(mask, torch.float32) (or float16/bfloat16 as needed).","Convert a non-float dtype variable: out_dtype = torch.tensor(0, dtype=out_dtype).float().dtype or just hardcode torch.float32.","Keep masks in bool and call .to(float) afterwards if you need integer semantics elsewhere."],"exampleFix":"// before\nout = to_standard_float_mask(mask, torch.uint8)\n// after\nout = to_standard_float_mask(mask, torch.float32)","handlingStrategy":"validation","validationCode":"if not out_dtype.is_floating_point:\n    out_dtype = torch.float32\nout = to_standard_float_mask(mask, out_dtype)","typeGuard":"def is_float_dtype(dtype: torch.dtype) -> bool:\n    return dtype.is_floating_point","tryCatchPattern":"try:\n    m = to_standard_float_mask(mask, out_dtype)\nexcept ValueError as e:\n    if \"must be a float type\" in str(e):\n        m = to_standard_float_mask(mask, torch.float32)","preventionTips":["Hardcode torch.float32 (or float16/bfloat16) for mask conversion.","Never pass torch.bool or integer dtypes; convert afterwards if needed.","Check the source dtype of mask tensors derived from integer images."],"tags":["python","pytorch","dtype","mask"],"backgroundTag":"invalid-dtype","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}