Spaces:
Paused
Paused
File size: 5,081 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | # 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 CanvasChunker transform."""
from absl.testing import absltest
from gemma.diffusion.hackable_diffusion_adapter.data import data
import numpy as np
EOS = 1234
PAD = -1
def _make_chunker(num_canvases=3, canvas_size=4):
return data.CanvasChunker(
num_canvases=num_canvases,
canvas_size=canvas_size,
eos_token=EOS,
pad_token=PAD,
)
class CanvasChunkerTest(absltest.TestCase):
def test_exact_fit(self):
"""Response length == num_canvases * canvas_size."""
chunker = _make_chunker(num_canvases=3, canvas_size=4)
response = np.arange(10, 22) # 12 tokens, exactly 3*4
features = chunker.map({"response": response})
np.testing.assert_array_equal(
features["canvas"],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
)
np.testing.assert_array_equal(
features["canvas_id"],
[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2],
)
np.testing.assert_array_equal(
features["canvas_mask"],
[True] * 12,
)
def test_short_response(self):
"""Response shorter than one canvas."""
chunker = _make_chunker(num_canvases=3, canvas_size=4)
response = np.array([10, 11])
features = chunker.map({"response": response})
np.testing.assert_array_equal(
features["canvas"],
[10, 11, EOS, EOS, PAD, PAD, PAD, PAD, PAD, PAD, PAD, PAD],
)
np.testing.assert_array_equal(
features["canvas_id"],
[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2],
)
np.testing.assert_array_equal(
features["canvas_mask"],
[
True,
True,
True,
True,
False,
False,
False,
False,
False,
False,
False,
False,
],
)
def test_overflow_truncation(self):
"""Response longer than total capacity."""
chunker = _make_chunker(num_canvases=2, canvas_size=4)
response = np.arange(10, 30) # 20 tokens, capacity is 8
features = chunker.map({"response": response})
# Only first 8 tokens kept.
np.testing.assert_array_equal(
features["canvas"],
[10, 11, 12, 13, 14, 15, 16, 17],
)
np.testing.assert_array_equal(
features["canvas_id"],
[0, 0, 0, 0, 1, 1, 1, 1],
)
np.testing.assert_array_equal(
features["canvas_mask"],
[True] * 8,
)
def test_partial_last_canvas(self):
"""Response fills 2.5 canvases (10 tokens into 3 canvases of 4)."""
chunker = _make_chunker(num_canvases=3, canvas_size=4)
response = np.arange(100, 110) # 10 tokens
features = chunker.map({"response": response})
np.testing.assert_array_equal(
features["canvas"],
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109, EOS, EOS],
)
np.testing.assert_array_equal(
features["canvas_id"],
[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2],
)
# All 3 canvases are valid because canvas 2 has real tokens + EOS.
np.testing.assert_array_equal(
features["canvas_mask"],
[True] * 12,
)
class CopyFieldTest(absltest.TestCase):
def test_basic_copy(self):
"""CopyField should copy the value to the new key."""
transform = data.CopyField(src_key="a", dst_key="b")
features = transform.map({"a": "hello"})
self.assertEqual(features["a"], "hello")
self.assertEqual(features["b"], "hello")
def test_copy_preserves_other_fields(self):
"""Other fields in the dict should be untouched."""
transform = data.CopyField(src_key="src", dst_key="dst")
features = transform.map({"src": "val", "other": 42})
self.assertEqual(features["src"], "val")
self.assertEqual(features["dst"], "val")
self.assertEqual(features["other"], 42)
def test_copy_numpy_array(self):
"""Copying a numpy array should work (shared reference)."""
transform = data.CopyField(src_key="arr", dst_key="arr_copy")
arr = np.array([1, 2, 3])
features = transform.map({"arr": arr})
np.testing.assert_array_equal(features["arr"], [1, 2, 3])
np.testing.assert_array_equal(features["arr_copy"], [1, 2, 3])
def test_copy_string(self):
"""Copying a string should give an independent value."""
transform = data.CopyField(src_key="text", dst_key="text_copy")
features = transform.map({"text": "Bonjour le monde"})
self.assertEqual(features["text_copy"], "Bonjour le monde")
if __name__ == "__main__":
absltest.main()
|