{"record":{"id":"3585cd6fb86fd442","repo":"open-mmlab/mmdetection","slug":"note-the-to-float-is-true-you-need-to-ensure-t","errorCode":null,"errorMessage":"Note: the \"to_float\" is True, you need to ensure that the behavior is reasonable.","messagePattern":"Note: the \"to_float\" is True, you need to ensure that the behavior is reasonable\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"mmdet/utils/dist_utils.py","lineNumber":127,"sourceCode":"    \"\"\"\n    warnings.warn(\n        'group` is deprecated. Currently only supports NCCL backend.')\n    _, world_size = get_dist_info()\n    if world_size == 1:\n        return py_dict\n\n    # all reduce logic across different devices.\n    py_key = list(py_dict.keys())\n    if not isinstance(py_dict, OrderedDict):\n        py_key_tensor = obj2tensor(py_key)\n        dist.broadcast(py_key_tensor, src=0)\n        py_key = tensor2obj(py_key_tensor)\n\n    tensor_shapes = [py_dict[k].shape for k in py_key]\n    tensor_numels = [py_dict[k].numel() for k in py_key]\n\n    if to_float:\n        warnings.warn('Note: the \"to_float\" is True, you need to '\n                      'ensure that the behavior is reasonable.')\n        flatten_tensor = torch.cat(\n            [py_dict[k].flatten().float() for k in py_key])\n    else:\n        flatten_tensor = torch.cat([py_dict[k].flatten() for k in py_key])\n\n    dist.all_reduce(flatten_tensor, op=dist.ReduceOp.SUM)\n    if op == 'mean':\n        flatten_tensor /= world_size\n\n    split_tensors = [\n        x.reshape(shape) for x, shape in zip(\n            torch.split(flatten_tensor, tensor_numels), tensor_shapes)\n    ]\n    out_dict = {k: v for k, v in zip(py_key, split_tensors)}\n    if isinstance(py_dict, OrderedDict):\n        out_dict = OrderedDict(out_dict)\n    return out_dict","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/utils/dist_utils.py#L109-L145","documentation":"Inside all_reduce_dict, when to_float=True (the default) the code warns that every tensor in the dict will be cast to float32 before the all-reduce and concatenated. This matters for integer-valued metrics: casting to float can lose precision for very large counts (beyond 2^24) and changes dtypes of the returned values, so the author asks you to confirm that is reasonable for your data.","triggerScenarios":"Calling all_reduce_dict(py_dict) without to_float=False — i.e. using the default — during distributed validation metric aggregation. The warning fires whenever world_size > 1 path is taken (before the flatten/cast) or even earlier in single-process after the group notice.","commonSituations":"Aggregating validation losses/mAP values (floats — fine, warning ignorable); aggregating raw sample counts or pixel counts across many ranks where values exceed float32 integer precision; code that later does exact integer comparisons on the reduced values.","solutions":["If your values are counts or integers, call all_reduce_dict(metrics, to_float=False) to keep original dtypes.","If your values are losses/accuracies (small floats), the default is fine — filter the warning: warnings.filterwarnings('ignore', message='.*to_float.*').","For large counts, reduce in chunks or scale down (e.g. count / world_size) if you must keep to_float=True.","Verify returned dtypes after reduction before doing integer-sensitive arithmetic."],"exampleFix":"# before\nreduced = all_reduce_dict(metrics)  # to_float=True default, warns\n# after\nreduced = all_reduce_dict(metrics, to_float=False)  # preserve original dtypes","handlingStrategy":"validation","validationCode":"import torch\n\ndef metrics_are_float_safe(py_dict) -> bool:\n    return all(\n        torch.is_tensor(v) and (not v.dtype.is_floating_point or v.abs().max() < 2**24)\n        for v in py_dict.values()\n    )","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass to_float=False when reducing integer counts or large-magnitude tensors.","Keep to_float=True only for losses/rates (small floats).","Check dtypes of the returned dict before integer-sensitive arithmetic."],"tags":["mmdetection","distributed","dtype","all-reduce","numerical-precision"],"backgroundTag":"numerical-precision-loss","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}