def test_pack_padded_long_sequence_forward_backward(total_length, padding_value, batch_first, shapes, seed, ctx, func_name): if not func_name.endswith("Cuda"): pytest.skip( "PackPaddedSequence tests except for Cuda for very long sequence skips.") from nbla_test_utils import function_tester rng = np.random.RandomState(seed) sequences = [rng.randn(*shape).astype(np.float32) for shape in shapes] padded_sequence = pad_sequence(sequences, batch_first) lengths = np.array([seq.shape[0] for seq in sequences]) inputs = [padded_sequence, lengths] func_args0 = [batch_first] func_args1 = [batch_first, padding_value, total_length] insert_identity = [True, False] # Forward function_tester(rng, F.pack_padded_sequence, ref_pack_padded_sequence, inputs, ctx=ctx, func_name=func_name, func_args=func_args0, backward=[False, False], atol_f=1e-3, atol_b=1e-2, insert_identity=insert_identity) # Backward import nnabla as nn padded_sequence0 = nn.Variable.from_numpy_array( inputs[0]).apply(need_grad=True) lengths = nn.Variable.from_numpy_array(inputs[1]) with nn.context_scope(ctx), nn.auto_forward(): # Pack backward padded_sequence0.g = rng.randn(*padded_sequence0.shape) packed_sequence0, batch_sizes = F.pack_padded_sequence( padded_sequence0, lengths, *func_args0) g = rng.randn(*packed_sequence0.shape) packed_sequence0.g = g packed_sequence0.parent.backward([padded_sequence0, lengths], [packed_sequence0, batch_sizes], [False, False]) # Unpack packed_sequence1 = nn.Variable.from_numpy_array(g) padded_sequence1, lengths = F.pad_packed_sequence( packed_sequence1, batch_sizes, *func_args1) # Compare w/o accum np.testing.assert_allclose(padded_sequence0.g.flatten(), padded_sequence1.d.flatten( )[:np.prod(padded_sequence0.shape)], atol=1e-4, err_msg="{} test (w/o accum) with long sequence failed.".format(func_name)) # Compare w/ accum packed_sequence0.parent.backward([padded_sequence0, lengths], [packed_sequence0, batch_sizes], [True, False]) np.testing.assert_allclose(padded_sequence0.g.flatten() / 2, padded_sequence1.d.flatten( )[:np.prod(padded_sequence0.shape)], atol=1e-4, err_msg="{} test (w/ accum) with long sequence failed.".format(func_name))
def test_pack_padded_sequence_forward_backward(batch_first, shapes, seed, ctx, func_name): from nbla_test_utils import function_tester rng = np.random.RandomState(seed) sequences = [rng.randn(*shape).astype(np.float32) for shape in shapes] padded_sequence = pad_sequence(sequences, batch_first) lengths = np.array([seq.shape[0] for seq in sequences]) inputs = [padded_sequence, lengths] func_args = [batch_first] insert_identity = [True, False] function_tester(rng, F.pack_padded_sequence, ref_pack_padded_sequence, inputs, ctx=ctx, func_name=func_name, func_args=func_args, backward=[True, False], disable_half_test=False, atol_f=1e-3, atol_b=1e-2, insert_identity=insert_identity)