容器看得到宿主 244 个核,配额只有 4–6 个;onnxruntime 默认按核数开 125 个线程。 10-15 容器实测 resnet50(4 核):默认 320–500 ms/次,限到 4 线程 22 ms;hmct 校准写死的 10 线程约 35–48 ms。 做法:worker 把 harness/container_site 放到 PYTHONPATH 最前,其中 sitecustomize 在 onnxruntime 第一次 import 时 给 InferenceSession 加上限(OMP_NUM_THREADS),不用 onnxruntime 的进程什么都不做。它盖掉的镜像自带 sitecustomize 只装 Ubuntu 的 apport 崩溃钩子。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
"""harness/container_site/sitecustomize.py caps onnxruntime sessions at OMP_NUM_THREADS (Salty containers)."""
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
SITE = Path(__file__).resolve().parents[1] / "harness/container_site"
|
|
FAKE = '''
|
|
class SessionOptions:
|
|
intra_op_num_threads = 0
|
|
class InferenceSession:
|
|
def __init__(self, path_or_bytes, sess_options=None, providers=None):
|
|
self.threads = sess_options.intra_op_num_threads
|
|
'''
|
|
CHILD = '''
|
|
import onnxruntime as o
|
|
def threads(n=None):
|
|
so = None
|
|
if n is not None:
|
|
so = o.SessionOptions(); so.intra_op_num_threads = n
|
|
return o.InferenceSession("m.onnx", sess_options=so).threads
|
|
print(threads(), threads(10), threads(1))
|
|
'''
|
|
|
|
|
|
class ContainerSite(unittest.TestCase):
|
|
def test_onnxruntime_sessions_are_capped_at_the_cpu_quota(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
(Path(td) / "onnxruntime").mkdir()
|
|
(Path(td) / "onnxruntime/__init__.py").write_text(FAKE)
|
|
env = dict(os.environ, PYTHONPATH=f"{SITE}{os.pathsep}{td}", OMP_NUM_THREADS="2")
|
|
out = subprocess.run([sys.executable, "-c", CHILD], env=env, capture_output=True, text=True, timeout=60)
|
|
self.assertEqual(out.stdout.split(), ["2", "2", "1"], out.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|