mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-23 19:32:23 +00:00
Add opset and IR check when loading model (#2413)
* Add opset and IR check. * Add test case for future opsets. https://github.com/microsoft/onnxruntime/issues/2371
This commit is contained in:
parent
b0cb0ef6e5
commit
6203a03cd9
5 changed files with 38 additions and 5 deletions
|
|
@ -91,6 +91,10 @@ Model::Model(std::unique_ptr<ModelProto> model_proto, const IOnnxRuntimeOpSchema
|
|||
" specifies which version of the ONNX OperatorSet is being imported.");
|
||||
}
|
||||
|
||||
if (!model_proto->has_ir_version() || model_proto->ir_version() > ONNX_NAMESPACE::Version::IR_VERSION) {
|
||||
throw std::invalid_argument("Unknown model file format version.");
|
||||
}
|
||||
|
||||
model_proto_ = std::move(model_proto);
|
||||
for (auto& prop : model_proto_->metadata_props()) {
|
||||
model_metadata_[prop.key()] = prop.value();
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ using IOnnxRuntimeOpSchemaRegistryList = std::list<std::shared_ptr<IOnnxRuntimeO
|
|||
class Model {
|
||||
public:
|
||||
static constexpr Version kNoVersion = INT64_MAX;
|
||||
|
||||
|
||||
// Construct model from scratch.
|
||||
explicit Model(const std::string& graph_name,
|
||||
bool is_onnx_domain_only = false,
|
||||
|
|
|
|||
|
|
@ -194,6 +194,23 @@ DomainToVersionMap SchemaRegistryManager::GetLatestOpsetVersions(bool is_onnx_on
|
|||
return domain_version_map;
|
||||
}
|
||||
|
||||
static bool IsDomainVersionBeyondSupportedRange(
|
||||
const std::string& domain,
|
||||
const int op_set_version) {
|
||||
// check the ONNX schema registry
|
||||
auto& onnx_domain_version_map =
|
||||
ONNX_NAMESPACE::OpSchemaRegistry::DomainToVersionRange::Instance().Map();
|
||||
|
||||
auto it = onnx_domain_version_map.find(domain);
|
||||
if (it != onnx_domain_version_map.end() && op_set_version > it->second.second) {
|
||||
// The domain is beyond what is registered.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Either all ONNX domains were within range, or the domains were not ONNX.
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return the schema with biggest version, which is not greater than specified
|
||||
// <op_set_version> in specified domain. The value of earliest_opset_where_unchanged
|
||||
// is also set to the earliest version preceding op_set_version where the operator
|
||||
|
|
@ -238,10 +255,14 @@ void SchemaRegistryManager::GetSchemaAndHistory(
|
|||
checked_registry_indices.push_back(index);
|
||||
}
|
||||
|
||||
// if not found in registered custom schema registry, search in ONNX schema registry
|
||||
*latest_schema = ONNX_NAMESPACE::OpSchemaRegistry::Schema(key, version, domain);
|
||||
if (*latest_schema != nullptr) {
|
||||
*earliest_opset_where_unchanged = (*latest_schema)->SinceVersion();
|
||||
// Reject versions greater than what is actually supported.
|
||||
*latest_schema = nullptr;
|
||||
if (!IsDomainVersionBeyondSupportedRange(domain, version)) {
|
||||
// if not found in registered custom schema registry, search in ONNX schema registry
|
||||
*latest_schema = ONNX_NAMESPACE::OpSchemaRegistry::Schema(key, version, domain);
|
||||
if (*latest_schema != nullptr) {
|
||||
*earliest_opset_where_unchanged = (*latest_schema)->SinceVersion();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -72,6 +72,14 @@ TEST(ONNXModelsTest, non_existing_model) {
|
|||
#endif
|
||||
}
|
||||
|
||||
TEST(ONNXModelsTest, future_opset) {
|
||||
// NOTE: this requires the current directory to be where onnxruntime_ir_UT.exe is located
|
||||
std::shared_ptr<Model> model;
|
||||
common::Status st = Model::Load("./testdata/add_opset_314159.onnx", model);
|
||||
ASSERT_FALSE(st.IsOK());
|
||||
ASSERT_EQ(st.Code(), common::INVALID_GRAPH);
|
||||
}
|
||||
|
||||
#ifdef ORT_RUN_EXTERNAL_ONNX_TESTS
|
||||
TEST(ONNXModelsTest1, bvlc_alexnet_1) {
|
||||
using ::google::protobuf::io::CodedInputStream;
|
||||
|
|
|
|||
BIN
onnxruntime/test/testdata/add_opset_314159.onnx
vendored
Normal file
BIN
onnxruntime/test/testdata/add_opset_314159.onnx
vendored
Normal file
Binary file not shown.
Loading…
Reference in a new issue