(self)
| 877 | self.assertLess(beam_search_outputs.scores[3, 3], 0.5 * NEG_INF) |
| 878 | |
| 879 | def test_postprocess(self): |
| 880 | input_dim, vocab_size = 3, 8 |
| 881 | cfg = CTCDecoderModel.default_config().set(input_dim=input_dim, vocab_size=vocab_size) |
| 882 | |
| 883 | # Initialize layer parameters. |
| 884 | layer: CTCDecoderModel = cfg.set(name="test").instantiate(parent=None) |
| 885 | layer_params = layer.initialize_parameters_recursively(jax.random.PRNGKey(123)) |
| 886 | |
| 887 | sequences = jnp.array( |
| 888 | [ |
| 889 | [[3, 0, 5, 0, 0, 6, 6, 2, 0, 0, 0], [5, 4, 4, 5, 5, 0, 0, 4, 3, 0, 0]], |
| 890 | [[4, 4, 4, 0, 4, 6, 4, 4, 2, 0, 0], [3, 5, 0, 0, 0, 6, 2, 2, 2, 2, 0]], |
| 891 | ], |
| 892 | ) |
| 893 | paddings = jnp.array( |
| 894 | [ |
| 895 | [0, 0, 0, 0, 0, 0, 0, 0, 1, 1], |
| 896 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 1], |
| 897 | ] |
| 898 | ).astype(jnp.bool) |
| 899 | scores = jnp.array( |
| 900 | [ |
| 901 | [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
| 902 | [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], |
| 903 | ] |
| 904 | ) |
| 905 | |
| 906 | @jax.jit |
| 907 | def jit_postprocess(sequences, paddings, scores): |
| 908 | outputs, _ = F( |
| 909 | layer, |
| 910 | inputs=dict(sequences=sequences, paddings=paddings, scores=scores), |
| 911 | is_training=True, |
| 912 | prng_key=jax.random.PRNGKey(456), |
| 913 | state=layer_params, |
| 914 | method="_postprocess_outputs", |
| 915 | ) |
| 916 | return outputs |
| 917 | |
| 918 | outputs: DecodeOutputs = jit_postprocess(sequences, paddings, scores) |
| 919 | self.assertNestedEqual( |
| 920 | outputs.raw_sequences, |
| 921 | jnp.array( |
| 922 | [ |
| 923 | [[3, 0, 5, 0, 0, 6, 6, 2, 0, 0], [5, 4, 4, 5, 5, 0, 0, 4, 0, 0]], |
| 924 | [[4, 4, 4, 0, 4, 6, 4, 4, 2, 0], [3, 5, 0, 0, 0, 6, 2, 2, 2, 0]], |
| 925 | ] |
| 926 | ), |
| 927 | ) |
| 928 | self.assertNestedEqual( |
| 929 | outputs.sequences, |
| 930 | jnp.array( |
| 931 | [ |
| 932 | [[3, 5, 6, 2, 0, 0, 0, 0, 0, 0], [5, 4, 5, 4, 0, 0, 0, 0, 0, 0]], |
| 933 | [[4, 4, 6, 4, 2, 0, 0, 0, 0, 0], [3, 5, 6, 2, 0, 0, 0, 0, 0, 0]], |
| 934 | ] |
| 935 | ), |
| 936 | ) |
nothing calls this directly
no test coverage detected