|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import pytest |
| 4 | +import logging |
| 5 | +import shutil |
| 6 | +import subprocess # nosec B404 |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any, Dict |
| 10 | + |
| 11 | +sys.path.insert(0, f"{os.path.dirname(__file__)}/../../../tests/") |
| 12 | +from python_tests.utils.atomic_download import AtomicDownloadManager # noqa |
| 13 | +from python_tests.utils.constants import get_ov_cache_dir # noqa |
| 14 | +from python_tests.utils.network import retry_request # noqa |
| 15 | + |
| 16 | +logging.basicConfig(level=logging.INFO) |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +MODELS: Dict[str, Dict[str, Any]] = { |
| 21 | + "bge-small-en-v1.5": { |
| 22 | + "name": "BAAI/bge-small-en-v1.5", |
| 23 | + "convert_args": ["--trust-remote-code", "--task", "feature-extraction"], |
| 24 | + }, |
| 25 | + "Qwen3-Embedding-0.6B": { |
| 26 | + "name": "Qwen/Qwen3-Embedding-0.6B", |
| 27 | + "convert_args": ["--trust-remote-code", "--task", "feature-extraction"], |
| 28 | + }, |
| 29 | + "ms-marco-TinyBERT-L2-v2": { |
| 30 | + "name": "cross-encoder/ms-marco-TinyBERT-L2-v2", |
| 31 | + "convert_args": ["--trust-remote-code", "--task", "text-classification"], |
| 32 | + }, |
| 33 | + "Qwen3-Reranker-0.6B": { |
| 34 | + "name": "Qwen/Qwen3-Reranker-0.6B", |
| 35 | + "convert_args": ["--trust-remote-code", "--task", "text-generation"], |
| 36 | + }, |
| 37 | + "tiny-random-qwen2vl": { |
| 38 | + "name": "katuni4ka/tiny-random-qwen2vl", |
| 39 | + "convert_args": ["--trust-remote-code", "--task", "image-text-to-text"], |
| 40 | + }, |
| 41 | + "tiny-random-llava-next-video": { |
| 42 | + "name": "katuni4ka/tiny-random-llava-next-video", |
| 43 | + "convert_args": ["--trust-remote-code", "--task", "image-text-to-text"], |
| 44 | + }, |
| 45 | + "nanoLLaVA": { |
| 46 | + "name": "qnguyen3/nanoLLaVA", |
| 47 | + "convert_args": ["--trust-remote-code", "--task", "image-text-to-text"], |
| 48 | + }, |
| 49 | + "tiny-random-llava": { |
| 50 | + "name": "katuni4ka/tiny-random-llava", |
| 51 | + "convert_args": ["--trust-remote-code", "--task", "image-text-to-text"], |
| 52 | + }, |
| 53 | + "tiny-random-stable-diffusion-xl": {"name": "echarlaix/tiny-random-stable-diffusion-xl", "convert_args": []}, |
| 54 | + "stable-diffusion-3-tiny-random": {"name": "yujiepan/stable-diffusion-3-tiny-random", "convert_args": []}, |
| 55 | + "tiny-random-flux": {"name": "katuni4ka/tiny-random-flux", "convert_args": []}, |
| 56 | + "tiny-random-flux-fill": {"name": "katuni4ka/tiny-random-flux-fill", "convert_args": []}, |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +def get_ov_cache_converted_models_dir(): |
| 61 | + return get_ov_cache_dir() / "converted_models" |
| 62 | + |
| 63 | + |
| 64 | +def convert_model(model_name): |
| 65 | + models_dir = get_ov_cache_converted_models_dir() |
| 66 | + |
| 67 | + model_id = model_name.split("/")[1] |
| 68 | + convert_args = MODELS[model_id]["convert_args"] |
| 69 | + model_path = Path(models_dir) / f"wwb_{model_id}" |
| 70 | + |
| 71 | + manager = AtomicDownloadManager(model_path) |
| 72 | + |
| 73 | + logger.info(f"Start convertion of: {model_name}") |
| 74 | + if manager.is_complete(): |
| 75 | + logger.info("Conversion command is already completed") |
| 76 | + return model_path |
| 77 | + |
| 78 | + def convert(temp_path: Path) -> None: |
| 79 | + command = [ |
| 80 | + "optimum-cli", |
| 81 | + "export", |
| 82 | + "openvino", |
| 83 | + "--model", |
| 84 | + model_name, |
| 85 | + "--weight-format", |
| 86 | + "fp16", |
| 87 | + *convert_args, |
| 88 | + str(temp_path), |
| 89 | + ] |
| 90 | + logger.info(f"Conversion command: {' '.join(command)}") |
| 91 | + retry_request(lambda: subprocess.run(command, check=True, text=True, capture_output=True)) |
| 92 | + |
| 93 | + try: |
| 94 | + manager.execute(convert) |
| 95 | + except subprocess.CalledProcessError as error: |
| 96 | + logger.exception(f"optimum-cli returned {error.returncode}. Output:\n{error.output}") |
| 97 | + raise |
| 98 | + return str(model_path) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.fixture(scope="session", autouse=True) |
| 102 | +def module_teardown(): |
| 103 | + ov_cache_dir = get_ov_cache_dir() |
| 104 | + ov_cache_converted_dir = get_ov_cache_converted_models_dir() |
| 105 | + logger.info(f"Creating directories: {ov_cache_converted_dir}") |
| 106 | + if not ov_cache_converted_dir.exists(): |
| 107 | + ov_cache_converted_dir.mkdir(exist_ok=True, parents=True) |
| 108 | + |
| 109 | + yield |
| 110 | + |
| 111 | + if os.environ.get("CLEANUP_CACHE", "false").lower() == "true": |
| 112 | + if ov_cache_dir.exists(): |
| 113 | + logger.info(f"Removing temporary directory: {ov_cache_dir}") |
| 114 | + shutil.rmtree(ov_cache_dir) |
| 115 | + else: |
| 116 | + logger.info(f"Skipped temporary directory cleanup because it doesn't exist: {ov_cache_dir}") |
| 117 | + |
| 118 | + |
| 119 | +def run_wwb(args, env=None): |
| 120 | + command = ["wwb"] + args |
| 121 | + base_env = {"TRANSFORMERS_VERBOSITY": "debug", "PYTHONIOENCODING": "utf-8", **os.environ} |
| 122 | + if env: |
| 123 | + base_env.update(env) |
| 124 | + try: |
| 125 | + return subprocess.check_output( |
| 126 | + command, |
| 127 | + stderr=subprocess.STDOUT, |
| 128 | + encoding="utf-8", |
| 129 | + env=base_env, |
| 130 | + ) |
| 131 | + except subprocess.CalledProcessError as error: |
| 132 | + logger.error(f"'{' '.join(map(str, command))}' returned {error.returncode}. Output:\n{error.output}") |
| 133 | + raise |
0 commit comments