#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