diff --git a/core/ANSModelPackage/ANSModelOpen.h b/core/ANSModelPackage/ANSModelOpen.h index 5ad6cb4..92fe39e 100644 --- a/core/ANSModelPackage/ANSModelOpen.h +++ b/core/ANSModelPackage/ANSModelOpen.h @@ -39,6 +39,7 @@ #include #include "ANSModelPackage.h" +#include "ANSOnnxShape.h" namespace ANSCENTER { namespace modelpkg { @@ -203,6 +204,19 @@ inline bool OpenModelPackage(const std::string& path, OpenedModel& out, out.inputHeight = in.value("height", 0); out.name = m.value("project", std::string()); if (out.name.empty()) out.name = path; + + // A package written before manifests carried an input size still has to + // answer, and the model in hand is the thing that knows. It costs nothing + // here: the bytes are already decrypted. + if (out.inputWidth <= 0 || out.inputHeight <= 0) { + onnxshape::InputShape shape; + std::string swhy; + if (onnxshape::fromMemory(out.model.data(), out.model.size(), shape, + swhy)) { + out.inputWidth = shape.width; + out.inputHeight = shape.height; + } + } return true; } diff --git a/core/ANSModelPackage/ANSOnnxShape.h b/core/ANSModelPackage/ANSOnnxShape.h new file mode 100644 index 0000000..e7d872a --- /dev/null +++ b/core/ANSModelPackage/ANSOnnxShape.h @@ -0,0 +1,241 @@ +#ifndef ANSONNXSHAPE_H +#define ANSONNXSHAPE_H +#pragma once +// ANSOnnxShape — the input geometry a model declares, read off the graph. +// +// WHY A READER NEEDS THIS. An engine has to know what its model expects before +// it can preprocess a single frame, and that answer has always come from +// whatever the bundle happened to carry: model_config.json's input_size, or an +// `input` object in a package manifest. Neither is present on every model — +// most of the bundles in the field are classes.names, a marker file and the +// export, and nothing else. The graph is always there, and the graph is what +// the session is built from, so where the metadata is silent the model itself +// is asked instead of every engine falling back to 640x640. +// +// TWO SOURCES, ONE PARSER. A package's model is already in memory, decrypted, +// and must never reach the disk; an inflated bundle's is a file that may be +// hundreds of megabytes. The template below reads either, and the file case +// SEEKS past the nodes and the initializers rather than reading them — the +// fields this wants sit after all of the weight on the wire. +// +// The path through the schema, for anyone checking this against onnx.proto: +// +// ModelProto.graph field 7, length-delimited +// GraphProto.input field 11, repeated (after node and +// initializer on the wire) +// ValueInfoProto.type field 2 +// TypeProto.tensor_type field 1 +// TypeProto.Tensor.shape field 2 +// TensorShapeProto.dim field 1, repeated +// Dimension.dim_value field 1, varint (dim_param means the +// axis is dynamic: reported as 0) +// +// An axis the graph leaves open is reported as zero, and a file that cannot be +// parsed is reported as a failure. Both mean the same thing to a caller: this +// model does not say, keep whatever default you had. Being wrong about a +// model's input size is worse than being silent about it. + +#include +#include +#include +#include +#include +#include + +namespace ANSCENTER { +namespace onnxshape { + +struct InputShape { + int channels = 0; // 0 where the graph does not fix that axis + int height = 0; + int width = 0; + bool known() const { return height > 0 && width > 0; } +}; + +namespace detail { + +// A model already in memory: the decrypted contents of a package. +struct MemSource { + const uint8_t* p = nullptr; + int64_t size = 0; + int64_t at = 0; + + int64_t tell() const { return at; } + void seek(int64_t to) { at = to < 0 ? 0 : (to > size ? size : to); } + int get() { return at < size ? int(p[at++]) : -1; } + bool ok() const { return true; } +}; + +// A model on disk, walked without being read. +struct FileSource { + std::ifstream f; + int64_t size = 0; + + int64_t tell() { return int64_t(f.tellg()); } + void seek(int64_t to) { f.clear(); f.seekg(std::streamoff(to), std::ios::beg); } + int get() { return f.get(); } + bool ok() { return bool(f); } +}; + +template +bool varint(Src& s, uint64_t& out) { + out = 0; + for (int shift = 0; shift < 64; shift += 7) { + const int c = s.get(); + if (c < 0) return false; + out |= uint64_t(c & 0x7f) << shift; + if ((c & 0x80) == 0) return true; + } + return false; +} + +template +bool tag(Src& s, uint32_t& field, uint32_t& wire) { + uint64_t key = 0; + if (!varint(s, key)) return false; + field = uint32_t(key >> 3); + wire = uint32_t(key & 0x7); + return field != 0; +} + +// Step over a field whose contents are of no interest. +template +bool skip(Src& s, uint32_t wire) { + uint64_t v = 0; + switch (wire) { + case 0: return varint(s, v); + case 1: s.seek(s.tell() + 8); return s.ok(); + case 5: s.seek(s.tell() + 4); return s.ok(); + case 2: + if (!varint(s, v)) return false; + s.seek(s.tell() + int64_t(v)); + return s.ok(); + default: return false; // groups: not used anywhere in onnx.proto + } +} + +// Find one length-delimited field inside [here, end) and stop on its payload. +template +bool seekField(Src& s, int64_t end, uint32_t want, uint64_t& len) { + while (s.ok() && s.tell() >= 0 && s.tell() < end) { + uint32_t field = 0, wire = 0; + if (!tag(s, field, wire)) return false; + if (field == want && wire == 2) return varint(s, len); + if (!skip(s, wire)) return false; + } + return false; +} + +// TensorShapeProto: the dimensions in order, a dynamic axis as 0. +template +std::vector dims(Src& s, int64_t end) { + std::vector out; + while (s.ok() && s.tell() >= 0 && s.tell() < end) { + uint32_t field = 0, wire = 0; + if (!tag(s, field, wire)) break; + if (field != 1 || wire != 2) { // only `dim` matters + if (!skip(s, wire)) break; + continue; + } + uint64_t len = 0; + if (!varint(s, len)) break; + const int64_t dimEnd = s.tell() + int64_t(len); + int64_t value = 0; // stays 0 for dim_param, i.e. a dynamic axis + while (s.ok() && s.tell() >= 0 && s.tell() < dimEnd) { + uint32_t df = 0, dw = 0; + if (!tag(s, df, dw)) break; + if (df == 1 && dw == 0) { + uint64_t v = 0; + if (!varint(s, v)) break; + value = int64_t(v); + } else if (!skip(s, dw)) { + break; + } + } + s.seek(dimEnd); + out.push_back(value); + } + return out; +} + +template +bool read(Src& s, int64_t end, InputShape& out, std::string& why) { + uint64_t len = 0; + if (!seekField(s, end, 7, len)) { // ModelProto.graph + why = "this does not look like an ONNX model"; + return false; + } + const int64_t graphEnd = s.tell() + int64_t(len); + if (!seekField(s, graphEnd, 11, len)) { // GraphProto.input + why = "the graph declares no input"; + return false; + } + const int64_t inputEnd = s.tell() + int64_t(len); + if (!seekField(s, inputEnd, 2, len)) { // ValueInfoProto.type + why = "the input declares no type"; + return false; + } + const int64_t typeEnd = s.tell() + int64_t(len); + if (!seekField(s, typeEnd, 1, len)) { // TypeProto.tensor_type + why = "the input is not a tensor"; + return false; + } + const int64_t tensorEnd = s.tell() + int64_t(len); + if (!seekField(s, tensorEnd, 2, len)) { // Tensor.shape + why = "the input tensor has no shape"; + return false; + } + const std::vector d = dims(s, s.tell() + int64_t(len)); + + // NCHW is the only layout these engines run. + if (d.size() != 4) { + why = "the input is not a four-dimensional image tensor"; + return false; + } + auto axis = [](int64_t v) { return (v > 0 && v < 1000000) ? int(v) : 0; }; + out.channels = axis(d[1]); + out.height = axis(d[2]); + out.width = axis(d[3]); + if (!out.known()) { + why = "the model takes a dynamic input size"; + return false; + } + return true; +} + +} // namespace detail + +// What a model already in memory declares. +inline bool fromMemory(const void* data, size_t size, InputShape& out, + std::string& why) { + if (!data || size == 0) { + why = "there is no model to read"; + return false; + } + detail::MemSource s{static_cast(data), int64_t(size), 0}; + return detail::read(s, int64_t(size), out, why); +} + +// What a model on disk declares. The file is walked, not loaded. +inline bool fromFile(const std::string& path, InputShape& out, + std::string& why) { + std::error_code ec; + const auto size = std::filesystem::file_size(path, ec); + if (ec || size == 0) { + why = "the model is not there to read"; + return false; + } + detail::FileSource s; + s.f.open(path, std::ios::binary); + s.size = int64_t(size); + if (!s.f) { + why = "the model could not be opened"; + return false; + } + return detail::read(s, int64_t(size), out, why); +} + +} // namespace onnxshape +} // namespace ANSCENTER + +#endif // ANSONNXSHAPE_H diff --git a/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp b/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp index eeca388..ac76b2e 100644 --- a/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp +++ b/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp @@ -18,6 +18,7 @@ #include #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(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(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);