Skip to content

[BUG] Duplicate schema registration messages when initializing Env with all ONNX schemas and loading a session #26319

@eKevinHoang

Description

@eKevinHoang

Describe the issue

Description

When creating an Ort::Env that registers all ONNX schemas by default, followed by creating a session, hundreds of duplicate schema registration messages are printed. In debug builds, session creation may fail entirely.

Observed messages (example):

Schema error: Trying to register schema with name Add (domain: , version: 1) from file onnx/defs/math/old.cc line 2642, but it is already registered from file onnx/defs/math/old.cc line 2642
Schema error: Trying to register schema with name TreeEnsembleClassifier (domain: ai.onnx.ml version: 5) from file onnx/defs/traditionalml/defs.cc line 908, but it is already registered from file onnx/defs/traditionalml/defs.cc line 908

Investigation

  • Environment::Initialize in ONNX Runtime calls:
    • RegisterOnnxOperatorSetSchema()
    • RegisterOnnxMLOperatorSetSchema()
  • These functions register all default ONNX schemas internally and return void. They do not provide any mechanism for ONNX Runtime to check for duplicates.
  • Later, when creating a session, ONNX Runtime internally loads model-specific schemas via SchemaRegistryManager::GetSchemaAndHistory().
    • Only schemas registered through SchemaRegistryManager are tracked in the history.
    • Duplicate registrations occur, resulting in repeated schema error messages.

Impact

  • In Release builds, session creation succeeds, but hundreds of schema error messages are printed, which:
    • Pollutes logs and makes it harder to identify real issues.
    • Increases memory usage for log handling.
    • Increases memory usage for schema management, which can be significant on edge devices with limited memory.
  • In Debug builds, session creation can fail due to duplicate schema registration, making debugging difficult.

My Suggestion

  1. Defer schema registration – Consider registering schemas only when they are actually needed for the model. This would reduce duplicate schema messages, lower memory overhead (both logs and schema storage), and avoid session creation failures in debug builds.
  2. ONNX-side support – If deferring registration is not feasible in ONNX Runtime, it may be worth requesting ONNX to provide a method to check whether a schema has already been registered. This approach avoids managing schemas in two separate places (ONNX and ONNX Runtime) and prevents duplicate registration issues.

To reproduce

Reproduction steps

  1. Use ONNX 1.19.1 and ONNX Runtime 1.23.1.
  2. Initialize Ort::Env.
  3. Create a session loading any model using common operators like Add.
  4. Observe repeated schema error messages in Release build or session failure in Debug build.

The code to reproduce is:

Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "ONNX Inference");
Ort::SessionOptions session_options;
session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
Ort::Session session(env, onnx_model_path, session_options);

Urgency

This issue is moderately urgent.

Platform

Mac

OS Version

15.7.1

ONNX Runtime Installation

Built from Source

ONNX Runtime Version or Commit ID

1.19.1

ONNX Runtime API

C++

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions