Add AMD support via ort alternative to trt

This commit is contained in:
2026-06-28 11:50:05 +02:00
parent a3ba53ddf7
commit 0ee131a692
27 changed files with 1357 additions and 977 deletions
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include <string>
// ── BackendConfig ─────────────────────────────────────────────────────────────
// Backend-neutral tuning knobs for the inference backends. Carried inside Config
// (as Config::trt) and handed to the make_face_detector / make_face_embedder
// factories. The core application sets these fields without knowing which
// backend (ONNX Runtime or TensorRT) will consume them.
//
// fp16: FP16 Tensor Core kernels — safe for both SCRFD and ArcFace.
// int8: INT8 quantisation — fast but UNSAFE for ArcFace without a
// calibration table (embedding cosine space will shift, breaking
// similarity thresholds). Safe for the SCRFD detector.
// cache_dir: TRT engines are compiled once and cached here. First run is
// slow (~3060 s per model); every subsequent run loads instantly.
//
// Shape profile (optional, set input_name + profile_{min,opt,max} to enable):
// Shape strings are trtexec-style, e.g. "1x3x112x112". When left empty the
// backend derives a sensible default from the model.
//
// ort_cache_dir: ORT optimized-model cache. On first load ORT writes a
// pre-optimized .ort file here; subsequent loads skip graph optimization.
// Empty string = disabled. Applies to all ORT providers (ROCm, CUDA, CPU).
struct BackendConfig {
bool fp16 = true;
bool int8 = false;
std::string cache_dir = "./trt_cache";
// Per-tensor optimisation profile. All four fields must be set together.
std::string input_name; // e.g. "input.1"
std::string profile_min; // e.g. "1x3x112x112"
std::string profile_opt; // e.g. "4x3x112x112"
std::string profile_max; // e.g. "8x3x112x112"
std::string ort_cache_dir = "./ort_cache";
};
+27
View File
@@ -0,0 +1,27 @@
#pragma once
#include "types.hpp"
#include <memory>
#include <vector>
// ── IFaceDetector ─────────────────────────────────────────────────────────────
// Backend-agnostic face detector interface. The core application detects faces
// through this interface without knowing whether the implementation is ONNX
// Runtime (SCRFD via ORT) or raw TensorRT (a pre-built SCRFD engine).
//
// The concrete implementation is selected at compile time by CMake
// (SAE_INFERENCE_BACKEND): exactly one of backends/ort_backend.cpp or
// backends/trt_backend.cpp is compiled and provides make_face_detector().
struct Config;
struct IFaceDetector {
virtual ~IFaceDetector() = default;
// Detect all faces in a BGR image. Thread-safety is backend-defined; callers
// in this project drive a detector from a single pipeline thread.
virtual std::vector<DetectedFace> detect(const cv::Mat& img) = 0;
};
// Construct the detector for the compiled-in backend. Reads cfg.detector_model,
// cfg.detector_engine, cfg.detector_conf, cfg.detector_nms and cfg.trt.
std::unique_ptr<IFaceDetector> make_face_detector(const Config& cfg);
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include "types.hpp"
#include <memory>
#include <vector>
// ── IFaceEmbedder ─────────────────────────────────────────────────────────────
// Backend-agnostic ArcFace embedder interface. The core application produces
// 512-d L2-normalised embeddings through this interface without knowing whether
// the implementation is ONNX Runtime or raw TensorRT.
//
// The concrete implementation is selected at compile time by CMake
// (SAE_INFERENCE_BACKEND): exactly one of backends/ort_backend.cpp or
// backends/trt_backend.cpp is compiled and provides make_face_embedder().
struct Config;
struct IFaceEmbedder {
virtual ~IFaceEmbedder() = default;
// Embed a batch of 112×112 BGR crops → one L2-normalised 512-d embedding
// each, parallel to the input.
virtual std::vector<Embedding> embed(const std::vector<cv::Mat>& crops) = 0;
// Largest batch the backend accepts in a single embed() call. TRT engines
// are capped by their build profile; ORT reports the configured batch size.
virtual int max_batch() const = 0;
Embedding embed_one(const cv::Mat& crop) { return embed({crop}).front(); }
};
// Construct the embedder for the compiled-in backend. Reads cfg.arcface_model,
// cfg.arcface_engine, cfg.embed_batch_size and cfg.trt.
std::unique_ptr<IFaceEmbedder> make_face_embedder(const Config& cfg);
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include <memory>
#include <vector>
// ── ISimilarityEngine ─────────────────────────────────────────────────────────
// Backend-agnostic gallery similarity engine for the identity matcher.
//
// The full reference gallery (n_gallery × 512 L2-normalised embeddings) is
// uploaded to the GPU once at construction and stays resident. Per frame, the
// small query matrix (n_faces × 512) is uploaded and a single SGEMM produces the
// similarity matrix S (n_gallery × n_faces, column-major) — i.e. S[g + f*n_gal]
// is cosine_similarity(gallery[g], query[f]).
//
// The GPU math backend (cuBLAS/CUDA or rocBLAS/HIP) is selected at compile time
// by CMake (SAE_GEMM_BACKEND); backends/gemm_backend.cpp provides
// make_similarity_engine(). The core matcher node sees only this interface and
// holds no CUDA/HIP/BLAS headers.
struct ISimilarityEngine {
virtual ~ISimilarityEngine() = default;
// Largest n_faces accepted by compute() per call (bounds GPU buffer sizes).
virtual int max_faces() const = 0;
// Compute similarities for n_faces query embeddings.
// query_row_major: n_faces × 512, row fi at query + fi*512.
// Returns a pointer to host memory holding S column-major: the gallery
// similarities for face fi start at result + fi*n_gallery. The pointer is
// owned by the engine and valid until the next compute() call.
virtual const float* compute(const float* query_row_major, int n_faces) = 0;
};
// gallery_row_major: n_gallery × 512, embedding i at gallery + i*512.
std::unique_ptr<ISimilarityEngine> make_similarity_engine(
const float* gallery_row_major, int n_gallery, int max_faces);