-
Notifications
You must be signed in to change notification settings - Fork 235
Added a new test to check whether some transformations is applied during conversion or completion #1651
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
alien-cyber
wants to merge
5
commits into
huggingface:main
Choose a base branch
from
alien-cyber:add-test-transformations
base: main
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.
+229
−0
Open
Added a new test to check whether some transformations is applied during conversion or completion #1651
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
78b90c8
Added test case to c heck whether a specific set of transformations h…
alien-cyber fb1c119
made it more readable
alien-cyber 088a8b4
corrected string matching algorithm
alien-cyber 897f734
Updated transformation test logic
alien-cyber 8ea25ff
expanded for different model classes
alien-cyber 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
|
|
||
| # Used by test_transformations.py to dynamically select the right class per architecture | ||
|
|
||
| ARCH_TO_MODEL_CLASS = { | ||
| # text-generation | ||
| "afmoe": "OVModelForCausalLM", | ||
| "gpt2": "OVModelForCausalLM", | ||
| "llama": "OVModelForCausalLM", | ||
| "mistral": "OVModelForCausalLM", | ||
| "qwen2": "OVModelForCausalLM", | ||
| "qwen3": "OVModelForCausalLM", | ||
|
|
||
|
|
||
| # image-text-to-text | ||
| "llava": "OVModelForVisualCausalLM", | ||
|
|
||
|
|
||
| # text-to-image / text-to-video | ||
| "stable-diffusion": "OVDiffusionPipeline", | ||
|
|
||
|
|
||
| # automatic-speech-recognition | ||
| "whisper": "OVModelForSpeechSeq2Seq", | ||
|
|
||
|
|
||
| # text2text-generation | ||
|
|
||
| "bart": "OVModelForSeq2SeqLM", | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # feature-extraction | ||
| "bert": "OVModelForFeatureExtraction", | ||
| "electra": "OVModelForFeatureExtraction", | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # zero-shot-image-classification | ||
| "clip": "OVModelForZeroShotImageClassification", | ||
| "siglip": "OVModelForZeroShotImageClassification", | ||
|
|
||
| } | ||
|
|
||
|
|
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,6 @@ | ||
|
|
||
| # Format: arch_name: Transformation1, Transformation2 | ||
|
|
||
|
|
||
| afmoe: MoEMatMulsFusion,FullyConnectedBiasFusion | ||
| gpt2: ConvertToCPUSpecificOpset,MatMulToFCFusion |
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,177 @@ | ||
| import os | ||
| import sys | ||
| import unittest | ||
|
|
||
| #This code is for eliminating unnecessary code text from the output | ||
| import pytest | ||
| @pytest.fixture(autouse=True, scope="session") | ||
| def set_tb_style(pytestconfig): | ||
| pytestconfig.option.tbstyle = "line" | ||
|
|
||
| # we are adding this , so the parent directory (tests/openvino/) is in the python search path for utils_test.py to be imported | ||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) | ||
|
|
||
| import subprocess | ||
| import textwrap | ||
| import re | ||
|
|
||
| from parameterized import parameterized | ||
| from utils_tests import MODEL_NAMES, OPENVINO_DEVICE, REMOTE_CODE_MODELS | ||
| from arch_to_model_class import ARCH_TO_MODEL_CLASS | ||
|
|
||
| # Maps architecture name -> list of transformation needed to be applied , as per expected_transformations.txt | ||
| def _load_expected_transformations(path): | ||
| result = {} | ||
| with open(path) as f: | ||
| for line in f: | ||
| line = line.strip() | ||
| if not line or line.startswith("#"): | ||
| continue | ||
| arch, _, transforms_str = line.partition(":") | ||
| result[arch.strip()] = [ | ||
| t.strip() for t in transforms_str.split(",") if t.strip() | ||
| ] | ||
| return result | ||
|
|
||
|
|
||
| _CONFIG_PATH = os.path.join( | ||
| os.path.dirname(__file__), "expected_transformations.txt" | ||
| ) | ||
| ARCH_TO_EXPECTED_TRANSFORMATIONS = _load_expected_transformations(_CONFIG_PATH) | ||
|
|
||
|
|
||
| def _capture_stderr_during(model_id, OPENVINO_DEVICE, trust_remote_code, model_class="OVModelForCausalLM"): | ||
| # Runs model loading in a subprocess to reliably capture OpenVINO C++ logs. | ||
|
|
||
| code = textwrap.dedent(f""" | ||
| import os | ||
| os.environ["OV_ENABLE_PROFILE_PASS"] = "1" | ||
|
|
||
| from optimum.intel import {model_class} | ||
|
|
||
| {model_class}.from_pretrained( | ||
| "{model_id}", | ||
| export=True, | ||
| compile=True, | ||
| device="{OPENVINO_DEVICE}", | ||
| trust_remote_code={trust_remote_code}, | ||
| ) | ||
| """) | ||
|
|
||
| result = subprocess.run( | ||
| [sys.executable, "-c", code], | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, | ||
| text=True, | ||
| ) | ||
|
|
||
| return result.stdout | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should use |
||
|
|
||
|
|
||
| # Remove separators and lowercase for fuzzy comparison. | ||
| def normalize(name: str) -> str: | ||
| return re.sub(r'[\s_\-]', '', name).lower() | ||
|
|
||
|
|
||
| # Extract transformation name — always last token before NUMBER ms +/- | ||
| def extract_transform_name(line: str) -> str | None: | ||
| match = re.search( | ||
| r'([A-Za-z][A-Za-z0-9_]*)\s+\d+ms\s*[+-]\s*$', | ||
| line.strip() | ||
| ) | ||
| return match.group(1) if match else None | ||
|
|
||
|
|
||
| # Algo to identify tranformations present with '+' in the log. | ||
| def check_failed_transformations(log: str, words: list[str]) -> dict: | ||
| applied_norm_plus = [] | ||
| applied_norm_minus = [] | ||
| found_not_applied=[] | ||
|
|
||
| for line in log.splitlines(): | ||
| stripped = line.strip() | ||
|
|
||
| if not stripped: | ||
| continue | ||
| name = extract_transform_name(stripped) | ||
|
|
||
| if name: | ||
| if stripped.endswith('+'): | ||
| applied_norm_plus.append(normalize(name)) | ||
| elif stripped.endswith('-'): | ||
| applied_norm_minus.append(normalize(name)) | ||
|
|
||
| remaining = {normalize(w): w for w in words} | ||
|
|
||
| for key in list(remaining.keys()): | ||
| if key in applied_norm_plus: | ||
| del remaining[key] | ||
| elif key in applied_norm_minus: | ||
| found_not_applied.append(remaining[key]) | ||
| del remaining[key] | ||
|
|
||
|
|
||
|
|
||
| return { | ||
| "not_found": list(remaining.values()), | ||
| "not_applied":found_not_applied | ||
| } | ||
|
|
||
|
|
||
|
|
||
| class OVTransformationTest(unittest.TestCase): | ||
|
|
||
| @parameterized.expand( | ||
| list(ARCH_TO_EXPECTED_TRANSFORMATIONS.items()) | ||
| ) | ||
| def test_transformations_applied( | ||
| self, | ||
| model_arch, | ||
| expected_transforms | ||
| ): | ||
| model_id = MODEL_NAMES[model_arch] | ||
| trust_remote_code = model_arch in REMOTE_CODE_MODELS | ||
| model_class = ARCH_TO_MODEL_CLASS.get(model_arch) | ||
|
|
||
| log_output = _capture_stderr_during( | ||
| model_id, | ||
| OPENVINO_DEVICE, | ||
| trust_remote_code, | ||
| model_class, | ||
| ) | ||
|
|
||
| result = check_failed_transformations( | ||
| log_output, | ||
| expected_transforms | ||
| ) | ||
|
|
||
| errors=[] | ||
| not_found = ", ".join(result["not_found"]) | ||
| not_applied = ", ".join(result["not_applied"]) | ||
| if not_applied: | ||
| err = ( | ||
| f"These transformations were not 'applied' for '{model_arch}' architecture: " | ||
| + not_applied | ||
| ) | ||
| errors.append(err) | ||
|
|
||
| if not_found: | ||
| err= ( | ||
| f"These transformations were not 'found' in the '{model_arch}' transformation set: " | ||
| + not_found | ||
| ) | ||
| errors.append(err) | ||
|
|
||
| RED = "\033[91m" | ||
| RESET = "\033[0m" | ||
| if errors: | ||
| # raise AssertionError("\n".join(errors)) | ||
| raise AssertionError(f"{RED}" + "\n".join(errors) + f"{RESET}") | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.
can add this param
we could pass a
cache_dirso that models are reused across runs instead of being re-exported each time.This should help make the test more scalable.