Skip to content

Commit bc775ff

Browse files
🧪 [testing] add edge case tests for mean_pooling (#245)
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 9663577 commit bc775ff

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

tests/test_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,48 @@ def test_full_mask(self) -> None:
116116
result = mean_pooling(model_output, mask)
117117
np.testing.assert_allclose(result[0], [3.0, 6.0])
118118

119+
def test_empty_mask(self) -> None:
120+
"""Empty mask should return zero vectors (avoid division by zero)."""
121+
model_output = np.array([[[1.0, 2.0], [3.0, 4.0]]])
122+
mask = np.array([[0, 0]], dtype=np.int64)
123+
124+
result = mean_pooling(model_output, mask)
125+
np.testing.assert_allclose(result[0], [0.0, 0.0])
126+
127+
def test_single_token(self) -> None:
128+
"""Single token sequences should work seamlessly."""
129+
model_output = np.array([[[5.0, 10.0]]])
130+
mask = np.array([[1]], dtype=np.int64)
131+
132+
result = mean_pooling(model_output, mask)
133+
np.testing.assert_allclose(result[0], [5.0, 10.0])
134+
135+
def test_multi_batch(self) -> None:
136+
"""Multiple batches with varied masking should be handled correctly."""
137+
model_output = np.array(
138+
[
139+
[[1.0, 2.0], [3.0, 4.0], [0.0, 0.0]],
140+
[[2.0, 4.0], [4.0, 8.0], [1.0, 1.0]],
141+
[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]],
142+
]
143+
)
144+
mask = np.array(
145+
[
146+
[1, 1, 0],
147+
[1, 1, 1],
148+
[0, 0, 0],
149+
],
150+
dtype=np.int64,
151+
)
152+
153+
result = mean_pooling(model_output, mask)
154+
# Batch 0: mean([1, 2], [3, 4]) -> [2, 3]
155+
np.testing.assert_allclose(result[0], [2.0, 3.0])
156+
# Batch 1: mean([2, 4], [4, 8], [1, 1]) -> [(2+4+1)/3, (4+8+1)/3] -> [7/3, 13/3] -> [2.333, 4.333]
157+
np.testing.assert_allclose(result[1], [7 / 3.0, 13 / 3.0])
158+
# Batch 2: empty mask -> [0, 0]
159+
np.testing.assert_allclose(result[2], [0.0, 0.0])
160+
119161

120162
class TestIterBatch:
121163
"""Tests for iter_batch utility function."""

0 commit comments

Comments
 (0)