|
| 1 | +# Copyright (C) 2025 Intel Corporation |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import pytest |
| 5 | +import subprocess # nosec B404 |
| 6 | +import logging |
| 7 | +from pathlib import Path |
| 8 | +import numpy as np |
| 9 | +import openvino as ov |
| 10 | +import openvino_genai as ov_genai |
| 11 | + |
| 12 | +from utils.constants import get_ov_cache_models_dir |
| 13 | +from utils.atomic_download import AtomicDownloadManager |
| 14 | +from utils.network import retry_request |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | +MODEL_ID = "tiny-random-latent-consistency" |
| 19 | +MODEL_NAME = "echarlaix/tiny-random-latent-consistency" |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="module") |
| 23 | +def image_generation_model(): |
| 24 | + models_dir = get_ov_cache_models_dir() |
| 25 | + model_path = Path(models_dir) / MODEL_ID / MODEL_NAME |
| 26 | + |
| 27 | + manager = AtomicDownloadManager(model_path) |
| 28 | + |
| 29 | + def convert_model(temp_path: Path) -> None: |
| 30 | + command = [ |
| 31 | + "optimum-cli", "export", "openvino", |
| 32 | + "--model", MODEL_NAME, |
| 33 | + "--trust-remote-code", |
| 34 | + "--weight-format", "fp16", |
| 35 | + str(temp_path) |
| 36 | + ] |
| 37 | + logger.info(f"Conversion command: {' '.join(command)}") |
| 38 | + retry_request(lambda: subprocess.run(command, check=True, text=True, capture_output=True)) |
| 39 | + |
| 40 | + try: |
| 41 | + manager.execute(convert_model) |
| 42 | + except subprocess.CalledProcessError as error: |
| 43 | + logger.exception(f"optimum-cli returned {error.returncode}. Output:\n{error.output}") |
| 44 | + raise |
| 45 | + |
| 46 | + return str(model_path) |
| 47 | + |
| 48 | + |
| 49 | +def get_random_image(height: int = 64, width: int = 64) -> ov.Tensor: |
| 50 | + image_data = np.random.randint(0, 255, (1, height, width, 3), dtype=np.uint8) |
| 51 | + return ov.Tensor(image_data) |
| 52 | + |
| 53 | + |
| 54 | +def get_mask_image(height: int = 64, width: int = 64) -> ov.Tensor: |
| 55 | + mask_data = np.zeros((1, height, width, 3), dtype=np.uint8) |
| 56 | + mask_data[:, height//4:3*height//4, width//4:3*width//4, :] = 255 |
| 57 | + return ov.Tensor(mask_data) |
| 58 | + |
| 59 | + |
| 60 | +class TestImageGenerationCallback: |
| 61 | + |
| 62 | + def test_text2image_with_simple_callback(self, image_generation_model): |
| 63 | + pipe = ov_genai.Text2ImagePipeline(image_generation_model, "CPU") |
| 64 | + |
| 65 | + callback_calls = [] |
| 66 | + |
| 67 | + def callback(step, num_steps, latent): |
| 68 | + callback_calls.append((step, num_steps)) |
| 69 | + return False |
| 70 | + |
| 71 | + image = pipe.generate( |
| 72 | + "test prompt", |
| 73 | + width=64, |
| 74 | + height=64, |
| 75 | + num_inference_steps=2, |
| 76 | + callback=callback |
| 77 | + ) |
| 78 | + |
| 79 | + assert len(callback_calls) > 0, "Callback should be called at least once" |
| 80 | + assert image is not None |
| 81 | + |
| 82 | + def test_text2image_with_stateful_callback(self, image_generation_model): |
| 83 | + pipe = ov_genai.Text2ImagePipeline(image_generation_model, "CPU") |
| 84 | + |
| 85 | + class ProgressTracker: |
| 86 | + def __init__(self): |
| 87 | + self.steps = [] |
| 88 | + self.total = 0 |
| 89 | + |
| 90 | + def reset(self, total): |
| 91 | + self.total = total |
| 92 | + self.steps = [] |
| 93 | + |
| 94 | + def update(self, step): |
| 95 | + self.steps.append(step) |
| 96 | + |
| 97 | + tracker = ProgressTracker() |
| 98 | + |
| 99 | + def callback(step, num_steps, latent): |
| 100 | + if tracker.total != num_steps: |
| 101 | + tracker.reset(num_steps) |
| 102 | + tracker.update(step) |
| 103 | + return False |
| 104 | + |
| 105 | + image = pipe.generate( |
| 106 | + "test prompt", |
| 107 | + width=64, |
| 108 | + height=64, |
| 109 | + num_inference_steps=2, |
| 110 | + callback=callback |
| 111 | + ) |
| 112 | + |
| 113 | + assert len(tracker.steps) > 0, "Callback should track steps" |
| 114 | + assert image is not None |
| 115 | + |
| 116 | + def test_text2image_callback_early_stop(self, image_generation_model): |
| 117 | + pipe = ov_genai.Text2ImagePipeline(image_generation_model, "CPU") |
| 118 | + |
| 119 | + callback_calls = [] |
| 120 | + |
| 121 | + def callback(step, num_steps, latent): |
| 122 | + callback_calls.append(step) |
| 123 | + return step >= 1 |
| 124 | + |
| 125 | + image = pipe.generate( |
| 126 | + "test prompt", |
| 127 | + width=64, |
| 128 | + height=64, |
| 129 | + num_inference_steps=5, |
| 130 | + callback=callback |
| 131 | + ) |
| 132 | + |
| 133 | + assert len(callback_calls) <= 3, "Callback should stop early" |
| 134 | + assert image is not None |
| 135 | + |
| 136 | + def test_text2image_multiple_generates_with_callback(self, image_generation_model): |
| 137 | + pipe = ov_genai.Text2ImagePipeline(image_generation_model, "CPU") |
| 138 | + |
| 139 | + for i in range(3): |
| 140 | + callback_calls = [] |
| 141 | + |
| 142 | + def callback(step, num_steps, latent): |
| 143 | + callback_calls.append(step) |
| 144 | + return False |
| 145 | + |
| 146 | + image = pipe.generate( |
| 147 | + f"test prompt {i}", |
| 148 | + width=64, |
| 149 | + height=64, |
| 150 | + num_inference_steps=2, |
| 151 | + callback=callback |
| 152 | + ) |
| 153 | + |
| 154 | + assert len(callback_calls) > 0 |
| 155 | + assert image is not None |
| 156 | + |
| 157 | + def test_image2image_with_callback(self, image_generation_model): |
| 158 | + pipe = ov_genai.Image2ImagePipeline(image_generation_model, "CPU") |
| 159 | + |
| 160 | + callback_calls = [] |
| 161 | + |
| 162 | + def callback(step, num_steps, latent): |
| 163 | + callback_calls.append((step, num_steps)) |
| 164 | + return False |
| 165 | + |
| 166 | + input_image = get_random_image() |
| 167 | + |
| 168 | + image = pipe.generate( |
| 169 | + "test prompt", |
| 170 | + input_image, |
| 171 | + strength=0.8, |
| 172 | + callback=callback |
| 173 | + ) |
| 174 | + |
| 175 | + assert len(callback_calls) > 0 |
| 176 | + assert image is not None |
| 177 | + |
| 178 | + def test_inpainting_with_callback(self, image_generation_model): |
| 179 | + pipe = ov_genai.InpaintingPipeline(image_generation_model, "CPU") |
| 180 | + |
| 181 | + callback_calls = [] |
| 182 | + |
| 183 | + def callback(step, num_steps, latent): |
| 184 | + callback_calls.append((step, num_steps)) |
| 185 | + return False |
| 186 | + |
| 187 | + input_image = get_random_image() |
| 188 | + mask_image = get_mask_image() |
| 189 | + |
| 190 | + image = pipe.generate( |
| 191 | + "test prompt", |
| 192 | + input_image, |
| 193 | + mask_image, |
| 194 | + callback=callback |
| 195 | + ) |
| 196 | + |
| 197 | + assert len(callback_calls) > 0 |
| 198 | + assert image is not None |
0 commit comments