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
+84 -29
View File
@@ -11,12 +11,37 @@
namespace ANSCENTER {
// ====================================================================
// Sealed packages (.ansmodel)
//
// TensorRT still writes a plan to disk and still reads it back on the
// next run — that is what makes the second start fast, and a plan is a
// built artefact that carries no model. What must never be written is
// the ONNX itself, so the builder parses the decrypted bytes from
// memory and the path below names nothing that is ever created: it
// only tells the plan where to live and what to be called. The folder
// is the one the zip flow used, so a package converted from a zip
// reuses the plan that zip already built.
// ====================================================================
std::string ANSRTYOLO::PackagePlanBasePath(const std::string& packagePath) {
const std::string folder = GenerateOutputFolder(
packagePath, GetFileNameWithoutExtension(packagePath), false);
return CreateFilePath(folder, "train_last.onnx");
}
// ====================================================================
// ANSODBase interface — OptimizeModel
// ====================================================================
bool ANSRTYOLO::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
if (!ANSODBase::OptimizeModel(fp16, optimizedModelFolder)) return false;
if (_fromPackage) {
// The plan is built from the decrypted bytes when the engine
// loads; there is no ONNX on disk to pre-optimise. Report the
// folder the plan is cached in so the caller sees a real path.
optimizedModelFolder = GetParentFolder(_modelFilePath);
return true;
}
if (!FileExist(_modelFilePath)) {
_logger.LogFatal("ANSRTYOLO::OptimizeModel", "Raw model file path does not exist", __FILE__, __LINE__);
return false;
@@ -96,21 +121,33 @@ namespace ANSCENTER {
m_options.engineFileDir = _modelFolder;
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
}
if (_fromPackage) {
// The package already carries the classes and the geometry,
// and there is no folder to look in. All that is needed here
// is where the built plan lives.
_modelFilePath = PackagePlanBasePath(modelZipFilePath);
m_options.engineFileDir = GetParentFolder(_modelFilePath);
// The defaults above have just overwritten the input size the
// package asked for; put it back.
ApplyPackageInputSize();
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
}
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
}
}
if (this->_loadEngineOnCreation) {
@@ -119,7 +156,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire(
m_poolKey, m_options, _modelFilePath,
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
_fromPackage ? _packageModel.data() : nullptr,
_fromPackage ? _packageModel.size() : 0);
m_usingSharedPool = (m_trtEngine != nullptr);
}
if (!m_trtEngine) {
@@ -221,7 +260,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire(
m_poolKey, m_options, _modelFilePath,
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
_fromPackage ? _packageModel.data() : nullptr,
_fromPackage ? _packageModel.size() : 0);
m_usingSharedPool = (m_trtEngine != nullptr);
}
if (!m_trtEngine) {
@@ -295,21 +336,33 @@ namespace ANSCENTER {
m_options.engineFileDir = _modelFolder;
m_options.precision = (_fp16 ? Precision::FP16 : Precision::FP32);
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
}
if (_fromPackage) {
// The package already carries the classes and the geometry,
// and there is no folder to look in. All that is needed here
// is where the built plan lives.
_modelFilePath = PackagePlanBasePath(modelZipFilePath);
m_options.engineFileDir = GetParentFolder(_modelFilePath);
// The defaults above have just overwritten the input size the
// package asked for; put it back.
ApplyPackageInputSize();
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
_modelFilePath = CreateFilePath(_modelFolder, "train_last.onnx");
if (FileExist(_modelConfigFile)) {
ModelType modelType;
std::vector<int> inputShape;
_classes = ANSUtilityHelper::GetConfigFileContent(_modelConfigFile, modelType, inputShape);
if (inputShape.size() == 2) {
if (inputShape[0] > 0) _modelConfig.inpHeight = inputShape[0];
if (inputShape[1] > 0) _modelConfig.inpWidth = inputShape[1];
}
}
else {
_classFilePath = CreateFilePath(_modelFolder, "classes.names");
std::ifstream isValid(_classFilePath);
if (!isValid) LoadClassesFromString();
else LoadClassesFromFile();
}
}
labelMap.clear();
@@ -322,7 +375,9 @@ namespace ANSCENTER {
static_cast<int>(m_options.precision), m_options.maxBatchSize };
m_trtEngine = EnginePoolManager<float>::instance().acquire(
m_poolKey, m_options, _modelFilePath,
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu);
SUB_VALS, DIV_VALS, NORMALIZE, m_maxSlotsPerGpu,
_fromPackage ? _packageModel.data() : nullptr,
_fromPackage ? _packageModel.size() : 0);
m_usingSharedPool = (m_trtEngine != nullptr);
}
if (!m_trtEngine) {