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:
@@ -39,6 +39,7 @@
|
||||
#include <vector>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 <cstdint>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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 <class Src>
|
||||
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 <class Src>
|
||||
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 <class Src>
|
||||
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 <class Src>
|
||||
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 <class Src>
|
||||
std::vector<int64_t> dims(Src& s, int64_t end) {
|
||||
std::vector<int64_t> 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 <class Src>
|
||||
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<int64_t> 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<const uint8_t*>(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
|
||||
Reference in New Issue
Block a user