|
5 | 5 | # pylint: disable=no-self-use,too-many-branches |
6 | 6 | import contextlib |
7 | 7 | import unittest |
8 | | -from typing import Callable, Literal, Optional |
| 8 | +from typing import Callable, Optional |
9 | 9 | from unittest import mock |
10 | 10 |
|
11 | 11 | import chex |
@@ -473,25 +473,71 @@ def test_extend_step( |
473 | 473 | StackedTransformerLayer.default_config(), |
474 | 474 | RepeatedTransformerLayer.default_config(), |
475 | 475 | ], |
| 476 | + cross_attention_mode=["none"], |
| 477 | + prefix_length=[jnp.array([1, 1])], |
| 478 | + method=["sample_decode"], |
| 479 | + pad_token_id=[-1], |
| 480 | + ) |
| 481 | + def test_decode_stack( |
| 482 | + self, stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id |
| 483 | + ): |
| 484 | + self._test_decode(stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id) |
| 485 | + |
| 486 | + @parameterized.product( |
| 487 | + stack_cfg=[StackedTransformerLayer.default_config()], |
476 | 488 | cross_attention_mode=["none", "full", "broadcast"], |
477 | | - num_decodes=[5], |
| 489 | + prefix_length=[jnp.array([1, 1])], |
| 490 | + method=["sample_decode"], |
| 491 | + pad_token_id=[-1], |
| 492 | + ) |
| 493 | + def test_decode_xatten( |
| 494 | + self, stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id |
| 495 | + ): |
| 496 | + self._test_decode(stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id) |
| 497 | + |
| 498 | + @parameterized.product( |
| 499 | + stack_cfg=[StackedTransformerLayer.default_config()], |
| 500 | + cross_attention_mode=["none"], |
478 | 501 | # Each is of shape [batch], representing per-example prefix lengths. |
479 | 502 | prefix_length=[jnp.array([1, 1]), jnp.array([1, 3, 6])], |
| 503 | + method=["sample_decode"], |
| 504 | + pad_token_id=[-1], |
| 505 | + ) |
| 506 | + def test_decode_prefix_len( |
| 507 | + self, stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id |
| 508 | + ): |
| 509 | + self._test_decode(stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id) |
| 510 | + |
| 511 | + @parameterized.product( |
| 512 | + stack_cfg=[StackedTransformerLayer.default_config()], |
| 513 | + cross_attention_mode=["none"], |
| 514 | + prefix_length=[jnp.array([1, 1])], |
480 | 515 | method=["sample_decode", "beam_search_decode"], |
| 516 | + pad_token_id=[-1], |
| 517 | + ) |
| 518 | + def test_decode_method( |
| 519 | + self, stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id |
| 520 | + ): |
| 521 | + self._test_decode(stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id) |
| 522 | + |
| 523 | + @parameterized.product( |
| 524 | + stack_cfg=[StackedTransformerLayer.default_config()], |
| 525 | + cross_attention_mode=["none"], |
| 526 | + prefix_length=[jnp.array([1, 1])], |
| 527 | + method=["sample_decode"], |
481 | 528 | pad_token_id=[0, -1], |
482 | 529 | ) |
483 | | - # pylint: disable-next=too-many-statements |
484 | | - def test_decode( |
485 | | - self, |
486 | | - stack_cfg: InstantiableConfig, |
487 | | - cross_attention_mode: Literal["none", "full", "broadcast"], |
488 | | - num_decodes: int, |
489 | | - prefix_length: utils.Tensor, |
490 | | - method: Literal["sample_decode", "beam_search_decode"], |
491 | | - pad_token_id: int, |
| 530 | + def test_decode_pad_token_id( |
| 531 | + self, stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id |
492 | 532 | ): |
| 533 | + self._test_decode(stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id) |
| 534 | + |
| 535 | + # Running everything as product tests causes CI timeout. |
| 536 | + # pylint: disable-next=too-many-statements |
| 537 | + def _test_decode(self, stack_cfg, cross_attention_mode, prefix_length, method, pad_token_id): |
493 | 538 | """Test beam search and sample decoding from a randomly initialized decoder.""" |
494 | 539 | batch_size, src_len, tgt_len, vocab_size = prefix_length.shape[0], 11, 10, 6 |
| 540 | + num_decodes = 5 |
495 | 541 | bos_id = eos_id = 1 |
496 | 542 | num_layers, num_heads = 3, 4 |
497 | 543 | hidden_dim, src_dim = 12, 10 |
@@ -714,7 +760,7 @@ def test_token_scores_match_between_decoded_and_prefix(self): |
714 | 760 | # No need to test multiple batches |
715 | 761 | batch_size = 1 |
716 | 762 | # Long enough target length to get enough token scores to compare. |
717 | | - target_length = 32 |
| 763 | + target_length = 16 |
718 | 764 | # Prefix length long enough to check that we reconstruct the prefix for the second pass |
719 | 765 | # from the output sequence of the first pass. |
720 | 766 | prefix_1_length = 4 |
|
0 commit comments