feat(ANSModelPackage): ask the model its input size when nothing else says
A package now carries what its model expects on the input, but every package written before that does not — and neither does the zip, which is most of what is in the field. The engine was then left to assume 640x640, which is wrong for a classification export at 224 and for anything trained at a rectangular size. The model itself always knows. ANSOnnxShape reads the first input's shape straight out of the ONNX protobuf, from memory or from a file, walking the top level and SEEKING past the nodes and the initializers rather than reading them — the fields it wants sit after all of the weight on the wire, and a 324 MB model answers in ten milliseconds. OpenModelPackage uses it only when the manifest is silent, which costs nothing there because the bytes are already decrypted in hand. An axis the graph leaves open is reported as zero and a file that cannot be parsed is a failure: both mean "this model does not say", and the caller keeps the default it had. Being wrong about an input size would be worse than being silent about it. Checked against python-onnx on all 46 models on this machine, static and dynamic, four-dimensional and not, with no disagreements. The test writes ONNX graphs byte by byte, with node and initializer ahead of the input as a real export has them, and packs one into a package whose manifest says nothing — which is the case this exists for. 40 checks, no failures. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "ANSModelOpen.h"
|
||||
#include "ANSOnnxShape.h"
|
||||
#include "ANSModelPackage.h"
|
||||
|
||||
extern "C" {
|
||||
@@ -301,6 +302,111 @@ static void testOpenForAnEngine(const fs::path& dataDir) {
|
||||
"describing a package answers without decrypting the model");
|
||||
}
|
||||
|
||||
// ── what a model says about its own input ─────────────────────────────────
|
||||
//
|
||||
// A package written before manifests carried an input size still has to
|
||||
// answer, so the model's own graph is read. These are hand-written graphs
|
||||
// with node and initializer ahead of the input, as a real export has them —
|
||||
// which also proves the reader seeks past the weights rather than reading
|
||||
// them.
|
||||
static std::string pbVarint(uint64_t v) {
|
||||
std::string out;
|
||||
do {
|
||||
uint8_t b = uint8_t(v & 0x7f);
|
||||
v >>= 7;
|
||||
if (v) b |= 0x80;
|
||||
out.push_back(char(b));
|
||||
} while (v);
|
||||
return out;
|
||||
}
|
||||
static std::string pbField(uint32_t num, const std::string& payload) {
|
||||
return pbVarint(uint64_t(num) << 3 | 2) + pbVarint(payload.size()) + payload;
|
||||
}
|
||||
static std::string pbDim(int64_t v) { // Dimension.dim_value
|
||||
return pbField(1, pbVarint(uint64_t(1) << 3 | 0) + pbVarint(uint64_t(v)));
|
||||
}
|
||||
static std::string pbDynamicDim(const std::string& name) {
|
||||
return pbField(1, pbField(2, name)); // dim_param: the axis is open
|
||||
}
|
||||
static std::string fakeOnnx(const std::string& dims) {
|
||||
const std::string tensor = pbField(1, pbField(2, dims));
|
||||
const std::string input = pbField(1, "images") + pbField(2, tensor);
|
||||
const std::string graph = pbField(1, std::string(4096, 'n')) +
|
||||
pbField(5, std::string(65536, 'w')) +
|
||||
pbField(11, input);
|
||||
return pbField(7, graph);
|
||||
}
|
||||
|
||||
static void testShapeFromTheGraph(const fs::path& dir) {
|
||||
onnxshape::InputShape shape;
|
||||
std::string why;
|
||||
|
||||
const std::string fixed = fakeOnnx(pbDim(1) + pbDim(3) + pbDim(224) + pbDim(224));
|
||||
CHECK(onnxshape::fromMemory(fixed.data(), fixed.size(), shape, why) &&
|
||||
shape.width == 224 && shape.height == 224 && shape.channels == 3,
|
||||
"a graph in memory gives the size it fixes, not a default");
|
||||
|
||||
const fs::path onFile = dir / "graph.onnx";
|
||||
{
|
||||
std::ofstream f(onFile, std::ios::binary | std::ios::trunc);
|
||||
f.write(fixed.data(), std::streamsize(fixed.size()));
|
||||
}
|
||||
onnxshape::InputShape viaFile;
|
||||
CHECK(onnxshape::fromFile(onFile.string(), viaFile, why) &&
|
||||
viaFile.width == shape.width && viaFile.height == shape.height,
|
||||
"a graph on disk reads the same as the same graph in memory");
|
||||
|
||||
const std::string dynamic = fakeOnnx(pbDynamicDim("batch") + pbDim(3) +
|
||||
pbDynamicDim("height") + pbDynamicDim("width"));
|
||||
CHECK(!onnxshape::fromMemory(dynamic.data(), dynamic.size(), shape, why) &&
|
||||
why.find("dynamic") != std::string::npos,
|
||||
"a model that takes any size says so rather than inventing one");
|
||||
const std::string rubbish(600, '\x7f');
|
||||
CHECK(!onnxshape::fromMemory(rubbish.data(), rubbish.size(), shape, why),
|
||||
"and something that is not a model is refused, not guessed at");
|
||||
|
||||
// End to end: a package whose manifest says nothing about size, opened the
|
||||
// way ANSODBase opens one.
|
||||
const Identity id = fixtureIdentity();
|
||||
modelpkg::SetProcessModelKeys(fixtureKeyring(id));
|
||||
modelpkg::PackRequest req;
|
||||
req.keyId = "ansk-v1-base";
|
||||
req.useX25519 = true;
|
||||
req.basePublic = id.basePub;
|
||||
req.nonceSeed = 21;
|
||||
req.signerId = "anssig-2026-09";
|
||||
req.signerSecret = id.signerSk;
|
||||
req.signerPublic = id.signerPk;
|
||||
req.rootKeyId = "ansroot-v1";
|
||||
req.notAfter = "2030-01-01T00:00:00Z";
|
||||
{
|
||||
Json cert{{"keyId", req.signerId},
|
||||
{"pub", bits::base64Encode(
|
||||
std::string(id.signerPk.begin(), id.signerPk.end()))},
|
||||
{"notAfter", req.notAfter}};
|
||||
const std::string cb = modelpkg::detail::certBytes(cert);
|
||||
req.rootSig.assign(64, 0);
|
||||
crypto_ed25519_sign(req.rootSig.data(), id.rootSk.data(),
|
||||
reinterpret_cast<const uint8_t*>(cb.data()), cb.size());
|
||||
}
|
||||
req.manifest = Json{{"task", "classification"},
|
||||
{"family", "yolo"},
|
||||
{"classes", Json::array({Json{{"id", 1}, {"name", "Good"}}})},
|
||||
{"models", Json::array({Json{{"id", "m0"},
|
||||
{"payload", "p0"},
|
||||
{"entry", true}}})}};
|
||||
CHECK(!req.manifest.contains("input"), "this manifest says nothing about size");
|
||||
req.models.push_back({"p0", std::vector<uint8_t>(fixed.begin(), fixed.end())});
|
||||
|
||||
const std::string path = (dir / "noinput.ansmodel").string();
|
||||
std::string err;
|
||||
CHECK(modelpkg::pack(path, req, err), ("pack: " + err).c_str());
|
||||
modelpkg::OpenedModel opened;
|
||||
CHECK(modelpkg::OpenModelPackage(path, opened, err), ("open: " + err).c_str());
|
||||
CHECK(opened.inputWidth == 224 && opened.inputHeight == 224,
|
||||
"so the engine is told 224 from the graph, not left to assume 640");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const fs::path dataDir = argc > 1 ? fs::path(argv[1]) : fs::path("data");
|
||||
const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
|
||||
@@ -313,6 +419,7 @@ int main(int argc, char** argv) {
|
||||
testInterop(dataDir);
|
||||
testRoundTripAndRefusals(tmp);
|
||||
testOpenForAnEngine(dataDir);
|
||||
testShapeFromTheGraph(tmp);
|
||||
|
||||
fs::remove_all(tmp, ec);
|
||||
std::printf("%d checks, %d failures\n", gChecks, gFailures);
|
||||
|
||||
Reference in New Issue
Block a user