diff --git a/docker/rocm/rootfs/etc/s6-overlay/s6-rc.d/compile-rocm-models/run b/docker/rocm/rootfs/etc/s6-overlay/s6-rc.d/compile-rocm-models/run index 418b0156b..f7b084ad7 100755 --- a/docker/rocm/rootfs/etc/s6-overlay/s6-rc.d/compile-rocm-models/run +++ b/docker/rocm/rootfs/etc/s6-overlay/s6-rc.d/compile-rocm-models/run @@ -2,6 +2,13 @@ # shellcheck shell=bash # Compile YoloV8 ONNX files into ROCm MIGraphX files +OVERRIDE=$(cd /opt/frigate && python3 -c 'import frigate.detectors.plugins.rocm as rocm; print(rocm.auto_override_gfx_version())') + +if ! test -z "$OVERRIDE"; then + echo "Using HSA_OVERRIDE_GFX_VERSION=${OVERRIDE}" + export HSA_OVERRIDE_GFX_VERSION=$OVERRIDE +fi + for onnx in /config/model_cache/yolov8/*.onnx do mxr="${onnx%.onnx}.mxr" diff --git a/frigate/detectors/plugins/rocm.py b/frigate/detectors/plugins/rocm.py index b188a5854..6f2c3c1a8 100644 --- a/frigate/detectors/plugins/rocm.py +++ b/frigate/detectors/plugins/rocm.py @@ -7,7 +7,7 @@ import ctypes from pydantic import Field from typing_extensions import Literal import glob -import cv2 +import subprocess from frigate.detectors.detection_api import DetectionApi from frigate.detectors.detector_config import BaseDetectorConfig @@ -18,14 +18,41 @@ logger = logging.getLogger(__name__) DETECTOR_KEY = "rocm" +def detect_gfx_version(): + return subprocess.getoutput("unset HSA_OVERRIDE_GFX_VERSION && /opt/rocm/bin/rocminfo | grep gfx |head -1|awk '{print $2}'") + +def auto_override_gfx_version(): + # If environment varialbe already in place, do not override + gfx_version = detect_gfx_version() + old_override = os.getenv('HSA_OVERRIDE_GFX_VERSION') + if old_override not in (None, ''): + logger.warning(f"AMD/ROCm: detected {gfx_version} but HSA_OVERRIDE_GFX_VERSION already present ({old_override}), not overriding!") + return old_override + mapping = { + 'gfx90c': '9.0.0', + 'gfx1031': '10.3.0', + 'gfx1103': '11.0.0', + } + override = mapping.get(gfx_version) + if override is not None: + logger.warning(f"AMD/ROCm: detected {gfx_version}, overriding HSA_OVERRIDE_GFX_VERSION={override}") + os.putenv('HSA_OVERRIDE_GFX_VERSION', override) + return override + return '' + + class ROCmDetectorConfig(BaseDetectorConfig): type: Literal[DETECTOR_KEY] conserve_cpu: bool = Field(default=True, title="Conserve CPU at the expense of latency (and reduced max throughput)") + auto_override_gfx: bool = Field(default=True, title="Automatically detect and override gfx version") class ROCmDetector(DetectionApi): type_key = DETECTOR_KEY def __init__(self, detector_config: ROCmDetectorConfig): + if detector_config.auto_override_gfx: + auto_override_gfx_version() + try: sys.path.append("/opt/rocm/lib") import migraphx