test_openvino.py 2.83 KB
Newer Older
1
2
import random
import tempfile
3
from pathlib import Path
4
5

import pytest
6
from optimum.intel import OVModelForCausalLM, OVModelForSeq2SeqLM
7
8
from transformers import AutoTokenizer

9
from lm_eval import evaluator
10
11
12
from lm_eval.api.registry import get_model


13
14
15
16
17
SUPPORTED_ARCHITECTURES_TASKS = [
    ("causal", "facebook/opt-125m", "lambada_openai",),
    ("causal", "hf-internal-testing/tiny-random-gpt2", "wikitext",),
    ("seq2seq", "hf-internal-testing/tiny-random-t5", "sst2",),
]
18
19


20
21
@pytest.mark.parametrize("backend,model_id,task", SUPPORTED_ARCHITECTURES_TASKS)
def test_evaluator(backend, model_id, task):
22
    with tempfile.TemporaryDirectory() as tmpdirname:
23
24
        model_cls = OVModelForCausalLM if backend == "causal" else OVModelForSeq2SeqLM
        model = model_cls.from_pretrained(
25
26
27
28
29
30
31
            model_id, export=True, use_cache=True
        )
        model.save_pretrained(tmpdirname)
        tokenizer = AutoTokenizer.from_pretrained(model_id)
        tokenizer.save_pretrained(tmpdirname)

        lm = get_model("openvino").create_from_arg_string(
32
            f"pretrained={tmpdirname},backend={backend}",
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
            {
                "batch_size": 1,
                "device": "cpu",
            },
        )

        def ll_fn(reqs):
            for ctx, cont in [req.args for req in reqs]:
                if len(ctx) == 0:
                    continue
                # space convention
                assert ctx[-1] != " "
                assert cont[0] == " " or ctx[-1] == "\n"

            res = []

            random.seed(42)
            for _ in reqs:
51
                res.extend([(-random.random(), False)])
52
53
54
55
56
57
58
59
60
61

            return res

        def ll_perp_fn(reqs):
            for (string,) in [req.args for req in reqs]:
                assert isinstance(string, str)

            res = []
            random.seed(42)
            for _ in reqs:
62
                res.extend([-random.random()])
63
64
65
66
67
68
69
70
71
72
73
74
75
76

            return res

        lm.loglikelihood = ll_fn
        lm.loglikelihood_rolling = ll_perp_fn

        limit = 10
        evaluator.simple_evaluate(
            model=lm,
            tasks=[task],
            num_fewshot=0,
            limit=limit,
            bootstrap_iters=10,
        )
77
78
79
80
81
82
83


def test_ov_config():
    """Test that if specified, a custom OpenVINO config is loaded correctly"""
    model_id = "hf-internal-testing/tiny-random-gpt2"
    with tempfile.TemporaryDirectory() as tmpdirname:
        config_file = str(Path(tmpdirname) / "ov_config.json")
84
        with open(Path(config_file), "w", encoding="utf-8") as f:
85
86
87
88
89
90
91
92
93
94
            f.write('{"DYNAMIC_QUANTIZATION_GROUP_SIZE" : "32"}')
        lm = get_model("openvino").create_from_arg_string(
            f"pretrained={model_id},ov_config={config_file}"
        )
    assert (
        lm.model.request.get_compiled_model().get_property(
            "DYNAMIC_QUANTIZATION_GROUP_SIZE"
        )
        == 32
    )