cmvr-visual/inference/tests/test_video_edit_onnx_threads.py

97 lines
3.5 KiB
Python
Raw Normal View History

"""Bounded ORT factory tests without ONNX, CUDA, or network access."""
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
sys.path.insert(0, str(Path(__file__).parents[1]))
def fake_modules(monkeypatch, fail_pose=False):
sessions = []
class Options:
def __init__(self):
self.config = {}
def add_session_config_entry(self, key, value):
self.config[key] = value
def inference_session(checkpoint, *, sess_options, providers):
assert type(sess_options) is Options
assert providers == ["CPUExecutionProvider"]
sessions.append((checkpoint, sess_options))
return object()
ort = SimpleNamespace(
SessionOptions=Options,
ExecutionMode=SimpleNamespace(ORT_SEQUENTIAL=object()),
InferenceSession=inference_session,
)
module = SimpleNamespace(onnxruntime=ort)
class Model:
def __init__(self, checkpoint, device, **kwargs):
assert device == "cpu"
if fail_pose and "end2end" in checkpoint:
raise RuntimeError("pose constructor failed")
self.session = module.onnxruntime.InferenceSession(
checkpoint, providers=["CPUExecutionProvider"]
)
module.Yolo = module.ViTPose = Model
monkeypatch.setitem(sys.modules, "pose2d", module)
monkeypatch.setitem(sys.modules, "onnxruntime", ort)
return module, ort, sessions, inference_session
@pytest.mark.parametrize("threads", [None, "2", "4"])
def test_detector_and_pose_sessions_have_bounded_sequential_pools(
tmp_path, monkeypatch, threads
):
from llm_services.video_edit.preprocess import load_cpu_pose_models
module, ort, sessions, original = fake_modules(monkeypatch)
if threads is None:
monkeypatch.delenv("OMP_NUM_THREADS", raising=False)
else:
monkeypatch.setenv("OMP_NUM_THREADS", threads)
detector, pose = load_cpu_pose_models(tmp_path)
assert detector.session is not pose.session
assert len(sessions) == 2 and sessions[0][1] is not sessions[1][1]
for _, options in sessions:
assert options.intra_op_num_threads == int(threads or "2")
assert options.inter_op_num_threads == 1
assert options.execution_mode is ort.ExecutionMode.ORT_SEQUENTIAL
assert options.config["session.intra_op.allow_spinning"] == "0"
assert options.config["session.inter_op.allow_spinning"] == "0"
assert module.onnxruntime is ort
assert ort.InferenceSession is original
@pytest.mark.parametrize("value", ["", "0", "-1", "2.0", "NaN", "65", " 2", "٢"])
def test_invalid_thread_budget_fails_before_constructing_sessions(
tmp_path, monkeypatch, value
):
from llm_services.video_edit.preprocess import load_cpu_pose_models
module, ort, sessions, original = fake_modules(monkeypatch)
monkeypatch.setenv("OMP_NUM_THREADS", value)
with pytest.raises(ValueError, match="OMP_NUM_THREADS"):
load_cpu_pose_models(tmp_path)
assert sessions == [] and module.onnxruntime is ort
assert ort.InferenceSession is original
def test_failed_pose_initialization_restores_module_facade(tmp_path, monkeypatch):
from llm_services.video_edit.preprocess import load_cpu_pose_models
module, ort, sessions, original = fake_modules(monkeypatch, fail_pose=True)
monkeypatch.setenv("OMP_NUM_THREADS", "2")
with pytest.raises(RuntimeError, match="pose constructor"):
load_cpu_pose_models(tmp_path)
assert len(sessions) == 1
assert module.onnxruntime is ort and ort.InferenceSession is original