(self, pad_id: int)
| 560 | |
| 561 | @parameterized.parameters([0, 1]) |
| 562 | def test_beam_search_decode_no_prefix(self, pad_id: int): |
| 563 | beam_size = 2 |
| 564 | |
| 565 | def token_to_scores(ids, cache): # pylint: disable=unused-argument |
| 566 | # Use id 2 then 3 for batch element 0 and id 3 then 2 for element 1. |
| 567 | log_probs = np.repeat( |
| 568 | np.expand_dims( |
| 569 | np.array( |
| 570 | [[-1e5, -1e6, -0.37, -1.17], [-1e5, -1e6, -1.17, -0.37]], dtype=np.float32 |
| 571 | ), |
| 572 | axis=1, |
| 573 | ), |
| 574 | [beam_size], |
| 575 | axis=1, |
| 576 | ) |
| 577 | log_probs = decoding.flatten_decoding_dim(log_probs) |
| 578 | return log_probs, {} |
| 579 | |
| 580 | # No prefix is passed. |
| 581 | inputs = np.full((2, 5), pad_id, dtype=np.int32) |
| 582 | beam_search_output = decoding.beam_search_decode( |
| 583 | inputs=inputs, |
| 584 | time_step=decoding.infer_initial_time_step(inputs, pad_id=pad_id), |
| 585 | cache={}, |
| 586 | tokens_to_scores=token_to_scores, |
| 587 | eos_id=EOS_ID, |
| 588 | num_decodes=beam_size, |
| 589 | pad_id=pad_id, |
| 590 | ) |
| 591 | |
| 592 | # Beam search scores are in the descending order. |
| 593 | self.assertTrue(np.all(np.diff(beam_search_output.scores) <= 0)) |
| 594 | self.assertTrue(np.all(np.diff(beam_search_output.live_scores) <= 0)) |
| 595 | np.testing.assert_array_equal( |
| 596 | beam_search_output.sequences, |
| 597 | np.array( |
| 598 | [ |
| 599 | [[1, pad_id, pad_id, pad_id, pad_id], [2, 1, pad_id, pad_id, pad_id]], |
| 600 | [[1, pad_id, pad_id, pad_id, pad_id], [3, 1, pad_id, pad_id, pad_id]], |
| 601 | ], |
| 602 | ), |
| 603 | ) |
| 604 | np.testing.assert_array_equal( |
| 605 | beam_search_output.live_sequences, |
| 606 | np.array([[[2, 2, 2, 2, 2], [2, 2, 2, 2, 3]], [[3, 3, 3, 3, 3], [3, 3, 3, 3, 2]]]), |
| 607 | ) |
| 608 | |
| 609 | @parameterized.product(prefix_length=[1, 3, 6, 7], low=[0, 1]) |
| 610 | def test_beam_init_with_inputs(self, prefix_length: int, low: int): |
nothing calls this directly
no test coverage detected