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