File size: 2,835 Bytes
2f86d9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2026 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for base evaluation metrics."""

import dataclasses

from absl.testing import absltest
from gemma.diffusion.hackable_diffusion_adapter.eval import base_metric
import jax.numpy as jnp


class MockTokenizer:

  def decode(self, tokens: list[int]) -> str:
    # Decode by converting positive tokens to strings, skipping pad/eos (-1)
    return " ".join(str(t) for t in tokens if t >= 0)


@dataclasses.dataclass(kw_only=True, frozen=True)
class DummySimpleMetric(base_metric.BaseSimpleTextMetric):

  # Override tokenizer to avoid loading heavy SentencePiece model in test.
  @property
  def tokenizer(self) -> MockTokenizer:
    return MockTokenizer()

  def score_example(self, generated_text: str, ground_truth_text: str) -> float:
    # Score is 1.0 if generated matches ground truth, else 0.0
    return 1.0 if generated_text == ground_truth_text else 0.0


class BaseMetricTest(absltest.TestCase):

  def test_simple_text_metric_2d(self):
    """Test BaseSimpleTextMetric with 2D token inputs."""
    metric = DummySimpleMetric(tokens="samples", ground_truth="batch.response")

    # Batches of shape [B, L]
    tokens = jnp.array([
        [1, 2, 3, -1],  # "1 2 3"
        [4, 5, -1, -1],  # "4 5"
    ])
    ground_truth = jnp.array([
        [1, 2, 3, -1],  # "1 2 3" (Match)
        [4, 6, -1, -1],  # "4 6"   (Mismatch)
    ])

    state = metric.get_state(tokens=tokens, ground_truth=ground_truth)
    computed = state.compute()

    # Match gives 1.0, mismatch gives 0.0 -> average is 0.5
    self.assertEqual(computed, 0.5)

  def test_simple_text_metric_3d_squeezing(self):
    """Test that BaseSimpleTextMetric squeezes 3D [B, L, 1] inputs to 2D."""
    metric = DummySimpleMetric(tokens="samples", ground_truth="batch.response")

    # Batches of shape [B, L, 1]
    tokens = jnp.array([
        [[1], [2], [-1]],  # "1 2"
        [[3], [4], [-1]],  # "3 4"
    ])
    ground_truth = jnp.array([
        [[1], [2], [-1]],  # "1 2" (Match)
        [[3], [4], [-1]],  # "3 4" (Match)
    ])

    state = metric.get_state(tokens=tokens, ground_truth=ground_truth)
    computed = state.compute()

    # Both match -> average is 1.0
    self.assertEqual(computed, 1.0)


if __name__ == "__main__":
  absltest.main()