|
| 1 | +import subprocess # nosec B404 |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +import pandas as pd |
| 6 | +import pytest |
| 7 | +import logging |
| 8 | + |
| 9 | +from transformers import AutoTokenizer |
| 10 | +from optimum.intel.openvino import OVModelForCausalLM, OVWeightQuantizationConfig |
| 11 | + |
| 12 | + |
| 13 | +logging.basicConfig(level=logging.INFO) |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +def run_wwb(args): |
| 18 | + logger.info(" ".join(["wwb"] + args)) |
| 19 | + result = subprocess.run( |
| 20 | + ["wwb"] + args, |
| 21 | + capture_output=True, |
| 22 | + text=True |
| 23 | + ) |
| 24 | + logger.info(result) |
| 25 | + return result |
| 26 | + |
| 27 | + |
| 28 | +model_id = "facebook/opt-125m" |
| 29 | +tmp_dir = tempfile.mkdtemp() |
| 30 | +base_model_path = os.path.join(tmp_dir, "opt125m") |
| 31 | +target_model_path = os.path.join(tmp_dir, "opt125m_int8") |
| 32 | + |
| 33 | + |
| 34 | +def setup_module(): |
| 35 | + logger.info("Create models") |
| 36 | + tokenizer = AutoTokenizer.from_pretrained(model_id) |
| 37 | + base_model = OVModelForCausalLM.from_pretrained(model_id) |
| 38 | + base_model.save_pretrained(base_model_path) |
| 39 | + tokenizer.save_pretrained(base_model_path) |
| 40 | + |
| 41 | + target_model = OVModelForCausalLM.from_pretrained( |
| 42 | + model_id, quantization_config=OVWeightQuantizationConfig(bits=8) |
| 43 | + ) |
| 44 | + target_model.save_pretrained(target_model_path) |
| 45 | + tokenizer.save_pretrained(target_model_path) |
| 46 | + |
| 47 | + |
| 48 | +def teardown_module(): |
| 49 | + logger.info("Remove models") |
| 50 | + shutil.rmtree(tmp_dir) |
| 51 | + |
| 52 | + |
| 53 | +def test_target_model(): |
| 54 | + result = run_wwb([ |
| 55 | + "--base-model", base_model_path, |
| 56 | + "--target-model", target_model_path, |
| 57 | + "--num-samples", "2", |
| 58 | + "--device", "CPU" |
| 59 | + ]) |
| 60 | + assert result.returncode == 0 |
| 61 | + assert "Metrics for model" in result.stdout |
| 62 | + assert "## Reference text" not in result.stdout |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture |
| 66 | +def test_gt_data(): |
| 67 | + with tempfile.NamedTemporaryFile(suffix=".csv") as tmpfile: |
| 68 | + temp_file_name = tmpfile.name |
| 69 | + |
| 70 | + result = run_wwb([ |
| 71 | + "--base-model", base_model_path, |
| 72 | + "--gt-data", temp_file_name, |
| 73 | + "--dataset", "EleutherAI/lambada_openai,en", |
| 74 | + "--dataset-field", "text", |
| 75 | + "--split", "test", |
| 76 | + "--num-samples", "2", |
| 77 | + "--device", "CPU" |
| 78 | + ]) |
| 79 | + import time |
| 80 | + time.sleep(1) |
| 81 | + data = pd.read_csv(temp_file_name) |
| 82 | + os.remove(temp_file_name) |
| 83 | + |
| 84 | + assert result.returncode == 0 |
| 85 | + assert len(data["questions"].values) == 2 |
| 86 | + |
| 87 | + |
| 88 | +def test_output_directory(): |
| 89 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 90 | + result = run_wwb([ |
| 91 | + "--base-model", base_model_path, |
| 92 | + "--target-model", target_model_path, |
| 93 | + "--num-samples", "2", |
| 94 | + "--device", "CPU", |
| 95 | + "--output", temp_dir |
| 96 | + ]) |
| 97 | + assert result.returncode == 0 |
| 98 | + assert "Metrics for model" in result.stdout |
| 99 | + assert os.path.exists(os.path.join(temp_dir, "metrics_per_qustion.csv")) |
| 100 | + assert os.path.exists(os.path.join(temp_dir, "metrics.csv")) |
| 101 | + |
| 102 | + |
| 103 | +def test_verbose(): |
| 104 | + result = run_wwb([ |
| 105 | + "--base-model", base_model_path, |
| 106 | + "--target-model", target_model_path, |
| 107 | + "--num-samples", "2", |
| 108 | + "--device", "CPU", |
| 109 | + "--verbose" |
| 110 | + ]) |
| 111 | + assert result.returncode == 0 |
| 112 | + assert "## Reference text" in result.stdout |
| 113 | + |
| 114 | + |
| 115 | +def test_language_autodetect(): |
| 116 | + with tempfile.NamedTemporaryFile(suffix=".csv") as tmpfile: |
| 117 | + temp_file_name = tmpfile.name |
| 118 | + |
| 119 | + result = run_wwb([ |
| 120 | + "--base-model", "Qwen/Qwen2-0.5B", |
| 121 | + "--gt-data", temp_file_name, |
| 122 | + "--num-samples", "2", |
| 123 | + "--device", "CPU" |
| 124 | + ]) |
| 125 | + data = pd.read_csv(temp_file_name) |
| 126 | + os.remove(temp_file_name) |
| 127 | + |
| 128 | + assert result.returncode == 0 |
| 129 | + assert "马克" in data["questions"].values[0] |
0 commit comments