Prevent OOM

This commit is contained in:
2026-06-18 05:55:31 +10:00
parent ebba70b9d9
commit b3436a61ee
44 changed files with 1797 additions and 66 deletions
+290
View File
@@ -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\<x>\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 ""
+5
View File
@@ -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%
+5
View File
@@ -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%
+77
View File
@@ -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
+131
View File
@@ -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<ver>_openvino<ver>.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 <N>
.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-<ver> 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'")
+115
View File
@@ -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.<hash>) 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_<commit>.
.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*<!DOCTYPE|^\s*<html') { throw "got HTML, not a header" }
Write-Host (" OK {0}" -f $name) -ForegroundColor Green
$ok++
} catch {
Write-Host (" FAIL {0} ({1})" -f $name, $_.Exception.Message) -ForegroundColor Red
$fail += $name
}
}
# Verify the new API version actually parsed
$apiLine = Select-String -LiteralPath (Join-Path $incDir 'onnxruntime_c_api.h') `
-Pattern 'define\s+ORT_API_VERSION\s+(\d+)' -ErrorAction SilentlyContinue | Select-Object -First 1
$api = if ($apiLine) { $apiLine.Matches[0].Groups[1].Value } else { '??' }
Write-Host (" ORT_API_VERSION in fetched headers: {0}" -f $api)
Write-Host ""
Write-Host "== Copying import libs from: $LibSource =="
$libsOk = $true
foreach ($l in @('onnxruntime.lib','onnxruntime_providers_shared.lib')) {
$src = Join-Path $LibSource $l
if (Test-Path -LiteralPath $src) {
Copy-Item -LiteralPath $src -Destination (Join-Path $libDir $l) -Force
Write-Host (" OK {0}" -f $l) -ForegroundColor Green
} else {
Write-Host (" MISSING {0} in {1}" -f $l, $LibSource) -ForegroundColor Yellow
if ($l -eq 'onnxruntime.lib') { $libsOk = $false }
}
}
Write-Host ""
if ($fail.Count -eq 0 -and $libsOk) {
Write-Host "== SDK assembled OK ==" -ForegroundColor Green
Write-Host (" $OutDir (include\ = {0} headers @ API {1}, lib\ = import libs)" -f $ok, $api)
Write-Host " Next:" -ForegroundColor Cyan
Write-Host (" .\bump_ort_sdk.ps1 -SdkPath `"$OutDir`" -Marker runtime1.24.0_openvino2025.3.0.txt -WhatIf")
exit 0
} else {
Write-Host "== INCOMPLETE ==" -ForegroundColor Red
if ($fail.Count) { Write-Host (" headers failed: {0}" -f ($fail -join ', ')) -ForegroundColor Red }
if (-not $libsOk) { Write-Host " onnxruntime.lib not found - set -LibSource to a folder that has it." -ForegroundColor Red }
exit 1
}
+6
View File
@@ -0,0 +1,6 @@
@echo off
REM Rebuild ANSCORE (Release/Ninja) with the MSVC+CUDA env set up.
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 --parallel
exit /b %errorlevel%
+243
View File
@@ -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.<minor> =>
API <minor>).
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.<minor> exposes
# C-API version <minor> (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
}