| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import torch |
|
|
| from nemo.collections.speechlm2.parts.label_prep import maybe_prepend_prompt_tokens |
|
|
|
|
| def test_maybe_prepend_prompt_tokens_with_source_tokens(): |
| """Test that prompt tokens are correctly prepended to all sequences and lengths are updated.""" |
| B, T_src, T_tgt, H = 2, 6, 6, 4 |
| PAD = 0 |
| prompt_len_0, prompt_len_1 = 3, 2 |
|
|
| |
| max_prompt_len = max(prompt_len_0, prompt_len_1) |
| prompt_tokens = torch.full((B, max_prompt_len), PAD, dtype=torch.long) |
| prompt_tokens[0, :prompt_len_0] = torch.tensor([10, 11, 12]) |
| prompt_tokens[1, :prompt_len_1] = torch.tensor([20, 21]) |
|
|
| |
| def embed_fn(token_ids): |
| return token_ids.unsqueeze(-1).expand(-1, -1, H).float() |
|
|
| |
| source_encoded = torch.arange(B * T_src * H).reshape(B, T_src, H).float() |
| source_encoded_lens = torch.tensor([5, 4]) |
|
|
| |
| target_tokens = torch.tensor( |
| [ |
| [1, 100, 101, 102, 2, 0], |
| [1, 200, 201, 2, 0, 0], |
| ] |
| ) |
| target_token_lens = torch.tensor([5, 4]) |
|
|
| |
| source_tokens = torch.tensor( |
| [ |
| [1, 50, 51, 52, 2, 0], |
| [1, 60, 61, 2, 0, 0], |
| ] |
| ) |
| source_token_lens = torch.tensor([5, 4]) |
|
|
| batch = { |
| "prompt_tokens": prompt_tokens, |
| "prompt_token_lens": torch.tensor([prompt_len_0, prompt_len_1]), |
| "target_tokens": target_tokens, |
| "target_token_lens": target_token_lens, |
| "source_tokens": source_tokens, |
| "source_token_lens": source_token_lens, |
| } |
|
|
| new_source_encoded, new_source_encoded_lens, new_target_tokens = maybe_prepend_prompt_tokens( |
| batch=batch, |
| embed_fn=embed_fn, |
| source_encoded=source_encoded, |
| source_encoded_lens=source_encoded_lens, |
| text_pad_id=PAD, |
| ) |
|
|
| |
| assert new_source_encoded.shape == (B, max_prompt_len + T_src, H) |
| assert new_target_tokens.shape == (B, max_prompt_len + T_tgt) |
| assert batch["source_tokens"].shape == (B, max_prompt_len + T_tgt) |
|
|
| |
| assert new_source_encoded_lens[0].item() == 5 + prompt_len_0 |
| assert new_source_encoded_lens[1].item() == 4 + prompt_len_1 |
| assert batch["target_token_lens"][0].item() == 5 + prompt_len_0 |
| assert batch["target_token_lens"][1].item() == 4 + prompt_len_1 |
| assert batch["source_token_lens"][0].item() == 5 + prompt_len_0 |
| assert batch["source_token_lens"][1].item() == 4 + prompt_len_1 |
|
|
| |
| |
| for h in range(H): |
| assert new_source_encoded[0, 0, h].item() == 10.0 |
| assert new_source_encoded[0, 1, h].item() == 11.0 |
| assert new_source_encoded[0, 2, h].item() == 12.0 |
| assert new_source_encoded[1, 0, h].item() == 20.0 |
| assert new_source_encoded[1, 1, h].item() == 21.0 |
|
|
| |
| for t in range(5): |
| assert torch.equal(new_source_encoded[0, prompt_len_0 + t], source_encoded[0, t]) |
| for t in range(4): |
| assert torch.equal(new_source_encoded[1, prompt_len_1 + t], source_encoded[1, t]) |
|
|
| |
| assert new_target_tokens[0, :prompt_len_0].tolist() == [PAD] * prompt_len_0 |
| assert new_target_tokens[0, prompt_len_0 : prompt_len_0 + 5].tolist() == [1, 100, 101, 102, 2] |
| assert new_target_tokens[1, :prompt_len_1].tolist() == [PAD] * prompt_len_1 |
| assert new_target_tokens[1, prompt_len_1 : prompt_len_1 + 4].tolist() == [1, 200, 201, 2] |
|
|
| |
| assert batch["source_tokens"][0, :prompt_len_0].tolist() == [PAD] * prompt_len_0 |
| assert batch["source_tokens"][0, prompt_len_0 : prompt_len_0 + 5].tolist() == [1, 50, 51, 52, 2] |
| assert batch["source_tokens"][1, :prompt_len_1].tolist() == [PAD] * prompt_len_1 |
| assert batch["source_tokens"][1, prompt_len_1 : prompt_len_1 + 4].tolist() == [1, 60, 61, 2] |
|
|
|
|
| def test_maybe_prepend_prompt_tokens_no_prompt(): |
| """Test that without prompt_tokens in batch, inputs are returned unchanged.""" |
| B, T_src, H = 1, 4, 4 |
| source_encoded = torch.randn(B, T_src, H) |
| source_encoded_lens = torch.tensor([3]) |
| target_tokens = torch.tensor([[1, 100, 2, 0]]) |
|
|
| batch = { |
| "target_tokens": target_tokens, |
| "target_token_lens": torch.tensor([3]), |
| } |
|
|
| out_encoded, out_lens, out_tokens = maybe_prepend_prompt_tokens( |
| batch=batch, |
| embed_fn=lambda x: x.unsqueeze(-1).expand(-1, -1, H).float(), |
| source_encoded=source_encoded, |
| source_encoded_lens=source_encoded_lens, |
| text_pad_id=0, |
| ) |
|
|
| assert torch.equal(out_encoded, source_encoded) |
| assert torch.equal(out_lens, source_encoded_lens) |
| assert torch.equal(out_tokens, target_tokens) |
|
|