optimum_lm.py 2.96 KB
Newer Older
1
import json
Lintang Sutawika's avatar
Lintang Sutawika committed
2
import logging
3
4
5
6
7
8
9
from importlib.util import find_spec
from pathlib import Path

from lm_eval.api.registry import register_model
from lm_eval.models.huggingface import HFLM


Lintang Sutawika's avatar
Lintang Sutawika committed
10
eval_logger = logging.getLogger(__name__)
11
12


13
14
15
16
17
18
@register_model("openvino")
class OptimumLM(HFLM):
    """
    Optimum Intel provides a simple interface to optimize Transformer models and convert them to \
    OpenVINO™ Intermediate Representation (IR) format to accelerate end-to-end pipelines on \
    Intel® architectures using OpenVINO™ runtime.
19
20
21
22

    To use an OpenVINO config, use `--model_args ov_config` to point to a json file with an OpenVINO config:
    `lm_eval --model openvino --model_args pretrained=gpt2,ov_config=config.json --task lambada_openai`
    Example json file contents: {"INFERENCE_PRECISION_HINT": "f32", "CACHE_DIR": "model_cache"}
23
24
25
26
27
28
29
30
    """

    def __init__(
        self,
        device="cpu",
        **kwargs,
    ) -> None:
        if "backend" in kwargs:
31
32
            assert kwargs["backend"] in ["causal", "seq2seq"], (
                "Currently, only OVModelForCausalLM or OVModelForSeq2SeqLM are supported."
Baber Abbasi's avatar
Baber Abbasi committed
33
            )
34
35
36
37
38

        self.openvino_device = device

        super().__init__(
            device=self.openvino_device,
39
            backend=kwargs.pop("backend", "causal"),
40
41
42
43
44
45
46
47
48
49
50
51
            **kwargs,
        )

    def _create_model(
        self,
        pretrained: str,
        revision="main",
        dtype="auto",
        trust_remote_code=False,
        **kwargs,
    ) -> None:
        if not find_spec("optimum"):
52
            raise ModuleNotFoundError(
53
54
55
                "package `optimum` is not installed. Please install it via `pip install optimum[openvino]`"
            )
        else:
56
            from optimum.intel.openvino import OVModelForCausalLM, OVModelForSeq2SeqLM
57
58

        model_kwargs = kwargs if kwargs else {}
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        if "ov_config" in model_kwargs:
            if not Path(model_kwargs["ov_config"]).exists():
                raise ValueError(
                    "ov_config should point to a .json file containing an OpenVINO config"
                )
            with open(model_kwargs["ov_config"]) as f:
                model_kwargs["ov_config"] = json.load(f)
                eval_logger.info(
                    f"Using custom OpenVINO config: {model_kwargs['ov_config']}"
                )

        else:
            model_kwargs["ov_config"] = {}
        model_kwargs["ov_config"].setdefault("CACHE_DIR", "")
73
74
75
76
77
        if "pipeline_parallel" in model_kwargs:
            if model_kwargs["pipeline_parallel"]:
                model_kwargs["ov_config"]["MODEL_DISTRIBUTION_POLICY"] = (
                    "PIPELINE_PARALLEL"
                )
78

79
80
        model_cls = OVModelForCausalLM if self.backend == "causal" else OVModelForSeq2SeqLM
        self._model = model_cls.from_pretrained(
81
82
83
84
85
86
            pretrained,
            revision=revision,
            trust_remote_code=trust_remote_code,
            device=self.openvino_device.upper(),
            **model_kwargs,
        )