Skip to content

Commit 81cd23e

Browse files
[ Test ][ PR2 ] Splitting Common.py (#1702)
Details: * Separate `comparation` from `common.py` * Move `longbench` to `utils\` * Move tokenizer_config to data Tickets: * [159925](https://jira.devtools.intel.com/browse/CVS-159925) MERGE AFTER: * #1691 --------- Co-authored-by: Ilya Lavrenov <ilya.lavrenov@intel.com>
1 parent 25bba07 commit 81cd23e

9 files changed

+61
-45
lines changed

tests/python_tests/common.py

+1-42
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from utils.generation_config import get_greedy, get_beam_search
1717
from utils.constants import get_default_llm_properties
1818
from utils.hugging_face import convert_models, get_hugging_face_models, run_hugging_face
19+
from utils.comparation import compare_generation_results
1920

2021
TESTS_ROOT = Path(__file__).parent
2122

@@ -155,34 +156,6 @@ def run_llm_pipeline(
155156
return generation_results
156157

157158

158-
def compare_generation_result(hf_result: GenerationResult, ov_result: GenerationResult, generation_config: GenerationConfig):
159-
if generation_config.is_beam_search():
160-
assert len(hf_result.m_scores) == len(ov_result.m_scores)
161-
for hf_score, ov_score in zip(hf_result.m_scores, ov_result.m_scores):
162-
# Note, that for fp32 / fp16 models scores are different less than 0.001
163-
assert abs(hf_score - ov_score) < 0.02
164-
165-
if not generation_config.include_stop_str_in_output and len(generation_config.stop_strings) > 0:
166-
assert len(hf_result.m_generation_ids) >= len(ov_result.m_generation_ids)
167-
for hf_text, ov_text in zip(hf_result.m_generation_ids, ov_result.m_generation_ids):
168-
assert ov_text in hf_text
169-
else:
170-
assert len(hf_result.m_generation_ids) == len(ov_result.m_generation_ids)
171-
for hf_text, ov_text in zip(hf_result.m_generation_ids, ov_result.m_generation_ids):
172-
assert hf_text == ov_text
173-
174-
175-
def compare_generation_results(prompts: List[str], hf_results: List[GenerationResult], ov_results: List[GenerationResult], generation_configs: List[GenerationConfig] | GenerationConfig):
176-
if type(generation_configs) is not list:
177-
generation_configs = [generation_configs]
178-
179-
assert len(prompts) == len(hf_results)
180-
assert len(prompts) == len(ov_results)
181-
182-
for prompt, ref_result, ov_result, generation_config in zip(prompts, hf_results, ov_results, generation_configs):
183-
print(f"Prompt = {prompt}\nReference result = {ref_result}\nOpenVINO result = {ov_result.m_generation_ids}")
184-
compare_generation_result(ref_result, ov_result, generation_config)
185-
186159
def run_llm_pipeline_with_ref(model_id: str,
187160
prompts: List[str],
188161
generation_config: GenerationConfig | dict,
@@ -238,20 +211,6 @@ def generate_and_compare_with_reference_text(models_path: Path, prompts: List[st
238211
for ref_text, ov_text in zip(ref_texts_for_this_prompt, ov_result.m_generation_ids):
239212
assert ref_text == ov_text
240213

241-
242-
def get_image_by_link(link):
243-
from PIL import Image
244-
import requests
245-
from openvino import Tensor
246-
import numpy as np
247-
248-
image = Image.open(requests.get(link, stream=True).raw)
249-
if image.mode != 'RGB':
250-
image = image.convert('RGB')
251-
image_data = np.array((np.array(image.getdata()) - 128).astype(np.byte)).reshape(1, image.size[1], image.size[0], 3)
252-
return Tensor(image_data)
253-
254-
255214
"""rt_info has the highest priority. Delete it to respect configs."""
256215
def delete_rt_info(configs: List[Tuple], temp_path):
257216
core = openvino.Core()

tests/python_tests/data/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (C) 2018-2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0

tests/python_tests/tokenizer_configs.py tests/python_tests/data/tokenizer_configs.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Copyright (C) 2018-2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
13

24
def get_tokenizer_configs():
35
return {

tests/python_tests/test_continuous_batching.py

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from pathlib import Path
1111
from openvino_genai import ContinuousBatchingPipeline, LLMPipeline, GenerationConfig, SchedulerConfig, Tokenizer, draft_model
1212

13+
from common import generate_and_compare_with_reference_text, \
14+
get_scheduler_config, run_cb_pipeline_with_ref
1315
from common import generate_and_compare_with_reference_text, \
1416
get_scheduler_config, run_cb_pipeline_with_ref
1517
from test_sampling import RandomSamplingTestStruct, get_current_platform_ref_texts

tests/python_tests/test_kv_cache_eviction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from transformers import AutoTokenizer
1919

2020
from common import TESTS_ROOT, run_cb_pipeline_with_ref
21-
from utils_longbench import dataset2maxlen, evaluate, preprocess_prompt, post_process_pred
21+
from utils.longbench import dataset2maxlen, evaluate, preprocess_prompt, post_process_pred
2222

2323
from utils.constants import get_default_llm_properties
2424

tests/python_tests/test_tokenizer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_chat_templates():
9494
"BramVanroy/Llama-2-13b-chat-dutch"
9595
}
9696

97-
from tokenizer_configs import get_tokenizer_configs
97+
from data.tokenizer_configs import get_tokenizer_configs
9898
return [(k, v) for k, v in get_tokenizer_configs().items() if k not in skipped_models]
9999

100100

tests/python_tests/test_vlm_pipeline.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import transformers
88
from optimum.intel.openvino import OVModelForVisualCausalLM
99
from openvino_genai import VLMPipeline, GenerationConfig
10-
from common import get_image_by_link
1110

1211
from utils.generation_config import get_beam_search, get_multinomial_all_parameters
1312
from utils.constants import get_default_llm_properties
@@ -54,6 +53,20 @@ def get_ov_model(model_id, cache):
5453
"katuni4ka/tiny-random-qwen2vl",
5554
]
5655

56+
57+
def get_image_by_link(link):
58+
from PIL import Image
59+
import requests
60+
from openvino import Tensor
61+
import numpy as np
62+
63+
image = Image.open(requests.get(link, stream=True).raw)
64+
if image.mode != 'RGB':
65+
image = image.convert('RGB')
66+
image_data = np.array((np.array(image.getdata()) - 128).astype(np.byte)).reshape(1, image.size[1], image.size[0], 3)
67+
return Tensor(image_data)
68+
69+
5770
@pytest.mark.precommit
5871
@pytest.mark.nightly
5972
@pytest.mark.parametrize("model_id", model_ids)
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright (C) 2018-2025 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from openvino_genai import GenerationResult, GenerationConfig
5+
from typing import List
6+
7+
def compare_generation_result(hf_result: GenerationResult,
8+
ov_result: GenerationResult,
9+
generation_config: GenerationConfig):
10+
if generation_config.is_beam_search():
11+
assert len(hf_result.m_scores) == len(ov_result.m_scores)
12+
for hf_score, ov_score in zip(hf_result.m_scores, ov_result.m_scores):
13+
# Note, that for fp32 / fp16 models scores are different less than 0.001
14+
assert abs(hf_score - ov_score) < 0.02
15+
16+
if not generation_config.include_stop_str_in_output and len(generation_config.stop_strings) > 0:
17+
assert len(hf_result.m_generation_ids) >= len(ov_result.m_generation_ids)
18+
for hf_text, ov_text in zip(hf_result.m_generation_ids, ov_result.m_generation_ids):
19+
assert ov_text in hf_text
20+
else:
21+
assert len(hf_result.m_generation_ids) == len(ov_result.m_generation_ids)
22+
for hf_text, ov_text in zip(hf_result.m_generation_ids, ov_result.m_generation_ids):
23+
assert hf_text == ov_text
24+
25+
26+
def compare_generation_results(prompts: List[str],
27+
hf_results: List[GenerationResult],
28+
ov_results: List[GenerationResult],
29+
generation_configs: List[GenerationConfig] | GenerationConfig):
30+
if type(generation_configs) is not list:
31+
generation_configs = [generation_configs]
32+
33+
assert len(prompts) == len(hf_results)
34+
assert len(prompts) == len(ov_results)
35+
36+
for prompt, ref_result, ov_result, generation_config in zip(prompts, hf_results, ov_results, generation_configs):
37+
print(f"Prompt = {prompt}\nReference result = {ref_result}\nOpenVINO result = {ov_result.m_generation_ids}")
38+
compare_generation_result(ref_result, ov_result, generation_config)
File renamed without changes.

0 commit comments

Comments
 (0)