|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. |
| 3 | +# Copyright 2023 The vLLM team. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# This file is a part of the vllm-ascend project. |
| 17 | +# Adapted from vllm-project/blob/main/tests/entrypoints/llm/test_accuracy.py |
| 18 | +# |
| 19 | + |
| 20 | +import gc |
| 21 | +import multiprocessing |
| 22 | +from multiprocessing import Queue |
| 23 | + |
| 24 | +import lm_eval |
| 25 | +import pytest |
| 26 | +import torch |
| 27 | + |
| 28 | +# pre-trained model path on Hugging Face. |
| 29 | +MODELS = ["deepseek-ai/DeepSeek-V2-Lite"] |
| 30 | +# Math reasoning benchmark (Grade School Math 8K). |
| 31 | +TASK = "gsm8k" |
| 32 | +# Answer validation requiring format consistency. |
| 33 | +FILTER = "exact_match,strict-match" |
| 34 | +# 3% relative tolerance for numerical accuracy. |
| 35 | +RTOL = 0.03 |
| 36 | +# Baseline accuracy after VLLM optimization. |
| 37 | +EXPECTED_VALUE = 0.316 |
| 38 | + |
| 39 | + |
| 40 | +def run_test(model_name, queue, more_args=None): |
| 41 | + model_args = f"pretrained={model_name},max_model_len=4096,trust_remote_code=True,tensor_parallel_size=4" |
| 42 | + if more_args is not None: |
| 43 | + model_args = f"{model_args},{more_args}" |
| 44 | + results = lm_eval.simple_evaluate( |
| 45 | + model="vllm", |
| 46 | + model_args=model_args, |
| 47 | + tasks=TASK, |
| 48 | + batch_size="auto", |
| 49 | + ) |
| 50 | + result = results["results"][TASK][FILTER] |
| 51 | + print("result:", result) |
| 52 | + queue.put(result) |
| 53 | + del results |
| 54 | + torch.npu.empty_cache() |
| 55 | + gc.collect() |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize("model", MODELS) |
| 59 | +def test_lm_eval_accuracy(model, monkeypatch: pytest.MonkeyPatch): |
| 60 | + with monkeypatch.context(): |
| 61 | + result_queue: Queue[float] = multiprocessing.Queue() |
| 62 | + p = multiprocessing.Process(target=run_test, |
| 63 | + args=( |
| 64 | + model, |
| 65 | + result_queue, |
| 66 | + )) |
| 67 | + p.start() |
| 68 | + p.join() |
| 69 | + result = result_queue.get() |
| 70 | + assert (EXPECTED_VALUE - RTOL < result < EXPECTED_VALUE + RTOL), \ |
| 71 | + f"Expected: {EXPECTED_VALUE}±{RTOL} | Measured: {result}" |
0 commit comments