Files
onnx--onnx/onnx/test/cpp/schema_registration_test.cc
wehub-resource-sync 5cbd3f29e3
Fuzz / Run fuzz harnesses (${{ github.event_name == 'schedule' && 'nightly' || 'smoke' }}) (push) Has been cancelled
Create Releases / call-mac (push) Has been cancelled
Create Releases / call-linux (push) Has been cancelled
Create Releases / call-sdist (push) Has been cancelled
Create Releases / call-win (push) Has been cancelled
Create Releases / call-pyodide (push) Has been cancelled
Windows_No_Exception_CI / build (x64, 3.10) (push) Has been cancelled
Check URLs / build (push) Has been cancelled
Create Releases / Attest CI build artifacts (push) Has been cancelled
Create Releases / Check for Publish release build to pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Check for Publish release build to test.pypi (rc-candidates) (push) Has been cancelled
Create Releases / Publish release build to test.pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish release build to pypi (push) Has been cancelled
Create Releases / test source distribution (push) Has been cancelled
clang-tidy / clang-tidy (push) Has been cancelled
Lint / Validate SBOM (push) Has been cancelled
Lint / Enforce style (push) Has been cancelled
CI / Test windows-2022, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=1, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=1, onnx_ml=1, autogen=1 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=0, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
Pixi CI / Install and lint (ubuntu-24.04-arm) (push) Has been cancelled
Pixi CI / Install and lint (windows-2022) (push) Has been cancelled
Pixi CI / Xcode generator build (push) Has been cancelled
Pixi CI / Install and test (macos-latest, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, default) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, default) (push) Has been cancelled
Pixi CI / Install and test (macos-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, oldies) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
Generate and publish ONNX docs / build (push) Has been cancelled
Generate and publish ONNX docs / deploy (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:41:19 +08:00

270 lines
9.3 KiB
C++

// Copyright (c) ONNX Project Contributors
//
// SPDX-License-Identifier: Apache-2.0
#include "gtest/gtest.h"
#include "onnx/defs/operator_sets.h"
#include "onnx/defs/schema.h"
namespace ONNX_NAMESPACE {
namespace Test {
TEST(SchemaRegistrationTest, DisabledOnnxStaticRegistrationAPICall) {
#ifdef __ONNX_DISABLE_STATIC_REGISTRATION
EXPECT_TRUE(IsOnnxStaticRegistrationDisabled());
#else
EXPECT_FALSE(IsOnnxStaticRegistrationDisabled());
#endif
}
// Schema of all versions are registered by default
// Further schema manipulation expects to be error-free
TEST(SchemaRegistrationTest, RegisterAllByDefaultAndManipulateSchema) {
#ifndef __ONNX_DISABLE_STATIC_REGISTRATION
// Expects all opset registered by default
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 0);
// Should find schema for all versions
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 1));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 6));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 7));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 13));
// Clear all opset schema registration
DeregisterOnnxOperatorSetSchema();
// Should not find any opset
EXPECT_EQ(nullptr, OpSchemaRegistry::Schema("Add"));
// Register all opset versions
RegisterOnnxOperatorSetSchema();
// Should find all opset
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add"));
#endif
}
// By default ONNX registers all opset versions and selective schema loading cannot be tested
// So these tests are run only when static registration is disabled
TEST(SchemaRegistrationTest, RegisterAndDeregisterAllOpsetSchemaVersion) {
#ifdef __ONNX_DISABLE_STATIC_REGISTRATION
// Clear all opset schema registration
DeregisterOnnxOperatorSetSchema();
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), -1);
// Should not find schema for any op
EXPECT_EQ(nullptr, OpSchemaRegistry::Schema("Acos"));
EXPECT_EQ(nullptr, OpSchemaRegistry::Schema("Add"));
EXPECT_EQ(nullptr, OpSchemaRegistry::Schema("Trilu"));
// Register all opset versions
RegisterOnnxOperatorSetSchema(0);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 0);
// Should find schema for all ops. Available versions are:
// Acos-7
// Add-1,6,7,13,14
// Trilu-14
auto schema = OpSchemaRegistry::Schema("Acos", 7);
EXPECT_NE(nullptr, schema);
EXPECT_EQ(schema->SinceVersion(), 7);
schema = OpSchemaRegistry::Schema("Add", 14);
EXPECT_NE(nullptr, schema);
EXPECT_EQ(schema->SinceVersion(), 14);
schema = OpSchemaRegistry::Schema("Trilu");
EXPECT_NE(nullptr, schema);
EXPECT_EQ(schema->SinceVersion(), 14);
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 1));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 6));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 7));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 13));
// Clear all opset schema registration
DeregisterOnnxOperatorSetSchema();
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), -1);
// Should not find schema for any op
EXPECT_EQ(nullptr, OpSchemaRegistry::Schema("Acos"));
EXPECT_EQ(nullptr, OpSchemaRegistry::Schema("Add"));
EXPECT_EQ(nullptr, OpSchemaRegistry::Schema("Trilu"));
#endif
}
TEST(SchemaRegistrationTest, RegisterSpecifiedOpsetSchemaVersion) {
#ifdef __ONNX_DISABLE_STATIC_REGISTRATION
DeregisterOnnxOperatorSetSchema();
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), -1);
RegisterOnnxOperatorSetSchema(13);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 13);
auto opSchema = OpSchemaRegistry::Schema("Add");
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 13);
// Should not find opset 12
opSchema = OpSchemaRegistry::Schema("Add", 12);
EXPECT_EQ(nullptr, opSchema);
// Should not find opset 14
opSchema = OpSchemaRegistry::Schema("Trilu");
EXPECT_EQ(nullptr, opSchema);
// Acos-7 is the latest Acos before specified 13
opSchema = OpSchemaRegistry::Schema("Acos", 13);
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 7);
#endif
}
// Register opset-11, then opset-14
// Expects Reg(11, 14) == Reg(11) U Reg(14)
TEST(SchemaRegistrationTest, RegisterMultipleOpsetSchemaVersions_UpgradeVersion) {
#ifdef __ONNX_DISABLE_STATIC_REGISTRATION
DeregisterOnnxOperatorSetSchema();
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), -1);
// Register opset 11
RegisterOnnxOperatorSetSchema(11);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 11);
// Register opset 14
// Do not fail on duplicate schema registration request
RegisterOnnxOperatorSetSchema(14, false);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 14);
// Acos-7 is the latest before/at opset 11 and 14
auto opSchema = OpSchemaRegistry::Schema("Acos");
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 7);
// Add-7 is the latest before/at opset 11
// Add-14 is the latest before/at opset 14
// Should find both Add-7,14
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 7));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 14));
// Should find the max version 14
opSchema = OpSchemaRegistry::Schema("Add");
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 14);
// Should find Add-7 as the max version <=13
opSchema = OpSchemaRegistry::Schema("Add", 13);
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 7);
// Should find opset 14
opSchema = OpSchemaRegistry::Schema("Trilu");
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 14);
#endif
}
// Register opset-14, then opset-11
// Expects Reg(14, 11) == Reg(11) U Reg(14)
TEST(SchemaRegistrationTest, RegisterMultipleOpsetSchemaVersions_DowngradeVersion) {
#ifdef __ONNX_DISABLE_STATIC_REGISTRATION
DeregisterOnnxOperatorSetSchema();
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), -1);
// Register opset 14
RegisterOnnxOperatorSetSchema(14);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 14);
// Register opset 11
// Do not fail on duplicate schema registration request
RegisterOnnxOperatorSetSchema(11, false);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 11);
// Acos-7 is the latest before/at opset 11 and 14
auto opSchema = OpSchemaRegistry::Schema("Acos");
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 7);
// Add-7 is the latest before/at opset 11
// Add-14 is the latest before/at opset 14
// Should find both Add-7,14
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 7));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 14));
// Should find the max version 14
opSchema = OpSchemaRegistry::Schema("Add");
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 14);
// Should find Add-7 as the max version <=13
opSchema = OpSchemaRegistry::Schema("Add", 13);
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 7);
// Should find opset 14
opSchema = OpSchemaRegistry::Schema("Trilu");
EXPECT_NE(nullptr, opSchema);
EXPECT_EQ(opSchema->SinceVersion(), 14);
#endif
}
// Register opset-11, then all versions
// Expects no error
TEST(SchemaRegistrationTest, RegisterSpecificThenAllVersion) {
#ifdef __ONNX_DISABLE_STATIC_REGISTRATION
DeregisterOnnxOperatorSetSchema();
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), -1);
// Register opset 11
RegisterOnnxOperatorSetSchema(11);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 11);
// Register all opset versions
// Do not fail on duplicate schema registration request
RegisterOnnxOperatorSetSchema(0, false);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 0);
// Should find schema for all ops
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Acos"));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add"));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Trilu"));
// Should find schema for all versions
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 1));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 6));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 7));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 13));
#endif
}
// Register all versions, then opset 11
// Expects no error
TEST(SchemaRegistrationTest, RegisterAllThenSpecificVersion) {
#ifdef __ONNX_DISABLE_STATIC_REGISTRATION
DeregisterOnnxOperatorSetSchema();
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), -1);
// Register all opset versions
RegisterOnnxOperatorSetSchema(0);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 0);
// Register opset 11
// Do not fail on duplicate schema registration request
RegisterOnnxOperatorSetSchema(11, false);
EXPECT_EQ(OpSchemaRegistry::Instance()->GetLoadedSchemaVersion(), 11);
// Should find schema for all ops
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Acos"));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add"));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Trilu"));
// Should find schema for all versions
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 1));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 6));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 7));
EXPECT_NE(nullptr, OpSchemaRegistry::Schema("Add", 13));
#endif
}
} // namespace Test
} // namespace ONNX_NAMESPACE