-
Notifications
You must be signed in to change notification settings - Fork 188
Add LM Eval Testing #945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add LM Eval Testing #945
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
168bd2f
allow LM Eval Testing
dsikka 8c5aa69
log metrics
dsikka 42a8f2a
fix check
dsikka 18c6777
remove upload for now
dsikka 0cbb219
fix docstring
dsikka 5ef3500
Merge branch 'main' into add_lm_eval
dsikka 25c564c
add config; add version
dsikka 52c4226
update
dsikka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cadence: "weekly" | ||
model: meta-llama/Meta-Llama-3-8B-Instruct | ||
scheme: FP8_DYNAMIC | ||
num_fewshot: 5 | ||
limit: 1000 | ||
task: "gsm8k" | ||
exact_match,flexible-extract: 0.753 | ||
exact_match,strict-match: 0.753 |
8 changes: 8 additions & 0 deletions
8
tests/e2e/vLLM/lm_eval_configs/int8_w8a8_dynamic_per_token.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cadence: "weekly" | ||
model: meta-llama/Meta-Llama-3-8B-Instruct | ||
scheme: INT8 | ||
num_fewshot: 5 | ||
limit: 250 | ||
task: "gsm8k" | ||
exact_match,flexible-extract: 0.728 | ||
exact_match,strict-match: 0.728 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
import numpy | ||
import pytest | ||
import yaml | ||
from loguru import logger | ||
|
||
from llmcompressor.core import active_session | ||
from tests.e2e.e2e_utils import run_oneshot_for_e2e_testing | ||
from tests.examples.utils import requires_gpu_count | ||
|
||
try: | ||
import lm_eval | ||
|
||
lm_eval_installed = True | ||
except ImportError: | ||
lm_eval_installed = False | ||
logger.warning("lm_eval is not installed. This test will be skipped") | ||
|
||
TEST_DATA_FILE = os.environ.get("TEST_DATA_FILE", None) | ||
|
||
|
||
# Will run each test case in its own process through run_tests.sh | ||
# emulating vLLM CI testing | ||
@requires_gpu_count(1) | ||
@pytest.mark.skipif( | ||
not lm_eval_installed, reason="lm eval is not installed, skipping test" | ||
) | ||
class TestLMEval: | ||
""" | ||
The following test quantizes a model using a preset scheme or recipe, | ||
and then evaluates the model using LM Eval. Each test case is focused on a | ||
specific quantization type (e.g W4A16 with grouped quantization, | ||
W4N16 with channel quantization). To add a new test case, a new config has to be | ||
added to the lm_eval_configs folder. The tests run on a cadence defined by the | ||
`cadence` field. Each config defines the model to quantize. Optionally, a dataset | ||
id and split can be provided for calibration. Finally, all config files must list | ||
a scheme. The scheme can be a preset scheme from | ||
https://github.com/neuralmagic/compressed-tensors/blob/main/src/compressed_tensors/quantization/quant_scheme.py | ||
or another identifier which can be used for the particular test case. If a recipe | ||
is not provided, it is assumed that the scheme provided is a preset scheme and will | ||
be used for quantization. Otherwise, the recipe will always be used if given. | ||
""" # noqa: E501 | ||
|
||
def set_up(self): | ||
eval_config = yaml.safe_load(Path(TEST_DATA_FILE).read_text(encoding="utf-8")) | ||
|
||
if os.environ.get("CADENCE", "commit") != eval_config.get("cadence"): | ||
pytest.skip("Skipping test; cadence mismatch") | ||
|
||
self.model = eval_config["model"] | ||
self.scheme = eval_config.get("scheme") | ||
self.dataset_id = eval_config.get("dataset_id") | ||
self.dataset_config = eval_config.get("dataset_config") | ||
self.dataset_split = eval_config.get("dataset_split") | ||
self.recipe = eval_config.get("recipe") | ||
self.quant_type = eval_config.get("quant_type") | ||
self.save_dir = eval_config.get("save_dir") | ||
self.task = eval_config.get("task") | ||
self.num_fewshot = eval_config.get("num_fewshot") | ||
self.limit = eval_config.get("limit") | ||
self.exact_flex = eval_config.get("exact_match,flexible-extract") | ||
self.exact_strict = eval_config.get("exact_match,strict-match") | ||
|
||
logger.info("========== RUNNING ==============") | ||
logger.info(self.scheme) | ||
|
||
self.device = "cuda:0" | ||
self.num_calibration_samples = 256 | ||
self.max_seq_length = 2048 | ||
|
||
def test_lm_eval(self): | ||
# Run vLLM with saved model | ||
self.set_up() | ||
if not self.save_dir: | ||
self.save_dir = self.model.split("/")[1] + f"-{self.scheme}" | ||
oneshot_model, tokenizer = run_oneshot_for_e2e_testing( | ||
model=self.model, | ||
device=self.device, | ||
num_calibration_samples=self.num_calibration_samples, | ||
max_seq_length=self.max_seq_length, | ||
scheme=self.scheme, | ||
dataset_id=self.dataset_id, | ||
dataset_config=self.dataset_config, | ||
dataset_split=self.dataset_split, | ||
recipe=self.recipe, | ||
quant_type=self.quant_type, | ||
) | ||
|
||
logger.info("================= SAVING TO DISK ======================") | ||
oneshot_model.save_pretrained(self.save_dir) | ||
tokenizer.save_pretrained(self.save_dir) | ||
recipe_path = os.path.join(self.save_dir, "recipe.yaml") | ||
|
||
# Use the session to fetch the recipe; | ||
# Reset session for next test case | ||
session = active_session() | ||
recipe_yaml_str = session.get_serialized_recipe() | ||
with open(recipe_path, "w") as fp: | ||
fp.write(recipe_yaml_str) | ||
session.reset() | ||
|
||
logger.info("================= Running LM Eval ======================") | ||
|
||
model_args = f"pretrained={self.save_dir}" | ||
results = lm_eval.simple_evaluate( | ||
model="hf", | ||
dsikka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
model_args=model_args, | ||
tasks=[self.task], | ||
num_fewshot=self.num_fewshot, | ||
limit=self.limit, | ||
device="cuda:0", | ||
batch_size=100, | ||
dsikka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
metrics = results["results"][self.task] | ||
exact_match_strict = metrics.get("exact_match,strict-match") | ||
exact_match_flex = metrics.get("exact_match,flexible-extract") | ||
logger.info("Exact Match, Strict") | ||
logger.info(exact_match_strict) | ||
logger.info("Exact Match, Flex") | ||
logger.info(exact_match_flex) | ||
assert numpy.isclose(exact_match_strict, self.exact_strict, rtol=0.05) | ||
assert numpy.isclose(exact_match_flex, self.exact_flex, rtol=0.05) | ||
self.tear_down() | ||
|
||
def tear_down(self): | ||
if self.save_dir is not None: | ||
shutil.rmtree(self.save_dir) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.