From b3436a61eeaed982ce256fb9380527649fdd5cd9 Mon Sep 17 00:00:00 2001 From: Tuan Nghia Nguyen Date: Thu, 18 Jun 2026 05:55:31 +1000 Subject: [PATCH] Prevent OOM --- .claude/settings.local.json | 93 +++++- CMakeLists.txt | 15 +- cmake/Dependencies.cmake | 58 +++- core/ANSLibsLoader/EPLoader.cpp | 2 +- core/ANSLicensingSystem/ANSLicense.cpp | 21 ++ core/ANSLicensingSystem/ANSLicense.h | 11 + core/ANSLicensingSystem/dllmain.cpp | 37 +++ engines/ONNXEngine/EPLoader.cpp | 2 +- engines/ONNXEngine/ONNXEngine.cpp | 14 +- engines/ONNXEngine/ONNXEngine.h | 7 +- engines/ONNXEngine/ONNXSAM3.cpp | 2 +- .../include/engine/EngineBuildLoadNetwork.inl | 24 +- include/GpuMemoryGuard.h | 96 ++++++ .../ANSOCR/ANSONNXOCR/PaddleOCRV5Engine.cpp | 2 +- modules/ANSODEngine/ANSEngineCommon.h | 17 + modules/ANSODEngine/ANSFaceDetectorEngine.cpp | 2 +- modules/ANSODEngine/ANSONNXCL.cpp | 2 +- modules/ANSODEngine/ANSONNXCL.h | 7 + modules/ANSODEngine/ANSONNXOBB.cpp | 2 +- modules/ANSODEngine/ANSONNXPOSE.cpp | 2 +- modules/ANSODEngine/ANSONNXRTMPOSE.h | 7 + modules/ANSODEngine/ANSONNXSAM3.cpp | 2 +- modules/ANSODEngine/ANSONNXSAM3.h | 12 +- modules/ANSODEngine/ANSONNXSEG.cpp | 2 +- modules/ANSODEngine/ANSONNXYOLO.cpp | 10 +- modules/ANSODEngine/ANSONNXYOLO.h | 15 +- modules/ANSODEngine/ANSYOLO12OD.cpp | 2 +- modules/ANSODEngine/ANSYOLOOD.cpp | 2 +- modules/ANSODEngine/NV12PreprocessHelper.cpp | 7 + modules/ANSODEngine/SCRFDFaceDetector.cpp | 20 ++ modules/ANSUtilities/ANSUtilities.cpp | 181 ++++++++++- modules/ANSUtilities/ANSUtilities.h | 44 ++- modules/ANSUtilities/dllmain.cpp | 39 +++ tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp | 87 +++++- .../ANSODEngine-UnitTest.cpp | 17 +- .../ANSUtilities-UnitTest.cpp | 128 +++++++- tools/Check-SharedCoreVersions.ps1 | 290 ++++++++++++++++++ tools/_build_frtest.bat | 5 + tools/_run_frtest.bat | 5 + tools/build_openvino_ep.bat | 77 +++++ tools/bump_ort_sdk.ps1 | 131 ++++++++ tools/fetch_ort_headers.ps1 | 115 +++++++ tools/rebuild_anscore.bat | 6 + tools/verify_ep.ps1 | 243 +++++++++++++++ 44 files changed, 1797 insertions(+), 66 deletions(-) create mode 100644 include/GpuMemoryGuard.h create mode 100644 tools/Check-SharedCoreVersions.ps1 create mode 100644 tools/_build_frtest.bat create mode 100644 tools/_run_frtest.bat create mode 100644 tools/build_openvino_ep.bat create mode 100644 tools/bump_ort_sdk.ps1 create mode 100644 tools/fetch_ort_headers.ps1 create mode 100644 tools/rebuild_anscore.bat create mode 100644 tools/verify_ep.ps1 diff --git a/.claude/settings.local.json b/.claude/settings.local.json index f9f51c1..0d3171c 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -232,7 +232,98 @@ "Bash(cmd /c \"call \\\\\"C:\\\\\\\\Program Files\\\\\\\\Microsoft Visual Studio\\\\\\\\2022\\\\\\\\Community\\\\\\\\VC\\\\\\\\Auxiliary\\\\\\\\Build\\\\\\\\vcvars64.bat\\\\\" && cmake --build C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\cmake-build-release --target ANSLicensingSystem\")", "PowerShell($vcvars = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\"; cmd /c \"`\"$vcvars`\" && cmake --build C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release --target ANSLicensingSystem 2>&1\" 2>&1 | Select-Object -Last 120)", "Bash(grep -rn \"^using namespace std\" \"C:/ANSLibs/anslicensing/\" 2>/dev/null | head -5; grep -rn \"^using namespace std\" \"C:/Projects/CLionProjects/ANSCORE/core/anslicensing/\" 2>/dev/null | head -5)", - "PowerShell(Start-Process powershell -Verb RunAs -ArgumentList '-NoProfile','-Command',\"try { Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace root\\\\wmi | Select-Object InstanceName,CurrentTemperature | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt } catch { '$_' | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt }\" -Wait; Get-Content C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt)" + "PowerShell(Start-Process powershell -Verb RunAs -ArgumentList '-NoProfile','-Command',\"try { Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace root\\\\wmi | Select-Object InstanceName,CurrentTemperature | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt } catch { '$_' | Out-File C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt }\" -Wait; Get-Content C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release\\\\_smoke\\\\thermal.txt)", + "Bash(ls \"C:/Projects/AIVISION/RTM_POSE\")", + "Bash(ls C:/Projects/AIVISION/ANSLittering 2>&1 | head -50)", + "WebFetch(domain:www.example-code.com)", + "Bash(find / -name \"CkHttp.h\" 2>/dev/null | head; echo \"---\"; find / -name \"CkRest.h\" 2>/dev/null | head)", + "Bash(ls cmake-build-* build out)", + "Bash(ls CMakePresets.json)", + "Bash(cmake --build . --target ANSUtilities)", + "Bash(cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" && cmake --build . --target ANSUtilities')", + "Bash(ls \"C:/Projects/CLionProjects/AIBOX\" 2>&1 | head -30)", + "mcp__desktop-commander__list_directory", + "mcp__desktop-commander__read_file", + "PowerShell(& \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\build_ansutil.bat\" 2>&1)", + "Bash(ls -la /c/Projects/ANSVIS 2>/dev/null || echo \"ANSVIS directory does not exist\")", + "PowerShell($cmake = \"C:\\\\Users\\\\nghia\\\\AppData\\\\Local\\\\Programs\\\\CLion 2026.1\\\\bin\\\\cmake\\\\win\\\\x64\\\\bin\\\\cmake.exe\"; & $cmake --build \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release-clangcl\" --target ANSUtilities -j 8 2>&1 | Select-Object -Last 40)", + "Bash(cmake --build C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\cmake-build-release-clangcl --target ANSODEngine -j 8)", + "Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release-clangcl\" --target ANSODEngine -j 8)", + "Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release-clangcl\" --target all -j 8)", + "Bash(cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8)", + "Bash(cmd.exe /c 'call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul && cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8')", + "PowerShell($ErrorActionPreference = 'Continue'; cmd /c '\"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvarsall.bat\" x64 >nul 2>&1 && set' | Where-Object { $_ -match '^\\(PATH|INCLUDE|LIB|LIBPATH|VC|WindowsSdk|UCRT|VSCMD_\\)' } | ForEach-Object { $name, $value = $_ -split '=', 2; [Environment]::SetEnvironmentVariable\\($name, $value\\) }; cmake --build \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" --target ANSODEngine -j 8 2>&1 | Select-Object -Last 30)", + "Bash(sleep 20 && cat \"C:/Users/nghia/AppData/Local/Temp/clear-elevated.txt\" 2>&1 | grep -E \"\\(clear-flag|before clear|with clear|after clear|RESULT|EvtClearLog\\)\" | head -20)", + "Bash(ls \"C:\\\\Projects\\\\SharedCore\")", + "Bash(ls \"C:\\\\Projects\\\\SharedCore\\\\ep\")", + "PowerShell(Get-ChildItem \"C:\\\\Projects\\\\SharedCore\\\\ep\\\\directml\" | Select-Object Name, Length, @{n='Ver';e={$_.VersionInfo.FileVersion}})", + "PowerShell(& \"C:\\\\Projects\\\\CLionProjects\\\\ANSCORE\\\\tools\\\\Check-SharedCoreVersions.ps1\" -Root \"C:\\\\Projects\\\\SharedCore\" -CompareWith \"C:\\\\ProgramData\\\\ANSCENTER\\\\Shared\")", + "PowerShell(Write-Output \"=== SSH dir contents ===\"; if \\(Test-Path \"$env:USERPROFILE\\\\.ssh\"\\) { Get-ChildItem \"$env:USERPROFILE\\\\.ssh\" -Force | Select-Object Name, Length } else { Write-Output \"No .ssh directory\" }; Write-Output \"=== ssh-agent service ===\"; Get-Service ssh-agent | Select-Object Name, Status, StartType; Write-Output \"=== git remote ===\"; git -C C:\\\\Projects\\\\CLionProjects\\\\ANSCORE remote -v)", + "Read(//c/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/**)", + "Read(//c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/bin/**)", + "Bash(tasklist)", + "Bash(echo \"=== Removing stale CMake caches \\(safe: regenerated by CLion\\) ===\" *)", + "Read(//c/ANSLibs/cudnn/**)", + "Read(//c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/include/**)", + "Read(//c/Program Files \\(x86\\)/Intel/oneAPI/**)", + "Read(//c/Program Files \\(x86\\)/**)", + "Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /SYMBOLS C:/ANSLibs/faiss/lib/faiss.lib)", + "Bash(winget --version)", + "Bash(winget search *)", + "Bash(winget install *)", + "Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property installationVersion)", + "Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property displayName)", + "Bash('C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe' -latest -property catalog_productDisplayVersion)", + "Bash(winget list *)", + "Bash(\"C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe\" -latest -property channelId)", + "Bash(./vs_installer.exe update *)", + "Bash(echo \"launch exit: $?\")", + "Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /ARCHIVEMEMBERS C:/ANSLibs/ODHUBYOLODarknet/lib/odhublib.lib)", + "Bash('C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.16.27023/bin/HostX64/x64/dumpbin.exe' /HEADERS C:/ANSLibs/ODHUBYOLODarknet/lib/odhublib.lib)", + "Read(//c/ANSLibs/TensorRT/lib/**)", + "Read(//c/ANSLibs/TensorRT/include/**)", + "Bash(ls -la --block-size=M nvinfer*.dll nvonnx*.dll)", + "Bash(stat -c '%n %s bytes' nvinfer*.dll nvonnx*.dll)", + "Bash(cd \"C:/Projects/SharedCore\" *)", + "Bash(cd \"C:/Projects/CLionProjects/ANSCORE\" *)", + "Read(//c/ANSLibs/**)", + "Read(//c/Windows/System32/**)", + "Bash(grep -niE \"ep/|ep\\\\\\\\\\\\\\\\|libraryDir|AddDllDirectory|SetDllDirectory|LoadLibrary|directml|openvino|cuda|cpu|subdir|\\\\\\\\\\\\\"ep\\\\\\\\\\\\\"|selectEP|chooseEP|provider\" EPLoader.cpp)", + "Bash(grep -viE \"^\\\\s*//|comment\")", + "Bash(echo \"=== Search for a 1.24 ONNX Runtime SDK \\(headers/lib\\) anywhere on disk ===\" *)", + "WebSearch", + "WebFetch(domain:github.com)", + "WebFetch(domain:raw.githubusercontent.com)", + "WebFetch(domain:onnxruntime.ai)", + "Bash([ -f \"$CM/OpenVINOTargets-debug.cmake\" ])", + "Bash(mv C:/ANSLibs/OpenVINO/runtime/cmake/OpenVINOTargets-debug.cmake C:/ANSLibs/OpenVINO/runtime/cmake/OpenVINOTargets-debug.cmake.disabled)", + "Bash(xargs -n1 basename)", + "PowerShell(cmd /c 'set \"PATH=C:\\\\ANSLibs\\\\Python311;C:\\\\ANSLibs\\\\Python311\\\\Scripts;%PATH%\" && call \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\VC\\\\Auxiliary\\\\Build\\\\vcvars64.bat\" >nul && call \"C:\\\\ANSLibs\\\\OpenVINO\\\\setupvars.bat\" >nul && cd /d C:\\\\Projects\\\\onnxruntime-ov && build.bat --update --config Release --use_openvino GPU --build_shared_lib --skip_tests --cmake_generator \"Visual Studio 17 2022\" 2>&1')", + "Bash(ls \"C:/Program *)", + "Bash(\"C:/Program Files/CMake/bin/cmake.exe\" --version)", + "Read(//c/Program Files/JetBrains/CLion 2026.1.3/bin/ninja/win/x64/**)", + "Bash(\"C:/Program Files/JetBrains/CLion 2026.1.3/bin/ninja/win/x64/ninja.exe\" -C \"C:/Projects/CLionProjects/ANSCORE/cmake-build-release\" -n)", + "Bash(sed -n '462,466p' core/ANSLibsLoader/EPLoader.cpp)", + "Bash(sed -n '576,581p' engines/ONNXEngine/ONNXEngine.cpp)", + "Bash(sed -n '63,66p' modules/ANSODEngine/ANSONNXCL.cpp)", + "Bash(awk 'NR>=1240 && NR<=1322 && /::|void |int |bool |Initialize|Detect|Run|LoadModel|Inference/{last=NR\": \"$0} END{}' modules/ANSODEngine/ANSFaceDetectorEngine.cpp)", + "Bash(grep -viE \"//|comment\")", + "Bash(sed -n '1,40p' tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp)", + "Bash(awk '/int main[ ]*\\\\\\(/{f=1} f{print NR\": \"$0} f&&/^}/{exit}' tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp)", + "Bash(cmd /c \"C:\\\\\\\\Projects\\\\\\\\CLionProjects\\\\\\\\ANSCORE\\\\\\\\tools\\\\\\\\_run_frtest.bat\")", + "Bash(mv __TRACKED_VAR__/ANSFR.dll.working __TRACKED_VAR__/ANSFR.dll)", + "Bash(md5sum __TRACKED_VAR__/ANSFR.dll)", + "Bash(BIN=\"C:/Projects/CLionProjects/ANSCORE/cmake-build-release/bin\" *)", + "Bash(echo \"=== first 30 lines \\(structure\\) ===\")", + "Read(//c/Users/Neo/Downloads/**)", + "Bash(echo \"\")", + "Bash(echo \"=== key error markers ===\")", + "Bash(sed -n '695,725p' modules/ANSODEngine/SCRFDFaceDetector.cpp)", + "Bash(sed -n '160,210p' modules/ANSODEngine/NV12PreprocessHelper.cpp)", + "Bash(sed -n '1,12p' modules/ANSODEngine/SCRFDFaceDetector.cpp)", + "Bash(sed -n '1,12p' modules/ANSODEngine/NV12PreprocessHelper.cpp)", + "Bash(sed -n '188,205p' modules/ANSODEngine/NV12PreprocessHelper.cpp)", + "Bash('C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v13.0/bin/cuobjdump.exe' C:/Projects/ANSTrainingTools/ODHUB/darknet/build/darknet/x64/odhublib.dll)" ] } } diff --git a/CMakeLists.txt b/CMakeLists.txt index 9736bd0..57c4207 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,14 @@ if(CMAKE_CUDA_COMPILER) set(ANSCORE_HAS_CUDA ON) set(CMAKE_CUDA_STANDARD 20) set(CMAKE_CUDA_STANDARD_REQUIRED ON) - set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 88 89 90 100) + # SASS (-real) for each shipping arch from Turing (sm_75) through Blackwell + # consumer (sm_120), plus compute_75 PTX (-virtual) so any newer/unlisted GPU + # JIT-compiles at first run (forward compatibility). NOTE: this only governs + # this project's own .cu files; prebuilt deps (odhublib, TensorRT, ORT, cuBLAS) + # carry their own arch coverage independently. + set(CMAKE_CUDA_ARCHITECTURES + 75-real 80-real 86-real 87-real 88-real 89-real 90-real 100-real 120-real + 75-virtual) if(MSVC) set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --use-fast-math -Xcompiler=/utf-8") else() @@ -36,6 +43,12 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) +# ANSCORE uses no C++20 modules. CMake auto-enables module dependency scanning +# for C++20 + Ninja, which adds a fragile per-TU scan/dyndep (.ddi/.modmap) step +# that can fail (empty .ddi -> "cannot open ...modmap" / dyndep parse errors), +# especially after an interrupted build. Disable it: faster builds, no dyndep. +set(CMAKE_CXX_SCAN_FOR_MODULES OFF) + # Output all binaries to a single directory set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index acd7c67..3eb619f 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -78,6 +78,30 @@ else() endif() target_compile_definitions(onnxruntime INTERFACE ORT_API_MANUAL_INIT) +# ── cuDNN ─────────────────────────────────────────────────────── +# TensorRT API headers (engines/TensorRTAPI) include , so +# cuDNN's include dir must be on the path. cuDNN ships separately from the +# CUDA toolkit, so wire it explicitly rather than relying on it being copied +# into the CUDA install. +if(NOT APPLE) + if(WIN32) + set(CUDNN_INCLUDE_DIR "${ANSLIBS_DIR}/cudnn/include") + set(CUDNN_LIB_DIR "${ANSLIBS_DIR}/cudnn/lib/x64") + else() + set(CUDNN_INCLUDE_DIR "${ANSLIBS_DIR}/cudnn/include") + set(CUDNN_LIB_DIR "${ANSLIBS_DIR}/cudnn/lib") + endif() + add_library(cudnn INTERFACE) + target_include_directories(cudnn INTERFACE ${CUDNN_INCLUDE_DIR}) + target_link_directories(cudnn INTERFACE ${CUDNN_LIB_DIR}) + if(WIN32) + target_link_libraries(cudnn INTERFACE cudnn.lib) + else() + target_link_libraries(cudnn INTERFACE cudnn) + endif() + message(STATUS "cuDNN: using ANSLibs at ${CUDNN_INCLUDE_DIR}") +endif() + # ── TensorRT (Windows + Linux only) ──────────────────────────── if(NOT APPLE) set(TENSORRT_INCLUDE_DIR "${ANSLIBS_DIR}/TensorRT/include") @@ -86,6 +110,9 @@ if(NOT APPLE) add_library(tensorrt INTERFACE) target_include_directories(tensorrt INTERFACE ${TENSORRT_INCLUDE_DIR}) target_link_directories(tensorrt INTERFACE ${TENSORRT_LIB_DIR}) + # cuDNN is a transitive dependency of the TensorRT API headers/engine, + # so anything linking `tensorrt` also gets cuDNN's headers and import lib. + target_link_libraries(tensorrt INTERFACE cudnn) if(WIN32) target_link_libraries(tensorrt INTERFACE nvinfer_plugin_10.lib) else() @@ -211,9 +238,34 @@ endif() # ── Intel MKL ─────────────────────────────────────────────────── add_library(mkl INTERFACE) if(WIN32) - set(MKL_LIB_DIR "C:/Program Files (x86)/Intel/oneAPI/mkl/2025.0/lib") - target_link_directories(mkl INTERFACE "${MKL_LIB_DIR}") - target_link_libraries(mkl INTERFACE mkl_rt.lib) + # oneAPI installs under .../oneAPI/mkl// where varies + # by install (e.g. 2025.0, 2025.3, or a "latest" junction) and the import + # lib lives in either lib/ (newer flat layout) or lib/intel64/ (older). + # Auto-detect rather than hardcoding so it survives MKL version bumps. + set(MKL_LIB_DIR "") + file(GLOB _mkl_versions "C:/Program Files (x86)/Intel/oneAPI/mkl/*") + list(SORT _mkl_versions) + list(REVERSE _mkl_versions) # prefer the newest version dir + foreach(_mkl_root IN LISTS _mkl_versions) + foreach(_sub "lib" "lib/intel64") + if(EXISTS "${_mkl_root}/${_sub}/mkl_rt.lib") + set(MKL_LIB_DIR "${_mkl_root}/${_sub}") + break() + endif() + endforeach() + if(MKL_LIB_DIR) + break() + endif() + endforeach() + if(MKL_LIB_DIR) + target_link_directories(mkl INTERFACE "${MKL_LIB_DIR}") + target_link_libraries(mkl INTERFACE mkl_rt.lib) + message(STATUS "Intel MKL: using ${MKL_LIB_DIR}") + else() + message(WARNING "Intel MKL not found under C:/Program Files (x86)/Intel/oneAPI/mkl. " + "ANSFR (and ANSLIB/ANSFR-UnitTest that link it) will fail to link. " + "Install Intel oneMKL or adjust MKL_LIB_DIR in cmake/Dependencies.cmake.") + endif() else() # Try system MKL on Linux find_library(MKL_RT_LIB mkl_rt PATHS /opt/intel/oneapi/mkl/latest/lib/intel64) diff --git a/core/ANSLibsLoader/EPLoader.cpp b/core/ANSLibsLoader/EPLoader.cpp index 35274f6..c789530 100644 --- a/core/ANSLibsLoader/EPLoader.cpp +++ b/core/ANSLibsLoader/EPLoader.cpp @@ -461,7 +461,7 @@ namespace ANSCENTER { ") returned null — the DLL may be corrupt."); s_ort_api = api; - Ort::Global::api_ = api; + Ort::InitApi(api); std::cout << "[EPLoader] ORT loaded successfully." << std::endl; std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl; diff --git a/core/ANSLicensingSystem/ANSLicense.cpp b/core/ANSLicensingSystem/ANSLicense.cpp index e4f235f..a335886 100644 --- a/core/ANSLicensingSystem/ANSLicense.cpp +++ b/core/ANSLicensingSystem/ANSLicense.cpp @@ -1427,6 +1427,27 @@ namespace ANSCENTER return str.c_str(); } + // Static, handle-free hardware id. Mirrors ANSLSHelper::GetCurrentHardwareId but has no + // instance/_logger, so diagnostics go to DebugView via ANS_DBG. Returns "" on failure. + std::string ANSLicenseHelper::GetHardwareId() { + try { + const wchar_t* hwid = KeyHelper::GetCurrentHardwareId(); + std::wstring result_t = std::wstring(hwid); + std::string result = WString2String(result_t); + ANS_DBG("ANSLicenseHelper", "GetHardwareId -> '%s'", result.c_str()); + return result; + } + catch (ANSCENTER::Licensing::Exception* ex) { + ANS_DBG("ANSLicenseHelper", "GetHardwareId failed: %s", ex->GetExceptionMessage()); + ex->Destroy(); + return ""; + } + catch (...) { + ANS_DBG("ANSLicenseHelper", "GetHardwareId failed: unknown exception"); + return ""; + } + } + bool ANSLicenseHelper::isPathSafe(const std::string& pathStr) { // Basic safety checks - customize as needed if (pathStr.empty()) return false; diff --git a/core/ANSLicensingSystem/ANSLicense.h b/core/ANSLicensingSystem/ANSLicense.h index b18bc8f..2d48b5f 100644 --- a/core/ANSLicensingSystem/ANSLicense.h +++ b/core/ANSLicensingSystem/ANSLicense.h @@ -523,6 +523,10 @@ namespace ANSCENTER [[nodiscard]] static bool isPathSafe(const std::string& pathStr); public: [[nodiscard]] static bool LicenseVerification(std::string licenseKey, int productId, std::string registrationName); + // Returns this machine's hardware id (same value as ANSLSHelper::GetCurrentHardwareId) + // but as a static call requiring no handle/instance — convenient for LabVIEW and other + // callers that just need the HWID. Returns "" on failure. + [[nodiscard]] static std::string GetHardwareId(); [[nodiscard]] static EngineType CheckHardwareInformation(); [[nodiscard]] static SystemHealthInfo GetSystemHealthInformation(); @@ -564,6 +568,13 @@ extern "C" __declspec(dllexport) void SetLicenseServiceURL(ANSCENTER::ANSLSHelpe extern "C" __declspec(dllexport) int GetLicenseData(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, LStrHandle licenseData); extern "C" __declspec(dllexport) int LoadLicenseTemplate(ANSCENTER::ANSLSHelper * *Handle, const char* templateFilePath); extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper * *Handle, LStrHandle currentHwID); +// Handle-free static hardware id, two flavours: +// - GetHardwareId: LabVIEW native string handle (LStrHandle) output. Returns 1 on success, 0 on failure. +// - GetHardwareIdBuffer: caller-provided C buffer; works from plain C/C++ too (no LabVIEW manager). +// Writes a null-terminated string into 'buffer' (capacity 'bufferSize') and returns the number of +// characters written (excluding the null terminator), or 0 on failure / insufficient buffer. +extern "C" __declspec(dllexport) int GetHardwareId(LStrHandle currentHwID); +extern "C" __declspec(dllexport) int GetHardwareIdBuffer(char* buffer, int bufferSize); extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey); extern "C" __declspec(dllexport) int GenerateKeyWithExpiredDate(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int expiredYear, int expiredMonth, int expiredDate, LStrHandle generatedKey); extern "C" __declspec(dllexport) int CheckLicenseKey(ANSCENTER::ANSLSHelper * *Handle, const char* licenseKey, const char* registrationName, int productId);// , LStrHandle strEnableFeature); // Check if the license key is valid diff --git a/core/ANSLicensingSystem/dllmain.cpp b/core/ANSLicensingSystem/dllmain.cpp index fd92860..32a00e8 100644 --- a/core/ANSLicensingSystem/dllmain.cpp +++ b/core/ANSLicensingSystem/dllmain.cpp @@ -109,6 +109,43 @@ extern "C" __declspec(dllexport) int GetCurrentHardwareId(ANSCENTER::ANSLSHelper return 0; } } +extern "C" __declspec(dllexport) int GetHardwareId(LStrHandle currentHwID) { + try { + std::string st = ANSCENTER::ANSLicenseHelper::GetHardwareId(); + int size = (int)st.length(); + if (size > 0) { + MgErr error; + error = DSSetHandleSize(currentHwID, sizeof(int32) + size * sizeof(uChar)); + if (error == noErr) + { + (*currentHwID)->cnt = size; + memcpy((*currentHwID)->str, st.c_str(), size); + return 1; + } + else return 0; + } + else return 0; + } + catch (...) { + return 0; + } +} +extern "C" __declspec(dllexport) int GetHardwareIdBuffer(char* buffer, int bufferSize) { + try { + if (!buffer || bufferSize <= 0) return 0; + std::string st = ANSCENTER::ANSLicenseHelper::GetHardwareId(); + int size = (int)st.length(); + if (size <= 0) return 0; + // Need room for the string plus the null terminator. + if (size + 1 > bufferSize) return 0; + memcpy(buffer, st.c_str(), size); + buffer[size] = '\0'; + return size; + } + catch (...) { + return 0; + } +} extern "C" __declspec(dllexport) int GenerateKey(ANSCENTER::ANSLSHelper * *Handle, int productId, const char* registrationName, int productFeature, int trialLicense, LStrHandle generatedKey) { try { if (!Handle || !(*Handle) || !registrationName) return 0; diff --git a/engines/ONNXEngine/EPLoader.cpp b/engines/ONNXEngine/EPLoader.cpp index 7e12d51..174b339 100644 --- a/engines/ONNXEngine/EPLoader.cpp +++ b/engines/ONNXEngine/EPLoader.cpp @@ -540,7 +540,7 @@ namespace ANSCENTER { ") returned null — the DLL may be corrupt."); s_ort_api = api; - Ort::Global::api_ = api; + Ort::InitApi(api); std::cout << "[EPLoader] ORT loaded successfully." << std::endl; std::cout << "[EPLoader] ORT DLL version : " << base->GetVersionString() << std::endl; diff --git a/engines/ONNXEngine/ONNXEngine.cpp b/engines/ONNXEngine/ONNXEngine.cpp index 6cc3b52..f87eede 100644 --- a/engines/ONNXEngine/ONNXEngine.cpp +++ b/engines/ONNXEngine/ONNXEngine.cpp @@ -141,7 +141,7 @@ namespace ANSCENTER { { try { // Make sure the Ort API pointer is initialised in THIS DLL. - if (Ort::Global::api_ == nullptr) { + if (Ort::detail::Global::Api() == nullptr) { Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); } @@ -574,10 +574,10 @@ namespace ANSCENTER { const auto& epInfo = EPLoader::Current(); ANS_DBG("OrtHandler", "initialize_handler: EPLoader type=%d dir=%s", static_cast(epInfo.type), epInfo.libraryDir.c_str()); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); - std::cout << "[ORT] api_ = " << (void*)Ort::Global::api_ << std::endl; + std::cout << "[ORT] api_ = " << (void*)Ort::detail::Global::Api() << std::endl; EngineType engine = (static_cast(m_engineType) == -1) ? epInfo.type : m_engineType; @@ -1900,4 +1900,12 @@ namespace ANSCENTER { return std::fabs(width() * height()); } + // Explicit instantiations of every BoundingBoxType member must live + // AFTER the template definitions above so the compiler actually emits + // symbols (the directives in ONNXEngine.h sit before the definitions + // and ClangCL, unlike MSVC, refuses to emit symbols from there). + template struct ANSCENTER::types::BoundingBoxType; + template struct ANSCENTER::types::BoundingBoxType; + template struct ANSCENTER::types::BoundingBoxType; + } // namespace ANSCENTER \ No newline at end of file diff --git a/engines/ONNXEngine/ONNXEngine.h b/engines/ONNXEngine/ONNXEngine.h index 1e2d19e..250abe0 100644 --- a/engines/ONNXEngine/ONNXEngine.h +++ b/engines/ONNXEngine/ONNXEngine.h @@ -73,9 +73,10 @@ namespace ANSCENTER { } }; - template class BoundingBoxType; - template class BoundingBoxType; - template class BoundingBoxType; + // Explicit instantiations now live at the end of ONNXEngine.cpp + // (after the template member definitions) so the compiler actually + // emits symbols. ClangCL refuses to emit them from a directive that + // precedes the definitions, whereas MSVC silently tolerated it. typedef BoundingBoxType Boxi; typedef BoundingBoxType Boxf; diff --git a/engines/ONNXEngine/ONNXSAM3.cpp b/engines/ONNXEngine/ONNXSAM3.cpp index f784476..b7edf7b 100644 --- a/engines/ONNXEngine/ONNXSAM3.cpp +++ b/engines/ONNXEngine/ONNXSAM3.cpp @@ -331,7 +331,7 @@ namespace ANSCENTER { // Initialize ORT API const auto& epInfo = EPLoader::Current(); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); m_env = new Ort::Env(ORT_LOGGING_LEVEL_ERROR, "ONNXSAM3"); diff --git a/engines/TensorRTAPI/include/engine/EngineBuildLoadNetwork.inl b/engines/TensorRTAPI/include/engine/EngineBuildLoadNetwork.inl index 09893ca..33fafde 100644 --- a/engines/TensorRTAPI/include/engine/EngineBuildLoadNetwork.inl +++ b/engines/TensorRTAPI/include/engine/EngineBuildLoadNetwork.inl @@ -4,6 +4,7 @@ #include #include "Utility.h" #include "TRTCompat.h" +#include "GpuMemoryGuard.h" // ============================================================================ // Crash-safe wrappers for TensorRT operations that can crash the process. @@ -805,13 +806,22 @@ bool Engine::loadNetwork(std::string trtModelPath, const std::array // (cudaMalloc failure during inference) when too many tasks are loaded. { size_t freeVRAM = 0, totalVRAM = 0; - cudaError_t memErr = cudaMemGetInfo(&freeVRAM, &totalVRAM); - constexpr size_t kMinFreeBytes = 256ULL * 1024 * 1024; // 256 MiB minimum - if (memErr != cudaSuccess) { - // cudaMemGetInfo failed — CUDA context may not be initialized on this thread. - // Don't reject: let TRT try to deserialize (it may succeed). - } else if (freeVRAM < kMinFreeBytes) { - m_lastLoadFailedVRAM = true; // signal to buildLoadNetwork: engine file is NOT corrupt + bool haveMem = ANSCENTER::GpuMemoryGuard::Query(freeVRAM, totalVRAM); + // (1) Admission control: refuse to load this engine on the GPU if it + // does not fit while keeping the GpuMemoryGuard safety margin free. + // Prevents VRAM over-subscription across concurrent engines (FR/OD/LPR/ + // liveness) that would otherwise OOM and permanently poison the CUDA + // context. The engine file is valid — m_lastLoadFailedVRAM tells the + // caller to preserve it (no rebuild) and just skip GPU load for now. + if (haveMem && !ANSCENTER::GpuMemoryGuard::CanLoad(buffer.size())) { + m_lastLoadFailedVRAM = true; + std::cerr << "[GpuMemGuard] REFUSING engine load on GPU " + << m_options.deviceIndex << ": need ~" + << (buffer.size() / (1024 * 1024)) << " MiB + " + << (ANSCENTER::GpuMemoryGuard::Margin() / (1024 * 1024)) + << " MiB margin, only " << (freeVRAM / (1024 * 1024)) + << " MiB free. Reduce concurrent engines/cameras or batch size." + << std::endl; return false; } if (m_verbose) { diff --git a/include/GpuMemoryGuard.h b/include/GpuMemoryGuard.h new file mode 100644 index 0000000..fd2cbae --- /dev/null +++ b/include/GpuMemoryGuard.h @@ -0,0 +1,96 @@ +#pragma once +// ============================================================================ +// GpuMemoryGuard — process-wide GPU VRAM safeguard. +// +// Purpose: prevent the out-of-memory cascade that happens when many TensorRT / +// CUDA engines (FR-SCRFD, OD-YOLO, LPR, liveness, ORT-CUDA, OpenCV-CUDA) share +// one GPU and collectively exhaust VRAM. The first cudaMalloc/stream OOM +// poisons the CUDA context ("permanently corrupted after N failures"), after +// which every GPU op fails and the service appears dead until restart. +// +// Three coordinated layers (all process-wide, thread-safe, lock-free): +// (1) CanLoad(need) — admission control: refuse to load an engine on GPU +// if it won't fit while keeping a safety margin free. +// (2) UnderPressure() — pressure guard: skip a frame's GPU work when free +// VRAM is below the margin (or breaker is open). +// (3) NoteOom()/Breaker— circuit breaker: after a caught OOM, open a short +// cooldown so callers back off instead of hammering +// the fragile context (which is what corrupts it). +// +// Margin default = 768 MiB; override at runtime with env ANSCENTER_GPU_MARGIN_MB. +// All queries operate on the CURRENT CUDA device (callers cudaSetDevice first). +// ============================================================================ +#include +#include +#include +#include +#include +#include + +namespace ANSCENTER { + +class GpuMemoryGuard { +public: + // ---- configuration ---------------------------------------------------- + static size_t Margin() { return s_marginBytes.load(std::memory_order_relaxed); } + static void SetMargin(size_t b) { s_marginBytes.store(b, std::memory_order_relaxed); } + + // ---- (1) admission control at load ------------------------------------ + // True if 'needBytes' can be committed on the current device while keeping + // the safety margin free. If VRAM can't be queried, returns true (never + // block on a failed query — let the normal path handle it). + static bool CanLoad(size_t needBytes) { + size_t freeB = 0, totalB = 0; + if (!Query(freeB, totalB)) return true; + return freeB >= needBytes + Margin(); + } + + // ---- (2) pressure guard at inference ---------------------------------- + // True if this frame should SKIP GPU work: free VRAM below margin, or the + // circuit breaker is currently open. + static bool UnderPressure() { + if (BreakerOpen()) return true; + size_t freeB = 0, totalB = 0; + if (!Query(freeB, totalB)) return false; + return freeB < Margin(); + } + + // ---- (3) OOM-safe alloc + circuit breaker ----------------------------- + static bool IsOom(cudaError_t e) { return e == cudaErrorMemoryAllocation; } + + // Open the breaker for a short cooldown after a (recoverable) OOM so all + // callers back off together instead of repeatedly faulting the context. + static void NoteOom() { + s_breakerUntilMs.store(NowMs() + kCooldownMs, std::memory_order_relaxed); + } + static bool BreakerOpen() { + return NowMs() < s_breakerUntilMs.load(std::memory_order_relaxed); + } + + // Lightweight current-device VRAM query. Clears the non-fatal sticky error + // that a failed query can leave on the context. + static bool Query(size_t& freeB, size_t& totalB) { + cudaError_t e = cudaMemGetInfo(&freeB, &totalB); + cudaGetLastError(); + return e == cudaSuccess; + } + +private: + static int64_t NowMs() { + return std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count(); + } + static size_t InitMargin() { + if (const char* e = std::getenv("ANSCENTER_GPU_MARGIN_MB")) { + long mb = std::atol(e); + if (mb > 0) return static_cast(mb) * 1024ULL * 1024ULL; + } + return 768ULL * 1024 * 1024; // 768 MiB + } + + static constexpr int64_t kCooldownMs = 1500; // breaker cooldown after OOM + inline static std::atomic s_marginBytes{ InitMargin() }; + inline static std::atomic s_breakerUntilMs{ 0 }; +}; + +} // namespace ANSCENTER diff --git a/modules/ANSOCR/ANSONNXOCR/PaddleOCRV5Engine.cpp b/modules/ANSOCR/ANSONNXOCR/PaddleOCRV5Engine.cpp index 04c082b..bfe0b37 100644 --- a/modules/ANSOCR/ANSONNXOCR/PaddleOCRV5Engine.cpp +++ b/modules/ANSOCR/ANSONNXOCR/PaddleOCRV5Engine.cpp @@ -162,7 +162,7 @@ bool PaddleOCRV5Engine::Initialize(const std::string& detModelPath, // Ensure this DLL's copy of Ort::Global::api_ is initialized. // BasicOrtHandler sets it in ONNXEngine.dll, but each DLL has its own // inline-static copy. Without this, inference calls from ANSOCR.dll crash. - if (Ort::Global::api_ == nullptr) { + if (Ort::detail::Global::Api() == nullptr) { Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); } diff --git a/modules/ANSODEngine/ANSEngineCommon.h b/modules/ANSODEngine/ANSEngineCommon.h index 3f315e1..8358658 100644 --- a/modules/ANSODEngine/ANSEngineCommon.h +++ b/modules/ANSODEngine/ANSEngineCommon.h @@ -761,6 +761,16 @@ namespace ANSCENTER float CosineSimilarity(const std::vector& a, const std::vector& b, bool normalized); }; class ANSENGINE_API ANSODBase { + public: + // Explicit non-copy/non-move: _modelLoading (std::atomic) deletes the + // implicit copy ops; spelling them out here gives ClangCL a clean + // anchor and documents that subclasses are never copied/moved (they + // are heap-allocated and passed by shared_ptr<...>). + ANSODBase(const ANSODBase&) = delete; + ANSODBase& operator=(const ANSODBase&) = delete; + ANSODBase(ANSODBase&&) = delete; + ANSODBase& operator=(ANSODBase&&) = delete; + ANSODBase() = default; protected: bool _licenseValid{ false }; bool _isInitialized{ false }; @@ -1085,6 +1095,13 @@ namespace ANSCENTER std::vector RunInferenceInTrackingMode(const cv::Mat& input, const std::string& camera_id, std::vector trackingObjectIds);// Get detected objects }; class ANSENGINE_API ANSFDBase { + public: + // Explicit non-copy/non-move (see ANSODBase for rationale). + ANSFDBase(const ANSFDBase&) = delete; + ANSFDBase& operator=(const ANSFDBase&) = delete; + ANSFDBase(ANSFDBase&&) = delete; + ANSFDBase& operator=(ANSFDBase&&) = delete; + ANSFDBase() = default; protected: bool _licenseValid{ false }; bool _isInitialized{ false }; diff --git a/modules/ANSODEngine/ANSFaceDetectorEngine.cpp b/modules/ANSODEngine/ANSFaceDetectorEngine.cpp index a110ff7..84b46e1 100644 --- a/modules/ANSODEngine/ANSFaceDetectorEngine.cpp +++ b/modules/ANSODEngine/ANSFaceDetectorEngine.cpp @@ -1316,7 +1316,7 @@ namespace ANSCENTER { try { const auto& ep = ANSCENTER::EPLoader::Current(); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(ANSCENTER::EPLoader::GetOrtApiRaw())); std::cout << "[ANSFDBase] EP ready: "<< ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; diff --git a/modules/ANSODEngine/ANSONNXCL.cpp b/modules/ANSODEngine/ANSONNXCL.cpp index 1ffb5e6..450e9bc 100644 --- a/modules/ANSODEngine/ANSONNXCL.cpp +++ b/modules/ANSODEngine/ANSONNXCL.cpp @@ -61,7 +61,7 @@ namespace ANSCENTER inputImageShape_ = targetInputShape; const auto& ep = ANSCENTER::EPLoader::Current(); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); std::cout << "[ANSONNXCL] EP ready: " << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; diff --git a/modules/ANSODEngine/ANSONNXCL.h b/modules/ANSODEngine/ANSONNXCL.h index 360ae22..f60659b 100644 --- a/modules/ANSODEngine/ANSONNXCL.h +++ b/modules/ANSODEngine/ANSONNXCL.h @@ -25,6 +25,13 @@ namespace ANSCENTER { // ──────────────────────────────────────────────────────────────────────── class ANSENGINE_API ANSONNXCL :public ANSODBase { public: + // Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale). + ANSONNXCL(const ANSONNXCL&) = delete; + ANSONNXCL& operator=(const ANSONNXCL&) = delete; + ANSONNXCL(ANSONNXCL&&) = delete; + ANSONNXCL& operator=(ANSONNXCL&&) = delete; + ANSONNXCL() = default; + virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override; virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override; virtual bool LoadModelFromFolder(std::string licenseKey, ModelConfig modelConfig, std::string modelName, std::string className, const std::string& modelFolder, std::string& labelMap)override; diff --git a/modules/ANSODEngine/ANSONNXOBB.cpp b/modules/ANSODEngine/ANSONNXOBB.cpp index f5b7877..feafafe 100644 --- a/modules/ANSODEngine/ANSONNXOBB.cpp +++ b/modules/ANSODEngine/ANSONNXOBB.cpp @@ -692,7 +692,7 @@ namespace ANSCENTER { deviceId_ = deviceId; const auto& ep = ANSCENTER::EPLoader::Current(); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); std::cout << "[ANSONNXOBB] EP ready: " << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; diff --git a/modules/ANSODEngine/ANSONNXPOSE.cpp b/modules/ANSODEngine/ANSONNXPOSE.cpp index 2a366f5..1832288 100644 --- a/modules/ANSODEngine/ANSONNXPOSE.cpp +++ b/modules/ANSODEngine/ANSONNXPOSE.cpp @@ -276,7 +276,7 @@ namespace ANSCENTER { deviceId_ = deviceId; const auto& ep = ANSCENTER::EPLoader::Current(); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); std::cout << "[ANSONNXPOSE] EP ready: " << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; diff --git a/modules/ANSODEngine/ANSONNXRTMPOSE.h b/modules/ANSODEngine/ANSONNXRTMPOSE.h index f0e14b8..68ac046 100644 --- a/modules/ANSODEngine/ANSONNXRTMPOSE.h +++ b/modules/ANSODEngine/ANSONNXRTMPOSE.h @@ -108,6 +108,13 @@ namespace ANSCENTER { // ==================================================================== class ANSENGINE_API ANSONNXRTMPOSE : public ANSODBase { public: + // Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale). + ANSONNXRTMPOSE(const ANSONNXRTMPOSE&) = delete; + ANSONNXRTMPOSE& operator=(const ANSONNXRTMPOSE&) = delete; + ANSONNXRTMPOSE(ANSONNXRTMPOSE&&) = delete; + ANSONNXRTMPOSE& operator=(ANSONNXRTMPOSE&&) = delete; + ANSONNXRTMPOSE() = default; + bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, diff --git a/modules/ANSODEngine/ANSONNXSAM3.cpp b/modules/ANSODEngine/ANSONNXSAM3.cpp index 6f5eda4..17ab7e7 100644 --- a/modules/ANSODEngine/ANSONNXSAM3.cpp +++ b/modules/ANSODEngine/ANSONNXSAM3.cpp @@ -204,7 +204,7 @@ namespace ANSCENTER } try { - m_engine = std::make_unique(modelFolder, EngineType::NVIDIA_GPU); + m_engine = std::make_unique(modelFolder, EngineType::NVIDIA_GPU); m_tokenLength = m_engine->getTokenLength(); _logger.LogDebug("ANSONNXSAM3::InitEngine", diff --git a/modules/ANSODEngine/ANSONNXSAM3.h b/modules/ANSODEngine/ANSONNXSAM3.h index 932b636..7686c80 100644 --- a/modules/ANSODEngine/ANSONNXSAM3.h +++ b/modules/ANSODEngine/ANSONNXSAM3.h @@ -16,6 +16,13 @@ namespace ANSCENTER class ANSENGINE_API ANSONNXSAM3 : public ANSODBase { public: + // Explicit non-copy/non-move (see ANSONNXYOLO for dllexport rationale). + ANSONNXSAM3(const ANSONNXSAM3&) = delete; + ANSONNXSAM3& operator=(const ANSONNXSAM3&) = delete; + ANSONNXSAM3(ANSONNXSAM3&&) = delete; + ANSONNXSAM3& operator=(ANSONNXSAM3&&) = delete; + ANSONNXSAM3() = default; + virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override; @@ -45,7 +52,10 @@ namespace ANSCENTER bool _fp16 = false; // ONNX Runtime engine - std::unique_ptr m_engine; + // Elaborated `class ONNXSAM3` disambiguates from the + // ANSCENTER::ModelType::ONNXSAM3 enumerator (ANSLicense.h) which + // shares the same unqualified name in this namespace. + std::unique_ptr m_engine; // Tokenizer (loaded from merges.txt) std::unique_ptr m_tokenizer; diff --git a/modules/ANSODEngine/ANSONNXSEG.cpp b/modules/ANSODEngine/ANSONNXSEG.cpp index a1f27dc..08a7da7 100644 --- a/modules/ANSODEngine/ANSONNXSEG.cpp +++ b/modules/ANSODEngine/ANSONNXSEG.cpp @@ -273,7 +273,7 @@ namespace ANSCENTER { deviceId_ = deviceId; const auto& ep = ANSCENTER::EPLoader::Current(); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); std::cout << "[ANSONNXSEG] EP ready: " << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; diff --git a/modules/ANSODEngine/ANSONNXYOLO.cpp b/modules/ANSODEngine/ANSONNXYOLO.cpp index 2f17bef..ad08b5e 100644 --- a/modules/ANSODEngine/ANSONNXYOLO.cpp +++ b/modules/ANSODEngine/ANSONNXYOLO.cpp @@ -129,7 +129,7 @@ namespace ANSCENTER { // future ORT shape-mismatch failure (e.g. /model.12/Concat for // non-stride-32 inputs) points at the offending HxW directly. // One line per session — does not flood DebugView. - static thread_local const ONNXYOLO* s_loggedFirst = nullptr; + static thread_local const class ONNXYOLO* s_loggedFirst = nullptr; if (s_loggedFirst != this) { s_loggedFirst = this; ANS_DBG("ONNXYOLO", @@ -1680,7 +1680,7 @@ namespace ANSCENTER { "Model file does not exist: " + _modelFilePath, __FILE__, __LINE__); return false; } - m_ortEngine = std::make_shared(_modelFilePath); + m_ortEngine = std::make_shared(_modelFilePath); return true; } catch (const std::exception& e) { @@ -1696,7 +1696,7 @@ namespace ANSCENTER { "Model file does not exist: " + _modelFilePath, __FILE__, __LINE__); return false; } - m_ortEngine = std::make_shared(_modelFilePath, engineType); + m_ortEngine = std::make_shared(_modelFilePath, engineType); return true; } catch (const std::exception& e) { @@ -2244,7 +2244,7 @@ namespace ANSCENTER { int numKps; std::vector classes; bool trackerEnabled, stabilizationEnabled; - std::shared_ptr engine; + std::shared_ptr engine; { ANS_DBG("ONNXYOLO", "DetectObjects: cam=%s acquiring mutex...", camera_id.c_str()); auto lk = TryLockWithTimeout("ANSONNXYOLO::DetectObjects"); @@ -2446,7 +2446,7 @@ namespace ANSCENTER { int numKps; std::vector classes; bool trackerEnabled, stabilizationEnabled; - std::shared_ptr engine; + std::shared_ptr engine; { auto lk = TryLockWithTimeout("ANSONNXYOLO::DetectObjectsBatch"); if (!lk.owns_lock()) return {}; diff --git a/modules/ANSODEngine/ANSONNXYOLO.h b/modules/ANSODEngine/ANSONNXYOLO.h index 22239d0..3a76392 100644 --- a/modules/ANSODEngine/ANSONNXYOLO.h +++ b/modules/ANSODEngine/ANSONNXYOLO.h @@ -205,6 +205,16 @@ namespace ANSCENTER { // ==================================================================== class ANSENGINE_API ANSONNXYOLO : public ANSODBase { public: + // Explicit non-copy/non-move. The class has atomic + shared_ptr members + // that already implicitly delete these, but __declspec(dllexport) + // makes ClangCL eagerly instantiate the implicit copy/move ctor + // bodies; an explicit user-declared =delete short-circuits that. + ANSONNXYOLO(const ANSONNXYOLO&) = delete; + ANSONNXYOLO& operator=(const ANSONNXYOLO&) = delete; + ANSONNXYOLO(ANSONNXYOLO&&) = delete; + ANSONNXYOLO& operator=(ANSONNXYOLO&&) = delete; + ANSONNXYOLO() = default; + bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, @@ -254,7 +264,10 @@ namespace ANSCENTER { // caught a DML E_FAIL and rebuilt the session — that was the // 0xc0000005 access violation observed in ANSODEngine.dll under // AMD/DirectML when multiple AI tasks ran in parallel. - std::shared_ptr m_ortEngine; + // Elaborated `class ONNXYOLO` disambiguates from the + // ANSCENTER::ModelType::ONNXYOLO enumerator (ANSLicense.h) which + // shares the same unqualified name in this namespace. + std::shared_ptr m_ortEngine; // ──────────────────────────────────────────────────────────────── // DML stay-on-GPU recovery state. diff --git a/modules/ANSODEngine/ANSYOLO12OD.cpp b/modules/ANSODEngine/ANSYOLO12OD.cpp index 8e5d5c8..adb492e 100644 --- a/modules/ANSODEngine/ANSYOLO12OD.cpp +++ b/modules/ANSODEngine/ANSYOLO12OD.cpp @@ -281,7 +281,7 @@ namespace ANSCENTER { { try { const auto& ep = ANSCENTER::EPLoader::Current(); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); std::cout << "[YOLO12OD] EP ready: " << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; diff --git a/modules/ANSODEngine/ANSYOLOOD.cpp b/modules/ANSODEngine/ANSYOLOOD.cpp index 75997de..263836a 100644 --- a/modules/ANSODEngine/ANSYOLOOD.cpp +++ b/modules/ANSODEngine/ANSYOLOOD.cpp @@ -208,7 +208,7 @@ namespace ANSCENTER { try { const auto& ep = ANSCENTER::EPLoader::Current(); - if (Ort::Global::api_ == nullptr) + if (Ort::detail::Global::Api() == nullptr) Ort::InitApi(static_cast(EPLoader::GetOrtApiRaw())); std::cout << "[YOLOOD] EP ready: " << ANSCENTER::EPLoader::EngineTypeName(ep.type) << std::endl; diff --git a/modules/ANSODEngine/NV12PreprocessHelper.cpp b/modules/ANSODEngine/NV12PreprocessHelper.cpp index dd519cc..51269da 100644 --- a/modules/ANSODEngine/NV12PreprocessHelper.cpp +++ b/modules/ANSODEngine/NV12PreprocessHelper.cpp @@ -2,6 +2,7 @@ #include "ANSGpuFrameRegistry.h" #include "ANSEngineCommon.h" #include "ANSLicense.h" // ANS_DBG macro +#include "GpuMemoryGuard.h" #include #include #include @@ -168,6 +169,12 @@ namespace ANSCENTER { } ++m_cudaFailStreak; + // (3) If this failure is a GPU OOM, open the process-wide circuit + // breaker so every engine (FR/OD/OCR/LPR) backs off via UnderPressure() + // before the streak reaches CUDA_FAIL_LIMIT and the context is lost. + if (GpuMemoryGuard::IsOom(err) || GpuMemoryGuard::IsOom(pending)) { + GpuMemoryGuard::NoteOom(); + } if (m_cudaFailStreak >= CUDA_FAIL_LIMIT && !m_cudaContextDead) { m_cudaContextDead = true; logger.LogFatal(std::string(tag) + "::NV12Helper", diff --git a/modules/ANSODEngine/SCRFDFaceDetector.cpp b/modules/ANSODEngine/SCRFDFaceDetector.cpp index 90ffc77..a56635a 100644 --- a/modules/ANSODEngine/SCRFDFaceDetector.cpp +++ b/modules/ANSODEngine/SCRFDFaceDetector.cpp @@ -2,6 +2,7 @@ #include "ANSGpuFrameRegistry.h" #include "NV12PreprocessHelper.h" // tl_currentGpuFrame() #include "Utility.h" +#include "GpuMemoryGuard.h" #include //#define FNS_DEBUG namespace ANSCENTER { @@ -638,6 +639,17 @@ namespace ANSCENTER { // Phase 2: CUDA preprocessing + inference (mutex released — pool dispatches to idle GPU slot) std::vector> inputs; bool usedNV12 = false; + + // (2) Pressure guard: if free GPU VRAM is below the safety margin (or the + // OOM circuit breaker is open), SKIP this frame's GPU work instead of + // allocating and triggering an OOM that permanently poisons the context. + if (ANSCENTER::GpuMemoryGuard::UnderPressure()) { + this->_logger.LogWarn("ANSSCRFDFD::Detect", + "GPU under memory pressure — skipping SCRFD frame to avoid OOM", + __FILE__, __LINE__); + return {}; + } + try { // Clear any sticky CUDA error from transient graph-capture failures cudaError_t priorErr = cudaGetLastError(); @@ -706,6 +718,14 @@ namespace ANSCENTER { m_nv12Helper.tickInference(); } catch (const std::exception& e) { + // (3) On a GPU OOM, open the circuit breaker so subsequent frames + // back off (UnderPressure) instead of repeatedly faulting the + // already-fragile CUDA context — that repetition is what corrupts it. + const std::string msg = e.what(); + if (msg.find("out of memory") != std::string::npos || + msg.find("Gpu API call") != std::string::npos) { + ANSCENTER::GpuMemoryGuard::NoteOom(); + } this->_logger.LogError( "ANSSCRFDFD::Detect", std::string("CUDA preprocessing failed: ") + e.what(), diff --git a/modules/ANSUtilities/ANSUtilities.cpp b/modules/ANSUtilities/ANSUtilities.cpp index 63b9b2a..c884ef7 100644 --- a/modules/ANSUtilities/ANSUtilities.cpp +++ b/modules/ANSUtilities/ANSUtilities.cpp @@ -1312,7 +1312,7 @@ namespace ANSCENTER return result; } // ==================================================================== - // GetANSLoggerErrorEvents + // GetANSLoggerErrorEvents / GetANSLoggerErrorEventsSinceLastCall // // Reads every Error-level event written by the "ANSLogger" source in // the Application channel using the modern winevt API. Events are @@ -1322,14 +1322,98 @@ namespace ANSCENTER // // DebugView: filter "[EVTLOG]". // ==================================================================== - std::string ANSUtilities::GetANSLoggerErrorEvents(bool clearAfterRead) { - // XPath: Provider name "ANSLogger" AND Level 2 (EVENTLOG_ERROR_TYPE). - // Forward direction keeps chronological insertion order so output - // matches a fresh "oldest first" scroll through Event Viewer. - constexpr const wchar_t* kQuery = - L"*[System[Provider[@Name='ANSLogger'] and Level=2]]"; - EVT_HANDLE hResults = EvtQuery(nullptr, L"Application", kQuery, + // ---- Watermark helpers (HKCU, no admin needed) ---------------------- + static constexpr const wchar_t* kWatermarkRegKey = + L"Software\\ANSCENTER\\ANSUtilities\\EventLogWatermark"; + static constexpr const wchar_t* kWatermarkValueName = L"ANSLogger"; + + static uint64_t ReadAnsLoggerWatermark() { + HKEY hKey = nullptr; + if (RegOpenKeyExW(HKEY_CURRENT_USER, kWatermarkRegKey, 0, + KEY_READ, &hKey) != ERROR_SUCCESS) { + return 0; + } + uint64_t value = 0; + DWORD size = sizeof(value), type = 0; + LONG rc = RegQueryValueExW(hKey, kWatermarkValueName, nullptr, + &type, reinterpret_cast(&value), + &size); + RegCloseKey(hKey); + return (rc == ERROR_SUCCESS && type == REG_QWORD) ? value : 0; + } + + static void WriteAnsLoggerWatermark(uint64_t value) { + HKEY hKey = nullptr; + if (RegCreateKeyExW(HKEY_CURRENT_USER, kWatermarkRegKey, 0, + nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, + nullptr, &hKey, nullptr) != ERROR_SUCCESS) { + ANS_DBG("EVTLOG", "watermark RegCreateKey failed err=%lu", + GetLastError()); + return; + } + LONG rc = RegSetValueExW(hKey, kWatermarkValueName, 0, REG_QWORD, + reinterpret_cast(&value), + sizeof(value)); + if (rc != ERROR_SUCCESS) { + ANS_DBG("EVTLOG", "watermark RegSetValue failed err=%ld", rc); + } + RegCloseKey(hKey); + } + + // Extracts NNN from a rendered event XML. + // Returns 0 if not present (shouldn't happen — every Windows event has one). + static uint64_t ExtractRecordIdFromXml(const std::wstring& xml) { + auto open = xml.find(L""); + if (open == std::wstring::npos) return 0; + open += 15; // length of "" + auto close = xml.find(L"", open); + if (close == std::wstring::npos) return 0; + std::wstring numStr = xml.substr(open, close - open); + return std::wcstoull(numStr.c_str(), nullptr, 10); + } + + // Returns the RecordID of the most recently written event in `channel`. + // Used to detect channel clears: if our stored watermark > channelMax, + // the channel was cleared and the new event counter restarted at 1, so + // we must reset our watermark or risk skipping all new events. + static uint64_t GetChannelMaxRecordId(const wchar_t* channel) { + EVT_HANDLE hQuery = EvtQuery(nullptr, channel, nullptr, + EvtQueryChannelPath | EvtQueryReverseDirection); + if (!hQuery) return 0; + + EVT_HANDLE hEvent = nullptr; + DWORD returned = 0; + uint64_t maxId = 0; + if (EvtNext(hQuery, 1, &hEvent, 1000, 0, &returned) && returned > 0) { + std::vector buf(4096); + DWORD used = 0, propCount = 0; + BOOL ok = EvtRender(nullptr, hEvent, EvtRenderEventXml, + static_cast(buf.size() * sizeof(wchar_t)), + buf.data(), &used, &propCount); + if (!ok && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + buf.resize(used / sizeof(wchar_t) + 1); + ok = EvtRender(nullptr, hEvent, EvtRenderEventXml, + static_cast(buf.size() * sizeof(wchar_t)), + buf.data(), &used, &propCount); + } + if (ok) maxId = ExtractRecordIdFromXml(std::wstring(buf.data())); + EvtClose(hEvent); + } + EvtClose(hQuery); + return maxId; + } + + // ---- Shared implementation ----------------------------------------- + // Runs `query` against the Application channel, formats events as + // "
: \n" + // and optionally clears the channel afterwards. If trackWatermark is + // true, also persists the highest EventRecordID seen to HKCU on return. + static std::string ReadAnsLoggerEventsImpl(const wchar_t* query, + bool clearAfterRead, + bool trackWatermark) + { + EVT_HANDLE hResults = EvtQuery(nullptr, L"Application", query, EvtQueryChannelPath | EvtQueryForwardDirection); if (!hResults) { ANS_DBG("EVTLOG", "EvtQuery failed err=%lu", GetLastError()); @@ -1337,6 +1421,7 @@ namespace ANSCENTER } std::string out; + uint64_t highestRecordId = 0; constexpr DWORD kBatch = 64; EVT_HANDLE events[kBatch]; DWORD returned = 0; @@ -1367,6 +1452,15 @@ namespace ANSCENTER } std::wstring xml(buf.data()); + // Track the highest RecordID we've seen this call so we can + // persist it as the new watermark on exit. Done before the + // continue-on-malformed-XML paths below so an unrenderable + // event still advances the watermark. + if (trackWatermark) { + uint64_t rid = ExtractRecordIdFromXml(xml); + if (rid > highestRecordId) highestRecordId = rid; + } + // ---- TimeCreated SystemTime='2026-05-18T11:52:07.123Z' ---- // EvtRender produces either single- or double-quoted attrs // depending on Windows version, so handle both. @@ -1462,18 +1556,89 @@ namespace ANSCENTER } EvtClose(hResults); + // Persist watermark BEFORE clearing — once EvtClearLog succeeds, + // future events restart at RecordID 1, and we'd never see them + // again if the watermark stayed at e.g. 547. + // Special case: if clear is also going to succeed, the new + // watermark is irrelevant (we'll reset on next call via the + // GetChannelMaxRecordId check), so writing it here is harmless. + if (trackWatermark && highestRecordId > 0) { + WriteAnsLoggerWatermark(highestRecordId); + ANS_DBG("EVTLOG", "watermark updated to %llu", highestRecordId); + } + if (clearAfterRead) { // Clears the entire Application channel — see header warning. + // DIAGNOSTIC: also print to stderr so callers without DebugView + // can see what's going on. Common failure codes: + // 5 ERROR_ACCESS_DENIED — process is not elevated + // 1314 ERROR_PRIVILEGE_NOT_HELD — token missing SeSecurityPriv + // 87 ERROR_INVALID_PARAMETER — channel name wrong if (!EvtClearLog(nullptr, L"Application", nullptr, 0)) { DWORD e = GetLastError(); ANS_DBG("EVTLOG", "EvtClearLog failed err=%lu (need admin)", e); + std::fprintf(stderr, + "[ANSUtilities] EvtClearLog(Application) FAILED, " + "GetLastError=%lu (5=AccessDenied, 1314=PrivNotHeld)\n", e); } else { ANS_DBG("EVTLOG", "EvtClearLog: Application channel cleared"); + std::fprintf(stderr, + "[ANSUtilities] EvtClearLog(Application) OK\n"); + // Clear succeeded → counter resets to 1, so our watermark + // is stale. Reset it now so the next call doesn't skip + // legitimate new events whose RecordID < old watermark. + if (trackWatermark) { + WriteAnsLoggerWatermark(0); + ANS_DBG("EVTLOG", "watermark reset to 0 after clear"); + } } } return out; } + // ---- Public: full read, no skip ------------------------------------ + std::string ANSUtilities::GetANSLoggerErrorEvents(bool clearAfterRead) { + // XPath: Provider name "ANSLogger" AND Level 2 (EVENTLOG_ERROR_TYPE). + // Forward direction keeps chronological insertion order so output + // matches a fresh "oldest first" scroll through Event Viewer. + return ReadAnsLoggerEventsImpl( + L"*[System[Provider[@Name='ANSLogger'] and Level=2]]", + clearAfterRead, + /*trackWatermark=*/false); + } + + // ---- Public: incremental read using HKCU watermark ----------------- + std::string ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(bool clearAfterRead) { + uint64_t watermark = ReadAnsLoggerWatermark(); + + // Channel-clear detection: if the channel's current max RecordID + // is lower than our stored watermark, the log was cleared (by us + // or by something else like Event Viewer's "Clear Log") and the + // counter restarted at 1. Reset the watermark so we don't skip + // events 1..watermark that are actually fresh. + uint64_t channelMax = GetChannelMaxRecordId(L"Application"); + if (channelMax > 0 && watermark > channelMax) { + ANS_DBG("EVTLOG", "watermark reset: stored=%llu channelMax=%llu " + "(channel was cleared externally)", + watermark, channelMax); + watermark = 0; + WriteAnsLoggerWatermark(0); + } + + // Embed the watermark in the XPath so Windows does the filtering; + // we never see events <= watermark, which is more efficient than + // post-filtering thousands of events on the client side. + wchar_t queryBuf[256]; + swprintf_s(queryBuf, _countof(queryBuf), + L"*[System[Provider[@Name='ANSLogger'] and Level=2 and " + L"(EventRecordID>%llu)]]", + watermark); + ANS_DBG("EVTLOG", "GetANSLoggerErrorEventsSinceLastCall watermark=%llu", + watermark); + return ReadAnsLoggerEventsImpl(queryBuf, clearAfterRead, + /*trackWatermark=*/true); + } + } // namespace ANSCENTER // ============================================================================ diff --git a/modules/ANSUtilities/ANSUtilities.h b/modules/ANSUtilities/ANSUtilities.h index 5dd9cdf..b7e056c 100644 --- a/modules/ANSUtilities/ANSUtilities.h +++ b/modules/ANSUtilities/ANSUtilities.h @@ -161,14 +161,40 @@ namespace ANSCENTER { // Time is converted to the user's local timezone and formatted // using LOCALE_USER_DEFAULT (matches Event Viewer's display). // - // If `clearAfterRead` is true, calls EvtClearLog on the entire - // Application channel after reading. WARNING: this wipes every - // event from every source in the Application log, not just - // ANSLogger entries — there is no per-event delete API in - // Windows. Requires Administrator privileges; without elevation - // the clear silently fails (events are still returned). + // clearAfterRead=true behaviour: + // * Calls EvtClearLog on the entire Application channel after + // reading. WARNING: this wipes every event from every source + // in the Application log, not just ANSLogger entries — there + // is no per-event delete API in Windows. + // * REQUIRES THE CALLING PROCESS TO BE ELEVATED (run as + // Administrator). Without elevation, EvtClearLog returns + // ERROR_ACCESS_DENIED (Win32 error 5) and the events remain + // in the log. The read still succeeds either way. + // * Failures are reported on stderr in the form + // "[ANSUtilities] EvtClearLog(Application) FAILED, GetLastError=N" + // and via DebugView under the "EVTLOG" tag. The C++ return + // value does NOT signal clear success — capture stderr or + // check Event Viewer afterwards to confirm. [[nodiscard]] static std::string GetANSLoggerErrorEvents(bool clearAfterRead = false); + // Same as GetANSLoggerErrorEvents but only returns events that + // have NOT been returned by a previous call from this user + // account. The highest EventRecordID seen is persisted to + // HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger + // (REG_QWORD) and used as a Windows XPath filter + // (EventRecordID > N) on the next call. No admin needed for the + // skip mechanism — only for the optional clearAfterRead. + // + // Channel-clear handling: if Windows resets the Application + // channel's RecordID counter (typical after EvtClearLog), this + // function detects that the channel's current max RecordID is + // less than the stored watermark and resets the watermark to 0 + // so no newly-written events are accidentally skipped. + // + // To force a re-read of all events, delete the registry value + // above and call again. + [[nodiscard]] static std::string GetANSLoggerErrorEventsSinceLastCall(bool clearAfterRead = false); + // Scan the local network for AIBOX / ANSVIS servers. // Runs two strategies in parallel and merges results by IP: // 1) UDP multicast probe to 230.50.200.150:58839 (with subnet @@ -398,6 +424,12 @@ extern "C" ANSULT_API int ScanANSDevicesCpp(int timeoutMs, std::string& resultJ extern "C" ANSULT_API int GetANSLoggerErrorEvents(int clearAfterRead, LStrHandle result); extern "C" ANSULT_API int GetANSLoggerErrorEventsCpp(int clearAfterRead, std::string& result); +// Watermarked variant: only returns events not seen by previous calls. +// Watermark is stored in HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger +// (REG_QWORD). No admin needed for the skip; only for clearAfterRead. +extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCall(int clearAfterRead, LStrHandle result); +extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCallCpp(int clearAfterRead, std::string& result); + // Unicode conversion utilities for LabVIEW extern "C" ANSULT_API int ANSConvertUTF8ToUTF16LE(const char* utf8Str, LStrHandle result, int includeBOM = 1); extern "C" ANSULT_API int ANSDecodeJsonUnicodeToUTF16LE(const char* escapedStr, LStrHandle result); diff --git a/modules/ANSUtilities/dllmain.cpp b/modules/ANSUtilities/dllmain.cpp index 857c63f..b14524f 100644 --- a/modules/ANSUtilities/dllmain.cpp +++ b/modules/ANSUtilities/dllmain.cpp @@ -3093,3 +3093,42 @@ extern "C" ANSULT_API int GetANSLoggerErrorEvents(int clearAfterRead, LStrHandle } catch (...) { return -1; } } + +// --------------------------------------------------------------------------- +// GetANSLoggerErrorEventsSinceLastCall — watermark-based incremental read. +// Stores highest EventRecordID seen in HKCU; subsequent calls return only +// newer events. No admin needed for the skip (only for clearAfterRead). +// --------------------------------------------------------------------------- +extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCallCpp(int clearAfterRead, std::string& result) { + ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp: clearAfterRead=%d", clearAfterRead); + try { + result = ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(clearAfterRead != 0); + return 1; + } + catch (const std::exception& e) { + ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp threw: %s", e.what()); + result.clear(); + return 0; + } + catch (...) { + ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCallCpp threw unknown"); + result.clear(); + return 0; + } +} + +extern "C" ANSULT_API int GetANSLoggerErrorEventsSinceLastCall(int clearAfterRead, LStrHandle result) { + ANS_DBG("ANSUTIL", "GetANSLoggerErrorEventsSinceLastCall: clearAfterRead=%d", clearAfterRead); + if (!result) return -1; + try { + std::string s; + GetANSLoggerErrorEventsSinceLastCallCpp(clearAfterRead, s); + int size = static_cast(s.size()); + MgErr error = DSSetHandleSize(result, sizeof(int32) + size * sizeof(uChar)); + if (error != noErr) return -2; + (*result)->cnt = size; + if (size > 0) memcpy((*result)->str, s.data(), size); + return 1; + } + catch (...) { return -1; } +} diff --git a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp index 4b4c3cf..34b1ba8 100644 --- a/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp +++ b/tests/ANSFR-UnitTest/ANSFR-UnitTest.cpp @@ -1631,6 +1631,88 @@ int TestCompleteFR1() { std::cout << "End of program.\n"; return 1; } +// ---- SCRFD-TRT face-detection reproduction on a video (headless) ---------- +int SCRFDVideoRepro() +{ + std::string licenseKey = ""; + const char* configFilePath = ""; + std::string databaseFilePath = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ANSFR.db"; + std::string recognizerFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFR\\ServerOptimised\\ANS_FaceRecognizer_v1.1_NVIDIAGeForceRTX3070LaptopGPU.zip"; + std::string detectorFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSFDET\\ServerOptimised\\ANS_GenericFD(GPU)_v1.0_NVIDIAGeForceRTX3070LaptopGPU.zip"; + std::string videoPath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4"; + + std::cout << "[REPRO] detector = " << detectorFile << std::endl; + std::cout << "[REPRO] recognizer = " << recognizerFile << std::endl; + + ANSCENTER::ANSFacialRecognition* h = nullptr; + int cr = CreateANSRFHandle(&h, licenseKey.c_str(), configFilePath, + databaseFilePath.c_str(), recognizerFile.c_str(), + detectorFile.c_str(), 0.3, 1, 1, 1); + std::cout << "[REPRO] CreateANSRFHandle = " << cr << std::endl; + + int le = LoadANSRFEngine(&h); + std::cout << "[REPRO] LoadANSRFEngine = " << le << std::endl; + + cv::VideoCapture cap(videoPath); + if (!cap.isOpened()) { std::cout << "[REPRO] ERROR: cannot open video " << videoPath << std::endl; return 1; } + + const std::string win = "ANSFR - SCRFD/TensorRT Face Detection (ESC to quit)"; + cv::namedWindow(win, cv::WINDOW_NORMAL); + cv::resizeWindow(win, 1280, 720); + + int frameIdx = 0, totalDet = 0, saved = 0; + cv::Mat frame; + while (cap.read(frame)) { + if (frame.empty()) break; + ++frameIdx; + if (frameIdx > 300) break; // proof run: first 300 frames + unsigned int bufLen = 0; + unsigned char* jpg = ANSCENTER::ANSFRHelper::CVMatToBytes(frame, bufLen); + std::string res = RunANSRFInferenceBinary(&h, jpg, frame.cols, frame.rows); + delete jpg; + + int n = 0; + try { + boost::property_tree::ptree pt; std::stringstream ss; ss << res; + boost::property_tree::read_json(ss, pt); + if (pt.count("results")) { + BOOST_FOREACH(const boost::property_tree::ptree::value_type& child, pt.get_child("results")) { + const boost::property_tree::ptree& r = child.second; + const int uid = GetData(r, "user_id"); + const std::string uname = GetData(r, "user_name"); + const float x = GetData(r, "x"); + const float y = GetData(r, "y"); + const float w = GetData(r, "width"); + const float ht = GetData(r, "height"); + cv::rectangle(frame, cv::Rect((int)x, (int)y, (int)w, (int)ht), cv::Scalar(0, 255, 0), 2); + cv::putText(frame, cv::format("%s:%d", uname.c_str(), uid), cv::Point((int)x, (int)y - 8), + cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 2, cv::LINE_AA); + ++n; + } + } + } catch (...) {} + totalDet += n; + + cv::putText(frame, cv::format("Frame %d faces detected = %d", frameIdx, n), + cv::Point(25, 45), cv::FONT_HERSHEY_SIMPLEX, 1.1, cv::Scalar(0, 255, 255), 2, cv::LINE_AA); + if (frameIdx % 30 == 0) { + std::string fp = cv::format("C:\\Programs\\DemoAssets\\ANSFR_proof_frame_%04d_faces%d.jpg", frameIdx, n); + cv::imwrite(fp, frame); + ++saved; + std::cout << "[REPRO] saved annotated frame: " << fp << std::endl; + } + cv::imshow(win, frame); + if (cv::waitKey(1) == 27) break; // ESC to quit + } + std::cout << "[REPRO] played " << frameIdx << " frames, total faces drawn = " << totalDet + << ", saved " << saved << " annotated images to C:\\Programs\\DemoAssets\\" << std::endl; + cap.release(); + cv::destroyAllWindows(); + // NOTE: intentionally skipping ReleaseANSRFHandle/cudaDeviceReset here — there is + // a known C++ exception at teardown (0xE06D7363); process exit reclaims everything. + return 0; +} + int main() { @@ -1653,8 +1735,7 @@ int main() // TestCompleteFR(); //TestFaceRecognition(); //FaceRecognitionBenchmark(); - ANSVISTestCPU_Lightweight(); - //ANSVISTestCPU(); - std::cin.get(); + SCRFDVideoRepro(); + return 0; } diff --git a/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp b/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp index 2e52bda..e11b4d0 100644 --- a/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp +++ b/tests/ANSODEngine-UnitTest/ANSODEngine-UnitTest.cpp @@ -1647,6 +1647,9 @@ int HelmetDetectionTest() { } + + + // ============================================================================ // ElasticGPUTest — Verify elastic pool, round-robin dispatch, multi-GPU, // concurrent inference, and idle-slot cleanup. @@ -2222,14 +2225,24 @@ int RTMPOSETest() { VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx return 0; } + +int FireNSmokeTest() { + int modelType = 16; // Custom model type for helmet detection, adjust as needed based on how the model is set up in the engine + std::string onnxclmodelFilePath = "C:\\Projects\\ANSVIS\\Models\\ANS_FireSmoke_v2.0\\ANS_FireSmoke_v2.0.zip"; + std::string videoFile = "E:\\Programs\\DemoAssets\\Videos\\FireNSmoke\\BCA1.mp4"; + + VideoDetectorEngine(onnxclmodelFilePath, videoFile, modelType);// 22 is RT, 21 is onnx + return 0; +} + int main() { //FallDetectorTest(); //ALPROD_Test(); - //FireNSmokeTest(); + FireNSmokeTest(); //ElasticGPUTest(); - RTMPOSETest_CPP(); + //RTMPOSETest_CPP(); //RTMPOSETest(); //YOLO26SEGYolo11Test(); //YOLO26POSEYolo11Test(); diff --git a/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp b/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp index c7090e4..b349ac0 100644 --- a/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp +++ b/tests/ANSUtilities-UnitTest/ANSUtilities-UnitTest.cpp @@ -1,3 +1,7 @@ +// windows.h must be included before any header that pulls in wincrypt.h +// (Chilkat, OpenCV transitively). HKEY/RegOpenKeyExW are used by the +// watermark readout in GetEventLogErrorsWatermarkTest. +#include #include #include "ANSUtilities.h" #include "ANSLLM.h" @@ -489,20 +493,123 @@ int GetEventLogErrorsTest() std::cout << "\nGetANSLoggerErrorEventsCpp rc=" << rc << " bytes=" << fromExtern.size() << std::endl; - // ── 4. (DANGER) clear the Application channel after reading ───────────── - // Uncomment ONLY if you actually want to wipe every event from every - // source in the Application log (not just ANSLogger). Run as Admin or - // the clear silently fails (the read still succeeds either way). - // - // std::string cleared; - // int rcClear = GetANSLoggerErrorEventsCpp(1 /* clearAfterRead */, cleared); - // std::cout << "After clear: rc=" << rcClear - // << " events returned=" << std::count(cleared.begin(), cleared.end(), '\n') - // << " (next call should report 0)" << std::endl; + // ── 4. DIAGNOSTIC: read → read+clear → read, prove whether the clear + // flag actually does anything (and if not, capture the OS error). + { + std::cout << "\n--- clear-flag diagnostic ---" << std::endl; + auto countLines = [](const std::string& s) { + int n = 0; + for (char c : s) if (c == '\n') ++n; + return n; + }; + + std::string before = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(false); + std::cout << " before clear : " << countLines(before) << " events" << std::endl; + + std::string cleared = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(true); + std::cout << " with clear=true call returned: " + << countLines(cleared) << " events" << std::endl; + + std::string after = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(false); + std::cout << " after clear : " << countLines(after) + << " events (should be 0 if clear worked)" << std::endl; + + const int beforeN = countLines(before); + const int afterN = countLines(after); + if (beforeN == 0) { + std::cout << " RESULT: inconclusive (no events to clear). " + "Check stderr above for the actual EvtClearLog status." + << std::endl; + } else if (afterN == 0) { + std::cout << " RESULT: clear SUCCEEDED — process was elevated." + << std::endl; + } else if (afterN >= beforeN) { + std::cout << " RESULT: clear FAILED. " + "Check stderr above for the GetLastError code " + "(5 = ACCESS_DENIED — re-run as Administrator)." + << std::endl; + } else { + std::cout << " RESULT: partial clear (" << beforeN << " -> " + << afterN << "). Likely a concurrent writer." + << std::endl; + } + } return lineCount; } +// ============================================================================ +// Example: ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall +// +// Watermark-based variant. Two consecutive calls demonstrate the skip: +// 1st call: returns every Error event from ANSLogger (same as the full read) +// 2nd call: returns 0 (or only events written between the two calls) +// The watermark is stored at +// HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger (REG_QWORD) +// To force a re-read of everything, delete that registry value. +// ============================================================================ +int GetEventLogErrorsWatermarkTest() +{ + std::cout << "\n===== GetANSLoggerErrorEventsSinceLastCall (watermark) =====" + << std::endl; + auto countLines = [](const std::string& s) { + int n = 0; + for (char c : s) if (c == '\n') ++n; + return n; + }; + + // Read the current watermark so we can show it before and after. + auto readWatermark = []() -> unsigned long long { + HKEY hKey = nullptr; + if (RegOpenKeyExW(HKEY_CURRENT_USER, + L"Software\\ANSCENTER\\ANSUtilities\\EventLogWatermark", + 0, KEY_READ, &hKey) != ERROR_SUCCESS) return 0ULL; + unsigned long long v = 0; + DWORD sz = sizeof(v), tp = 0; + LONG rc = RegQueryValueExW(hKey, L"ANSLogger", nullptr, &tp, + reinterpret_cast(&v), &sz); + RegCloseKey(hKey); + return (rc == ERROR_SUCCESS && tp == REG_QWORD) ? v : 0ULL; + }; + + std::cout << " watermark before: " << readWatermark() << std::endl; + + std::string first = + ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(false); + std::cout << " 1st call returned: " << countLines(first) << " event(s)" + << std::endl; + std::cout << " watermark after 1st call: " << readWatermark() << std::endl; + + std::string second = + ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(false); + std::cout << " 2nd call returned: " << countLines(second) + << " event(s) (should be 0 if no new errors were logged " + "between the two calls)" << std::endl; + + if (countLines(second) == 0 && countLines(first) > 0) { + std::cout << " RESULT: watermark works — duplicates suppressed." + << std::endl; + } else if (countLines(first) == 0) { + std::cout << " RESULT: nothing to test (no ANSLogger errors in log)." + << std::endl; + } else { + std::cout << " RESULT: 2nd call returned " << countLines(second) + << " event(s) — either new errors arrived between calls, " + "or the watermark didn't persist." << std::endl; + } + + // Note: passing the LabVIEW extern below as well so its parameter wiring + // is exercised. Pass 0 = no clear; clear path is identical to the + // already-tested admin-required version in GetEventLogErrorsTest. + std::string fromExtern; + int rc = GetANSLoggerErrorEventsSinceLastCallCpp(0, fromExtern); + std::cout << " GetANSLoggerErrorEventsSinceLastCallCpp rc=" << rc + << " bytes=" << fromExtern.size() + << " (should be 0 — already watermarked above)" << std::endl; + + return countLines(first); +} + int main() { //ANSLLMOllamaTest(); @@ -519,5 +626,6 @@ int main() ScanDevicesTest(); GetEventLogErrorsTest(); + GetEventLogErrorsWatermarkTest(); } diff --git a/tools/Check-SharedCoreVersions.ps1 b/tools/Check-SharedCoreVersions.ps1 new file mode 100644 index 0000000..d897747 --- /dev/null +++ b/tools/Check-SharedCoreVersions.ps1 @@ -0,0 +1,290 @@ +<# +.SYNOPSIS + Reports versions and ABI compatibility of the inference runtime stack + (CUDA, cuDNN, TensorRT, OpenVINO, DirectML, ONNX Runtime per-EP) inside an + ANSCENTER "Shared" folder, and optionally diffs two such folders. + +.DESCRIPTION + Rerun this after upgrading any DLL in SharedCore to confirm the pieces still + line up. It does three things: + 1. Reads the version of every key library. + - ORT: FileVersion of ep\\onnxruntime.dll + - OpenVINO / DirectML / cuDNN: FileVersion resource + - CUDA components: inferred from the versioned file name (cudart64_13 => CUDA 13) + - TensorRT: nvinfer_*.dll has NO version resource, so we p/invoke its + exported getInferLibVersion() (packed int, e.g. 101501 => 10.15.1). + 2. Uses dumpbin /DEPENDENTS (if Visual Studio is installed) to list which + CUDA / cuDNN / TensorRT / OpenVINO / DirectML DLLs each EP actually + imports, and verifies every one exists in the root. + 3. Flags gaps: empty ep\cpu with no flat fallback, missing dependencies, + ORT-version split across EP folders. + + NOTE: the TensorRT probe uses a plain [DllImport] to nvinfer's exported + getInferLibVersion(). It deliberately avoids LoadLibrary+GetProcAddress+ + GetDelegateForFunctionPointer, because that dynamic-resolution pattern trips + Windows Defender AMSI ("malicious content") and blocks the whole script. + +.PARAMETER Root + The Shared folder to inspect. Default: C:\Projects\SharedCore + +.PARAMETER CompareWith + Optional second Shared folder to diff against Root (e.g. the deployed + C:\ProgramData\ANSCENTER\Shared). Reports any version mismatches. + +.PARAMETER SkipTensorRT + Skip the TensorRT p/invoke probe (useful if your AV still objects, or to run + purely on file-version metadata). + +.EXAMPLE + .\Check-SharedCoreVersions.ps1 + +.EXAMPLE + .\Check-SharedCoreVersions.ps1 -Root C:\Projects\SharedCore ` + -CompareWith C:\ProgramData\ANSCENTER\Shared +#> +[CmdletBinding()] +param( + [string] $Root = "C:\Projects\SharedCore", + [string] $CompareWith, + [switch] $SkipTensorRT +) + +$ErrorActionPreference = "Stop" + +# ---------------------------------------------------------------------------- +# Helpers +# ---------------------------------------------------------------------------- +function Get-FileVer([string]$path) { + if (Test-Path -LiteralPath $path) { (Get-Item -LiteralPath $path).VersionInfo.FileVersion } + else { $null } +} + +# Windows PowerShell 5.1 has no ?? operator - use this instead. +function Coalesce($value, $fallback) { + if ($value) { $value } else { $fallback } +} + +function Find-Dumpbin { + $candidates = Get-ChildItem ` + "C:\Program Files\Microsoft Visual Studio\*\*\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe", ` + "C:\Program Files (x86)\Microsoft Visual Studio\*\*\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe" ` + -ErrorAction SilentlyContinue | Sort-Object FullName -Descending + if ($candidates) { return $candidates[0].FullName } + return $null +} + +# Declares plain DllImports to nvinfer's getInferLibVersion. Compiled once per +# session. The DLLs are resolved via PATH, so callers must put the root dir on +# PATH before invoking (Get-TensorRTVersion does this). +function Initialize-TrtProbe { + if ('AnsTrtProbe.Lib' -as [type]) { return } + Add-Type -Namespace AnsTrtProbe -Name Lib -MemberDefinition @' +[System.Runtime.InteropServices.DllImport("nvinfer_dispatch_10.dll", EntryPoint = "getInferLibVersion", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] +public static extern int Dispatch10(); +[System.Runtime.InteropServices.DllImport("nvinfer_lean_10.dll", EntryPoint = "getInferLibVersion", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] +public static extern int Lean10(); +[System.Runtime.InteropServices.DllImport("nvinfer_10.dll", EntryPoint = "getInferLibVersion", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)] +public static extern int Full10(); +'@ +} + +# Returns the packed TensorRT version int (e.g. 101501) or $null. +function Get-TensorRTVersion([string]$dir) { + if ($SkipTensorRT) { return $null } + if (-not (Get-ChildItem -LiteralPath $dir -Filter 'nvinfer*_10.dll' -ErrorAction SilentlyContinue)) { + return $null + } + $origPath = $env:Path + $env:Path = "$dir;$env:Path" # so the stub and its deps resolve + try { + Initialize-TrtProbe + foreach ($m in 'Dispatch10','Lean10','Full10') { + try { + $v = [AnsTrtProbe.Lib]::$m() + if ($v -gt 0) { return $v } + } catch { } + } + return $null + } finally { + $env:Path = $origPath + } +} + +function Format-TensorRT($raw) { + if (-not $raw) { return "UNKNOWN (probe skipped or failed)" } + $maj = [math]::Floor($raw / 10000) + $min = [math]::Floor(($raw % 10000) / 100) + $pat = $raw % 100 + return "$maj.$min.$pat (raw $raw)" +} + +# Returns the lower-cased set of dependency DLLs (CUDA/TRT/OV/DML family) that +# $path imports, via dumpbin /DEPENDENTS. Empty array if dumpbin unavailable. +function Get-Deps([string]$dumpbin, [string]$path) { + if (-not $dumpbin -or -not (Test-Path -LiteralPath $path)) { return @() } + $rx = '(?i)cudart|cublas|cudnn|cufft|cusparse|cusolver|nvinfer|nvonnx|nvrtc|directml|openvino' + & $dumpbin /DEPENDENTS $path 2>$null | + Select-String -Pattern '^\s+\S+\.dll\s*$' | + ForEach-Object { $_.Line.Trim().ToLower() } | + Where-Object { $_ -match $rx } | + Sort-Object -Unique +} + +$epMap = [ordered]@{ + "cpu" = "CPU" + "cuda" = "NVIDIA_GPU (CUDA + TensorRT)" + "directml" = "AMD_GPU (DirectML)" + "openvino" = "OPENVINO_GPU (Intel)" +} + +# ---------------------------------------------------------------------------- +# Inspect one root +# ---------------------------------------------------------------------------- +function Inspect-Root([string]$root) { + if (-not (Test-Path -LiteralPath $root)) { + Write-Host "ERROR: root not found: $root" -ForegroundColor Red + return $null + } + $dumpbin = Find-Dumpbin + + Write-Host "" + Write-Host "==================================================================" + Write-Host " Shared root : $root" + Write-Host " dumpbin : $(if($dumpbin){$dumpbin}else{'NOT FOUND - dependency checks skipped'})" + Write-Host "==================================================================" + + $result = [ordered]@{ Root = $root; Ort = [ordered]@{} } + + # ---- Core libraries ----------------------------------------------------- + $trtRaw = Get-TensorRTVersion $root + $result.TensorRT = $trtRaw + Write-Host "" + Write-Host "-- Core runtime libraries -----------------------------------------" + Write-Host ("{0,-14} {1}" -f "TensorRT:", (Format-TensorRT $trtRaw)) + Write-Host ("{0,-14} {1}" -f "OpenVINO:", (Coalesce (Get-FileVer (Join-Path $root 'openvino.dll')) 'MISSING')) + Write-Host ("{0,-14} {1}" -f "DirectML:", (Coalesce (Get-FileVer (Join-Path $root 'DirectML.dll')) 'MISSING')) + Write-Host ("{0,-14} {1}" -f "cuDNN:", (Coalesce (Get-FileVer (Join-Path $root 'cudnn64_9.dll')) 'MISSING')) + # CUDA toolkit generation is encoded in the file name suffix. + $cudaFiles = Get-ChildItem -LiteralPath $root -Filter "cudart64_*.dll" -ErrorAction SilentlyContinue + if ($cudaFiles) { + $gen = ($cudaFiles[0].BaseName -replace 'cudart64_','') + Write-Host ("{0,-14} {1}" -f "CUDA:", "toolkit $gen.x (cudart64_$gen.dll present)") + } else { + Write-Host ("{0,-14} {1}" -f "CUDA:", "MISSING (no cudart64_*.dll)") + } + + # ---- ORT per EP --------------------------------------------------------- + Write-Host "" + Write-Host "-- ONNX Runtime per EP --------------------------------------------" + foreach ($ep in $epMap.Keys) { + $epDir = Join-Path $root "ep\$ep" + $ortDll = Join-Path $epDir "onnxruntime.dll" + $ver = Get-FileVer $ortDll + $result.Ort[$ep] = $ver + if ($ver) { + Write-Host ("{0,-10} {1,-26} [{2}]" -f $ep, $ver, $epMap[$ep]) + } elseif (Test-Path -LiteralPath $epDir) { + Write-Host ("{0,-10} {1,-26} [{2}]" -f $ep, "EMPTY / no onnxruntime.dll", $epMap[$ep]) + } else { + Write-Host ("{0,-10} {1,-26} [{2}]" -f $ep, "FOLDER MISSING", $epMap[$ep]) + } + } + + # ---- Dependency / presence check --------------------------------------- + Write-Host "" + Write-Host "-- EP dependency resolution (imports must exist in root) ----------" + $missingDeps = @() + $epProviders = @{ + "cuda" = @("onnxruntime_providers_cuda.dll","onnxruntime_providers_tensorrt.dll") + "directml" = @() # DML is built into onnxruntime.dll; needs DirectML.dll from root + "openvino" = @("onnxruntime_providers_openvino.dll") + "cpu" = @() + } + foreach ($ep in $epProviders.Keys) { + $epDir = Join-Path $root "ep\$ep" + if (-not (Test-Path -LiteralPath (Join-Path $epDir 'onnxruntime.dll'))) { continue } + $checkFiles = @("onnxruntime.dll") + $epProviders[$ep] + $deps = @() + foreach ($cf in $checkFiles) { $deps += Get-Deps $dumpbin (Join-Path $epDir $cf) } + $deps = $deps | Sort-Object -Unique + if (-not $deps) { continue } + Write-Host (" [{0}] imports: {1}" -f $ep, ($deps -join ', ')) + foreach ($d in $deps) { + # provider DLLs live in the ep folder; everything else in root + $inEp = Test-Path -LiteralPath (Join-Path $epDir $d) + $inRoot = Test-Path -LiteralPath (Join-Path $root $d) + if (-not $inEp -and -not $inRoot) { + Write-Host (" MISSING: $d") -ForegroundColor Red + $missingDeps += "$ep -> $d" + } + } + } + if (-not $missingDeps) { Write-Host " All imported CUDA/TRT/OV/DML DLLs are present." -ForegroundColor Green } + $result.MissingDeps = $missingDeps + + # ---- Gap checks --------------------------------------------------------- + Write-Host "" + Write-Host "-- Findings -------------------------------------------------------" + $findings = @() + + # CPU reachability: EngineType::CPU needs ep\cpu\onnxruntime.dll OR a flat root onnxruntime.dll + $cpuDll = Test-Path -LiteralPath (Join-Path $root 'ep\cpu\onnxruntime.dll') + $flatDll = Test-Path -LiteralPath (Join-Path $root 'onnxruntime.dll') + if (-not $cpuDll -and -not $flatDll) { + $findings += "CPU branch UNREACHABLE: ep\cpu is empty and no flat onnxruntime.dll fallback. A CPU-only machine will fail to load ORT." + } + + # ORT version split across EP folders + $distinct = $result.Ort.Values | Where-Object { $_ } | Sort-Object -Unique + if ($distinct.Count -gt 1) { + $findings += "ORT versions differ across EP folders ($($distinct -join ' / ')). Fine ONLY because EPLoader loads one EP per process - never mix DLLs across folders." + } + + if ($missingDeps) { $findings += "Unresolved dependencies: $($missingDeps -join '; ')" } + + if (-not $findings) { + Write-Host " No issues detected." -ForegroundColor Green + } else { + foreach ($f in $findings) { Write-Host " ! $f" -ForegroundColor Yellow } + } + + return $result +} + +# ---------------------------------------------------------------------------- +# Main +# ---------------------------------------------------------------------------- +$primary = Inspect-Root $Root + +if ($CompareWith) { + $secondary = Inspect-Root $CompareWith + if ($primary -and $secondary) { + Write-Host "" + Write-Host "==================================================================" + Write-Host " DIFF: $Root vs $CompareWith" + Write-Host "==================================================================" + $mismatch = $false + + if ($primary.TensorRT -ne $secondary.TensorRT) { + Write-Host (" TensorRT: {0} vs {1}" -f (Format-TensorRT $primary.TensorRT), (Format-TensorRT $secondary.TensorRT)) -ForegroundColor Yellow + $mismatch = $true + } + foreach ($ep in $epMap.Keys) { + $a = $primary.Ort[$ep]; $b = $secondary.Ort[$ep] + if ($a -ne $b) { + Write-Host (" ORT ep\{0}: {1} vs {2}" -f $ep, (Coalesce $a 'MISSING'), (Coalesce $b 'MISSING')) -ForegroundColor Yellow + $mismatch = $true + } + } + foreach ($lib in @('openvino.dll','DirectML.dll','cudnn64_9.dll')) { + $a = Get-FileVer (Join-Path $Root $lib); $b = Get-FileVer (Join-Path $CompareWith $lib) + if ($a -ne $b) { + Write-Host (" {0}: {1} vs {2}" -f $lib, (Coalesce $a 'MISSING'), (Coalesce $b 'MISSING')) -ForegroundColor Yellow + $mismatch = $true + } + } + if (-not $mismatch) { Write-Host " Identical across both roots." -ForegroundColor Green } + } +} + +Write-Host "" diff --git a/tools/_build_frtest.bat b/tools/_build_frtest.bat new file mode 100644 index 0000000..c8a7252 --- /dev/null +++ b/tools/_build_frtest.bat @@ -0,0 +1,5 @@ +@echo off +call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" >nul 2>nul +cd /d "C:\Projects\CLionProjects\ANSCORE" +"C:\Program Files\CMake\bin\cmake.exe" --build cmake-build-release --target ANSFR-UnitTest +exit /b %errorlevel% diff --git a/tools/_run_frtest.bat b/tools/_run_frtest.bat new file mode 100644 index 0000000..f01bc22 --- /dev/null +++ b/tools/_run_frtest.bat @@ -0,0 +1,5 @@ +@echo off +set "PATH=C:\Projects\CLionProjects\ANSCORE\cmake-build-release\bin;C:\Projects\SharedCore;%PATH%" +cd /d "C:\Projects\CLionProjects\ANSCORE\cmake-build-release\bin" +ANSFR-UnitTest.exe +echo [REPRO] exitcode=%errorlevel% diff --git a/tools/build_openvino_ep.bat b/tools/build_openvino_ep.bat new file mode 100644 index 0000000..ba410e8 --- /dev/null +++ b/tools/build_openvino_ep.bat @@ -0,0 +1,77 @@ +@echo off +REM ============================================================================ +REM build_openvino_ep.bat +REM Build the ONNX Runtime OpenVINO Execution Provider (ORT 1.24 / API 24) and +REM stage the 3 resulting DLLs into SharedCore\ep\openvino so verify_ep.ps1 +REM -RequiredApi 24 goes green. +REM +REM Self-contained: sets up MSVC + an isolated Python venv + OpenVINO env, then +REM builds and stages. Run from a PLAIN cmd.exe (NOT PowerShell - OpenVINO's +REM setupvars.bat env vars only persist in cmd). +REM +REM Usage: tools\build_openvino_ep.bat +REM ============================================================================ +setlocal enableextensions + +REM ---- Paths (edit here if your layout differs) ------------------------------ +set "ORT_SRC=C:\Projects\onnxruntime-ov" +set "PYDIR=C:\ANSLibs\Python311" +set "OV_SETUP=C:\ANSLibs\OpenVINO\setupvars.bat" +set "EP_DST=C:\Projects\SharedCore\ep\openvino" +set "OV_DEVICE=GPU" + +echo === [1/6] Locate MSVC (vcvars64) === +set "VCVARS=" +for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath 2^>nul`) do set "VSPATH=%%i" +if defined VSPATH if exist "%VSPATH%\VC\Auxiliary\Build\vcvars64.bat" set "VCVARS=%VSPATH%\VC\Auxiliary\Build\vcvars64.bat" +if not defined VCVARS set "VCVARS=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" +if not exist "%VCVARS%" ( echo ERROR: vcvars64.bat not found. Install VS 2022 C++ workload. & exit /b 1 ) +call "%VCVARS%" || ( echo ERROR: vcvars64 failed & exit /b 1 ) + +echo === [2/6] OpenVINO environment === +if not exist "%OV_SETUP%" ( echo ERROR: OpenVINO setupvars not found at %OV_SETUP% & exit /b 1 ) +call "%OV_SETUP%" +REM Release-only OpenVINO archives still ship a Debug cmake config whose DLLs are +REM absent, which breaks find_package(OpenVINO). Neutralize it (safe: ANSCORE links +REM OpenVINO via explicit paths, not find_package, and this build is Release-only). +set "OV_DBG=C:\ANSLibs\OpenVINO\runtime\cmake\OpenVINOTargets-debug.cmake" +if exist "%OV_DBG%" ( ren "%OV_DBG%" "OpenVINOTargets-debug.cmake.disabled" && echo neutralized OpenVINO Debug cmake config ) +REM This ANSLibs OpenVINO is stripped of its runtime DLLs (deployed to SharedCore), +REM but find_package(OpenVINO) validates DLL presence. Restore Release DLLs from SharedCore. +set "OV_REL=C:\ANSLibs\OpenVINO\runtime\bin\intel64\Release" +if not exist "%OV_REL%\openvino_onnx_frontend.dll" ( + echo restoring OpenVINO Release DLLs from SharedCore... + if not exist "%OV_REL%" mkdir "%OV_REL%" + copy /Y "C:\Projects\SharedCore\openvino*.dll" "%OV_REL%\" >nul + copy /Y "C:\Projects\SharedCore\tbb*.dll" "%OV_REL%\" >nul +) + +echo === [3/6] Isolated Python venv (keeps build deps out of %PYDIR%) === +if not exist "%PYDIR%\python.exe" ( echo ERROR: python not at %PYDIR% & exit /b 1 ) +if not exist "%ORT_SRC%\.buildvenv\Scripts\python.exe" ( + "%PYDIR%\python.exe" -m venv "%ORT_SRC%\.buildvenv" || ( echo ERROR: venv create failed & exit /b 1 ) +) +call "%ORT_SRC%\.buildvenv\Scripts\activate.bat" +python --version || ( echo ERROR: venv python not usable & exit /b 1 ) +python -m pip install --upgrade pip setuptools wheel packaging numpy >nul + +echo === [4/6] Build (Release, shared lib, OpenVINO %OV_DEVICE%) === +cd /d "%ORT_SRC%" || ( echo ERROR: ORT source not found at %ORT_SRC% & exit /b 1 ) +REM NOTE: full path, not bare 'build.bat' - VS dev env sets +REM NoDefaultCurrentDirectoryInExePath, so cmd won't resolve build.bat from CWD. +call "%ORT_SRC%\build.bat" --config Release --use_openvino %OV_DEVICE% --build_shared_lib --parallel --skip_tests --cmake_generator "Visual Studio 17 2022" +if errorlevel 1 ( echo ERROR: build failed - see log above & exit /b 1 ) + +echo === [5/6] Stage DLLs into %EP_DST% (with one-time backup) === +set "OUT=%ORT_SRC%\build\Windows\Release\Release" +if not exist "%OUT%\onnxruntime_providers_openvino.dll" ( echo ERROR: build output missing in %OUT% & exit /b 1 ) +if not exist "%EP_DST%._bak_123" ( xcopy /E /I /Y "%EP_DST%" "%EP_DST%._bak_123" >nul ) +copy /Y "%OUT%\onnxruntime.dll" "%EP_DST%\" >nul +copy /Y "%OUT%\onnxruntime_providers_openvino.dll" "%EP_DST%\" >nul +copy /Y "%OUT%\onnxruntime_providers_shared.dll" "%EP_DST%\" >nul + +echo === [6/6] Done === +echo Staged into %EP_DST% (1.23 backup at %EP_DST%._bak_123) +echo Validate: +echo powershell -ExecutionPolicy Bypass -File C:\Projects\CLionProjects\ANSCORE\tools\verify_ep.ps1 -RequiredApi 24 +endlocal diff --git a/tools/bump_ort_sdk.ps1 b/tools/bump_ort_sdk.ps1 new file mode 100644 index 0000000..1c9435f --- /dev/null +++ b/tools/bump_ort_sdk.ps1 @@ -0,0 +1,131 @@ +<# +.SYNOPSIS + Bump the link-time ONNX Runtime SDK in C:\ANSLibs\onnxruntime to a new version + (e.g. 1.23 -> 1.24), safely and reversibly. ANSCORE compiles against this folder. + +.DESCRIPTION + Replaces C:\ANSLibs\onnxruntime\{include,lib} + the runtime_openvino.txt + marker with a new ORT SDK, after validating the new SDK and backing up the old one. + + It does NOT touch the runtime DLLs in SharedCore\ep - those are a separate step. + ANSCORE only #includes onnxruntime_cxx_api.h (the EP-agnostic C++ API), so any + matching ORT build's include\ works (GPU build's headers are fine). + + SEQUENCING NOTE: bumping the link header to API N makes any runtime EP folder + whose core is < N a latent risk. Unify the runtime (incl. the OpenVINO EP) to the + target version FIRST, then run this. After running, rebuild ANSCORE and validate + the runtime with: tools\verify_ep.ps1 -RequiredApi + +.PARAMETER SdkPath + Folder of the NEW ORT SDK. Must contain: + include\onnxruntime_c_api.h (with the new ORT_API_VERSION) + lib\onnxruntime.lib + Typically the unzipped onnxruntime-win-x64-gpu- release, or an Intel + OpenVINO ORT build of the same version. + +.PARAMETER AnsLibs + ANSLibs root. Default: C:\ANSLibs + +.PARAMETER Marker + New marker filename to drop in (documents the version, mirrors the existing + 'runtime1.23.0_openvino2025.3.0.txt'). Optional - if omitted, no marker change. + +.PARAMETER WhatIf + Show what would happen without changing anything. + +.EXAMPLE + # Dry run first + .\bump_ort_sdk.ps1 -SdkPath D:\dl\onnxruntime-win-x64-gpu-1.24.0 ` + -Marker runtime1.24.0_openvino2025.3.0.txt -WhatIf + # Then for real + .\bump_ort_sdk.ps1 -SdkPath D:\dl\onnxruntime-win-x64-gpu-1.24.0 ` + -Marker runtime1.24.0_openvino2025.3.0.txt +#> +[CmdletBinding(SupportsShouldProcess = $true)] +param( + [Parameter(Mandatory = $true)][string] $SdkPath, + [string] $AnsLibs = "C:\ANSLibs", + [string] $Marker +) + +$ErrorActionPreference = "Stop" + +function Get-OrtApiFromHeader([string]$header) { + if (-not (Test-Path -LiteralPath $header)) { return $null } + $line = Select-String -LiteralPath $header -Pattern 'define\s+ORT_API_VERSION\s+(\d+)' | Select-Object -First 1 + if ($line) { return [int]$line.Matches[0].Groups[1].Value } + return $null +} + +if (-not (Test-Path -LiteralPath $SdkPath)) { + Write-Host "FAIL - SdkPath not found: $SdkPath" -ForegroundColor Red + Write-Host "Point -SdkPath at an unzipped ORT release folder (with include\ and lib\)." -ForegroundColor Yellow + exit 2 +} + +$ortDir = Join-Path $AnsLibs 'onnxruntime' +$newInc = Join-Path $SdkPath 'include' +$newLib = Join-Path $SdkPath 'lib' +$newHdr = Join-Path $newInc 'onnxruntime_c_api.h' + +# ---- Validate NEW sdk ------------------------------------------------------- +Write-Host "== Validating new SDK at: $SdkPath ==" +$problems = @() +if (-not (Test-Path -LiteralPath $newHdr)) { $problems += "missing include\onnxruntime_c_api.h" } +if (-not (Test-Path -LiteralPath (Join-Path $newInc 'onnxruntime_cxx_api.h'))) { $problems += "missing include\onnxruntime_cxx_api.h" } +if (-not (Test-Path -LiteralPath (Join-Path $newLib 'onnxruntime.lib'))) { $problems += "missing lib\onnxruntime.lib" } +if ($problems) { + Write-Host "FAIL - new SDK is incomplete:" -ForegroundColor Red + $problems | ForEach-Object { Write-Host " - $_" -ForegroundColor Red } + Write-Host "Point -SdkPath at an unzipped ORT release that has include\ and lib\." -ForegroundColor Yellow + exit 2 +} + +$newApi = Get-OrtApiFromHeader $newHdr +$oldApi = Get-OrtApiFromHeader (Join-Path $ortDir 'include\onnxruntime_c_api.h') +$newApiDisp = if ($newApi) { $newApi } else { '??' } +$oldApiDisp = if ($oldApi) { $oldApi } else { 'none' } +Write-Host (" new ORT_API_VERSION : {0}" -f $newApiDisp) +Write-Host (" current (ANSLibs) : {0}" -f $oldApiDisp) +if ($newApi -and $oldApi -and $newApi -le $oldApi) { + Write-Host ("WARNING: new API ({0}) is not greater than current ({1}). Continue only if intentional." -f $newApi,$oldApi) -ForegroundColor Yellow +} + +# ---- Back up CURRENT onnxruntime SDK --------------------------------------- +$stamp = (Get-Date -Format 'yyyyMMdd_HHmmss') +$backup = Join-Path $AnsLibs ("onnxruntime.bak_$stamp") +Write-Host "" +Write-Host "== Backing up current SDK -> $backup ==" +if ($PSCmdlet.ShouldProcess($ortDir, "copy to $backup")) { + Copy-Item -LiteralPath $ortDir -Destination $backup -Recurse -Force +} + +# ---- Swap include\ and lib\ ------------------------------------------------- +Write-Host "== Installing new include\ and lib\ ==" +foreach ($sub in @('include','lib')) { + $dst = Join-Path $ortDir $sub + $src = Join-Path $SdkPath $sub + if ($PSCmdlet.ShouldProcess($dst, "replace from $src")) { + if (Test-Path -LiteralPath $dst) { Remove-Item -LiteralPath $dst -Recurse -Force } + Copy-Item -LiteralPath $src -Destination $dst -Recurse -Force + } +} + +# ---- Marker file ------------------------------------------------------------ +if ($Marker) { + Write-Host "== Updating version marker -> $Marker ==" + if ($PSCmdlet.ShouldProcess($ortDir, "swap marker to $Marker")) { + Get-ChildItem -LiteralPath $ortDir -Filter 'runtime*.txt' -File -ErrorAction SilentlyContinue | + ForEach-Object { Remove-Item -LiteralPath $_.FullName -Force } + New-Item -ItemType File -Path (Join-Path $ortDir $Marker) -Force | Out-Null + } +} + +Write-Host "" +Write-Host "== DONE ==" -ForegroundColor Green +Write-Host "Next steps:" -ForegroundColor Cyan +$apiForSteps = if ($newApi) { $newApi } else { 'N' } +Write-Host (" 1. Clean-rebuild ANSCORE (it now compiles against ORT_API_VERSION {0})." -f $apiForSteps) +Write-Host (" 2. Validate the runtime: tools\verify_ep.ps1 -RequiredApi {0}" -f $apiForSteps) +Write-Host " (the openvino EP folder must also be at the new version or it will FAIL.)" +Write-Host (" 3. If anything breaks, revert: Remove-Item '$ortDir' -Recurse -Force; Rename-Item '$backup' 'onnxruntime'") diff --git a/tools/fetch_ort_headers.ps1 b/tools/fetch_ort_headers.ps1 new file mode 100644 index 0000000..501e1a4 --- /dev/null +++ b/tools/fetch_ort_headers.ps1 @@ -0,0 +1,115 @@ +<# +.SYNOPSIS + Assemble a complete link-time ONNX Runtime C/C++ SDK (include\ + lib\) by + downloading the public headers at a given source commit and pairing them with + an existing import lib. Output feeds straight into tools\bump_ort_sdk.ps1. + +.DESCRIPTION + ORT nightly builds (e.g. 1.24.YYYYMMDD.N.) ship DLLs + import libs but + the matching C/C++ headers are only the public source files in the GitHub repo + at the build's commit. ANSCORE compiles against ORT_API_VERSION via these + headers, so this script pulls the exact header set for -Commit and copies in a + matching onnxruntime.lib, producing a ready-to-install SDK folder. + + Default -Commit 058787c matches SharedCore\ep\cuda (1.24.20260218) AND the + onnxruntime.lib already on disk under LabellingTool\vendor (same build). + +.PARAMETER Commit + ORT source commit/branch/tag to fetch headers from. Default: 058787c (API 24). + +.PARAMETER OutDir + Where to assemble the SDK. Default: %TEMP%\ort_sdk_. + +.PARAMETER LibSource + Folder containing onnxruntime.lib (+ onnxruntime_providers_shared.lib) of the + SAME build. Default: the LabellingTool vendor folder found on this machine. + +.EXAMPLE + .\fetch_ort_headers.ps1 + .\bump_ort_sdk.ps1 -SdkPath $env:TEMP\ort_sdk_058787c -Marker runtime1.24.0_openvino2025.3.0.txt -WhatIf +#> +[CmdletBinding()] +param( + [string] $Commit = "058787c", + [string] $OutDir, + [string] $LibSource = "C:\Projects\CLionProjects\LabellingTool\vendor\onnxruntime-win\lib" +) + +$ErrorActionPreference = "Stop" +$ProgressPreference = "SilentlyContinue" +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +if (-not $OutDir) { $OutDir = Join-Path $env:TEMP "ort_sdk_$Commit" } +$incDir = Join-Path $OutDir 'include' +$libDir = Join-Path $OutDir 'lib' +New-Item -ItemType Directory -Force -Path $incDir, $libDir | Out-Null + +$base = "https://raw.githubusercontent.com/microsoft/onnxruntime/$Commit/include/onnxruntime/core" + +# Header set ANSCORE links against (mirrors ANSLibs, minus unused training headers). +# Each entry: subpath under .../core/ -> flattened into include\ +$headers = @( + "session/onnxruntime_c_api.h", + "session/onnxruntime_cxx_api.h", + "session/onnxruntime_cxx_inline.h", + "session/onnxruntime_float16.h", + "session/onnxruntime_lite_custom_op.h", + "session/onnxruntime_run_options_config_keys.h", + "session/onnxruntime_session_options_config_keys.h", + "session/onnxruntime_ep_c_api.h", + "session/onnxruntime_ep_device_ep_metadata_keys.h", + "providers/cpu/cpu_provider_factory.h", + "framework/provider_options.h" +) + +Write-Host "== Fetching ORT headers @ $Commit -> $incDir ==" +$ok = 0; $fail = @() +foreach ($h in $headers) { + $name = Split-Path $h -Leaf + $url = "$base/$h" + try { + Invoke-WebRequest -Uri $url -OutFile (Join-Path $incDir $name) -UseBasicParsing + # Guard against a 200-with-HTML (GitHub 404 page) sneaking through + $first = Get-Content -LiteralPath (Join-Path $incDir $name) -TotalCount 1 -ErrorAction SilentlyContinue + if ($first -match '^\s*nul 2>nul +cd /d "C:\Projects\CLionProjects\ANSCORE" +"C:\Program Files\CMake\bin\cmake.exe" --build cmake-build-release --parallel +exit /b %errorlevel% diff --git a/tools/verify_ep.ps1 b/tools/verify_ep.ps1 new file mode 100644 index 0000000..5566f6b --- /dev/null +++ b/tools/verify_ep.ps1 @@ -0,0 +1,243 @@ +<# +.SYNOPSIS + Acceptance gate for an ANSCENTER ep\ ONNX Runtime tree. Returns exit code 0 + (all PASS) or 1 (any FAIL) so it can be wired into CI / a release checklist. + +.DESCRIPTION + Run this after swapping any onnxruntime*.dll in SharedCore\ep (e.g. when + unifying all EP folders onto a single ORT version). It enforces the three + rules that make an ep\ tree correct for ANSCORE: + + CHECK 1 - API floor. Each EP folder's core onnxruntime.dll must expose an + ORT C-API version >= the version ANSCORE links against + (-RequiredApi, default 23 for ORT 1.23). ORT is backward + compatible, so a NEWER core (1.24 => API 24) is fine; an OLDER + one is not. API is derived from the file version (1. => + API ). + + CHECK 2 - Provider/core lock. Every onnxruntime_providers_*.dll in a + folder must have the EXACT same file version as that folder's + onnxruntime.dll. ORT provider DLLs are version-locked to the core; + a mismatch is a silent crash waiting to happen. + + CHECK 3 - Backend resolution. Every CUDA / cuDNN / TensorRT / OpenVINO / + DirectML DLL imported by the core or its providers must exist in + the ep folder, the Shared root, or System32. + + It also prints a UNIFICATION summary (how many distinct core versions exist + across the folders) so you can see when you've reached one-version-everywhere. + +.PARAMETER EpRoot + The ep\ folder to verify. Default: C:\Projects\SharedCore\ep + (You may also pass the Shared root itself; the script appends \ep if needed.) + +.PARAMETER SharedRoot + The Shared folder that holds the backend DLLs (nvinfer_10.dll, openvino.dll, + cudart64_13.dll, DirectML.dll, ...). Default: parent of EpRoot. + +.PARAMETER RequiredApi + Minimum acceptable ORT C-API version. Default: 23 (ANSCORE links ORT 1.23). + +.EXAMPLE + .\verify_ep.ps1 + .\verify_ep.ps1 -EpRoot C:\ProgramData\ANSCENTER\Shared\ep + .\verify_ep.ps1 -RequiredApi 24 # after bumping ANSCORE's link SDK to 1.24 +#> +[CmdletBinding()] +param( + [string] $EpRoot = "C:\Projects\SharedCore\ep", + [string] $SharedRoot, + [int] $RequiredApi = 23 +) + +$ErrorActionPreference = "Stop" + +# --------------------------------------------------------------------------- +# Helpers (kept consistent with Check-SharedCoreVersions.ps1) +# --------------------------------------------------------------------------- +function Coalesce($value, $fallback) { if ($value) { $value } else { $fallback } } + +function Get-FileVer([string]$path) { + if (Test-Path -LiteralPath $path) { (Get-Item -LiteralPath $path).VersionInfo.FileVersion } + else { $null } +} + +function Find-Dumpbin { + $c = Get-ChildItem ` + "C:\Program Files\Microsoft Visual Studio\*\*\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe", ` + "C:\Program Files (x86)\Microsoft Visual Studio\*\*\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe" ` + -ErrorAction SilentlyContinue | Sort-Object FullName -Descending + if ($c) { return $c[0].FullName } + return $null +} + +# Derive the ORT C-API version from a file version string. ORT 1. exposes +# C-API version (1.23.* => 23, 1.24.* => 24). Returns $null if unparsable. +function Get-OrtApi([string]$fileVer) { + if (-not $fileVer) { return $null } + $parts = $fileVer.Split('.') + if ($parts.Count -lt 2) { return $null } + $minor = 0 + if ([int]::TryParse($parts[1], [ref]$minor)) { return $minor } + return $null +} + +# Lower-cased CUDA/TRT/OV/DML deps imported by $path (via dumpbin /DEPENDENTS). +function Get-BackendDeps([string]$dumpbin, [string]$path) { + if (-not $dumpbin -or -not (Test-Path -LiteralPath $path)) { return @() } + $rx = '(?i)cudart|cublas|cudnn|cufft|cusparse|cusolver|nvinfer|nvonnx|nvrtc|directml|openvino' + & $dumpbin /DEPENDENTS $path 2>$null | + Select-String -Pattern '^\s+\S+\.dll\s*$' | + ForEach-Object { $_.Line.Trim().ToLower() } | + Where-Object { $_ -match $rx } | + Sort-Object -Unique +} + +# --------------------------------------------------------------------------- +# Resolve paths +# --------------------------------------------------------------------------- +if (-not (Test-Path -LiteralPath $EpRoot)) { + Write-Host "ERROR: EpRoot not found: $EpRoot" -ForegroundColor Red; exit 2 +} +# Allow passing the Shared root instead of the ep folder. +if ((Split-Path $EpRoot -Leaf) -ine 'ep' -and (Test-Path -LiteralPath (Join-Path $EpRoot 'ep'))) { + $EpRoot = Join-Path $EpRoot 'ep' +} +if (-not $SharedRoot) { $SharedRoot = Split-Path $EpRoot -Parent } +$sys32 = Join-Path $env:WINDIR 'System32' +$dumpbin = Find-Dumpbin + +# Providers expected per EP (DML is baked into the core; CPU has none). +$epProviders = [ordered]@{ + "cpu" = @() + "cuda" = @("onnxruntime_providers_cuda.dll","onnxruntime_providers_tensorrt.dll") + "directml" = @() + "openvino" = @("onnxruntime_providers_openvino.dll") +} + +Write-Host "" +Write-Host "==================================================================" +Write-Host " verify_ep - ANSCORE ONNX Runtime acceptance gate" +Write-Host " ep root : $EpRoot" +Write-Host " shared root : $SharedRoot" +Write-Host " required API : >= $RequiredApi (ANSCORE link version)" +Write-Host " dumpbin : $(Coalesce $dumpbin 'NOT FOUND - CHECK 3 will be skipped')" +Write-Host "==================================================================" + +$overallFail = $false +$coreVersions = @{} # ep -> core file version (for unification summary) + +foreach ($ep in $epProviders.Keys) { + $epDir = Join-Path $EpRoot $ep + $core = Join-Path $epDir 'onnxruntime.dll' + Write-Host "" + Write-Host ("-- ep\{0} ------------------------------------------------------" -f $ep) + + if (-not (Test-Path -LiteralPath $core)) { + if (Test-Path -LiteralPath $epDir) { + Write-Host " SKIP: folder present but no onnxruntime.dll" -ForegroundColor DarkYellow + } else { + Write-Host " SKIP: folder absent" -ForegroundColor DarkGray + } + continue + } + + $coreVer = Get-FileVer $core + $coreVersions[$ep] = $coreVer + $epFail = $false + + # ----- CHECK 1: API floor ------------------------------------------------ + $api = Get-OrtApi $coreVer + if ($null -eq $api) { + Write-Host (" CHECK 1 API : FAIL - cannot parse version '{0}'" -f $coreVer) -ForegroundColor Red + $epFail = $true + } elseif ($api -lt $RequiredApi) { + Write-Host (" CHECK 1 API : FAIL - core {0} => API {1} < required {2}" -f $coreVer,$api,$RequiredApi) -ForegroundColor Red + $epFail = $true + } else { + Write-Host (" CHECK 1 API : PASS - core {0} => API {1} (>= {2})" -f $coreVer,$api,$RequiredApi) -ForegroundColor Green + } + + # ----- CHECK 2: provider/core version lock ------------------------------- + $providers = Get-ChildItem -LiteralPath $epDir -Filter 'onnxruntime_providers_*.dll' -ErrorAction SilentlyContinue + if (-not $providers) { + Write-Host " CHECK 2 LOCK : PASS - no provider DLLs in folder (core-only EP)" -ForegroundColor Green + } else { + $bad = @() + foreach ($p in $providers) { + $pv = $p.VersionInfo.FileVersion + if ($pv -ne $coreVer) { $bad += ("{0} ({1})" -f $p.Name, (Coalesce $pv 'no-version')) } + } + if ($bad) { + Write-Host (" CHECK 2 LOCK : FAIL - version != core {0}: {1}" -f $coreVer, ($bad -join ', ')) -ForegroundColor Red + $epFail = $true + } else { + Write-Host (" CHECK 2 LOCK : PASS - {0} provider DLL(s) all == {1}" -f $providers.Count, $coreVer) -ForegroundColor Green + } + } + + # ----- CHECK 3: backend resolution --------------------------------------- + if (-not $dumpbin) { + Write-Host " CHECK 3 DEPS : SKIP - dumpbin not available" -ForegroundColor DarkYellow + } else { + $checkFiles = @('onnxruntime.dll') + $epProviders[$ep] + $deps = @() + foreach ($cf in $checkFiles) { $deps += Get-BackendDeps $dumpbin (Join-Path $epDir $cf) } + $deps = $deps | Sort-Object -Unique + if (-not $deps) { + Write-Host " CHECK 3 DEPS : PASS - no CUDA/TRT/OV/DML imports" -ForegroundColor Green + } else { + $missing = @() + foreach ($d in $deps) { + $found = (Test-Path -LiteralPath (Join-Path $epDir $d)) -or ` + (Test-Path -LiteralPath (Join-Path $SharedRoot $d)) -or ` + (Test-Path -LiteralPath (Join-Path $sys32 $d)) + if (-not $found) { $missing += $d } + } + if ($missing) { + Write-Host (" CHECK 3 DEPS : FAIL - missing: {0}" -f ($missing -join ', ')) -ForegroundColor Red + Write-Host (" (imports: {0})" -f ($deps -join ', ')) -ForegroundColor DarkGray + $epFail = $true + } else { + Write-Host (" CHECK 3 DEPS : PASS - all {0} backend import(s) resolved" -f $deps.Count) -ForegroundColor Green + Write-Host (" {0}" -f ($deps -join ', ')) -ForegroundColor DarkGray + } + } + } + + if ($epFail) { $overallFail = $true } +} + +# --------------------------------------------------------------------------- +# Unification summary +# --------------------------------------------------------------------------- +Write-Host "" +Write-Host "-- Unification summary --------------------------------------------" +$distinct = $coreVersions.Values | Where-Object { $_ } | Sort-Object -Unique +if ($distinct.Count -eq 0) { + Write-Host " No ORT cores found." -ForegroundColor Red + $overallFail = $true +} elseif ($distinct.Count -eq 1) { + Write-Host (" UNIFIED: all EP folders on a single ORT version ({0})." -f $distinct[0]) -ForegroundColor Green +} else { + Write-Host (" {0} distinct ORT versions across folders:" -f $distinct.Count) -ForegroundColor Yellow + foreach ($ep in $coreVersions.Keys) { + Write-Host (" ep\{0,-9} {1}" -f $ep, $coreVersions[$ep]) + } + Write-Host " Safe at runtime (one EP per process) but not yet unified - see tools recommendation." -ForegroundColor Yellow +} + +# --------------------------------------------------------------------------- +# Verdict +# --------------------------------------------------------------------------- +Write-Host "" +Write-Host "==================================================================" +if ($overallFail) { + Write-Host " RESULT: FAIL - one or more EP folders did not pass." -ForegroundColor Red + Write-Host "==================================================================" + exit 1 +} else { + Write-Host " RESULT: PASS - ep\ tree is correct for ANSCORE." -ForegroundColor Green + Write-Host "==================================================================" + exit 0 +}