-
Notifications
You must be signed in to change notification settings - Fork 302
Fix for deadlock in python callback #3073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sgonorov
wants to merge
4
commits into
openvinotoolkit:master
Choose a base branch
from
sgonorov:fix_deadlock_update_from_release
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+265
−36
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,198 @@ | ||
| # Copyright (C) 2025 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import pytest | ||
| import subprocess # nosec B404 | ||
| import logging | ||
| from pathlib import Path | ||
| import numpy as np | ||
| import openvino as ov | ||
| import openvino_genai as ov_genai | ||
|
|
||
| from utils.constants import get_ov_cache_converted_models_dir | ||
| from utils.atomic_download import AtomicDownloadManager | ||
| from utils.network import retry_request | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| MODEL_ID = "tiny-random-latent-consistency" | ||
| MODEL_NAME = "echarlaix/tiny-random-latent-consistency" | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def image_generation_model(): | ||
| models_dir = get_ov_cache_converted_models_dir() | ||
| model_path = Path(models_dir) / MODEL_ID / MODEL_NAME | ||
|
|
||
| manager = AtomicDownloadManager(model_path) | ||
|
|
||
| def convert_model(temp_path: Path) -> None: | ||
| command = [ | ||
| "optimum-cli", "export", "openvino", | ||
| "--model", MODEL_NAME, | ||
| "--trust-remote-code", | ||
| "--weight-format", "fp16", | ||
| str(temp_path) | ||
| ] | ||
| logger.info(f"Conversion command: {' '.join(command)}") | ||
| retry_request(lambda: subprocess.run(command, check=True, text=True, capture_output=True)) | ||
|
|
||
| try: | ||
| manager.execute(convert_model) | ||
| except subprocess.CalledProcessError as error: | ||
| logger.exception(f"optimum-cli returned {error.returncode}. Output:\n{error.output}") | ||
| raise | ||
|
|
||
| return str(model_path) | ||
|
|
||
|
|
||
| def get_random_image(height: int = 64, width: int = 64) -> ov.Tensor: | ||
| image_data = np.random.randint(0, 255, (1, height, width, 3), dtype=np.uint8) | ||
| return ov.Tensor(image_data) | ||
|
|
||
|
|
||
| def get_mask_image(height: int = 64, width: int = 64) -> ov.Tensor: | ||
| mask_data = np.zeros((1, height, width, 3), dtype=np.uint8) | ||
| mask_data[:, height//4:3*height//4, width//4:3*width//4, :] = 255 | ||
| return ov.Tensor(mask_data) | ||
|
|
||
|
|
||
| class TestImageGenerationCallback: | ||
|
|
||
| def test_text2image_with_simple_callback(self, image_generation_model): | ||
| pipe = ov_genai.Text2ImagePipeline(image_generation_model, "CPU") | ||
|
|
||
| callback_calls = [] | ||
|
|
||
| def callback(step, num_steps, latent): | ||
| callback_calls.append((step, num_steps)) | ||
| return False | ||
|
|
||
| image = pipe.generate( | ||
| "test prompt", | ||
| width=64, | ||
| height=64, | ||
| num_inference_steps=2, | ||
| callback=callback | ||
| ) | ||
|
|
||
| assert len(callback_calls) > 0, "Callback should be called at least once" | ||
| assert image is not None | ||
|
|
||
| def test_text2image_with_stateful_callback(self, image_generation_model): | ||
| pipe = ov_genai.Text2ImagePipeline(image_generation_model, "CPU") | ||
|
|
||
| class ProgressTracker: | ||
| def __init__(self): | ||
| self.steps = [] | ||
| self.total = 0 | ||
|
|
||
| def reset(self, total): | ||
| self.total = total | ||
| self.steps = [] | ||
|
|
||
| def update(self, step): | ||
| self.steps.append(step) | ||
|
|
||
| tracker = ProgressTracker() | ||
|
|
||
| def callback(step, num_steps, latent): | ||
| if tracker.total != num_steps: | ||
| tracker.reset(num_steps) | ||
| tracker.update(step) | ||
| return False | ||
|
|
||
| image = pipe.generate( | ||
| "test prompt", | ||
| width=64, | ||
| height=64, | ||
| num_inference_steps=2, | ||
| callback=callback | ||
| ) | ||
|
|
||
| assert len(tracker.steps) > 0, "Callback should track steps" | ||
| assert image is not None | ||
|
|
||
| def test_text2image_callback_early_stop(self, image_generation_model): | ||
| pipe = ov_genai.Text2ImagePipeline(image_generation_model, "CPU") | ||
|
|
||
| callback_calls = [] | ||
|
|
||
| def callback(step, num_steps, latent): | ||
| callback_calls.append(step) | ||
| return step >= 1 | ||
|
|
||
| image = pipe.generate( | ||
| "test prompt", | ||
| width=64, | ||
| height=64, | ||
| num_inference_steps=5, | ||
| callback=callback | ||
| ) | ||
|
|
||
| assert len(callback_calls) <= 3, "Callback should stop early" | ||
| assert image is not None | ||
|
|
||
| def test_text2image_multiple_generates_with_callback(self, image_generation_model): | ||
| pipe = ov_genai.Text2ImagePipeline(image_generation_model, "CPU") | ||
|
|
||
| for i in range(3): | ||
| callback_calls = [] | ||
|
|
||
| def callback(step, num_steps, latent): | ||
| callback_calls.append(step) | ||
| return False | ||
|
|
||
| image = pipe.generate( | ||
| f"test prompt {i}", | ||
| width=64, | ||
| height=64, | ||
| num_inference_steps=2, | ||
| callback=callback | ||
| ) | ||
|
|
||
| assert len(callback_calls) > 0 | ||
| assert image is not None | ||
|
|
||
| def test_image2image_with_callback(self, image_generation_model): | ||
| pipe = ov_genai.Image2ImagePipeline(image_generation_model, "CPU") | ||
|
|
||
| callback_calls = [] | ||
|
|
||
| def callback(step, num_steps, latent): | ||
| callback_calls.append((step, num_steps)) | ||
| return False | ||
|
|
||
| input_image = get_random_image() | ||
|
|
||
| image = pipe.generate( | ||
| "test prompt", | ||
| input_image, | ||
| strength=0.8, | ||
| callback=callback | ||
| ) | ||
|
|
||
| assert len(callback_calls) > 0 | ||
| assert image is not None | ||
|
|
||
| def test_inpainting_with_callback(self, image_generation_model): | ||
| pipe = ov_genai.InpaintingPipeline(image_generation_model, "CPU") | ||
|
|
||
| callback_calls = [] | ||
|
|
||
| def callback(step, num_steps, latent): | ||
| callback_calls.append((step, num_steps)) | ||
| return False | ||
|
|
||
| input_image = get_random_image() | ||
| mask_image = get_mask_image() | ||
|
|
||
| image = pipe.generate( | ||
| "test prompt", | ||
| input_image, | ||
| mask_image, | ||
| callback=callback | ||
| ) | ||
|
|
||
| assert len(callback_calls) > 0 | ||
| assert image is not None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m_torch_tensor is always set in constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but when we're using move it may be an empty Python object as far as i understand. So it's a little bit defensive here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
TorchTensorAllocatormovable? It defines a constructor which should disable default move constructotThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was movable before i've added destructor (constructor doesn't disable generation of default move methods). When i've added destructor it became unmovable, so i've had to specify default move constructor and copy/assign methods.