2026-09-12 22:21:39 +10:00
|
|
|
#ifndef ANSMODELOPEN_H
|
|
|
|
|
#define ANSMODELOPEN_H
|
|
|
|
|
#pragma once
|
|
|
|
|
// ANSModelOpen — the one place that knows how a model package reaches an engine.
|
|
|
|
|
//
|
|
|
|
|
// An engine's "model" has always been a path: a password-protected zip that
|
|
|
|
|
// ANSODBase inflates into a folder, after which the engine picks out
|
|
|
|
|
// train_last.onnx, classes.names and model_config.json. A .ansmodel package
|
|
|
|
|
// replaces all four with one sealed file, and this header is what turns it
|
|
|
|
|
// back into the things the engines already understand — the model's bytes, the
|
|
|
|
|
// class list, the task and the input size.
|
|
|
|
|
//
|
|
|
|
|
// THE PLAINTEXT NEVER REACHES THE DISK. That is the whole reason the format
|
|
|
|
|
// exists. OpenModelPackage decrypts into a buffer the caller owns; the caller
|
|
|
|
|
// hands that buffer to a session and wipes it. Nothing here writes a file.
|
|
|
|
|
//
|
2026-09-12 23:05:00 +10:00
|
|
|
// WHERE THE KEY COMES FROM. Three places, in this order: what the build ships
|
|
|
|
|
// with (ANSModelKeys.h, filled in by the key ceremony), what the environment
|
|
|
|
|
// offers a developer, and whatever a caller sets with SetProcessModelKeys().
|
|
|
|
|
// Nothing here reads a key out of the package or off the disk. With no keys at
|
|
|
|
|
// all, a package is refused with a sentence that says so, and every existing
|
|
|
|
|
// zip still loads — a build made without the ceremony runs everything except
|
|
|
|
|
// packages.
|
2026-09-12 22:21:39 +10:00
|
|
|
//
|
|
|
|
|
// The base private key must never sit in the binary as one contiguous
|
|
|
|
|
// constant; assembling it is the job of whatever calls SetProcessModelKeys,
|
|
|
|
|
// not of this header, which is why this header has no key material in it.
|
|
|
|
|
//
|
|
|
|
|
// The development fallback (ANSCORE_MODEL_KEY_ID / ANSCORE_MODEL_BASE_PRIVATE
|
|
|
|
|
// / ANSCORE_MODEL_ROOT_ID / ANSCORE_MODEL_ROOT_PUBLIC, all base64) exists so
|
|
|
|
|
// the path can be exercised before that ceremony exists. It is read once and
|
|
|
|
|
// announced, because a key that came from the environment is not the key a
|
|
|
|
|
// shipped product should be using.
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2026-09-12 23:05:00 +10:00
|
|
|
#include "ANSModelKeys.h"
|
2026-09-12 22:21:39 +10:00
|
|
|
#include "ANSModelPackage.h"
|
2026-09-12 22:47:47 +10:00
|
|
|
#include "ANSOnnxShape.h"
|
2026-09-12 22:21:39 +10:00
|
|
|
|
|
|
|
|
namespace ANSCENTER {
|
|
|
|
|
namespace modelpkg {
|
|
|
|
|
|
|
|
|
|
// ── the keys this process opens packages with ─────────────────────────────
|
|
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
|
|
inline std::mutex& keysMutex() {
|
|
|
|
|
static std::mutex m;
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Keyring& keyringStorage() {
|
|
|
|
|
static Keyring ring;
|
|
|
|
|
return ring;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool keyFromB64(const char* b64, Key& out) {
|
|
|
|
|
if (!b64 || !*b64) return false;
|
|
|
|
|
std::vector<uint8_t> raw;
|
|
|
|
|
if (!bits::base64Decode(b64, raw) || raw.size() != out.size()) return false;
|
|
|
|
|
std::memcpy(out.data(), raw.data(), out.size());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 23:05:00 +10:00
|
|
|
// True once this process's keyring is settled — either because the build's own
|
|
|
|
|
// keys were installed, or because a caller replaced them outright. A caller
|
|
|
|
|
// that sets keys means exactly that: what it set is what this process holds.
|
|
|
|
|
inline bool& keysSettled() {
|
|
|
|
|
static bool settled = false;
|
|
|
|
|
return settled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Development only: keys from the environment, and said out loud.
|
|
|
|
|
inline void loadEnvKeys(Keyring& ring) {
|
2026-09-12 22:21:39 +10:00
|
|
|
const char* id = std::getenv("ANSCORE_MODEL_KEY_ID");
|
|
|
|
|
const char* priv = std::getenv("ANSCORE_MODEL_BASE_PRIVATE");
|
|
|
|
|
Key k{};
|
|
|
|
|
if (id && *id && keyFromB64(priv, k)) {
|
|
|
|
|
ring.addX25519(id, k);
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"[modelpkg] base key %s taken from the environment - for "
|
|
|
|
|
"development, not for a shipped build\n",
|
|
|
|
|
id);
|
|
|
|
|
}
|
|
|
|
|
const char* rootId = std::getenv("ANSCORE_MODEL_ROOT_ID");
|
|
|
|
|
const char* rootPub = std::getenv("ANSCORE_MODEL_ROOT_PUBLIC");
|
|
|
|
|
Key r{};
|
|
|
|
|
if (rootId && *rootId && keyFromB64(rootPub, r)) {
|
|
|
|
|
ring.addRoot(rootId, r);
|
|
|
|
|
std::fprintf(stderr,
|
|
|
|
|
"[modelpkg] root key %s taken from the environment\n",
|
|
|
|
|
rootId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
|
|
inline void SetProcessModelKeys(Keyring ring) {
|
|
|
|
|
std::lock_guard<std::mutex> lk(detail::keysMutex());
|
|
|
|
|
detail::keyringStorage() = std::move(ring);
|
2026-09-12 23:05:00 +10:00
|
|
|
detail::keysSettled() = true;
|
2026-09-12 22:21:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Keyring ProcessModelKeys() {
|
|
|
|
|
std::lock_guard<std::mutex> lk(detail::keysMutex());
|
|
|
|
|
Keyring& ring = detail::keyringStorage();
|
2026-09-12 23:05:00 +10:00
|
|
|
if (!detail::keysSettled()) {
|
|
|
|
|
detail::keysSettled() = true;
|
|
|
|
|
AddBuiltInKeys(ring); // what this build was given at the ceremony
|
|
|
|
|
detail::loadEnvKeys(ring); // and what a developer's environment adds
|
|
|
|
|
}
|
2026-09-12 22:21:39 +10:00
|
|
|
return ring; // by value: the caller may open on another thread
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// True once this build can open anything at all. A product that has not had
|
|
|
|
|
// keys installed should say "this build cannot open model packages" rather
|
|
|
|
|
// than let every load fail one at a time.
|
|
|
|
|
inline bool HaveProcessModelKeys() {
|
|
|
|
|
const Keyring ring = ProcessModelKeys();
|
|
|
|
|
return !ring.x25519.empty() || !ring.keys.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── recognising one ───────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
inline bool IsPackagePath(const std::string& path) {
|
|
|
|
|
return path.size() > 9 &&
|
|
|
|
|
path.compare(path.size() - 9, 9, ".ansmodel") == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── what an engine needs out of one ───────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
// Everything the inflated folder used to carry, in the shape the engines read
|
|
|
|
|
// it: the model itself, the classes in order, and the geometry that
|
|
|
|
|
// model_config.json held.
|
|
|
|
|
struct OpenedModel {
|
|
|
|
|
std::vector<uint8_t> model; // decrypted; wipe it once loaded
|
|
|
|
|
std::vector<std::string> classes;
|
|
|
|
|
std::string task; // detection | classification | segmentation | obb | pose
|
|
|
|
|
std::string family; // yolo | yolo10 | rfdetr
|
|
|
|
|
std::string runtime; // onnx | openvino-ir
|
|
|
|
|
std::string legacyMarker; // what the zip would have carried
|
|
|
|
|
std::string name; // what to call this model in a log
|
|
|
|
|
int inputWidth = 0; // 0 when the package does not say
|
|
|
|
|
int inputHeight = 0;
|
|
|
|
|
std::string warning; // show it; the model still loaded
|
|
|
|
|
|
|
|
|
|
void wipe() {
|
|
|
|
|
if (!model.empty()) std::memset(model.data(), 0, model.size());
|
|
|
|
|
model.clear();
|
|
|
|
|
model.shrink_to_fit();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Read what a package says about itself without decrypting the model. Cheap
|
|
|
|
|
// enough to call on a folder full of packages.
|
|
|
|
|
inline bool DescribeModelPackage(const std::string& path, OpenedModel& out,
|
|
|
|
|
std::string& err) {
|
|
|
|
|
Package pkg;
|
|
|
|
|
if (!open(path, ProcessModelKeys(), pkg, err)) return false;
|
|
|
|
|
const Json& m = pkg.manifest;
|
|
|
|
|
out.task = m.value("task", std::string("detection"));
|
|
|
|
|
out.family = m.value("family", std::string());
|
|
|
|
|
out.runtime = m.value("runtime", std::string("onnx"));
|
|
|
|
|
out.legacyMarker =
|
|
|
|
|
m.value("legacy", Json::object()).value("marker", std::string());
|
|
|
|
|
out.warning = pkg.warning;
|
|
|
|
|
out.classes.clear();
|
|
|
|
|
for (const auto& c : m.value("classes", Json::array()))
|
|
|
|
|
out.classes.push_back(c.value("name", std::string()));
|
|
|
|
|
const Json in = m.value("input", Json::object());
|
|
|
|
|
out.inputWidth = in.value("width", 0);
|
|
|
|
|
out.inputHeight = in.value("height", 0);
|
|
|
|
|
out.name = m.value("project", std::string());
|
|
|
|
|
if (out.name.empty()) out.name = path;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open one all the way: the description above, plus the model's bytes.
|
|
|
|
|
//
|
|
|
|
|
// The caller owns those bytes and should wipe them (OpenedModel::wipe) the
|
|
|
|
|
// moment the session has copied what it needs.
|
|
|
|
|
inline bool OpenModelPackage(const std::string& path, OpenedModel& out,
|
|
|
|
|
std::string& err) {
|
|
|
|
|
if (!HaveProcessModelKeys()) {
|
|
|
|
|
err = "this build has no model-package key, so it cannot open " + path;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Package pkg;
|
|
|
|
|
if (!open(path, ProcessModelKeys(), pkg, err)) return false;
|
|
|
|
|
|
|
|
|
|
const std::string entry = pkg.entryPayloadId();
|
|
|
|
|
if (entry.empty()) {
|
|
|
|
|
err = "the package names no model to run";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!readPayload(pkg, entry, out.model, err)) return false;
|
|
|
|
|
|
|
|
|
|
const Json& m = pkg.manifest;
|
|
|
|
|
out.task = m.value("task", std::string("detection"));
|
|
|
|
|
out.family = m.value("family", std::string());
|
|
|
|
|
out.runtime = m.value("runtime", std::string("onnx"));
|
|
|
|
|
out.legacyMarker =
|
|
|
|
|
m.value("legacy", Json::object()).value("marker", std::string());
|
|
|
|
|
out.warning = pkg.warning;
|
|
|
|
|
out.classes.clear();
|
|
|
|
|
for (const auto& c : m.value("classes", Json::array()))
|
|
|
|
|
out.classes.push_back(c.value("name", std::string()));
|
|
|
|
|
const Json in = m.value("input", Json::object());
|
|
|
|
|
out.inputWidth = in.value("width", 0);
|
|
|
|
|
out.inputHeight = in.value("height", 0);
|
|
|
|
|
out.name = m.value("project", std::string());
|
|
|
|
|
if (out.name.empty()) out.name = path;
|
2026-09-12 22:47:47 +10:00
|
|
|
|
2026-09-13 00:19:15 +10:00
|
|
|
// WHAT AN OLDER PACKAGE CANNOT TELL US, and its model can. The input
|
|
|
|
|
// size, when the manifest predates the field; and the head, because a
|
|
|
|
|
// package whose family is "yolo" may hold an end-to-end export — the box
|
|
|
|
|
// used to take the family from the backend marker, and the marker names
|
|
|
|
|
// the runtime rather than the head. It costs nothing to ask here: the
|
|
|
|
|
// bytes are already decrypted.
|
|
|
|
|
{
|
|
|
|
|
onnxshape::GraphShape g;
|
2026-09-12 22:47:47 +10:00
|
|
|
std::string swhy;
|
2026-09-13 00:19:15 +10:00
|
|
|
if (onnxshape::graphFromMemory(out.model.data(), out.model.size(), g,
|
|
|
|
|
swhy)) {
|
|
|
|
|
if (out.inputWidth <= 0 || out.inputHeight <= 0) {
|
|
|
|
|
out.inputWidth = g.input.width;
|
|
|
|
|
out.inputHeight = g.input.height;
|
|
|
|
|
}
|
|
|
|
|
if (g.endToEnd() && out.family == "yolo") {
|
|
|
|
|
out.family = "yolo10";
|
|
|
|
|
out.warning = out.warning.empty()
|
|
|
|
|
? "this package says family yolo, but its head is "
|
|
|
|
|
"end-to-end; reading it as yolo10"
|
|
|
|
|
: out.warning;
|
|
|
|
|
}
|
2026-09-12 22:47:47 +10:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-12 22:21:39 +10:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace modelpkg
|
|
|
|
|
} // namespace ANSCENTER
|
|
|
|
|
|
|
|
|
|
#endif // ANSMODELOPEN_H
|