#ifndef ANSONNXSHAPE_H #define ANSONNXSHAPE_H #pragma once // ANSOnnxShape — the input geometry a model declares, read off the graph. // // WHY A READER NEEDS THIS. A node has to know what its model expects before it // can preprocess a single frame, and until now that answer came from whatever // the bundle happened to carry: model_config.json's input_size in a folder, an // `input` object in a package manifest. Neither is present on every model. The // graph always is, and the graph is the thing the session is built from — so // where the metadata is silent, ask the model itself rather than assume 640. // // TWO SOURCES, ONE PARSER. A package's model is already in memory, decrypted, // and must never reach the disk; an imported folder's model 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) // GraphProto.output field 12, repeated // 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) // ModelProto.metadata_props field 14, repeated StringStringEntryProto // StringStringEntryProto.key field 1, string // StringStringEntryProto.value field 2, string // // 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 #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; } }; // What the whole graph says about itself: what goes in, and what shape comes // out — which is the difference between a head that needs decoding and one // that has already done it. struct GraphShape { InputShape input; std::vector output; // the FIRST output's dims; empty when unknown bool exportSaysEndToEnd = false; // the model's own metadata, when it says // AN END-TO-END HEAD, [1, N, 6]: rows of (x1 y1 x2 y2 score class) with the // NMS already inside the graph. Telling one apart from an ordinary YOLO head // matters more than it looks: feeding [1,N,6] to a v8 decoder yields boxes // everywhere with coordinates read as confidences — plausible-looking // garbage rather than an error. // // The trap is a two-class v8 export, whose [1, 4+nc, N] is [1, 6, 8400] — // a 6 in the shape, and not end-to-end. So the 6 must be the LAST axis, and // the axis before it must be a detection count rather than an anchor count; // an end-to-end head emits max_det rows, which is hundreds, never thousands. // When the export's own metadata says end2end, that settles it outright. bool endToEnd() const { if (exportSaysEndToEnd) return true; return output.size() == 3 && output[0] == 1 && output[2] == 6 && output[1] > 0 && output[1] <= 1000; } }; 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; } // One ValueInfoProto (a graph input or output), given a stream on its payload: // its dims, or empty when it is not a shaped tensor. template std::vector valueDims(Src& s, int64_t valueEnd) { uint64_t len = 0; if (!seekField(s, valueEnd, 2, len)) return {}; // ValueInfoProto.type const int64_t typeEnd = s.tell() + int64_t(len); if (!seekField(s, typeEnd, 1, len)) return {}; // TypeProto.tensor_type const int64_t tensorEnd = s.tell() + int64_t(len); if (!seekField(s, tensorEnd, 2, len)) return {}; // Tensor.shape return dims(s, s.tell() + int64_t(len)); } // The graph's first input and first output, and whether the export declared // itself end-to-end. `end` is the whole file; `graphStart` lets the output be // found after the input, since both hang off the same graph. template bool readGraph(Src& s, int64_t end, GraphShape& 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 graphStart = s.tell(); const int64_t graphEnd = graphStart + int64_t(len); if (!seekField(s, graphEnd, 11, len)) { // GraphProto.input why = "the graph declares no input"; return false; } const std::vector in = valueDims(s, s.tell() + int64_t(len)); // The output, from the graph's start again: it sits after the input on the // wire, but seeking from the start costs nothing and does not depend on it. s.seek(graphStart); if (seekField(s, graphEnd, 12, len)) // GraphProto.output out.output = valueDims(s, s.tell() + int64_t(len)); // The export's own word on its head, which beats any shape rule. s.seek(0); while (s.ok() && s.tell() >= 0 && s.tell() < end) { uint32_t field = 0, wire = 0; if (!tag(s, field, wire)) break; if (field != 14 || wire != 2) { // ModelProto.metadata_props if (!skip(s, wire)) break; continue; } uint64_t entryLen = 0; if (!varint(s, entryLen)) break; const int64_t entryEnd = s.tell() + int64_t(entryLen); std::string key, value; while (s.ok() && s.tell() >= 0 && s.tell() < entryEnd) { uint32_t ef = 0, ew = 0; if (!tag(s, ef, ew)) break; if ((ef == 1 || ef == 2) && ew == 2) { uint64_t n = 0; if (!varint(s, n)) break; std::string text; for (uint64_t i = 0; i < n; ++i) { const int c = s.get(); if (c < 0) break; text.push_back(char(c)); } (ef == 1 ? key : value) = text; } else if (!skip(s, ew)) { break; } } s.seek(entryEnd); if (key == "end2end") { for (char& c : value) c = char(::tolower(static_cast(c))); out.exportSaysEndToEnd = (value == "true" || value == "1"); } } // NCHW is the only layout these engines run. if (in.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.input.channels = axis(in[1]); out.input.height = axis(in[2]); out.input.width = axis(in[3]); if (!out.input.known()) { why = "the model takes a dynamic input size"; return false; } return true; } template bool read(Src& s, int64_t end, InputShape& out, std::string& why) { GraphShape g; if (!readGraph(s, end, g, why)) return false; out = g.input; 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); } // The whole graph, from a model already in memory. inline bool graphFromMemory(const void* data, size_t size, GraphShape& 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::readGraph(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); } // The whole graph, from a model on disk. Walked, not loaded. inline bool graphFromFile(const std::string& path, GraphShape& 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::readGraph(s, int64_t(size), out, why); } } // namespace onnxshape } // namespace ANSCENTER #endif // ANSONNXSHAPE_H