feat(ODEngine): open a model package and run it without unpacking it

An engine's model has always been a path to a password-protected zip:
ANSODBase inflates it into a folder, and every engine then picks out
train_last.onnx, classes.names and model_config.json. A .ansmodel
package replaces all four with one sealed file, and this is the part
that turns it back into what the engines already understand.

ANSODBase::LoadModel and ::Initialize now recognise a .ansmodel path and
take a second route: the package is opened with the process keyring, the
model is decrypted into memory, and the classes, task and input geometry
come out of the manifest instead of loose files. Nothing is extracted,
so the unprotected model never touches the disk — which is the reason
the format exists, and it is one function's job to keep.

The engines follow it down. BasicOrtHandler gains a constructor that
takes the bytes rather than a path; it keeps a view of them, not a copy,
because the weights are in RAM either way once a session exists and a
copy would only double what every loaded model costs. The owner of the
plaintext is ANSODBase, which holds it for as long as the engine object
lives — that is what lets the DirectML recovery path rebuild a session
after a driver fault — and wipes it on teardown.

Wired through ANSONNXYOLO (and so the classification facade), ANSONNXOBB,
ANSONNXPOSE, ANSONNXSEG and YOLO12OD. YOLOOD is left alone: it loads
darknet weights and OpenCV DNN graphs, neither of which the writer
produces. A zip still loads exactly as before, on every engine.

Keys come from SetProcessModelKeys, as in ANSMINDCORE, with the same
development fallback from the environment. With none set, a package is
refused with a sentence saying so and every zip still works, so a build
made before the key ceremony runs everything except packages.

The engine layer needs CUDA to compile, so the contract it depends on is
checked where it can be: ANSModelPackage-UnitTest now opens the fixture
ANSMINDCORE wrote through the same glue ANSODBase uses, and covers the
refusal a keyless build gives. 32 checks, no failures.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 22:21:39 +10:00
co-authored by Claude Opus 5
parent 6c164c3d0f
commit 080910c8ac
13 changed files with 723 additions and 33 deletions
+50
View File
@@ -20,6 +20,7 @@
#include <chrono>
#include "ANSMOT.h"
#include "onnxruntime_cxx_api.h"
#include "ANSModelSource.h"
#define USEONNXOV
#define RETAINDETECTEDFRAMES 80
@@ -797,6 +798,55 @@ namespace ANSCENTER
void CheckLicense();
void LoadClassesFromString();
void LoadClassesFromFile();
// ────────────────────────────────────────────────────────────────
// A model that arrived as an .ansmodel package.
//
// A package replaces the whole inflated folder: the model, the
// classes and the geometry model_config.json used to carry all
// come out of it, and nothing is written to disk. LoadModelPackage
// fills these in; the engine then builds its session from
// _packageModel instead of from _modelFilePath.
//
// The bytes stay for as long as the engine object does, because
// the ONNX handler holds a view of them and rebuilds its session
// from that view when a GPU driver fault forces a recreate.
// WipeModelPackage() clears them on teardown.
// ────────────────────────────────────────────────────────────────
bool _fromPackage{ false };
std::vector<unsigned char> _packageModel;
std::string _packageTask; // detection, classification, …
std::string _packageFamily; // yolo | yolo10 | rfdetr
std::string _packageMarker; // what the zip would have carried
std::string _packageName; // what logs call this model
int _packageInputWidth{ 0 }; // 0: the package does not say
int _packageInputHeight{ 0 };
// Where this engine's session should read its model from: the
// package's bytes when one was opened, the file otherwise. Engines
// keep their own _modelFilePath, so it is passed in.
ModelSource CurrentModelSource(const std::string& filePath) const {
if (_fromPackage && !_packageModel.empty())
return ModelSource::Memory(_packageModel.data(),
_packageModel.size(), _packageName);
return ModelSource::File(filePath);
}
// Engines reset the input geometry to their own default after the
// base class has loaded, which would throw away a package's own
// (a classification model is 224, not 640). This puts it back.
void ApplyPackageInputSize() {
if (_packageInputWidth > 0) _modelConfig.inpWidth = _packageInputWidth;
if (_packageInputHeight > 0) _modelConfig.inpHeight = _packageInputHeight;
}
static bool IsModelPackagePath(const std::string& path);
// Opens one, decrypts the model into _packageModel, and fills
// _classes and _modelConfig's geometry from the manifest. Returns
// false with the reason logged.
bool LoadModelPackage(const std::string& path);
void WipeModelPackage();
std::recursive_mutex _mutex;
// Guard: true while Initialize/LoadModel is in progress on any thread.
// Inference callers can check this to fail-fast instead of blocking.