test_detokenize.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import pytest
  2. from transformers import AutoTokenizer
  3. from aphrodite.transformers_utils.tokenizer import detokenize_incrementally
  4. TRUTH = [
  5. "Tell me your favorite story.",
  6. "Transformers have revolutionized almost all natural language processing (NLP) tasks but suffer from memory and computational complexity that scales quadratically with sequence length. In contrast, recurrent neural networks (RNNs) exhibit linear scaling in memory and computational requirements but struggle to match the same performance as Transformers due to limitations in parallelization and scalability. We propose a novel model architecture, Receptance Weighted Key Value (RWKV), that combines the efficient parallelizable training of Transformers with the efficient inference of RNNs. Our approach leverages a linear attention mechanism and allows us to formulate the model as either a Transformer or an RNN, which parallelizes computations during training and maintains constant computational and memory complexity during inference, leading to the first non-transformer architecture to be scaled to tens of billions of parameters. Our experiments reveal that RWKV performs on par with similarly sized Transformers, suggesting that future work can leverage this architecture to create more efficient models. This work presents a significant step towards reconciling the trade-offs between computational efficiency and model performance in sequence processing tasks." # noqa: E501
  7. "トランスフォーマーは、ほぼすべての自然言語処理に革命をもたらしました",
  8. ]
  9. TOKENIZERS = [
  10. "EleutherAI/gpt-j-6b",
  11. "EleutherAI/pythia-70m-deduped",
  12. "meta-llama/llama-2-7b-hf",
  13. "/mistralai/Mistral-7B-v0.1",
  14. ]
  15. def _run_incremental_decode(tokenizer, all_input_ids,
  16. skip_special_tokens: bool):
  17. decoded_text = ""
  18. offset = 0
  19. token_offset = 0
  20. prev_tokens = None
  21. for i in range(len(all_input_ids)):
  22. new_tokens, text, offset, token_offset = detokenize_incrementally(
  23. tokenizer,
  24. all_input_ids[:i + 1],
  25. prev_tokens,
  26. offset,
  27. token_offset,
  28. skip_special_tokens=skip_special_tokens)
  29. decoded_text += text
  30. if prev_tokens is None:
  31. prev_tokens = new_tokens
  32. else:
  33. prev_tokens += new_tokens
  34. return decoded_text
  35. @pytest.mark.parametrize("truth", TRUTH)
  36. @pytest.mark.parametrize("tokenizer_id", TOKENIZERS)
  37. @pytest.mark.parametrize("skip_special_tokens", (True, False))
  38. def test_decode_streaming(tokenizer_id, truth, skip_special_tokens):
  39. tokenizer = AutoTokenizer.from_pretrained(tokenizer_id)
  40. all_input_ids = tokenizer(truth, add_special_tokens=False)["input_ids"]
  41. if skip_special_tokens:
  42. all_input_ids = (
  43. [tokenizer_id.bos_token_id] if tokenizer.bos_token_id is not None
  44. else []) + all_input_ids + [tokenizer.eos_token_id] # type: ignore
  45. decoded_text = _run_incremental_decode(
  46. tokenizer, all_input_ids, skip_special_tokens=skip_special_tokens)
  47. assert decoded_text == truth