Fix AMD ALPR_OCR and custom model
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <typeinfo>
|
||||
#include <deque>
|
||||
#include <unordered_map>
|
||||
#include <chrono> // DmlInferenceGuard latency timing
|
||||
|
||||
#include "onnxruntime_cxx_api.h"
|
||||
#include "opencv2/opencv.hpp"
|
||||
@@ -337,6 +338,108 @@ namespace ANSCENTER {
|
||||
// (AMD fields go here — none yet)
|
||||
};
|
||||
|
||||
// ====================================================================
|
||||
// DmlSerialState — process-wide DirectML inference serialization
|
||||
//
|
||||
// AMD Radeon iGPUs (Radeon 680M / 780M, observed) cannot sustain
|
||||
// 2-way concurrent inference on DirectML. Under sustained
|
||||
// parallel load the WDDM driver eventually deadlocks the D3D12
|
||||
// command queue when multiple ORT sessions submit work
|
||||
// simultaneously, producing a "dual stall" pattern (both threads
|
||||
// inside ORT::Run() with no exception thrown) and ultimately an
|
||||
// access violation deep inside the DirectML EP cleanup path.
|
||||
//
|
||||
// Policy: every DirectML inference call on AMD_GPU is serialized
|
||||
// through DmlInferenceGuard's unconditional lock. Lazy "trip on
|
||||
// first slow Run()" was tried first (a flag flipped to true on
|
||||
// exception or >1500 ms latency) but field-proven inadequate —
|
||||
// the trip fires AFTER a slow Run() returns, by which point a
|
||||
// sibling thread has already entered Run() concurrently and
|
||||
// wedged. Always-serialize is the only safe answer on AMD.
|
||||
//
|
||||
// This class still tracks an "ever-tripped" flag for diagnostics
|
||||
// — the TripFromException / TripFromLatency calls log a marker
|
||||
// the first time a DML hiccup is observed, so field traces
|
||||
// clearly show whether the AMD driver had a degraded event. The
|
||||
// flag does NOT gate the lock; the lock is unconditional on AMD.
|
||||
//
|
||||
// Intel (OpenVINO) and NVIDIA (CUDA/TRT) bypass the lock entirely
|
||||
// — DmlInferenceGuard short-circuits on ep != AMD_GPU. They keep
|
||||
// peakDepth=2 parallel inference unchanged; zero behavioral
|
||||
// change on those platforms.
|
||||
//
|
||||
// Storage lives in the ONNXEngine sources compiled into
|
||||
// ANSODEngine.dll so all higher-level modules (ANSODEngine,
|
||||
// ANSOCR, ...) share a single process-global mutex.
|
||||
// ====================================================================
|
||||
class ONNXENGINE_API DmlSerialState {
|
||||
public:
|
||||
// True when serial mode has been tripped.
|
||||
static bool IsSerialMode();
|
||||
|
||||
// Trip helpers. Both are idempotent — only the first caller
|
||||
// per process actually flips the flag and emits the
|
||||
// diagnostic. Called by:
|
||||
// exception path → ANSONNXYOLO::HandleDmlRunFailure, OCR
|
||||
// latency path → DmlInferenceGuard destructor on slow Run()
|
||||
static void TripFromException(const char* context, const char* msg);
|
||||
static void TripFromLatency (const char* context, double ms);
|
||||
|
||||
// 1500 ms — comfortably above normal AMD DML latency and well
|
||||
// under WDDM TDR.
|
||||
static double SlowThresholdMs();
|
||||
|
||||
// Internal — only DmlInferenceGuard should call these.
|
||||
static void AcquireMutex();
|
||||
static void ReleaseMutex();
|
||||
};
|
||||
|
||||
// ====================================================================
|
||||
// DmlInferenceGuard — RAII guard for DirectML inference call sites
|
||||
//
|
||||
// Construct around every Ort::Session::Run() that may execute on
|
||||
// a DirectML EP. Behavior:
|
||||
//
|
||||
// 1. ep != AMD_GPU → entire guard is a no-op (zero overhead on
|
||||
// Intel / NVIDIA / CPU). The destructor's latency check and
|
||||
// lock release are also skipped via _ep == AMD_GPU and
|
||||
// _locked == false guards.
|
||||
// 2. ep == AMD_GPU → unconditionally acquires the global DML
|
||||
// mutex; only one DML inference in the entire process runs
|
||||
// at a time. On scope exit, releases the mutex and (if the
|
||||
// call exceeded DmlSerialState::SlowThresholdMs()) records a
|
||||
// diagnostic trip marker — the marker does NOT change
|
||||
// behavior, since AMD is already serial; it just shows up in
|
||||
// logs as evidence of a degraded GPU event.
|
||||
//
|
||||
// Why unconditional serialization on AMD: empirical testing on
|
||||
// Radeon 780M shows the WDDM driver wedges when 2 ORT sessions
|
||||
// submit DML inference concurrently for >~30 s. Lazy "lock only
|
||||
// after first slow call" was found insufficient — by the time the
|
||||
// trip fires, a sibling thread is already inside the wedge-prone
|
||||
// Run() with no way to back out. Always-lock is the only path
|
||||
// that guarantees the dual-stall window never opens. The cost is
|
||||
// ~30% lower throughput on AMD (depth=1 vs depth=2) for
|
||||
// unbounded uptime instead of crashing every 30 s.
|
||||
// ====================================================================
|
||||
class ONNXENGINE_API DmlInferenceGuard {
|
||||
public:
|
||||
explicit DmlInferenceGuard(EngineType ep, const char* context);
|
||||
~DmlInferenceGuard();
|
||||
|
||||
// Non-copyable / non-movable — RAII scope guard only.
|
||||
DmlInferenceGuard(const DmlInferenceGuard&) = delete;
|
||||
DmlInferenceGuard& operator=(const DmlInferenceGuard&) = delete;
|
||||
DmlInferenceGuard(DmlInferenceGuard&&) = delete;
|
||||
DmlInferenceGuard& operator=(DmlInferenceGuard&&) = delete;
|
||||
|
||||
private:
|
||||
EngineType _ep;
|
||||
const char* _context;
|
||||
bool _locked;
|
||||
std::chrono::steady_clock::time_point _t0;
|
||||
};
|
||||
|
||||
// ====================================================================
|
||||
// BasicOrtHandler
|
||||
// ====================================================================
|
||||
@@ -410,6 +513,21 @@ namespace ANSCENTER {
|
||||
// (which require the input name) BEFORE the real session is
|
||||
// created. Returns an empty string on failure.
|
||||
static std::string QueryModelInputName(const std::string& onnxPath);
|
||||
protected:
|
||||
// Tear down the existing ORT session/env/memory-info and rebuild
|
||||
// them with `newEngine` as the requested EP. Used by subclass
|
||||
// self-heal paths (e.g. ONNXOCRRecognizer / ONNXOCRDetector) when
|
||||
// an Ort::Exception fires from a Run() — the session is recreated
|
||||
// either on the same EP (transient driver hiccup recovery) or on
|
||||
// CPU (permanent fallback). Throws on failure; caller should
|
||||
// treat that as "give up for this call" and return an empty
|
||||
// result. Subclass-owned auxiliary state (input_node_dims,
|
||||
// input_node_names, ...) is repopulated by initialize_handler.
|
||||
// Caller is responsible for any subclass-specific re-init that
|
||||
// sits on top of the base session (e.g. dictionary loading on
|
||||
// ONNXOCRRecognizer is not affected because it lives in the
|
||||
// subclass and survives a session swap).
|
||||
void Reinitialize(EngineType newEngine);
|
||||
private:
|
||||
void initialize_handler();
|
||||
protected:
|
||||
|
||||
Reference in New Issue
Block a user