From 3c122150ae391c19a808dd7ca65bd20908f3c806 Mon Sep 17 00:00:00 2001 From: "Rmdev.pro" Date: Wed, 18 Nov 2026 17:36:11 -0500 Subject: [PATCH] Fix ONNX Runtime CPU memory arena configuration (#25003) * Fix CPU memory arena option on ONNX Runtime sessions * Limit CPU arena correction to CPU enrichment sessions * Retain default arena in GPU runner integration regression * Preserve native detector session-options contract --- frigate/detectors/detection_runners.py | 2 ++ frigate/test/test_detection_runners.py | 12 ++++++++++-- frigate/test/test_runner_devices.py | 5 ++++- frigate/util/model.py | 15 ++------------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/frigate/detectors/detection_runners.py b/frigate/detectors/detection_runners.py index 80c2fc57f9..0a14a596a2 100644 --- a/frigate/detectors/detection_runners.py +++ b/frigate/detectors/detection_runners.py @@ -153,6 +153,8 @@ def get_ort_session_options( sess_options = ort.SessionOptions() if cpu: + # ORT reads the CPU arena flag from session options, not provider options. + sess_options.enable_cpu_mem_arena = False # idle intra-op threads otherwise spin for ~1s after every run, keeping # most cores busy while an enrichment runs even once a second sess_options.add_session_config_entry("session.intra_op.allow_spinning", "0") diff --git a/frigate/test/test_detection_runners.py b/frigate/test/test_detection_runners.py index 21e4a18fb4..3fe141b67c 100644 --- a/frigate/test/test_detection_runners.py +++ b/frigate/test/test_detection_runners.py @@ -59,10 +59,18 @@ class TestGetOrtSessionOptions(unittest.TestCase): ort.GraphOptimizationLevel.ORT_ENABLE_ALL, ) + def test_only_cpu_enrichment_models_disable_the_cpu_arena(self): + for model_type in EnrichmentModelTypeEnum: + for cpu in [False, True]: + with self.subTest(model_type=model_type, cpu=cpu): + options = get_ort_session_options(model_type.value, cpu=cpu) + self.assertEqual(options.enable_cpu_mem_arena, not cpu) + def test_detector_models_use_defaults(self): for model_type in [None, ModelTypeEnum.rfdetr.value]: - with self.subTest(model_type=model_type): - self.assertIsNone(get_ort_session_options(model_type, cpu=True)) + for cpu in [False, True]: + with self.subTest(model_type=model_type, cpu=cpu): + self.assertIsNone(get_ort_session_options(model_type, cpu=cpu)) class TestCudaGraphRunner(unittest.TestCase): diff --git a/frigate/test/test_runner_devices.py b/frigate/test/test_runner_devices.py index cd6c76c69d..6803e15372 100644 --- a/frigate/test/test_runner_devices.py +++ b/frigate/test/test_runner_devices.py @@ -140,6 +140,9 @@ class TestLoadRegistry(unittest.TestCase): spinning = None self.assertEqual(spinning, expected) + self.assertEqual( + options.enable_cpu_mem_arena, provider != "CPUExecutionProvider" + ) class TestLoadedDeviceSnapshot(unittest.TestCase): @@ -560,4 +563,4 @@ class TestMIGraphX(unittest.TestCase): providers, options = model_util.get_ort_providers() self.assertEqual(providers, ["CPUExecutionProvider"]) - self.assertEqual(options, [{"enable_cpu_mem_arena": False}]) + self.assertEqual(options, [{}]) diff --git a/frigate/util/model.py b/frigate/util/model.py index fbf21ae432..9e7c4edc73 100644 --- a/frigate/util/model.py +++ b/frigate/util/model.py @@ -320,14 +320,7 @@ def get_ort_providers( device: str | None = "AUTO", ) -> tuple[list[str], list[dict[str, Any]]]: if force_cpu: - return ( - ["CPUExecutionProvider"], - [ - { - "enable_cpu_mem_arena": False, - } - ], - ) + return ["CPUExecutionProvider"], [{}] providers = [] options = [] @@ -389,11 +382,7 @@ def get_ort_providers( continue elif provider == "CPUExecutionProvider": providers.append(provider) - options.append( - { - "enable_cpu_mem_arena": False, - } - ) + options.append({}) elif provider == "AzureExecutionProvider": # Skip Azure provider - not typically available on local hardware # and prevents fallback to OpenVINO when it's the first provider