|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# flake8: noqa |
| 3 | +"""Tests Model Optimizer nvfp4 models against ground truth generation |
| 4 | +Note: these tests will only pass on B200 |
| 5 | +""" |
| 6 | +import os |
| 7 | +from typing import List |
| 8 | + |
| 9 | +import pytest |
| 10 | +from transformers import AutoTokenizer |
| 11 | + |
| 12 | +from tests.quantization.utils import is_quant_method_supported |
| 13 | +from vllm import LLM, SamplingParams |
| 14 | + |
| 15 | +os.environ["TOKENIZERS_PARALLELISM"] = "true" |
| 16 | + |
| 17 | +MAX_MODEL_LEN = 1024 |
| 18 | + |
| 19 | +MODELS = ["nvidia/Llama-3.3-70B-Instruct-FP4"] |
| 20 | + |
| 21 | +EXPECTED_STRS_MAP = { |
| 22 | + "nvidia/Llama-3.3-70B-Instruct-FP4": [ |
| 23 | + 'vLLM (Vectorized Large Language Model) is indeed a high-throughput and memory-efficient inference', |
| 24 | + 'Here are the major milestones in the development of artificial intelligence (AI) from 1950 to ', |
| 25 | + 'Artificial intelligence (AI) and human intelligence (HI) are two distinct forms of intelligence that process', |
| 26 | + 'A neural network is a type of machine learning model inspired by the structure and function of the human brain', |
| 27 | + 'In the heart of a cutting-edge robotics lab, a team of engineers had been working tirelessly to push', |
| 28 | + 'The COVID-19 pandemic has had a profound impact on global economic structures and future business models, leading', |
| 29 | + 'The Mona Lisa, painted by Leonardo da Vinci in the early 16th century, is one of', |
| 30 | + 'Here are the translations:\n\n* Japanese: (Sasuga no tori ga miwa o ts' |
| 31 | + ] |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +# This test compares against golden strings for exact match since |
| 36 | +# there is no baseline implementation to compare against |
| 37 | +# and is unstable w.r.t specifics of the fp4 implementation or |
| 38 | +# the hardware being run on. |
| 39 | +# Disabled to prevent it from breaking the build |
| 40 | +@pytest.mark.skip( |
| 41 | + reason= |
| 42 | + "Prevent unstable test based on golden strings from breaking the build " |
| 43 | + " and test input model being too large and hanging the system.") |
| 44 | +@pytest.mark.quant_model |
| 45 | +@pytest.mark.skipif(not is_quant_method_supported("nvfp4"), |
| 46 | + reason="nvfp4 is not supported on this GPU type.") |
| 47 | +@pytest.mark.parametrize("model_name", MODELS) |
| 48 | +def test_models(example_prompts, model_name) -> None: |
| 49 | + model = LLM( |
| 50 | + model=model_name, |
| 51 | + max_model_len=MAX_MODEL_LEN, |
| 52 | + trust_remote_code=True, |
| 53 | + enforce_eager=True, |
| 54 | + quantization="nvfp4", |
| 55 | + ) |
| 56 | + |
| 57 | + tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 58 | + formatted_prompts = [ |
| 59 | + tokenizer.apply_chat_template([{ |
| 60 | + "role": "user", |
| 61 | + "content": prompt |
| 62 | + }], |
| 63 | + tokenize=False, |
| 64 | + add_generation_prompt=True) |
| 65 | + for prompt in example_prompts |
| 66 | + ] |
| 67 | + params = SamplingParams(max_tokens=20, temperature=0) |
| 68 | + generations: List[str] = [] |
| 69 | + # Note: these need to be run 1 at a time due to numerical precision, |
| 70 | + # since the expected strs were generated this way. |
| 71 | + for prompt in formatted_prompts: |
| 72 | + outputs = model.generate(prompt, params) |
| 73 | + generations.append(outputs[0].outputs[0].text) |
| 74 | + del model |
| 75 | + |
| 76 | + print(model_name, generations) |
| 77 | + expected_strs = EXPECTED_STRS_MAP[model_name] |
| 78 | + for i in range(len(example_prompts)): |
| 79 | + generated_str = generations[i] |
| 80 | + expected_str = expected_strs[i] |
| 81 | + assert expected_str == generated_str, ( |
| 82 | + f"Test{i}:\nExpected: {expected_str!r}\nvLLM: {generated_str!r}") |
0 commit comments