|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for metrax base metrics.""" |
| 16 | + |
| 17 | +from absl.testing import absltest |
| 18 | +from absl.testing import parameterized |
| 19 | +import jax.numpy as jnp |
| 20 | +import keras |
| 21 | +import metrax |
| 22 | +from metrax import base_metrics |
| 23 | +import numpy as np |
| 24 | + |
| 25 | +np.random.seed(42) |
| 26 | +BATCHES = 4 |
| 27 | +BATCH_SIZE = 8 |
| 28 | +OUTPUT = np.random.uniform(size=(BATCHES, BATCH_SIZE)) |
| 29 | +OUTPUT_F16 = OUTPUT.astype(jnp.float16) |
| 30 | +OUTPUT_F32 = OUTPUT.astype(jnp.float32) |
| 31 | +OUTPUT_BF16 = OUTPUT.astype(jnp.bfloat16) |
| 32 | +OUTPUT_BS1 = np.random.uniform(size=(BATCHES, 1)).astype(jnp.float32) |
| 33 | +SAMPLE_WEIGHTS = np.tile( |
| 34 | + [0.5, 1, 0, 0, 0, 0, 0, 0], |
| 35 | + (BATCHES, 1), |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +class BaseMetricsTest(parameterized.TestCase): |
| 40 | + |
| 41 | + def test_basic_division(self): |
| 42 | + x = jnp.array([10.0, 20.0, 30.0]) |
| 43 | + y = jnp.array([2.0, 4.0, 5.0]) |
| 44 | + expected = jnp.array([5.0, 5.0, 6.0]) |
| 45 | + result = base_metrics.divide_no_nan(x, y) |
| 46 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 47 | + |
| 48 | + def test_division_by_zero(self): |
| 49 | + x = jnp.array([10.0, 20.0, 30.0]) |
| 50 | + y = jnp.array([2.0, 0.0, 5.0]) |
| 51 | + expected = jnp.array([5.0, 0.0, 6.0]) |
| 52 | + result = base_metrics.divide_no_nan(x, y) |
| 53 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 54 | + |
| 55 | + def test_all_zeros_denominator(self): |
| 56 | + x = jnp.array([10.0, 20.0, 30.0]) |
| 57 | + y = jnp.array([0.0, 0.0, 0.0]) |
| 58 | + expected = jnp.array([0.0, 0.0, 0.0]) |
| 59 | + result = base_metrics.divide_no_nan(x, y) |
| 60 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 61 | + |
| 62 | + def test_all_zeros_numerator(self): |
| 63 | + x = jnp.array([0.0, 0.0, 0.0]) |
| 64 | + y = jnp.array([2.0, 4.0, 5.0]) |
| 65 | + expected = jnp.array([0.0, 0.0, 0.0]) |
| 66 | + result = base_metrics.divide_no_nan(x, y) |
| 67 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 68 | + |
| 69 | + def test_mixed_zeros(self): |
| 70 | + x = jnp.array([10.0, 0.0, 30.0, 0.0]) |
| 71 | + y = jnp.array([2.0, 0.0, 5.0, 4.0]) |
| 72 | + expected = jnp.array([5.0, 0.0, 6.0, 0.0]) |
| 73 | + result = base_metrics.divide_no_nan(x, y) |
| 74 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 75 | + |
| 76 | + def test_scalar_inputs(self): |
| 77 | + x = jnp.array(10.0) |
| 78 | + y = jnp.array(2.0) |
| 79 | + expected = jnp.array(5.0) |
| 80 | + result = base_metrics.divide_no_nan(x, y) |
| 81 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 82 | + |
| 83 | + def test_scalar_denominator_zero(self): |
| 84 | + x = jnp.array(10.0) |
| 85 | + y = jnp.array(0.0) |
| 86 | + expected = jnp.array(0.0) |
| 87 | + result = base_metrics.divide_no_nan(x, y) |
| 88 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 89 | + |
| 90 | + def test_negative_values(self): |
| 91 | + x = jnp.array([-10.0, 20.0, -30.0]) |
| 92 | + y = jnp.array([2.0, -4.0, 5.0]) |
| 93 | + expected = jnp.array([-5.0, -5.0, -6.0]) |
| 94 | + result = base_metrics.divide_no_nan(x, y) |
| 95 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 96 | + |
| 97 | + def test_negative_and_zero_values(self): |
| 98 | + x = jnp.array([-10.0, 20.0, -30.0, 10.0]) |
| 99 | + y = jnp.array([2.0, -4.0, 0.0, 0.0]) |
| 100 | + expected = jnp.array([-5.0, -5.0, 0.0, 0.0]) |
| 101 | + result = base_metrics.divide_no_nan(x, y) |
| 102 | + self.assertTrue(jnp.array_equal(result, expected)) |
| 103 | + |
| 104 | + @parameterized.named_parameters( |
| 105 | + ('basic_f16', OUTPUT_F16, None), |
| 106 | + ('basic_f32', OUTPUT_F32, None), |
| 107 | + ('basic_bf16', OUTPUT_BF16, None), |
| 108 | + ('batch_size_one', OUTPUT_BS1, None), |
| 109 | + ('weighted_f16', OUTPUT_F16, SAMPLE_WEIGHTS), |
| 110 | + ('weighted_f32', OUTPUT_F32, SAMPLE_WEIGHTS), |
| 111 | + ('weighted_bf16', OUTPUT_BF16, SAMPLE_WEIGHTS), |
| 112 | + ) |
| 113 | + def test_average(self, values, sample_weights): |
| 114 | + """Test that `Average` metric computes correct values.""" |
| 115 | + if sample_weights is None: |
| 116 | + sample_weights = jnp.ones_like(values) |
| 117 | + sample_weights = jnp.array(sample_weights, dtype=values.dtype) |
| 118 | + metric = metrax.Average.from_model_output( |
| 119 | + values=values, |
| 120 | + sample_weights=sample_weights, |
| 121 | + ) |
| 122 | + |
| 123 | + keras_mean = keras.metrics.Mean(dtype=values.dtype) |
| 124 | + keras_mean.update_state(values, sample_weights) |
| 125 | + keras_metrics = keras_mean.result() |
| 126 | + keras_metrics = jnp.array(keras_metrics, dtype=values.dtype) |
| 127 | + |
| 128 | + # Use lower tolerance for lower precision dtypes. |
| 129 | + rtol = 1e-2 if values.dtype in (jnp.float16, jnp.bfloat16) else 1e-05 |
| 130 | + atol = 1e-2 if values.dtype in (jnp.float16, jnp.bfloat16) else 1e-05 |
| 131 | + np.testing.assert_allclose( |
| 132 | + metric.compute(), |
| 133 | + keras_metrics, |
| 134 | + rtol=rtol, |
| 135 | + atol=atol, |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + absltest.main() |
0 commit comments