def test_metric_result_precision_no_lower_than_float32(input_dtype):
    """Test that the ResultMetric only stores values in float32 or higher precision for numerical stability."""
    metadata = _Metadata("foo", "bar", reduce_fx="sum")
    metadata.sync = _Sync()
    metric = _ResultMetric(metadata, is_tensor=True)
    assert metric.value.dtype == torch.float

    # in bfloat16, truncation would occur at 256 (8 bit exponent)
    # in int8, overflow would occur at 128
    for i in range(1000):
        metric.update(tensor(1.0, dtype=input_dtype), 1)
        assert metric.value.dtype == torch.float32

    total = metric.compute()
    assert total.item() == 1000.0
    assert total.dtype == torch.float32
Test that the ResultMetric only stores values in float32 or higher precision for numerical stability.
tests/tests_pytorch/core/test_metric_result_integration.py