feat(ANSODEngine): run a sealed model on TensorRT without unsealing it

A .ansmodel reaching the GPU path went to ModelOptimizer, which knows one
way to open a model — unzip it — and failed on the first byte. Even past
that, the TensorRT engine only ever parsed an ONNX file, so the one thing
the format exists to prevent, a decrypted model on disk, was the only way
it could have run.

The builder now takes its bytes from memory when it has them. The path it
is given still names the plan that gets cached beside it, because a plan
is a built artefact and carries no model: the second run loads it and
never needs the ONNX at all. For a package the plan lands in the folder
the zip flow used, so a model converted from a zip reuses the plan that
zip already built.

OptimizeModelStr recognises a package and skips the unzip-and-optimise
leg entirely: the handle below it opens the package in memory and builds
the plan from the decrypted bytes, which is the same work by a shorter
road.

Still on the file path: the other TensorRT handlers (segmentation, pose,
classification). Only the YOLO detector reads a package today.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-13 09:38:45 +10:00
co-authored by Claude Opus 5
parent 1e369fdfef
commit cd99fcf6d9
7 changed files with 195 additions and 63 deletions
+17
View File
@@ -188,6 +188,17 @@ public:
/// Full path of the engine file resolved by the last buildLoadNetwork().
[[nodiscard]] const std::string& engineFilePath() const { return m_engineFilePath; }
// ── ONNX bytes held in memory (sealed .ansmodel packages) ───────────────
// A package is decrypted into memory and the plaintext model must never
// reach the disk, so the parser reads these bytes instead of opening
// onnxModelPath. The path is still passed in and still matters: it names
// the TensorRT plan that gets cached beside it, and a plan is a built
// artefact, not the model. Set the bytes before any build call; they are
// wiped by clearOnnxBytes() and by the destructor.
void setOnnxBytes(const void* data, size_t size);
void clearOnnxBytes();
[[nodiscard]] bool hasOnnxBytes() const { return !m_onnxBytes.empty(); }
/// Return the CUDA device index that preprocessing should target.
/// In pool mode this returns the device of the first alive slot;
/// in single-engine mode it returns m_options.deviceIndex.
@@ -244,6 +255,9 @@ public:
// retries until build succeeds or all candidates are exhausted.
// Fixed-spatial models fall through to a single build() call.
bool buildWithRetry(std::string onnxModelPath, const std::array<float, 3>& subVals, const std::array<float, 3>& divVals, bool normalize);
// Where the ONNX bytes come from: the in-memory buffer when one was set,
// otherwise the file at onnxModelPath.
bool readOnnxBytes(const std::string& onnxModelPath, std::vector<char>& buffer) const;
void getDeviceNames(std::vector<std::string>& deviceNames);
void clearGpuBuffers();
int getBindingIndexByName(const std::string& name);
@@ -401,6 +415,7 @@ private:
bool m_usingCachedEngine = false;
bool m_skipEngineCache = false; // true = bypass TRTEngineCache (for OptimizeModelStr)
bool m_skipOnnxRebuild = false; // true = don't delete/rebuild corrupt engine files (elastic growth)
std::vector<char> m_onnxBytes; // model bytes in memory (package); empty = read from disk
bool m_rebuiltFromOnnx = false; // last buildLoadNetwork built from ONNX (no matching cached engine)
std::string m_engineFilePath; // engine file resolved by last buildLoadNetwork
bool m_skipOnnxBuild = false; // true = skip ONNX→TRT build if no cached engine (demand growth)
@@ -575,6 +590,8 @@ template <typename T> Engine<T>::Engine() {
m_lastSetInputDims.clear();
}
template <typename T> Engine<T>::~Engine() {
// A decrypted model must not outlive the engine that parsed it.
try { clearOnnxBytes(); } catch (...) {}
// =========================================================================
// CRITICAL: Destructors must NEVER throw. Every CUDA/TRT call below is
// wrapped in try/catch so that a failure in one step does not skip the