test_modelopt.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # flake8: noqa
  2. """Tests Model Optimizer fp8 models against ground truth generation
  3. Note: these tests will only pass on H100
  4. """
  5. import os
  6. from typing import List
  7. import pytest
  8. from transformers import AutoTokenizer
  9. from aphrodite import LLM, SamplingParams
  10. from tests.quantization.utils import is_quant_method_supported
  11. os.environ["TOKENIZERS_PARALLELISM"] = "true"
  12. MAX_MODEL_LEN = 1024
  13. MODELS = ["nvidia/Llama-3.1-8B-Instruct-FP8"]
  14. EXPECTED_STRS_MAP = {
  15. "nvidia/Llama-3.1-8B-Instruct-FP8": [
  16. "You're referring to VLLM, a high-performance Large Language Model (LLM) inference and",
  17. 'Here are the major milestones in the development of artificial intelligence (AI) from 1950 to ',
  18. 'The comparison between artificial intelligence (AI) and human intelligence in terms of processing information is a complex and',
  19. 'A neural network is a complex system modeled after the human brain, consisting of interconnected nodes or "ne',
  20. '**The Spark of Imagination**\n\nZeta-5, a sleek and efficient robot, whir',
  21. 'The COVID-19 pandemic has had a profound impact on global economic structures and business models, leading to',
  22. 'The Mona Lisa, painted by Leonardo da Vinci in the early 16th century, is one of',
  23. 'Here are the translations:\n\n**Japanese:** 「早起きは早く獲物をとる'
  24. ]
  25. }
  26. # This test compares against golden strings for exact match since
  27. # there is no baseline implementation to compare against
  28. # and is unstable w.r.t specifics of the fp8 implementation or
  29. # the hardware being run on.
  30. # Disabled to prevent it from breaking the build
  31. @pytest.mark.skip(
  32. reason=
  33. "Prevent unstable test based on golden strings from breaking the build.")
  34. @pytest.mark.skipif(not is_quant_method_supported("fp8"),
  35. reason="fp8 is not supported on this GPU type.")
  36. @pytest.mark.parametrize("model_name", MODELS)
  37. def test_models(example_prompts, model_name) -> None:
  38. model = LLM(
  39. model=model_name,
  40. max_model_len=MAX_MODEL_LEN,
  41. trust_remote_code=True,
  42. enforce_eager=True,
  43. quantization="modelopt",
  44. )
  45. tokenizer = AutoTokenizer.from_pretrained(model_name)
  46. formatted_prompts = [
  47. tokenizer.apply_chat_template([{
  48. "role": "user",
  49. "content": prompt
  50. }],
  51. tokenize=False,
  52. add_generation_prompt=True)
  53. for prompt in example_prompts
  54. ]
  55. params = SamplingParams(max_tokens=20, temperature=0)
  56. generations: List[str] = []
  57. # Note: these need to be run 1 at a time due to numerical precision,
  58. # since the expected strs were generated this way.
  59. for prompt in formatted_prompts:
  60. outputs = model.generate(prompt, params)
  61. generations.append(outputs[0].outputs[0].text)
  62. del model
  63. print(model_name, generations)
  64. expected_strs = EXPECTED_STRS_MAP[model_name]
  65. for i in range(len(example_prompts)):
  66. generated_str = generations[i]
  67. expected_str = expected_strs[i]
  68. assert expected_str == generated_str, (
  69. f"Test{i}:\nExpected: {expected_str!r}\nvLLM: {generated_str!r}")