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

Method test_predict

axlearn/audio/decoder_asr_test.py:384–436  ·  view source on GitHub ↗
(self, blank_id)

Source from the content-addressed store, hash-verified

382
383 @parameterized.parameters([0, 1])
384 def test_predict(self, blank_id):
385 input_dim, vocab_size = 6, 8
386 cfg = CTCDecoderModel.default_config().set(
387 input_dim=input_dim,
388 vocab_size=vocab_size,
389 blank_id=blank_id,
390 )
391 # Initialize layer parameters.
392 layer: CTCDecoderModel = cfg.set(name="test").instantiate(parent=None)
393 prng_key = jax.random.PRNGKey(123)
394 prng_key, init_key = jax.random.split(prng_key)
395 layer_params = layer.initialize_parameters_recursively(init_key)
396 self.assertEqual(
397 {"lm_head": dict(weight=(input_dim, vocab_size), bias=(vocab_size,))},
398 shapes(layer_params),
399 )
400
401 batch_size, max_seq_len = 2, 10
402 seq_len = jnp.array([7, 5])
403 # [batch_size, max_seq_len, dim] with the same data across sequences.
404 inputs = jnp.tile(
405 jax.random.normal(jax.random.PRNGKey(123), [1, max_seq_len, input_dim]),
406 [batch_size, 1, 1],
407 )
408 # [batch_size, max_seq_len].
409 paddings = jnp.arange(max_seq_len) >= seq_len[:, None]
410
411 # Generate different padding data.
412 padding_data = jax.random.normal(
413 jax.random.PRNGKey(130), [batch_size, max_seq_len, input_dim]
414 )
415 # Generate input sequences with the same data at non-pad positions.
416 inputs = jnp.where(paddings[..., None], padding_data, inputs)
417
418 @jax.jit
419 def jit_predict(input_batch):
420 outputs, _ = F(
421 layer,
422 inputs=dict(input_batch=input_batch),
423 is_training=True,
424 prng_key=prng_key,
425 state=layer_params,
426 method="predict",
427 )
428 return outputs
429
430 outputs = jit_predict(dict(inputs=inputs, paddings=paddings))
431 self.assertSequenceEqual((batch_size, max_seq_len, vocab_size), outputs.shape)
432 # Check that the outputs are the same in the non-padded positions.
433 assert_allclose(outputs[0, : seq_len[1]], outputs[1, : seq_len[1]])
434 outputs_at_padding = paddings[:, :, None] * outputs
435 # Check that all padding position have 0 outputs.
436 self.assertTrue(jnp.all(jnp.logical_not(outputs_at_padding)))
437
438 @parameterized.parameters([0, 1])
439 def test_forward(self, blank_id):

Callers

nothing calls this directly

Calls 6

shapesFunction · 0.90
assert_allcloseFunction · 0.90
setMethod · 0.45
default_configMethod · 0.45
instantiateMethod · 0.45

Tested by

no test coverage detected