Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 18, 2025

Fix IsOnnxStaticRegistrationDisabled() inline function breaking schema registration in external modules

Issue Summary:
The IsOnnxStaticRegistrationDisabled() function was defined as an inline function in onnx/defs/operator_sets.h with behavior based on compile-time macro __ONNX_DISABLE_STATIC_REGISTRATION. This breaks encapsulation when external projects (like ONNX Runtime) use prebuilt ONNX libraries, as the function's return value depends on the caller's compile-time macro rather than ONNX's actual build configuration.

Root Cause:
Inline functions with preprocessor-dependent behavior are expanded at the call site, causing the function to use the caller's compile-time configuration instead of the library's build configuration.

Solution:

  • Analyzed the issue and understood the codebase
  • Removed inline function from operator_sets.h and replaced with declaration
  • Added implementation in schema.cc that reads the actual build-time configuration
  • Added comprehensive documentation explaining usage and responsibilities
  • Ran linter to verify code quality (passed)
  • Verified build compatibility

Changes Made:

  1. onnx/defs/operator_sets.h: Changed inline function to declaration with comprehensive documentation
  2. onnx/defs/schema.cc: Added function implementation

Documentation Added:
Added detailed comments explaining:

  • What the function returns (true if static registration is disabled)
  • Linking module's responsibilities when static registration is disabled (must call registration methods)
  • What happens when static registration is enabled (automatic registration during initialization)
  • List of all registration methods that may need to be called:
    • RegisterOnnxOperatorSetSchema() for ai.onnx domain
    • RegisterOnnxMLOperatorSetSchema() for ai.onnx.ml domain
    • RegisterOnnxTrainingOperatorSetSchema() for ai.onnx.training domain
    • RegisterOnnxPreviewOperatorSetSchema() for ai.onnx.preview domain

Testing Results:

  • ✅ Linter: No issues found
  • ✅ Previously tested: All 95 tests pass
  • ✅ Specific Test: SchemaRegistrationTest.DisabledOnnxStaticRegistrationAPICall validates correct behavior

Impact:

  • Ensures IsOnnxStaticRegistrationDisabled() always reflects ONNX library's actual build configuration
  • Fixes encapsulation for external projects (like ONNX Runtime) using ONNX as prebuilt library
  • Prevents inconsistent schema registration behavior
  • Clear documentation helps external module developers understand their responsibilities
  • No breaking changes to API
  • Maintains full backward compatibility

Fixes #7399

Original prompt

This section details on the original issue you should resolve

<issue_title>[CodeDesignBug] Inline IsOnnxStaticRegistrationDisabled() breaks schema registration in external modules</issue_title>
<issue_description># Bug Report

Is the issue related to model conversion?

No, this issue is not related to model conversion. It is a design problem in ONNX core related to how static registration is exposed through the function IsOnnxStaticRegistrationDisabled().

Describe the bug

IsOnnxStaticRegistrationDisabled() is defined as an inline function in onnx/defs/operator_sets.h:

inline bool IsOnnxStaticRegistrationDisabled() {
#ifdef __ONNX_DISABLE_STATIC_REGISTRATION
  return true;
#else
  return false;
#endif
}

This design causes a packaging and consistency problem because its behavior depends on the compile-time macro __ONNX_DISABLE_STATIC_REGISTRATION. If another module (e.g., ONNX Runtime or a user application) calls this function, it must define the same macro at compile time to get a consistent result.
Otherwise, this breaks encapsulation and makes it difficult to use ONNX as a prebuilt library or in mixed build environments.

System information

ONNX 1.19.1

Reproduction instructions

  1. Build ONNX with __ONNX_DISABLE_STATIC_REGISTRATION defined.
  2. In a separate project (e.g., ONNX Runtime or your own application), include ONNX headers without defining this macro.
  3. Call bool disabled = IsOnnxStaticRegistrationDisabled();
  4. Observe that the return value does not reflect the actual ONNX build configuration.
    This can cause inconsistent behavior, such as conditional schema registration being executed incorrectly.

Expected behavior

IsOnnxStaticRegistrationDisabled() should always reflect the actual build-time configuration of the ONNX library, regardless of the caller’s compile-time macros.

My suggestion

To achieve this, the function should not be inlined in the header.
Instead:

  • Only declare the prototype in the header.
  • Implement it in a .cc file (e.g., schema.cc) where it can access the real build-time flag or internal variable.
    This ensures consistent behavior across all binaries linking against ONNX, improving encapsulation and packaging reliability.
  • Since this function is only used for schema registration, there is no need for it to be inlined.

Notes

ONNX Runtime needs to call

RegisterOnnxOperatorSetSchema()
RegisterOnnxMLOperatorSetSchema()
RegisterOnnxTrainingOperatorSetSchema()

These calls are necessary only when ONNX static registration is disabled.
If static registration is already enabled, invoking them again will cause duplicate schema registration errors.

Since this function’s return value depends on the compile-time macro __ONNX_DISABLE_STATIC_REGISTRATION, external projects (like ONNX Runtime) must manually ensure that their build flags match ONNX’s internal configuration — which breaks encapsulation and can easily lead to inconsistent behavior.

Additionally, when static registration is enabled, some registration functions such as RegisterOnnxTrainingOperatorSetSchema() and RegisterOnnxPreviewOperatorSetSchema() are always called inside SchemasRegisterer(). It allocates memory for all training/preview schemas, even if some of them are not used by the model. This may lead to unnecessary memory usage, particularly on memory-constrained devices.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #7399


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Fix IsOnnxStaticRegistrationDisabled function for proper schema registration Fix IsOnnxStaticRegistrationDisabled() inline function breaking schema registration in external modules Oct 18, 2025
Copilot AI requested a review from justinchuby October 18, 2025 04:01
Copilot finished work on behalf of justinchuby October 18, 2025 04:01
@justinchuby justinchuby marked this pull request as ready for review October 19, 2025 14:06
@justinchuby justinchuby requested review from a team as code owners October 19, 2025 14:06
Signed-off-by: Justin Chu <[email protected]>
@codecov
Copy link

codecov bot commented Oct 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.48%. Comparing base (f72aa57) to head (49203a0).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7409   +/-   ##
=======================================
  Coverage   54.48%   54.48%           
=======================================
  Files         512      512           
  Lines       31929    31929           
  Branches     2868     2868           
=======================================
  Hits        17397    17397           
  Misses      13749    13749           
  Partials      783      783           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@justinchuby
Copy link
Member

@eKevinHoang does this look ok to you? Thanks

@justinchuby justinchuby enabled auto-merge (squash) October 24, 2025 13:19
@justinchuby justinchuby disabled auto-merge October 24, 2025 13:19
@justinchuby justinchuby enabled auto-merge (squash) October 24, 2025 13:22
@justinchuby
Copy link
Member

@gramalingam for approval. Thanks

@gramalingam
Copy link
Contributor

@copilot can you address the feedback/comment?

Clarify the responsibilities of the linking module when static registration
is disabled vs enabled, as requested in code review feedback.

Co-authored-by: gramalingam <[email protected]>
auto-merge was automatically disabled October 24, 2025 20:56

Head branch was pushed to by a user without write access

Copilot AI requested a review from gramalingam October 24, 2025 20:57
Copilot finished work on behalf of gramalingam October 24, 2025 20:57
@github-project-automation github-project-automation bot moved this from In progress to Reviewer approved in PR Tracker Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewer approved

Development

Successfully merging this pull request may close these issues.

[CodeDesignBug] Inline IsOnnxStaticRegistrationDisabled() breaks schema registration in external modules

6 participants