forked from EleutherAI/lm-evaluation-harness
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add causalLM OpenVino models (EleutherAI#1290)
* added intel optimum * added intel optimum in readme * modified intel optimum * modified intel optimum * modified intel optimum * modified install optimum * modified path of IR file * added openvino_device * added openvino_device2 * changed optimum-causal to openvino-causal * Update README.md * Update README.md * remove `lm_eval.base` import * update openvino-causal -> openvino ; pass device through super().__init__() * Update README.md * Add optimum to tests dependencies * apply pre-commit * fix so tests pass --------- Co-authored-by: Hailey Schoelkopf <[email protected]> Co-authored-by: haileyschoelkopf <[email protected]>
- Loading branch information
1 parent
5b0b8a5
commit 97a67d2
Showing
7 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,69 @@ | ||
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 | ||
|
||
|
||
@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. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
device="cpu", | ||
**kwargs, | ||
) -> None: | ||
if "backend" in kwargs: | ||
# optimum currently only supports causal models | ||
assert ( | ||
kwargs["backend"] == "causal" | ||
), "Currently, only OVModelForCausalLM is supported." | ||
|
||
self.openvino_device = device | ||
|
||
super().__init__( | ||
device=self.openvino_device, | ||
backend=kwargs.get("backend", "causal"), | ||
**kwargs, | ||
) | ||
|
||
def _create_model( | ||
self, | ||
pretrained: str, | ||
revision="main", | ||
dtype="auto", | ||
trust_remote_code=False, | ||
**kwargs, | ||
) -> None: | ||
if not find_spec("optimum"): | ||
raise Exception( | ||
"package `optimum` is not installed. Please install it via `pip install optimum[openvino]`" | ||
) | ||
else: | ||
from optimum.intel.openvino import OVModelForCausalLM | ||
|
||
model_kwargs = kwargs if kwargs else {} | ||
model_file = Path(pretrained) / "openvino_model.xml" | ||
if model_file.exists(): | ||
export = False | ||
else: | ||
export = True | ||
kwargs["ov_config"] = { | ||
"PERFORMANCE_HINT": "LATENCY", | ||
"NUM_STREAMS": "1", | ||
"CACHE_DIR": "", | ||
} | ||
|
||
self._model = OVModelForCausalLM.from_pretrained( | ||
pretrained, | ||
revision=revision, | ||
trust_remote_code=trust_remote_code, | ||
export=export, | ||
device=self.openvino_device.upper(), | ||
**model_kwargs, | ||
) |
This file contains 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 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,76 @@ | ||
import random | ||
import tempfile | ||
|
||
import pytest | ||
from optimum.intel import OVModelForCausalLM | ||
from transformers import AutoTokenizer | ||
|
||
import lm_eval.evaluator as evaluator | ||
import lm_eval.tasks as tasks | ||
from lm_eval.api.registry import get_model | ||
|
||
|
||
tasks.initialize_tasks() | ||
|
||
SUPPORTED_ARCHITECTURES_TASKS = { | ||
"facebook/opt-125m": "lambada_openai", | ||
"hf-internal-testing/tiny-random-gpt2": "wikitext", | ||
} | ||
|
||
|
||
@pytest.mark.parametrize("model_id,task", SUPPORTED_ARCHITECTURES_TASKS.items()) | ||
def test_evaluator(model_id, task): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
model = OVModelForCausalLM.from_pretrained( | ||
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( | ||
f"pretrained={tmpdirname}", | ||
{ | ||
"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: | ||
res.append((-random.random(), False)) | ||
|
||
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: | ||
res.append(-random.random()) | ||
|
||
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, | ||
) |