feat: Implement unified memory management for ONNX model inference

- Introduced a new memory management policy for ONNX Runtime to release temporary GPU memory after inference completion.
- Established a shared inference entry point for all ONNX models, differentiating between long-lived sessions and temporary memory arenas.
- Updated CUDA sessions to utilize a shrinkable memory arena, reclaiming GPU memory post-inference.
- Ensured consistent memory release semantics across BEN2, DA3, MoGe-2, and Upscale models while reusing existing sessions.
- Added lifecycle tests to validate temporary memory release and session reuse.
- Modified model adapters to integrate the new memory management features, ensuring backward compatibility with existing inference behavior.
- Enhanced unit tests to cover new memory release scenarios and verify correct behavior across different execution providers.
This commit is contained in:
2026-09-01 13:22:35 +08:00
parent 4d4b892d64
commit 2ff7b04f56
24 changed files with 767 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ import sys
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
import numpy as np
from PIL import Image
@@ -20,6 +21,9 @@ import onnx_ben2
class FakeSession:
def get_providers(self):
return ["CPUExecutionProvider"]
def get_inputs(self):
return [SimpleNamespace(name="input")]
@@ -34,6 +38,25 @@ class FakeSession:
class Ben2OnnxTest(unittest.TestCase):
def test_infer_forwards_memory_release_boundary(self):
session = FakeSession()
with patch.object(
onnx_ben2,
"run_session",
side_effect=lambda model, names, feeds, **_options: model.run(
names, feeds
),
) as run:
result = onnx_ben2.infer(
session,
Image.new("RGB", (40, 20)),
release_memory=False,
)
self.assertEqual(result.size, (40, 20))
self.assertFalse(run.call_args.kwargs["release_memory"])
def test_preprocess_matches_model_layout(self):
image = Image.new("RGB", (40, 20), (64, 128, 192))
result = onnx_ben2.preprocess(image)