|
15 | 15 | import tests.utils as test_utils |
16 | 16 |
|
17 | 17 |
|
18 | | -class TestSequenceGenerator(unittest.TestCase): |
| 18 | +class TestSequenceGeneratorBase(unittest.TestCase): |
| 19 | + |
| 20 | + def assertHypoTokens(self, hypo, tokens): |
| 21 | + self.assertTensorEqual(hypo['tokens'], torch.LongTensor(tokens)) |
| 22 | + |
| 23 | + def assertHypoScore(self, hypo, pos_probs, normalized=True, lenpen=1.): |
| 24 | + pos_scores = torch.FloatTensor(pos_probs).log() |
| 25 | + self.assertAlmostEqual(hypo['positional_scores'], pos_scores) |
| 26 | + self.assertEqual(pos_scores.numel(), hypo['tokens'].numel()) |
| 27 | + score = pos_scores.sum() |
| 28 | + if normalized: |
| 29 | + score /= pos_scores.numel()**lenpen |
| 30 | + self.assertLess(abs(score - hypo['score']), 1e-6) |
| 31 | + |
| 32 | + def assertAlmostEqual(self, t1, t2): |
| 33 | + self.assertEqual(t1.size(), t2.size(), "size mismatch") |
| 34 | + self.assertLess((t1 - t2).abs().max(), 1e-4) |
| 35 | + |
| 36 | + def assertTensorEqual(self, t1, t2): |
| 37 | + self.assertEqual(t1.size(), t2.size(), "size mismatch") |
| 38 | + self.assertEqual(t1.ne(t2).long().sum(), 0) |
| 39 | + |
| 40 | + |
| 41 | +class TestSequenceGenerator(TestSequenceGeneratorBase): |
19 | 42 |
|
20 | 43 | def setUp(self): |
21 | 44 | self.tgt_dict, self.w1, self.w2, src_tokens, src_lengths, self.model = ( |
@@ -133,28 +156,8 @@ def test_no_stop_early(self): |
133 | 156 | self.assertHypoTokens(hypos[1][1], [w1, w2, w1, eos]) |
134 | 157 | self.assertHypoScore(hypos[1][1], [0.7, 0.4, 0.4, 1.0]) |
135 | 158 |
|
136 | | - def assertHypoTokens(self, hypo, tokens): |
137 | | - self.assertTensorEqual(hypo['tokens'], torch.LongTensor(tokens)) |
138 | 159 |
|
139 | | - def assertHypoScore(self, hypo, pos_probs, normalized=True, lenpen=1.): |
140 | | - pos_scores = torch.FloatTensor(pos_probs).log() |
141 | | - self.assertAlmostEqual(hypo['positional_scores'], pos_scores) |
142 | | - self.assertEqual(pos_scores.numel(), hypo['tokens'].numel()) |
143 | | - score = pos_scores.sum() |
144 | | - if normalized: |
145 | | - score /= pos_scores.numel()**lenpen |
146 | | - self.assertLess(abs(score - hypo['score']), 1e-6) |
147 | | - |
148 | | - def assertAlmostEqual(self, t1, t2): |
149 | | - self.assertEqual(t1.size(), t2.size(), "size mismatch") |
150 | | - self.assertLess((t1 - t2).abs().max(), 1e-4) |
151 | | - |
152 | | - def assertTensorEqual(self, t1, t2): |
153 | | - self.assertEqual(t1.size(), t2.size(), "size mismatch") |
154 | | - self.assertEqual(t1.ne(t2).long().sum(), 0) |
155 | | - |
156 | | - |
157 | | -class TestDiverseBeamSearch(unittest.TestCase): |
| 160 | +class TestDiverseBeamSearch(TestSequenceGeneratorBase): |
158 | 161 |
|
159 | 162 | def setUp(self): |
160 | 163 | # construct dummy dictionary |
@@ -232,25 +235,156 @@ def test_diverse_beam_search(self): |
232 | 235 | self.assertHypoTokens(hypos[1][1], [w1, w2, eos]) |
233 | 236 | self.assertHypoScore(hypos[1][1], [0.7, 0.4, 0.9]) |
234 | 237 |
|
235 | | - def assertHypoTokens(self, hypo, tokens): |
236 | | - self.assertTensorEqual(hypo['tokens'], torch.LongTensor(tokens)) |
237 | 238 |
|
238 | | - def assertHypoScore(self, hypo, pos_probs, normalized=True, lenpen=1.): |
| 239 | +class TestTopPSamplingSearch(TestSequenceGeneratorBase): |
| 240 | + |
| 241 | + def setUp(self): |
| 242 | + # construct dummy dictionary |
| 243 | + d = test_utils.dummy_dictionary(vocab_size=2) |
| 244 | + self.assertEqual(d.pad(), 1) |
| 245 | + self.assertEqual(d.eos(), 2) |
| 246 | + self.assertEqual(d.unk(), 3) |
| 247 | + self.eos = d.eos() |
| 248 | + self.w1 = 4 |
| 249 | + self.w2 = 5 |
| 250 | + |
| 251 | + # construct source data |
| 252 | + self.src_tokens = torch.LongTensor([ |
| 253 | + [self.w1, self.w2, self.eos], |
| 254 | + [self.w1, self.w2, self.eos], |
| 255 | + ]) |
| 256 | + self.src_lengths = torch.LongTensor([2, 2]) |
| 257 | + |
| 258 | + args = argparse.Namespace() |
| 259 | + unk = 0. |
| 260 | + # The minimal probability of top 2 tokens. |
| 261 | + self.min_top2_prob = 0.75 |
| 262 | + # The minimal probability of the top 1 token. |
| 263 | + self.min_top1_prob = 0.4 |
| 264 | + |
| 265 | + w1_prob = self.min_top1_prob |
| 266 | + w2_prob = self.min_top2_prob - self.min_top1_prob |
| 267 | + eos_prob = 1 - self.min_top2_prob |
| 268 | + |
| 269 | + args.beam_probs = [ |
| 270 | + # step 0: |
| 271 | + torch.FloatTensor([ |
| 272 | + # eos w1 w2 |
| 273 | + [0.0, unk, 1.0, 0.0], |
| 274 | + [0.0, unk, 1.0, 0.0], |
| 275 | + [0.0, unk, 1.0, 0.0], |
| 276 | + [0.0, unk, 1.0, 0.0], |
| 277 | + ]), |
| 278 | + # step 1: |
| 279 | + torch.FloatTensor([ |
| 280 | + # eos w1 w2 |
| 281 | + [eos_prob, unk, w1_prob, w2_prob], |
| 282 | + [eos_prob, unk, w1_prob, w2_prob], |
| 283 | + [eos_prob, unk, w1_prob, w2_prob], |
| 284 | + [eos_prob, unk, w1_prob, w2_prob], |
| 285 | + ]), |
| 286 | + # step 2: |
| 287 | + torch.FloatTensor([ |
| 288 | + # eos w1 w2 |
| 289 | + [1.0, unk, 0.0, 0.0], |
| 290 | + [1.0, unk, 0.0, 0.0], |
| 291 | + [1.0, unk, 0.0, 0.0], |
| 292 | + [1.0, unk, 0.0, 0.0], |
| 293 | + ]), |
| 294 | + ] |
| 295 | + |
| 296 | + task = test_utils.TestTranslationTask.setup_task(args, d, d) |
| 297 | + self.model = task.build_model(args) |
| 298 | + self.tgt_dict = task.target_dictionary |
| 299 | + |
| 300 | + def test_topp_sampling_search_low_prob(self): |
| 301 | + # Given a prob low enough to top-P sampling, we expect only the top |
| 302 | + # 1 token to be sampled, which always results in the same output. |
| 303 | + low_sampling_topp = self.min_top1_prob/2.0 |
| 304 | + generator = SequenceGenerator( |
| 305 | + self.tgt_dict, beam_size=2, sampling=True, |
| 306 | + sampling_topp=low_sampling_topp |
| 307 | + ) |
| 308 | + sample = { |
| 309 | + 'net_input': { |
| 310 | + 'src_tokens': self.src_tokens, |
| 311 | + 'src_lengths': self.src_lengths |
| 312 | + } |
| 313 | + } |
| 314 | + hypos = generator.generate([self.model], sample) |
| 315 | + eos, w1 = self.eos, self.w1 |
| 316 | + # sentence 1, beam 1 |
| 317 | + self.assertHypoTokens(hypos[0][0], [w1, w1, eos]) |
| 318 | + self.assertHypoScore(hypos[0][0], [1.0, 0.4, 1.0]) |
| 319 | + # sentence 1, beam 2 |
| 320 | + self.assertHypoTokens(hypos[0][1], [w1, w1, eos]) |
| 321 | + self.assertHypoScore(hypos[0][1], [1.0, 0.4, 1.0]) |
| 322 | + # sentence 2, beam 1 |
| 323 | + self.assertHypoTokens(hypos[1][0], [w1, w1, eos]) |
| 324 | + self.assertHypoScore(hypos[1][0], [1.0, 0.4, 1.0]) |
| 325 | + # sentence 2, beam 2 |
| 326 | + self.assertHypoTokens(hypos[1][1], [w1, w1, eos]) |
| 327 | + self.assertHypoScore(hypos[1][1], [1.0, 0.4, 1.0]) |
| 328 | + |
| 329 | + def test_topp_sampling_search_high_prob(self): |
| 330 | + # Given a prob high enough to top-P sampling, any of the top 2 |
| 331 | + # tokens could be sampled. This can cause different outputs. |
| 332 | + high_sampling_topp = (self.min_top1_prob+self.min_top2_prob)/2.0 |
| 333 | + generator = SequenceGenerator( |
| 334 | + self.tgt_dict, beam_size=2, sampling=True, |
| 335 | + sampling_topp=high_sampling_topp |
| 336 | + ) |
| 337 | + sample = { |
| 338 | + 'net_input': { |
| 339 | + 'src_tokens': self.src_tokens, |
| 340 | + 'src_lengths': self.src_lengths |
| 341 | + } |
| 342 | + } |
| 343 | + hypos = generator.generate([self.model], sample) |
| 344 | + eos, w1, w2 = self.eos, self.w1, self.w2 |
| 345 | + # sentence 1, beam 1 |
| 346 | + self.assertTrue(self.hypoTokens(hypos[0][0], [w1, w1, eos]) or |
| 347 | + self.hypoTokens(hypos[0][0], [w1, w2, eos])) |
| 348 | + self.assertTrue(self.hypoScore(hypos[0][0], [1.0, 0.4, 1.0]) or |
| 349 | + self.hypoScore(hypos[0][0], [1.0, 0.35, 1.0])) |
| 350 | + |
| 351 | + # sentence 1, beam 2 |
| 352 | + self.assertTrue(self.hypoTokens(hypos[0][1], [w1, w1, eos]) or |
| 353 | + self.hypoTokens(hypos[0][1], [w1, w2, eos])) |
| 354 | + self.assertTrue(self.hypoScore(hypos[0][1], [1.0, 0.4, 1.0]) or |
| 355 | + self.hypoScore(hypos[0][1], [1.0, 0.35, 1.0])) |
| 356 | + |
| 357 | + # sentence 2, beam 1 |
| 358 | + self.assertTrue(self.hypoTokens(hypos[1][0], [w1, w1, eos]) or |
| 359 | + self.hypoTokens(hypos[1][0], [w1, w2, eos])) |
| 360 | + self.assertTrue(self.hypoScore(hypos[1][0], [1.0, 0.4, 1.0]) or |
| 361 | + self.hypoScore(hypos[1][0], [1.0, 0.35, 1.0])) |
| 362 | + |
| 363 | + # sentence 2, beam 2 |
| 364 | + self.assertTrue(self.hypoTokens(hypos[1][1], [w1, w1, eos]) or |
| 365 | + self.hypoTokens(hypos[1][1], [w1, w2, eos])) |
| 366 | + self.assertTrue(self.hypoScore(hypos[1][1], [1.0, 0.4, 1.0]) or |
| 367 | + self.hypoScore(hypos[1][1], [1.0, 0.35, 1.0])) |
| 368 | + |
| 369 | + def hypoTokens(self, hypo, tokens): |
| 370 | + return self.tensorEqual(hypo['tokens'], torch.LongTensor(tokens)) |
| 371 | + |
| 372 | + def hypoScore(self, hypo, pos_probs, normalized=True, lenpen=1.): |
239 | 373 | pos_scores = torch.FloatTensor(pos_probs).log() |
240 | | - self.assertAlmostEqual(hypo['positional_scores'], pos_scores) |
241 | | - self.assertEqual(pos_scores.numel(), hypo['tokens'].numel()) |
| 374 | + if not self.almostEqual(hypo['positional_scores'], pos_scores): |
| 375 | + return False |
| 376 | + if pos_scores.numel() != hypo['tokens'].numel(): |
| 377 | + return False |
242 | 378 | score = pos_scores.sum() |
243 | 379 | if normalized: |
244 | | - score /= pos_scores.numel()**lenpen |
245 | | - self.assertLess(abs(score - hypo['score']), 1e-6) |
| 380 | + score /= pos_scores.numel() ** lenpen |
| 381 | + return abs(score - hypo['score']) < 1e-6 |
246 | 382 |
|
247 | | - def assertAlmostEqual(self, t1, t2): |
248 | | - self.assertEqual(t1.size(), t2.size(), "size mismatch") |
249 | | - self.assertLess((t1 - t2).abs().max(), 1e-4) |
| 383 | + def almostEqual(self, t1, t2): |
| 384 | + return t1.size() == t2.size() and (t1 - t2).abs().max() < 1e-4 |
250 | 385 |
|
251 | | - def assertTensorEqual(self, t1, t2): |
252 | | - self.assertEqual(t1.size(), t2.size(), "size mismatch") |
253 | | - self.assertEqual(t1.ne(t2).long().sum(), 0) |
| 386 | + def tensorEqual(self, t1, t2): |
| 387 | + return t1.size() == t2.size() and t1.ne(t2).long().sum() == 0 |
254 | 388 |
|
255 | 389 |
|
256 | 390 | if __name__ == '__main__': |
|
0 commit comments