Compare commits
5
Commits
43ebd25d07
...
6344062179
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6344062179 | ||
|
|
044fedd344 | ||
|
|
005f3bc3d5 | ||
|
|
13180921c9 | ||
|
|
a4ea532f36 |
@@ -761,6 +761,10 @@ void CVideoDecoder::Start() {
|
||||
if (m_pContext) {
|
||||
avcodec_flush_buffers(m_pContext);
|
||||
}
|
||||
// Re-arm keyframe gating: after a flush the DPB is empty, so wait for the
|
||||
// next IDR before delivering frames again (avoids post-restart green/blocky).
|
||||
m_bWaitKeyframe = true;
|
||||
m_nKeyframeWaitCount = 0;
|
||||
m_bRunning = TRUE;
|
||||
log_print(HT_LOG_INFO, "%s, Video decoder started\r\n", __FUNCTION__);
|
||||
}
|
||||
@@ -1093,6 +1097,78 @@ BOOL CVideoDecoder::readFrame()
|
||||
continue;
|
||||
}
|
||||
|
||||
// Mid-stream corruption gating — re-arm the keyframe gate on decode errors.
|
||||
//
|
||||
// Packet loss on lossy links (Wi-Fi cameras) drops slices mid-GOP:
|
||||
// FFmpeg logs "First slice in a frame missing" / "Could not find ref
|
||||
// with POC n", conceals the damage, and marks the output frame via
|
||||
// AVFrame::decode_error_flags (missing refs, concealment active, ...).
|
||||
// Every subsequent frame in the GOP references the concealed one, so
|
||||
// rather than display green/blocky garbage until the next IDR, drop
|
||||
// the frame and re-arm the keyframe gate — the display holds the last
|
||||
// clean frame and resumes on the next clean keyframe.
|
||||
if (m_pFrame->decode_error_flags != 0) {
|
||||
if (!m_bWaitKeyframe.load(std::memory_order_relaxed)) {
|
||||
m_bWaitKeyframe.store(true, std::memory_order_relaxed);
|
||||
m_nKeyframeWaitCount = 0;
|
||||
ANS_DBG("MEDIA_Decode",
|
||||
"corrupt frame (decode_error_flags=0x%x) — re-arming keyframe gate",
|
||||
m_pFrame->decode_error_flags);
|
||||
} else {
|
||||
// Count corrupt drops toward the safety valve so a stream that
|
||||
// is corrupt for a long stretch eventually shows *something*
|
||||
// (concealed frames) instead of freezing indefinitely.
|
||||
++m_nKeyframeWaitCount;
|
||||
}
|
||||
if (m_nKeyframeWaitCount < 300) {
|
||||
av_frame_unref(m_pFrame);
|
||||
continue;
|
||||
}
|
||||
// Safety valve tripped: fall through and let the frame pass
|
||||
// (concealment artifacts beat a permanently frozen display).
|
||||
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
// Startup keyframe gating — discard everything until the first IDR.
|
||||
//
|
||||
// On (re)connect we join the stream mid-GOP. Until a keyframe arrives,
|
||||
// every frame references pictures we never received; the decoder (and
|
||||
// NVDEC especially) conceals them into green / blocky garbage and reports
|
||||
// success, so there is no error flag to test. Waiting for the first
|
||||
// keyframe gives a clean intra reference, after which inter frames decode
|
||||
// correctly. This is the standard RTSP "wait for IDR" behaviour.
|
||||
//
|
||||
// Keyframe flag is set from the bitstream parse and is reliable for both
|
||||
// SW and HW decode (it does not depend on the HW surface). pict_type is a
|
||||
// fallback. The counter is a safety valve: if a stream never advertises a
|
||||
// keyframe within ~300 frames (~10 s @30fps) we stop gating rather than
|
||||
// blank the display forever.
|
||||
if (m_bWaitKeyframe.load(std::memory_order_relaxed)) {
|
||||
// pict_type==I is version-independent and set for HEVC IDR/I frames
|
||||
// on both SW and HW decode. AV_FRAME_FLAG_KEY (FFmpeg 5.1+) is added
|
||||
// when available for robustness; guarded so it also builds against
|
||||
// the older vendored headers that predate that flag.
|
||||
bool isKey = (m_pFrame->pict_type == AV_PICTURE_TYPE_I);
|
||||
#if defined(AV_FRAME_FLAG_KEY)
|
||||
isKey = isKey || ((m_pFrame->flags & AV_FRAME_FLAG_KEY) != 0);
|
||||
#endif
|
||||
if (isKey) {
|
||||
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
|
||||
ANS_DBG("MEDIA_Decode",
|
||||
"keyframe acquired after %d dropped frame(s) — resuming output",
|
||||
m_nKeyframeWaitCount);
|
||||
} else if (++m_nKeyframeWaitCount >= 300) {
|
||||
m_bWaitKeyframe.store(false, std::memory_order_relaxed);
|
||||
ANS_DBG("MEDIA_Decode",
|
||||
"no keyframe after %d frames — releasing gate (safety valve)",
|
||||
m_nKeyframeWaitCount);
|
||||
} else {
|
||||
// Pre-IDR frame: drop it (would render as green/blocky garbage).
|
||||
av_frame_unref(m_pFrame);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_pFrame->format == m_hwPixFmt)
|
||||
{
|
||||
// CUDA HW accel: clone the HW frame BEFORE transfer so inference
|
||||
|
||||
@@ -165,6 +165,14 @@ private:
|
||||
std::atomic<BOOL> m_bRunning;
|
||||
BOOL m_bHardwareDecoderEnabled; // Track if hardware decoder is enabled
|
||||
bool m_bCudaHWAccel; // true when using AV_HWDEVICE_TYPE_CUDA
|
||||
// Startup keyframe gating. On connect/reconnect we join the stream mid-GOP,
|
||||
// before the first IDR, so early frames reference data we never received and
|
||||
// the decoder conceals them (green / blocky macroblocks). We drop everything
|
||||
// until the first keyframe (clean reference point), then deliver normally.
|
||||
// The frame counter is a safety valve so a stream that never advertises a
|
||||
// keyframe can't blank the display forever.
|
||||
std::atomic<bool> m_bWaitKeyframe{ true }; // true until first IDR after (re)start
|
||||
int m_nKeyframeWaitCount{ 0 }; // frames dropped while waiting
|
||||
int m_hwGpuIndex; // GPU index assigned by HWDecoderPool (-1 = legacy)
|
||||
AVFrame* m_pCudaHWFrame; // Cloned CUDA HW frame (device ptrs) for inference
|
||||
const AVCodec* m_pCodec;
|
||||
|
||||
@@ -1405,6 +1405,80 @@ cv::Mat CVideoPlayer::avframeNV12ToCvMat(const AVFrame* frame)
|
||||
m_nv12OrigWidth = width;
|
||||
m_nv12OrigHeight = height;
|
||||
|
||||
// Preferred path: libyuv NV12→RGB24 (BGR in memory, matches cv::Mat).
|
||||
// SIMD (SSSE3/AVX2) and ~3-5x faster than sws_scale's quality config.
|
||||
// Crucially it honors src_stride_uv, so the padded strides/height that
|
||||
// NVDEC and D3D11VA emit (e.g. 1080→1088 rows, aligned strides) convert
|
||||
// correctly — that padding is what cv::cvtColorTwoPlane mishandled,
|
||||
// reading the chroma plane at the wrong offset and rendering a green
|
||||
// cast with striped noise toward the bottom of the frame.
|
||||
// Coefficients are chosen from the frame's signalled colorspace/range,
|
||||
// mirroring initNV12SwsContext() (BT.709 for HD/4K, BT.601 for SD,
|
||||
// BT.2020 if tagged). The constants map studio-range YUV to full-range
|
||||
// BGR, so the result is display-ready — no manual range expansion.
|
||||
#if defined(ANSCORE_HAS_LIBYUV) && ANSCORE_HAS_LIBYUV
|
||||
{
|
||||
const bool fullRange = (frame->color_range == AVCOL_RANGE_JPEG);
|
||||
const libyuv::YuvConstants* yuvc;
|
||||
switch (frame->colorspace) {
|
||||
case AVCOL_SPC_BT470BG:
|
||||
case AVCOL_SPC_SMPTE170M:
|
||||
yuvc = fullRange ? &libyuv::kYuvJPEGConstants : &libyuv::kYuvI601Constants;
|
||||
break;
|
||||
case AVCOL_SPC_BT2020_NCL:
|
||||
case AVCOL_SPC_BT2020_CL:
|
||||
yuvc = fullRange ? &libyuv::kYuvV2020Constants : &libyuv::kYuv2020Constants;
|
||||
break;
|
||||
case AVCOL_SPC_BT709:
|
||||
yuvc = fullRange ? &libyuv::kYuvF709Constants : &libyuv::kYuvH709Constants;
|
||||
break;
|
||||
default:
|
||||
// Auto-detect by resolution (matches initNV12SwsContext()).
|
||||
if (width >= 1280 || height >= 720)
|
||||
yuvc = fullRange ? &libyuv::kYuvF709Constants : &libyuv::kYuvH709Constants;
|
||||
else
|
||||
yuvc = fullRange ? &libyuv::kYuvJPEGConstants : &libyuv::kYuvI601Constants;
|
||||
break;
|
||||
}
|
||||
cv::Mat bgrImage(height, width, CV_8UC3);
|
||||
int ret = libyuv::NV12ToRGB24Matrix(
|
||||
frame->data[0], frame->linesize[0],
|
||||
frame->data[1], frame->linesize[1],
|
||||
bgrImage.data, static_cast<int>(bgrImage.step),
|
||||
yuvc, width, height);
|
||||
if (ret == 0) {
|
||||
return bgrImage;
|
||||
}
|
||||
ANS_DBG("MEDIA_Convert",
|
||||
"libyuv NV12ToRGB24 failed (ret=%d) — falling back to sws_scale", ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Fallback path: sws_scale via the dedicated NV12→BGR24 context. Also
|
||||
// stride-safe (honors frame->linesize[]) and colorspace/range-correct;
|
||||
// used on non-libyuv builds or if libyuv ever reports an error.
|
||||
{
|
||||
// Dedicated lock (not _mutex) so close()/dtor cannot free the context
|
||||
// mid-scale, while still keeping onVideoFrame off the hot _mutex.
|
||||
std::lock_guard<std::mutex> nv12Lock(m_nv12SwsMutex);
|
||||
initNV12SwsContext(frame);
|
||||
if (m_nv12SwsCtx) {
|
||||
cv::Mat bgrImage(height, width, CV_8UC3);
|
||||
uint8_t* dst[1] = { bgrImage.data };
|
||||
int dstStride[1] = { static_cast<int>(bgrImage.step[0]) };
|
||||
int ret = sws_scale(m_nv12SwsCtx, frame->data, frame->linesize, 0,
|
||||
height, dst, dstStride);
|
||||
if (ret > 0) {
|
||||
return bgrImage;
|
||||
}
|
||||
ANS_DBG("MEDIA_Convert",
|
||||
"NV12 sws_scale failed (ret=%d) — falling back to cvtColorTwoPlane", ret);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: OpenCV two-plane conversion. Correct only when the planes
|
||||
// are unpadded (linesize == width); kept for builds/inputs where the
|
||||
// sws context could not be created.
|
||||
cv::Mat yPlane(height, width, CV_8UC1, frame->data[0], frame->linesize[0]);
|
||||
cv::Mat uvPlane(height / 2, width / 2, CV_8UC2, frame->data[1], frame->linesize[1]);
|
||||
|
||||
@@ -2200,10 +2274,15 @@ CVideoPlayer::~CVideoPlayer()
|
||||
sws_freeContext(swsCtx);
|
||||
swsCtx = nullptr;
|
||||
}
|
||||
{
|
||||
// Lock order: _mutex (held) → m_nv12SwsMutex. Protects against a
|
||||
// consumer thread still inside avframeNV12ToCvMat's sws_scale.
|
||||
std::lock_guard<std::mutex> nv12Lock(m_nv12SwsMutex);
|
||||
if (m_nv12SwsCtx != nullptr) {
|
||||
sws_freeContext(m_nv12SwsCtx);
|
||||
m_nv12SwsCtx = nullptr;
|
||||
}
|
||||
}
|
||||
if (this->_tjInstance) {
|
||||
tjDestroy(this->_tjInstance);
|
||||
this->_tjInstance = nullptr;
|
||||
|
||||
@@ -428,6 +428,13 @@ protected:
|
||||
int m_nv12LastWidth = 0, m_nv12LastHeight = 0;
|
||||
int m_nv12LastColorspace = -1;
|
||||
int m_nv12LastRange = -1;
|
||||
// Guards m_nv12SwsCtx create/use/free. The NV12→BGR conversion deliberately
|
||||
// runs WITHOUT _mutex (see avframeToCvMat, commit "Fix RTSP stall by
|
||||
// releasing _mutex during BGR conversion"), so this dedicated lock protects
|
||||
// the context against close()/dtor freeing it mid-sws_scale without
|
||||
// reintroducing contention with onVideoFrame. Lock order: _mutex → this
|
||||
// (close()/dtor hold _mutex first); avframeNV12ToCvMat takes only this lock.
|
||||
std::mutex m_nv12SwsMutex;
|
||||
|
||||
void initSwsContext(int width, int height, AVPixelFormat pixFmt, AVPixelFormat outputPixFmt = AV_PIX_FMT_YUVJ420P);
|
||||
void initNV12SwsContext(const AVFrame* frame);
|
||||
|
||||
@@ -1435,7 +1435,15 @@ trt_cache_create_context:
|
||||
// ============================================================================
|
||||
|
||||
if (m_verbose) std::cout << "\nInfo: Starting warm-up inference..." << std::endl;
|
||||
warmUp(m_verbose ? 10 : 1);
|
||||
if (!warmUp(m_verbose ? 10 : 1)) {
|
||||
// A failed warmup means this engine cannot run inference (wrong-device
|
||||
// buffers, poisoned context, ...). Report failure so pool creation
|
||||
// rejects the slot instead of silently adding a broken engine that
|
||||
// corrupts every subsequent inference dispatched to it.
|
||||
std::cout << "Error: Warm-up inference FAILED on GPU[" << m_options.deviceIndex
|
||||
<< "] -- engine rejected (loadNetwork returns false)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (m_verbose) std::cout << "Info: Warm-up complete" << std::endl;
|
||||
|
||||
} // end of trt_cache_create_context scope
|
||||
@@ -1808,6 +1816,25 @@ bool Engine<T>::build(std::string onnxModelPath, const std::array<float, 3>& sub
|
||||
// Set hardware compatibility
|
||||
config->setHardwareCompatibilityLevel(nvinfer1::HardwareCompatibilityLevel::kNONE);
|
||||
|
||||
// -- Blackwell (SM 12.x) aux-stream workaround ----------------------------
|
||||
// On sm_120 (RTX 50-series), TRT 10.15's Myelin runtime faults when a second
|
||||
// concurrent execution context runs with auto-selected aux streams:
|
||||
// "Internal Error: MyelinCheckException ... l2cm_layer_internal.cpp:174"
|
||||
// followed by CUDA error 700 (illegal memory access) poisoning the context.
|
||||
// Aux streams only provide intra-inference parallelism (irrelevant for the
|
||||
// small per-slot models we run); throughput comes from the engine pool's
|
||||
// one-context-per-slot design, which is unaffected. Force 0 aux streams on
|
||||
// Blackwell until NVIDIA fixes the Myelin multi-stream path.
|
||||
// NOTE: baked in at build time -- stale cached .engine files must be deleted
|
||||
// for this to take effect.
|
||||
#if NV_TENSORRT_MAJOR >= 10
|
||||
if (prop.major >= 12) {
|
||||
config->setMaxAuxStreams(0);
|
||||
std::cout << "Aux streams disabled on SM " << prop.major << "." << prop.minor
|
||||
<< " (Blackwell Myelin multi-stream workaround)" << std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
// -- TensorRT 10+ tactic DRAM pool ----------------------------------------
|
||||
// Separate scratch pool for kernel-selection during build. Without this,
|
||||
// tactic evaluation competes with the workspace allocation and tactics
|
||||
|
||||
@@ -606,6 +606,25 @@ bool Engine<T>::runInferenceFromPool(
|
||||
bool kickedGrowth = false;
|
||||
auto _poolAcquireStart = std::chrono::steady_clock::now();
|
||||
|
||||
// -- DEVICE AFFINITY: find which GPU the caller's inputs live on ------
|
||||
// Dispatching to a slot on the same GPU avoids the pinned-host migration
|
||||
// copy (~1 ms per 1080p frame) that runInference performs for foreign
|
||||
// inputs. Queried outside the slot mutex — it's a cheap driver lookup.
|
||||
int inputDevice = -1;
|
||||
for (const auto& batch : inputs) {
|
||||
for (const auto& img : batch) {
|
||||
if (!img.data) continue;
|
||||
cudaPointerAttributes attr{};
|
||||
if (cudaPointerGetAttributes(&attr, img.data) == cudaSuccess &&
|
||||
attr.type == cudaMemoryTypeDevice) {
|
||||
inputDevice = attr.device;
|
||||
}
|
||||
cudaGetLastError(); // clear any error the query left behind
|
||||
break; // first image is representative
|
||||
}
|
||||
if (inputDevice >= 0) break;
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_slotMutex);
|
||||
|
||||
@@ -616,6 +635,23 @@ bool Engine<T>::runInferenceFromPool(
|
||||
const size_t n = m_slots.size();
|
||||
if (n > 0) {
|
||||
const size_t start = m_nextSlotHint.load() % n;
|
||||
|
||||
// Pass A: idle slot on the inputs' own GPU (zero-copy dispatch)
|
||||
if (inputDevice >= 0) {
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
auto& s = m_slots[(start + i) % n];
|
||||
if (!s.busy && s.engine && s.deviceIndex == inputDevice) {
|
||||
s.busy = true;
|
||||
slot = &s;
|
||||
m_nextSlotHint = (start + i + 1) % n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pass B: any idle slot (round-robin) — runInference migrates
|
||||
// the inputs across GPUs when needed.
|
||||
if (!slot) {
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
auto& s = m_slots[(start + i) % n];
|
||||
if (!s.busy && s.engine) { // alive and idle
|
||||
@@ -626,6 +662,7 @@ bool Engine<T>::runInferenceFromPool(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!slot) {
|
||||
ANS_DBG("TRT_Pool", "ALL SLOTS BUSY: %zu slots, active=%d — waiting for free slot",
|
||||
|
||||
@@ -130,13 +130,31 @@ static inline cudaError_t cudaStreamSynchronize_Safe(cudaStream_t stream) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Engine<T>::warmUp(int iterations) {
|
||||
bool Engine<T>::warmUp(int iterations) {
|
||||
if (m_verbose) {
|
||||
std::cout << "\n========================================" << std::endl;
|
||||
std::cout << "Engine Warmup" << std::endl;
|
||||
std::cout << "========================================" << std::endl;
|
||||
}
|
||||
|
||||
// Ensure this thread's CUDA device matches the engine BEFORE the dummy-input
|
||||
// uploads below. warmUp() does GPU work (GpuMat uploads) ahead of
|
||||
// runInference()'s own device guard, so it must not rely on the caller
|
||||
// having left the right device current (multi-GPU pool creation touches
|
||||
// several devices on one thread).
|
||||
{
|
||||
int currentDev = -1;
|
||||
cudaGetDevice(¤tDev);
|
||||
if (currentDev != m_options.deviceIndex) {
|
||||
std::cout << "Warning [Warmup]: thread was on GPU[" << currentDev
|
||||
<< "], engine expects GPU[" << m_options.deviceIndex
|
||||
<< "] -- switching device" << std::endl;
|
||||
cudaSetDevice(m_options.deviceIndex);
|
||||
}
|
||||
}
|
||||
|
||||
bool allBatchesOk = true;
|
||||
|
||||
// Determine batch sizes to warm up
|
||||
std::vector<int> batchSizes;
|
||||
|
||||
@@ -212,7 +230,9 @@ void Engine<T>::warmUp(int iterations) {
|
||||
auto firstTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
|
||||
if (!firstSuccess) {
|
||||
if (m_verbose) std::cout << " ✗ ERROR: First iteration failed for batch=" << batchSize << std::endl;
|
||||
std::cout << " ✗ ERROR: Warmup first iteration failed for batch=" << batchSize
|
||||
<< " on GPU[" << m_options.deviceIndex << "]" << std::endl;
|
||||
allBatchesOk = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -231,7 +251,9 @@ void Engine<T>::warmUp(int iterations) {
|
||||
for (int i = 1; i < iters; ++i) {
|
||||
bool success = runInference(dummyInputs, dummyOutputs);
|
||||
if (!success) {
|
||||
if (m_verbose) std::cout << " ✗ ERROR: Iteration " << i << " failed" << std::endl;
|
||||
std::cout << " ✗ ERROR: Warmup iteration " << i << " failed for batch="
|
||||
<< batchSize << " on GPU[" << m_options.deviceIndex << "]" << std::endl;
|
||||
allBatchesOk = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -262,6 +284,7 @@ void Engine<T>::warmUp(int iterations) {
|
||||
std::cout << "Kernels pre-compiled for all batch sizes." << std::endl;
|
||||
std::cout << "========================================\n" << std::endl;
|
||||
}
|
||||
return allBatchesOk;
|
||||
}
|
||||
template <typename T>
|
||||
bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& inputs,std::vector<std::vector<std::vector<T>>>& featureVectors) {
|
||||
@@ -778,6 +801,118 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 1.5: INPUT DEVICE MIGRATION (multi-GPU pool dispatch).
|
||||
//
|
||||
// The caller prepares input GpuMats on ITS device (e.g. camera preprocessing
|
||||
// on GPU 0), but round-robin dispatch may select a slot on another GPU.
|
||||
// Kernels cannot touch cross-device memory without peer access: preprocessing
|
||||
// a device-A GpuMat with a device-B stream raises an async illegal memory
|
||||
// access that permanently poisons every CUDA context in the process
|
||||
// (sticky error 700). Detect foreign inputs and migrate them onto this
|
||||
// engine's device via a UVA copy (cudaMemcpyDefault routes over PCIe).
|
||||
std::vector<std::vector<cv::cuda::GpuMat>> migratedInputs;
|
||||
const std::vector<std::vector<cv::cuda::GpuMat>>* activeInputs = &inputs;
|
||||
{
|
||||
bool anyForeign = false;
|
||||
for (size_t i = 0; i < numInputs && !anyForeign; ++i) {
|
||||
for (const auto& img : inputs[i]) {
|
||||
if (!img.data) continue;
|
||||
cudaPointerAttributes attr{};
|
||||
if (cudaPointerGetAttributes(&attr, img.data) == cudaSuccess &&
|
||||
attr.type == cudaMemoryTypeDevice &&
|
||||
attr.device != m_options.deviceIndex) {
|
||||
anyForeign = true;
|
||||
}
|
||||
cudaGetLastError(); // clear any error the attribute query left behind
|
||||
break; // first image per tensor is representative
|
||||
}
|
||||
}
|
||||
|
||||
if (anyForeign) {
|
||||
migratedInputs.resize(numInputs);
|
||||
for (size_t i = 0; i < numInputs; ++i) {
|
||||
migratedInputs[i].reserve(inputs[i].size());
|
||||
for (const auto& src : inputs[i]) {
|
||||
cudaPointerAttributes attr{};
|
||||
const bool foreign = src.data &&
|
||||
cudaPointerGetAttributes(&attr, src.data) == cudaSuccess &&
|
||||
attr.type == cudaMemoryTypeDevice &&
|
||||
attr.device != m_options.deviceIndex;
|
||||
cudaGetLastError();
|
||||
if (!foreign) { migratedInputs[i].push_back(src); continue; }
|
||||
|
||||
// Two-hop copy through pinned host staging. A direct
|
||||
// device-to-device cudaMemcpy2D across GPUs is not supported
|
||||
// (2D copies have no peer path, and GeForce has no P2P), so:
|
||||
// hop 1: strided D2H from the foreign GPU into pinned host
|
||||
// hop 2: strided H2D onto this engine's GPU
|
||||
// Synchronous copies keep the single staging buffer safe to
|
||||
// reuse across images; ~1 ms per 1080p frame over PCIe 4.
|
||||
struct PinnedStaging { // thread-local, grow-only
|
||||
void* ptr = nullptr;
|
||||
size_t cap = 0;
|
||||
~PinnedStaging() { if (ptr) cudaFreeHost(ptr); }
|
||||
};
|
||||
thread_local PinnedStaging tl_staging;
|
||||
|
||||
const size_t rowBytes = static_cast<size_t>(src.cols) * src.elemSize();
|
||||
const size_t needed = rowBytes * static_cast<size_t>(src.rows);
|
||||
if (needed > tl_staging.cap) {
|
||||
if (tl_staging.ptr) cudaFreeHost(tl_staging.ptr);
|
||||
tl_staging.ptr = nullptr; tl_staging.cap = 0;
|
||||
if (cudaMallocHost(&tl_staging.ptr, needed) != cudaSuccess) {
|
||||
std::cout << "Error [MultiGPU]: pinned staging alloc failed ("
|
||||
<< needed << " bytes)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
tl_staging.cap = needed;
|
||||
}
|
||||
|
||||
// Hop 1: D2H. UVA lets us read the foreign pointer from this
|
||||
// device's context; if the runtime rejects it, retry with the
|
||||
// source device current.
|
||||
cudaError_t mErr = cudaMemcpy2D(
|
||||
tl_staging.ptr, rowBytes, src.data, src.step,
|
||||
rowBytes, src.rows, cudaMemcpyDeviceToHost);
|
||||
if (mErr != cudaSuccess) {
|
||||
cudaGetLastError();
|
||||
cudaSetDevice(attr.device);
|
||||
mErr = cudaMemcpy2D(
|
||||
tl_staging.ptr, rowBytes, src.data, src.step,
|
||||
rowBytes, src.rows, cudaMemcpyDeviceToHost);
|
||||
cudaSetDevice(m_options.deviceIndex);
|
||||
}
|
||||
if (mErr != cudaSuccess) {
|
||||
std::cout << "Error [MultiGPU]: migration hop1 (D2H from GPU["
|
||||
<< attr.device << "]) failed: " << cudaGetErrorString(mErr) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Hop 2: H2D onto this engine's GPU.
|
||||
cv::cuda::GpuMat dst(src.rows, src.cols, src.type());
|
||||
mErr = cudaMemcpy2D(
|
||||
dst.data, dst.step, tl_staging.ptr, rowBytes,
|
||||
rowBytes, src.rows, cudaMemcpyHostToDevice);
|
||||
if (mErr != cudaSuccess) {
|
||||
std::cout << "Error [MultiGPU]: migration hop2 (H2D to GPU["
|
||||
<< m_options.deviceIndex << "]) failed: "
|
||||
<< cudaGetErrorString(mErr) << std::endl;
|
||||
return false;
|
||||
}
|
||||
migratedInputs[i].push_back(dst);
|
||||
}
|
||||
}
|
||||
activeInputs = &migratedInputs;
|
||||
|
||||
static std::atomic<bool> s_migrationLogged{false};
|
||||
if (!s_migrationLogged.exchange(true)) {
|
||||
std::cout << "Info [MultiGPU]: inputs resided on another GPU -- migrated to GPU["
|
||||
<< m_options.deviceIndex << "] via UVA copy (logged once; recurs per"
|
||||
<< " cross-device dispatch)" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 2: Preprocess + D2D copies — all on m_inferenceStream (no null stream).
|
||||
//
|
||||
// All OpenCV CUDA ops (convertTo, subtract, divide, split) in blobFromGpuMats
|
||||
@@ -794,7 +929,7 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
|
||||
preprocessedBuffers.reserve(numInputs);
|
||||
|
||||
for (size_t i = 0; i < numInputs; ++i) {
|
||||
const auto& batchInput = inputs[i];
|
||||
const auto& batchInput = (*activeInputs)[i];
|
||||
|
||||
// Preprocess on m_inferenceStream (not the null stream).
|
||||
preprocessedBuffers.push_back(
|
||||
@@ -811,8 +946,23 @@ bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>>& i
|
||||
m_inferenceStream);
|
||||
|
||||
if (copyErr != cudaSuccess) {
|
||||
// Rich diagnostics — this fires on multi-GPU slot warmup failures
|
||||
// (Blackwell debugging). Identify WHICH precondition broke:
|
||||
// wrong current device, empty blob (failed allocation), null dest
|
||||
// buffer, or a pre-existing sticky async error.
|
||||
int curDev = -1; cudaGetDevice(&curDev);
|
||||
std::cout << "Error: Failed to copy input " << i
|
||||
<< " to inference buffer: " << cudaGetErrorString(copyErr) << std::endl;
|
||||
<< " to inference buffer: " << cudaGetErrorString(copyErr) << "\n"
|
||||
<< " [diag] engine deviceIndex = " << m_options.deviceIndex
|
||||
<< ", thread current device = " << curDev << "\n"
|
||||
<< " [diag] src blob ptr = " << static_cast<const void*>(blobMat.data)
|
||||
<< " (" << blobMat.rows << "x" << blobMat.cols
|
||||
<< ", elemSize " << blobMat.elemSize() << ")\n"
|
||||
<< " [diag] dst buffer ptr = " << m_buffers[i]
|
||||
<< ", copySize = " << copySize << " bytes\n"
|
||||
<< " [diag] inference stream = " << static_cast<const void*>(m_inferenceStream)
|
||||
<< ", pending lastError = " << cudaGetErrorString(cudaGetLastError())
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,6 +207,21 @@ std::string Engine<T>::serializeEngineOptions(const ANSCENTER::Options& options,
|
||||
+ std::to_string(CUDNN_MAJOR) + "."
|
||||
+ std::to_string(CUDNN_MINOR);
|
||||
|
||||
// -- Blackwell aux-stream workaround marker --------------------------------
|
||||
// On SM 12.x the builder forces maxAuxStreams=0 (see EngineBuildLoadNetwork.inl:
|
||||
// TRT 10.15 Myelin faults with concurrent contexts + aux streams). Tag those
|
||||
// engines so caches built *before* the workaround (with aux streams baked in)
|
||||
// miss and rebuild automatically instead of loading and crashing.
|
||||
#if NV_TENSORRT_MAJOR >= 10
|
||||
{
|
||||
cudaDeviceProp prop{};
|
||||
if (cudaGetDeviceProperties(&prop, options.deviceIndex) == cudaSuccess
|
||||
&& prop.major >= 12) {
|
||||
engineName += ".aux0";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return engineName;
|
||||
}
|
||||
|
||||
@@ -231,6 +246,32 @@ cv::cuda::GpuMat Engine<T>::blobFromGpuMats(const std::vector<cv::cuda::GpuMat>
|
||||
thread_local cv::cuda::GpuMat tl_blob;
|
||||
thread_local cv::cuda::GpuMat tl_floatImg;
|
||||
thread_local int tl_blobMaxElems = 0;
|
||||
thread_local int tl_blobDevice = -1;
|
||||
|
||||
// DEVICE AFFINITY: the cached GpuMats live on ONE device. When the same
|
||||
// thread serves engines on different GPUs (multi-GPU pool creation warms
|
||||
// probe on GPU A, then slots on GPU B), reusing a device-A blob with a
|
||||
// device-B stream causes invalid-argument launches and an async illegal
|
||||
// memory access that permanently poisons both CUDA contexts (sticky 700).
|
||||
// Detect the device switch and rebuild the cache on the current device.
|
||||
{
|
||||
int curDev = -1;
|
||||
cudaGetDevice(&curDev);
|
||||
if (curDev != tl_blobDevice) {
|
||||
if (tl_blobDevice >= 0) {
|
||||
// Free on the owning device — the async allocator frees via the
|
||||
// CURRENT device's null-stream pool, so switch, release, switch back.
|
||||
cudaSetDevice(tl_blobDevice);
|
||||
tl_blob.release();
|
||||
tl_floatImg.release();
|
||||
cudaSetDevice(curDev);
|
||||
ANS_DBG("TRT_Preproc", "blobFromGpuMats: device switch GPU[%d]->GPU[%d], preproc cache rebuilt",
|
||||
tl_blobDevice, curDev);
|
||||
}
|
||||
tl_blobMaxElems = 0;
|
||||
tl_blobDevice = curDev;
|
||||
}
|
||||
}
|
||||
|
||||
if (totalElems > tl_blobMaxElems) {
|
||||
tl_blob = cv::cuda::GpuMat(1, totalElems, CV_32FC1);
|
||||
|
||||
@@ -240,7 +240,9 @@ public:
|
||||
void clearGpuBuffers();
|
||||
int getBindingIndexByName(const std::string& name);
|
||||
void UpdateOptions(const ANSCENTER::Options& options) { m_options = options; }
|
||||
void warmUp(int iterations = 5);
|
||||
// Returns false if any warmup inference failed — callers must treat the
|
||||
// engine as unusable (a slot that fails warmup must NOT join the pool).
|
||||
bool warmUp(int iterations = 5);
|
||||
void setVerbose(bool v) { m_verbose = v; }
|
||||
void setDisableGraphs(bool v) { m_disableGraphs = v; }
|
||||
|
||||
|
||||
@@ -833,7 +833,7 @@ int RSTPTestCVClient() {
|
||||
// std::string rtspUrl = "rtsp://admin:admin123@192.168.1.23:554";//"rtsp://alex:admin123@192.168.1.11:554";//
|
||||
//std::string rtspUrl = "rtsp://anscamuser:SonTay2020@anssontay.tplinkdns.com:554/Streaming/Channels/401";// "rtsp://admin:JA!uV_mx@42.1.117.99:8093/1/1";
|
||||
|
||||
std::string rtspUrl ="rtsp://admin:AnsHome16%&@192.168.1.104:554";//"rtsp://alex:admin123@192.168.1.11:554";//112
|
||||
std::string rtspUrl ="rtsp://admin:AnsHome16%&@192.168.1.103:554";//"rtsp://alex:admin123@192.168.1.11:554";//112
|
||||
|
||||
CreateANSRTSPHandle(&rtspClient, "", "", "", rtspUrl.c_str());
|
||||
//void SetRTSPImageQuality(ANSCENTER::ANSRTSPClient** Handle, int mode)
|
||||
@@ -887,7 +887,10 @@ int RSTPTestCVClient() {
|
||||
// ✅ Check if the image is valid BEFORE accessing it
|
||||
if (!image || image->empty()) {
|
||||
ANSCV_ReleaseImage_S(&image);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
// Wait via waitKey (not sleep) so the HighGUI window keeps pumping
|
||||
// Windows messages — a raw sleep here makes the "Image" window go
|
||||
// "Not Responding" whenever the stream stalls for a few seconds.
|
||||
if (cv::waitKey(1000) == 27) break;
|
||||
continue; // Skip processing if the image is empty
|
||||
}
|
||||
|
||||
@@ -1060,7 +1063,10 @@ int FPTestCVClient() {
|
||||
// ✅ Check if the image is valid BEFORE accessing it
|
||||
if (!image || image->empty()) {
|
||||
ANSCV_ReleaseImage_S(&image);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
// Wait via waitKey (not sleep) so the HighGUI window keeps pumping
|
||||
// Windows messages — a raw sleep here makes the "Image" window go
|
||||
// "Not Responding" whenever the stream stalls for a few seconds.
|
||||
if (cv::waitKey(1000) == 27) break;
|
||||
continue; // Skip processing if the image is empty
|
||||
}
|
||||
|
||||
@@ -1549,7 +1555,10 @@ int VideoPlayerClientTest() {
|
||||
// ✅ Check if the image is valid BEFORE accessing it
|
||||
if (!image || image->empty()) {
|
||||
ANSCV_ReleaseImage_S(&image);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
// Wait via waitKey (not sleep) so the HighGUI window keeps pumping
|
||||
// Windows messages — a raw sleep here makes the "Image" window go
|
||||
// "Not Responding" whenever the stream stalls for a few seconds.
|
||||
if (cv::waitKey(1000) == 27) break;
|
||||
continue; // Skip processing if the image is empty
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
// opens OpenCV windows (cv::imshow) for the same RTSP stream.
|
||||
#include "ANSOpenCV.h"
|
||||
#include "ANSRTSP.h"
|
||||
#include "ans_imshow.hpp"
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
@@ -731,7 +731,7 @@ int ANSVISTest() {
|
||||
|
||||
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\ANSVIS_Issues\\face.mp4";
|
||||
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\Face31.mp4";
|
||||
std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
|
||||
std::string videoFilePath = "C:\\Programs\\DemoAssets\\Videos\\classroom.mp4";
|
||||
//std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\TestFR\\school1.mp4";// "C:\\ProgramData\\ANSCENTER\\Shared\\classroom.mp4";
|
||||
|
||||
const char* configFilePath = "";
|
||||
@@ -1713,15 +1713,31 @@ int SCRFDVideoRepro()
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// ── GPU visibility bisect (Blackwell Myelin l2cm debugging) ─────────────
|
||||
// Usage: ANSFR-UnitTest.exe --gpu 0 → only GPU0 visible (Test A, display GPU)
|
||||
// ANSFR-UnitTest.exe --gpu 1 → only GPU1 visible (Test B, headless GPU)
|
||||
// ANSFR-UnitTest.exe → all GPUs (default behaviour)
|
||||
// Sets CUDA_VISIBLE_DEVICES before the first CUDA call in this process, so
|
||||
// the CUDA runtime only enumerates the chosen physical GPU (it becomes
|
||||
// device 0 inside the process).
|
||||
for (int i = 1; i + 1 < argc; ++i) {
|
||||
if (std::string(argv[i]) == "--gpu") {
|
||||
_putenv_s("CUDA_VISIBLE_DEVICES", argv[i + 1]);
|
||||
std::cout << "==============================================\n"
|
||||
<< " GPU BISECT: CUDA_VISIBLE_DEVICES=" << argv[i + 1] << "\n"
|
||||
<< " (only this physical GPU is visible to CUDA)\n"
|
||||
<< "==============================================" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
//FaceDetectorTest();
|
||||
//TestFaceRecognition();
|
||||
//TestCompleteFR1();
|
||||
|
||||
//ANSVISImageTest();
|
||||
//ANSVISTest();
|
||||
ANSVISTest();
|
||||
//ANSVISTestFilePlayer();
|
||||
// ANSVISRecordingTest();
|
||||
//FRStressTest();
|
||||
@@ -1735,7 +1751,7 @@ int main()
|
||||
// TestCompleteFR();
|
||||
//TestFaceRecognition();
|
||||
//FaceRecognitionBenchmark();
|
||||
SCRFDVideoRepro();
|
||||
//SCRFDVideoRepro();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user