OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
# Copyright (C) 2018-2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
"""
Copyright (C) 2018-2026 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json
import os
import platform
import numpy as np
import pathlib
import pytest
from common.samples_common_test_class import get_devices, get_cmd_output, prepend
from openvino import opset8 as opset
from openvino.tools.benchmark.utils.utils import parse_devices
import openvino as ov
def get_executable(sample_language):
executable = 'benchmark_app'
if sample_language == 'C++':
executable = pathlib.Path(os.environ['IE_APP_PATH'], 'benchmark_app').with_suffix('.exe' if os.name == 'nt' else '')
assert executable.exists()
return executable
return 'benchmark_app'
def create_random_4bit_bin_file(tmp_path, shape, name):
fullname = tmp_path / name
rs = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(0)))
pack_shape = [x for x in shape]
pack_shape[-1] = pack_shape[-1]*4
rand_data = (rs.uniform(0, 15, list(pack_shape)) >= 7).astype(int).flatten()
raw_data = np.packbits(rand_data)
with open(fullname, "wb") as f:
f.write(raw_data)
def create_low_latency_model():
inp = opset.parameter([1], ov.Type.f32, name='input')
relu = opset.relu(inp)
result = opset.result(relu, name='relu')
return ov.Model([result], [inp], 'low_latency_model')
def read_csv_report(report_path):
report = {}
with report_path.open(encoding='utf-8') as file:
for line in file:
line = line.strip()
if not line or ';' not in line:
continue
parts = [item for item in line.split(';') if item]
if len(parts) >= 2:
report[parts[0]] = parts[1]
return report
def get_avg_latency_from_report(report_data):
for key, value in report_data.items():
normalized = key.lower()
if normalized in {'avg latency', 'average latency (ms)', 'latency_avg'}:
return float(value)
raise AssertionError(f'Average latency entry was not found in report: {report_data}')
def verify(sample_language, device, api=None, nireq=None, shape=None, data_shape=None, iop=None, nstreams=None,
layout=None, pin=None, cache=None, tmp_path=None, model='bvlcalexnet-12.onnx',
inp='dog-224x224.bmp', batch='1', niter='10', max_irate=None, tm=None):
output = get_cmd_output(
get_executable(sample_language),
*prepend(cache, inp, model, tmp_path),
*('-nstreams', nstreams) if nstreams else '',
*('-layout', layout) if layout else '',
*('-nireq', nireq) if nireq else '',
*('-max_irate', max_irate) if max_irate else '',
*('-shape', shape) if shape else '',
*('-data_shape', data_shape) if data_shape else '',
*('-iop', iop) if iop else '',
*('-hint', 'none') if nstreams or pin else '',
*('-pin', pin) if pin else '',
*('-api', api) if api else '',
*('-dump_config', tmp_path / 'conf.json') if tmp_path else '',
*('-exec_graph_path', tmp_path / 'exec_graph.xml') if tmp_path else '',
*('-b', batch) if batch else '',
*('-niter', niter) if niter else '10',
*('-t', tm) if tm else '',
'-d', device
)
assert 'FPS' in output
# No Windows support due to the lack of the ‘psutil’ module in the CI infrastructure
# No Macos support due to no /proc/self/status file
if platform.system() == "Linux":
assert 'Compile model ram used' in output
if tmp_path:
assert (tmp_path / 'exec_graph.xml').exists()
with (tmp_path / 'conf.json').open(encoding='utf-8') as file:
config_json = json.load(file)
if 'CPU' == device:
assert 'CPU' in config_json
assert not nstreams or config_json['CPU']['NUM_STREAMS'] == nstreams
assert (not pin
or pin == 'YES' and config_json['CPU']['ENABLE_CPU_PINNING'] == 'YES'
or pin == 'NO' and config_json['CPU']['ENABLE_CPU_PINNING'] == 'NO')
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
def test_benchmark_app_help(sample_language):
get_cmd_output(get_executable(sample_language), '-h')
def test_parse_devices_with_empty_entries():
assert parse_devices('MULTI:CPU,,GPU') == ['MULTI', 'CPU', 'GPU']
assert parse_devices('MULTI:CPU, ,GPU') == ['MULTI', 'CPU', 'GPU']
assert parse_devices('HETERO:CPU,') == ['HETERO', 'CPU']
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('api', ['sync', 'async'])
@pytest.mark.parametrize('nireq', ['4', ''])
@pytest.mark.parametrize('device', get_devices())
def test_nireq(sample_language, api, nireq, device, cache, tmp_path):
verify(sample_language, device, api=api, nireq=nireq, cache=cache, tmp_path=tmp_path)
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('max_irate', ['', '0', '10'])
@pytest.mark.parametrize('device', get_devices())
def test_max_irate(sample_language, device, max_irate, cache, tmp_path):
verify(sample_language, device, max_irate=max_irate, cache=cache, tmp_path=tmp_path)
@pytest.mark.skipif('CPU' not in get_devices(), reason='affinity is a CPU property')
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('pin', ['YES', 'NO'])
def test_pin(sample_language, pin, cache, tmp_path):
verify(sample_language, 'CPU', pin=pin, nstreams='2', cache=cache, tmp_path=tmp_path)
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', sorted({'CPU', 'GPU'} & set(get_devices()))) # Determenisitic order is required for --numprocesses
def test_simple(sample_language, device, cache, tmp_path):
verify(sample_language, device, cache=cache, tmp_path=tmp_path)
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('api', ['sync', 'async'])
@pytest.mark.parametrize('device', get_devices())
def test_api(sample_language, api, device, cache, tmp_path):
verify(sample_language, device, api=api, cache=cache, tmp_path=tmp_path)
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
def test_reshape(sample_language, device, cache, tmp_path):
verify(sample_language, device, shape='data_0[2,3,224,224]', cache=cache, tmp_path=tmp_path)
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
def test_dynamic_shape(sample_language, device, cache, tmp_path):
verify(sample_language, device, model='efficientnet-lite4-11-qdq.onnx',
shape='[?,224,224,3]', data_shape='[1,224,224,3][2,224,224,3]', layout='[NHWC]', cache=cache, tmp_path=tmp_path)
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', ['CPU'])
@pytest.mark.parametrize('inp', [None, 'random_4bit_data.bin'])
def test_4bit_precision_input(sample_language, device, inp, cache, tmp_path):
inp_type = ov.Type.i4
inp_shape = [128] # only pass scalar.
input = opset.parameter(inp_shape, inp_type, name='in')
cvt = opset.convert(input, ov.Type.f32)
result = opset.result(cvt, name='cvt')
model_4bit = ov.Model([result], [input], 'model_with_4bit_input')
if inp != None and inp.endswith(".bin"):
create_random_4bit_bin_file(tmp_path, inp_shape, inp)
verify(sample_language, device, model=model_4bit, inp=inp, cache=cache, tmp_path=tmp_path, batch=None, tm='1')
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
def test_out_of_tensor_size_range_npy_multibatch(sample_language, device, cache, tmp_path):
inp = tmp_path / 'batch2.npy'
with open(inp, "wb") as batch2_npy:
np.save(
batch2_npy,
np.random.RandomState(
np.random.MT19937(np.random.SeedSequence(0))
).uniform(0, 256, [2, 3, 224, 224]).astype(np.uint8)
)
# benchmark_app reads batch from model or cmd, not from npy.
# benchmark_app still verifyes npy shape for python impl.
verify(sample_language, device, inp=inp, cache=cache, tmp_path=tmp_path, batch='2')
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
@pytest.mark.parametrize('pv_in_tensor_names', [{'87', 'input.1'}, {'87', 'input.1', 'pixel_values'}, {''}, set()])
@pytest.mark.parametrize('pv_out_tensor_names', [{''}])
@pytest.mark.parametrize('am_in_tensor_names', [{'877', 'attention_mask'}])
@pytest.mark.parametrize('am_out_tensor_names', [{''}])
def test_input_tensor_with_multiple_names(sample_language, device, pv_in_tensor_names, pv_out_tensor_names, am_in_tensor_names, am_out_tensor_names, cache, tmp_path):
'''
Tests the ability to resolve the correct tensor name from the -shape cmd argument when a tensor has multiple assigned names.
'''
pv_param_shape = [-1, -1, -1, -1]
pv_data_shape = [1, 3, 224, 224]
pv_param_node = opset.parameter(pv_param_shape, ov.Type.f32, name="pixel_values")
pv_param_node.output(0).set_names(pv_in_tensor_names)
pv_cvt_node = opset.convert(pv_param_node, ov.Type.f16)
pv_result_node = opset.result(pv_cvt_node, name='pv_result')
pv_result_node.output(0).set_names(pv_out_tensor_names)
am_param_shape = [-1, -1]
am_data_shape = [1, 77]
am_param_node = opset.parameter(am_param_shape, ov.Type.i64, name="attention_mask")
am_param_node.output(0).set_names(am_in_tensor_names)
am_cvt_node = opset.convert(am_param_node, ov.Type.i32)
am_result_node = opset.result(am_cvt_node, name='am_result')
am_result_node.output(0).set_names(am_out_tensor_names)
model = ov.Model([pv_result_node, am_result_node], [pv_param_node, am_param_node], 'model_with_multi_name_input_tensor')
data_shape = f'pixel_values{pv_data_shape},attention_mask{am_data_shape}'
# We intentionally set shape here instead of data_shape to catch an assert from benchmark_app,
# if one of command line input shapes has not been captured correctly.
verify(sample_language, device, shape=data_shape, model=model, inp=None, cache=cache, tmp_path=tmp_path, batch=None, tm='1')
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
@pytest.mark.parametrize('in_node_name, in_tensor_names, out_node_name, out_tensor_names', [
('input', {'actual_input'}, 'actual_input', {'input'}),
])
def test_input_output_tensor_name_collision(sample_language, device, in_node_name, in_tensor_names, out_node_name, out_tensor_names, cache, tmp_path):
'''
Tests the priority of resolving the correct tensor name from the -iop cmd argument in case of name collisions between input/output tensors and nodes.
'''
data_shape = [1, 3, 224, 224]
param_node = opset.parameter(data_shape, ov.Type.f32, name=in_node_name)
param_node.output(0).set_names(in_tensor_names)
cvt_node = opset.convert(param_node, ov.Type.f16)
result_node = opset.result(cvt_node.output(0), name=out_node_name)
result_node.output(0).set_names(out_tensor_names)
model = ov.Model([result_node], [param_node], 'model_with_name_collision')
iop = f'{list(in_tensor_names)[0]}:i32,{list(out_tensor_names)[0]}:i64'
inp = tmp_path / 'i32_tensor.npy'
# actual_input is int32 so we generate corresponding data. i64 tensor would fail.
with open(inp, "wb") as i32_tensor:
np.save(
i32_tensor,
np.random.RandomState(
np.random.MT19937(np.random.SeedSequence(0))
).uniform(0, 256, data_shape).astype(np.int32)
)
verify(sample_language, device, iop=iop, model=model, inp=inp, cache=cache, tmp_path=tmp_path, batch=None, tm='1')
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
def test_benchmark_app_no_warmup_flag(sample_language, device, cache, tmp_path):
"""Test that -no_warmup flag skips warmup inference and produces correct output"""
# test with warmup (default behavior)
output_with_warmup = get_cmd_output(
get_executable(sample_language),
*prepend(cache, 'dog-224x224.bmp', 'bvlcalexnet-12.onnx', tmp_path),
'-d', device,
'-niter', '1',
'-nireq', '1'
)
# verify default behavior shows warmup message
assert 'FPS' in output_with_warmup
assert 'First inference took' in output_with_warmup
# test without warmup
output_no_warmup = get_cmd_output(
get_executable(sample_language),
*prepend(cache, 'dog-224x224.bmp', 'bvlcalexnet-12.onnx', tmp_path),
'-d', device,
'-niter', '1',
'-nireq', '1',
'-no_warmup'
)
# verify -no_warmup behavior
assert 'FPS' in output_no_warmup
assert 'Skipping warmup inference due to -no_warmup flag' in output_no_warmup
assert 'First inference took' not in output_no_warmup
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
def test_benchmark_app_no_warmup_help(sample_language):
"""Test that -no_warmup option appears in help output"""
output = get_cmd_output(get_executable(sample_language), '-h')
assert '-no_warmup' in output
assert 'Skip warmup inference' in output
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
def test_compile_only_via_niter_0(sample_language, device, cache, tmp_path):
"""Test that -niter 0 compiles the model and exits without running inference."""
output = get_cmd_output(
get_executable(sample_language),
*prepend(cache, 'dog-224x224.bmp', 'bvlcalexnet-12.onnx', tmp_path),
'-d', device,
'-niter', '0',
)
assert 'FPS' not in output
assert 'Model compiled successfully' in output
assert 'Skipping inference due to -niter 0' in output
assert '(skipped)' in output
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
def test_compile_only_niter_0_dynamic_shape(sample_language, device, tmp_path):
"""Test that -niter 0 compiles a dynamic-shape model without requiring -data_shape or -i."""
param = opset.parameter([-1, -1, 3, -1], ov.Type.f32, name='input')
result = opset.result(opset.relu(param), name='output')
model = ov.Model([result], [param], 'dynamic_model')
model_path = tmp_path / 'dynamic_model.xml'
ov.save_model(model, model_path)
output = get_cmd_output(
get_executable(sample_language),
'-m', model_path,
'-d', device,
'-niter', '0',
)
assert 'FPS' not in output
assert 'Model compiled successfully' in output
assert 'Skipping inference due to -niter 0' in output
@pytest.mark.parametrize('device', get_devices())
def test_compile_only_niter_0_dynamic_shape_with_batch(device, tmp_path):
"""Test that -b combined with -niter 0 and a dynamic-shape model is handled correctly.
C++: dataShape is empty for dynamic inputs in compile_only mode (no -data_shape/-i provided).
The batch update block must not access dataShape.at(batch_index) — it should warn and
skip instead.
Python: batch update uses partial_shape (always populated from the model), so -b is applied
correctly without any special handling or warning.
"""
param = opset.parameter([-1, 3, -1, -1], ov.Type.f32, name='input')
param.set_layout(ov.Layout('NCHW'))
result = opset.result(opset.relu(param), name='output')
model = ov.Model([result], [param], 'dynamic_model_with_batch')
model_path = tmp_path / 'dynamic_model.xml'
ov.save_model(model, model_path)
# C++: -b is ignored (cannot update empty dataShape), a warning is emitted
cpp_output = get_cmd_output(
get_executable('C++'),
'-m', model_path,
'-d', device,
'-niter', '0',
'-b', '4',
)
assert 'FPS' not in cpp_output
assert 'Model compiled successfully' in cpp_output
assert 'Skipping inference due to -niter 0' in cpp_output
assert '-b option is ignored in compile_only mode' in cpp_output
# Python: -b is applied to partial_shape before compilation — compiles successfully, no warning
py_output = get_cmd_output(
get_executable('Python'),
'-m', model_path,
'-d', device,
'-niter', '0',
'-b', '4',
)
assert 'FPS' not in py_output
assert 'Model compiled successfully' in py_output
assert 'Skipping inference due to -niter 0' in py_output
assert '-b option is ignored in compile_only mode' not in py_output
# Note: devices which do not support export_model() are excluded.
@pytest.mark.parametrize('device', sorted({'CPU', 'GPU'} & set(get_devices())))
def test_compile_only_niter_0_compiled_blob(device, tmp_path):
"""Test that -niter 0 works with a pre-compiled blob model (C++ only, blob path)."""
param = opset.parameter([1, 3, 224, 224], ov.Type.f32, name='input')
result = opset.result(opset.relu(param), name='output')
model = ov.Model([result], [param], 'static_model')
blob_path = tmp_path / 'model.blob'
core = ov.Core()
blob_path.write_bytes(core.compile_model(model, device).export_model().getvalue())
output = get_cmd_output(
get_executable('C++'),
'-m', blob_path,
'-d', device,
'-niter', '0',
)
assert 'FPS' not in output
assert 'Model compiled successfully' in output
assert 'Skipping inference due to -niter 0' in output
@pytest.mark.parametrize('device', get_devices())
def test_compile_only_via_long_form_number_iterations(device, cache, tmp_path):
"""Test that --number_iterations 0 (long form) also triggers compile-only mode in Python.
Previously only the short -niter flag was detected; this covers the fix that uses
args.number_iterations == 0 instead of is_flag_set_in_command_line('niter')."""
output = get_cmd_output(
get_executable('Python'),
*prepend(cache, 'dog-224x224.bmp', 'bvlcalexnet-12.onnx', tmp_path),
'-d', device,
'--number_iterations', '0',
)
assert 'FPS' not in output
assert 'Model compiled successfully' in output
assert 'Skipping inference due to -niter 0' in output
assert '(skipped)' in output
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
def test_compile_only_sync_nireq_skips_validation(sample_language, device, cache, tmp_path):
"""Both C++ and Python: -api sync -nireq 2 -niter 0 must not raise the nireq > niter
validation error.
"""
# compile-only: nireq > niter is allowed, inference is entirely skipped
output = get_cmd_output(
get_executable(sample_language),
*prepend(cache, 'dog-224x224.bmp', 'bvlcalexnet-12.onnx', tmp_path),
'-d', device,
'-api', 'sync',
'-nireq', '2',
'-niter', '0',
)
assert 'FPS' not in output
assert 'Model compiled successfully' in output
assert 'Skipping inference due to -niter 0' in output
# sanity-check: the nireq > niter guard must still fire for normal runs
try:
get_cmd_output(
get_executable(sample_language),
*prepend(cache, 'dog-224x224.bmp', 'bvlcalexnet-12.onnx', tmp_path),
'-d', device,
'-api', 'sync',
'-nireq', '2',
'-niter', '1',
)
pytest.fail('Expected an error for nireq > niter in sync mode, but no error was raised')
except Exception as e:
error_text = str(getattr(e, 'output', str(e)))
# C++ message: "Number of iterations should be greater than number of infer requests when using sync API."
# Python message: "Number of infer requests should be less than or equal to number of iterations in sync mode."
assert ('Number of iterations should be greater than number of infer requests' in error_text or
'Number of infer requests should be less than or equal to number of iterations' in error_text)
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
def test_niter_0_in_help(sample_language):
"""Test that -niter help text documents the compile-only special case."""
output = get_cmd_output(get_executable(sample_language), '-h')
assert '-niter' in output
assert 'niter 0' in output
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('device', get_devices())
@pytest.mark.parametrize('api', ['sync', 'async'])
def test_benchmark_app_no_warmup_with_api_modes(sample_language, device, api, cache, tmp_path):
"""Test -no_warmup flag works with different API modes"""
output = get_cmd_output(
get_executable(sample_language),
*prepend(cache, 'dog-224x224.bmp', 'bvlcalexnet-12.onnx', tmp_path),
'-d', device,
'-niter', '1',
'-nireq', '1',
'-api', api,
'-no_warmup'
)
assert 'FPS' in output
assert 'Skipping warmup inference due to -no_warmup flag' in output
assert 'First inference took' not in output
@pytest.mark.skipif('CPU' not in get_devices(), reason='sub-millisecond formatting is validated on CPU')
@pytest.mark.parametrize('sample_language', ['C++', 'Python'])
@pytest.mark.parametrize('json_stats', [False, True])
def test_benchmark_app_sub_ms_console_output_only(sample_language, json_stats, cache, tmp_path):
model = create_low_latency_model()
report_folder = tmp_path / ('json_report' if json_stats else 'csv_report')
report_folder.mkdir()
output = get_cmd_output(
get_executable(sample_language),
*prepend(cache, model=model, tmp_path=tmp_path),
'-d', 'CPU',
'-api', 'sync',
'-nireq', '1',
'-niter', '20',
'-report_type', 'no_counters',
'-report_folder', report_folder,
*(('-json_stats=true',) if json_stats else ()),
)
assert 'Latency:' in output
assert 'Average:' in output
assert ' us' in output
assert 'Throughput' in output
if json_stats:
with (report_folder / 'benchmark_report.json').open(encoding='utf-8') as file:
report = json.load(file)['execution_results']
else:
report = read_csv_report(report_folder / 'benchmark_report.csv')
assert get_avg_latency_from_report(report) < 1.0