MCPcopy Create free account
hub / github.com/apple/axlearn / test_prefix_merger

Method test_prefix_merger

axlearn/audio/decoder_asr_test.py:738–877  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

736
737 @set_threefry_partitionable(False) # TODO(markblee): update for threefry_partitionable True
738 def test_prefix_merger(self):
739 # Use a small vocab_size to encourage similar prefixes.
740 input_dim, vocab_size, num_decodes = 6, 3, 4
741 cfg: CTCDecoderModel.Config = CTCDecoderModel.default_config().set(
742 input_dim=input_dim,
743 vocab_size=vocab_size,
744 )
745 # Initialize layer parameters.
746 layer: CTCDecoderModel = cfg.set(name="test").instantiate(parent=None)
747 prng_key = jax.random.PRNGKey(123)
748 decode_key, init_key, input_key = jax.random.split(prng_key, num=3)
749 layer_params = layer.initialize_parameters_recursively(init_key)
750
751 batch_size, max_seq_len = 4, 8
752 seq_len = jnp.array([8, 5, 4, 6])
753 # [batch_size, max_seq_len, dim].
754 inputs = jax.random.normal(input_key, [batch_size, max_seq_len, input_dim]) * 1000
755 # [batch_size, max_seq_len].
756 paddings = jnp.arange(max_seq_len) >= seq_len[:, None]
757
758 @functools.partial(jax.jit, static_argnames=("method", "prefix_merger", "num_decodes"))
759 def jit_method(inputs, prng_key, method, prefix_merger=None, num_decodes=None):
760 if prefix_merger is not None:
761 inputs["prefix_merger"] = prefix_merger
762 if num_decodes is not None:
763 inputs["num_decodes"] = num_decodes
764 outputs, _ = F(
765 layer,
766 inputs=inputs,
767 is_training=True,
768 prng_key=prng_key,
769 state=layer_params,
770 method=method,
771 )
772 return outputs
773
774 input_batch = dict(input_batch=dict(inputs=inputs, paddings=paddings))
775
776 # Decode without merging.
777 beam_search_outputs: DecodeOutputs = jit_method(
778 input_batch,
779 prng_key=decode_key,
780 method="beam_search_decode",
781 num_decodes=num_decodes,
782 )
783 self.assertNestedEqual(
784 jnp.array(
785 [
786 [
787 # Without merging prefixes, sequences 0 and 1 are duplicates.
788 [1, 2, 1, 2, 1, 0, 0, 0],
789 [1, 2, 1, 2, 1, 0, 0, 0],
790 [1, 2, 1, 2, 1, 2, 0, 0],
791 [1, 2, 2, 1, 0, 0, 0, 0],
792 ],
793 [
794 # Sequence 0 and 3 are duplicates.
795 [2, 1, 1, 2, 0, 0, 0, 0],

Callers

nothing calls this directly

Calls 6

CommonPrefixMergerClass · 0.90
assertNestedEqualMethod · 0.80
setMethod · 0.45
default_configMethod · 0.45
instantiateMethod · 0.45

Tested by

no test coverage detected