forked from huggingface/optimum-habana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_openclip_vqa.py
81 lines (64 loc) · 2.5 KB
/
test_openclip_vqa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import os
import re
import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from .test_examples import TIME_PERF_FACTOR
if os.environ.get("GAUDI2_CI", "0") == "1":
# Gaudi2 CI baselines
MODELS_TO_TEST = {
"bf16": [
("laion/CLIP-ViT-g-14-laion2B-s12B-b42K", 1472),
("microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224", 1816),
],
}
else:
# Gaudi1 CI baselines
MODELS_TO_TEST = {
"bf16": [
("laion/CLIP-ViT-g-14-laion2B-s12B-b42K", 550),
("microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224", 1200),
],
}
def _install_requirements():
PATH_TO_EXAMPLE_DIR = Path(__file__).resolve().parent.parent / "examples"
cmd_line = (
f"pip install -r {PATH_TO_EXAMPLE_DIR / 'visual-question-answering' / 'openclip_requirements.txt'}".split()
)
p = subprocess.Popen(cmd_line)
return_code = p.wait()
assert return_code == 0
def _test_openclip_vqa(model_name: str, baseline: float):
_install_requirements()
command = ["python3"]
path_to_example_dir = Path(__file__).resolve().parent.parent / "examples"
env_variables = os.environ.copy()
command += [
f"{path_to_example_dir / 'visual-question-answering' / 'run_openclip_vqa.py'}",
f"--model_name_or_path {model_name}",
"--bf16",
"--use_hpu_graphs",
]
with TemporaryDirectory() as tmp_dir:
command.append(f"--output_dir {tmp_dir}")
print(f"\n\nCommand to test: {' '.join(command)}\n")
pattern = re.compile(r"([\"\'].+?[\"\'])|\s")
command = [x for y in command for x in re.split(pattern, y) if x]
proc = subprocess.run(command, env=env_variables)
# Ensure the run finished without any issue
# Use try-except to avoid logging the token if used
try:
assert proc.returncode == 0
except AssertionError as e:
if "'--token', 'hf_" in e.args[0]:
e.args = (f"The following command failed:\n{' '.join(command[:-2])}",)
raise
with open(Path(tmp_dir) / "results.json") as fp:
results = json.load(fp)
# Ensure performance requirements (throughput) are met
assert results["throughput"] >= (2 - TIME_PERF_FACTOR) * baseline
@pytest.mark.parametrize("model_name, baseline", MODELS_TO_TEST["bf16"])
def test_openclip_vqa_bf16(model_name: str, baseline: float):
_test_openclip_vqa(model_name, baseline)