Implementation:Onnx Onnx Domain Constants
| Knowledge Sources | |
|---|---|
| Domains | Constants, ONNX Specification |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for defining ONNX domain, dimension denotation, and type denotation constants provided by the ONNX library.
Description
The onnx/common/constants.h header centralizes all string constants defined by the ONNX specification, organized into three categories.
Domain constants define the namespaces for ONNX operators:
- ONNX_DOMAIN (empty string "") - the default ONNX operator domain
- AI_ONNX_DOMAIN ("ai.onnx") - the explicit form of the default domain (equivalent to ONNX_DOMAIN in protobuf)
- AI_ONNX_ML_DOMAIN ("ai.onnx.ml") - the ONNX ML operator domain
- AI_ONNX_TRAINING_DOMAIN ("ai.onnx.training") - the training operator domain
- AI_ONNX_PREVIEW_TRAINING_DOMAIN ("ai.onnx.preview.training") - preview training operators
Two utility functions are provided: NormalizeDomain() converts "ai.onnx" to the empty string (since both are equivalent in protobuf), and IsOnnxDomain() checks if a domain string refers to the main ONNX domain.
Dimension denotation constants provide semantic labels for tensor dimensions: DATA_BATCH, DATA_CHANNEL, DATA_TIME, DATA_FEATURE, FILTER_IN_CHANNEL, FILTER_OUT_CHANNEL, and FILTER_SPATIAL.
Type denotation constants provide semantic labels for data types: TENSOR, IMAGE, AUDIO, and TEXT.
The header also defines OPTIONAL_VALUE (false) used for marking optional values in operator definitions.
Usage
Use these constants throughout the ONNX codebase whenever referencing domain names, dimension denotations, or type denotations. Always use the named constants rather than string literals to ensure consistency and to benefit from compile-time checking. Use NormalizeDomain() when comparing domain strings that may use either the empty-string or "ai.onnx" form.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/common/constants.h
Signature
namespace ONNX_NAMESPACE {
// ONNX domains
constexpr const char* AI_ONNX_ML_DOMAIN = "ai.onnx.ml";
constexpr const char* AI_ONNX_TRAINING_DOMAIN = "ai.onnx.training";
constexpr const char* AI_ONNX_PREVIEW_TRAINING_DOMAIN = "ai.onnx.preview.training";
constexpr const char* ONNX_DOMAIN = "";
constexpr const char* AI_ONNX_DOMAIN = "ai.onnx";
inline std::string NormalizeDomain(const std::string& domain);
inline bool IsOnnxDomain(const std::string& domain);
constexpr bool OPTIONAL_VALUE = false;
// Dimension denotations
constexpr const char* DATA_BATCH = "DATA_BATCH";
constexpr const char* DATA_CHANNEL = "DATA_CHANNEL";
constexpr const char* DATA_TIME = "DATA_TIME";
constexpr const char* DATA_FEATURE = "DATA_FEATURE";
constexpr const char* FILTER_IN_CHANNEL = "FILTER_IN_CHANNEL";
constexpr const char* FILTER_OUT_CHANNEL = "FILTER_OUT_CHANNEL";
constexpr const char* FILTER_SPATIAL = "FILTER_SPATIAL";
// Type denotations
constexpr const char* TENSOR = "TENSOR";
constexpr const char* IMAGE = "IMAGE";
constexpr const char* AUDIO = "AUDIO";
constexpr const char* TEXT = "TEXT";
} // namespace ONNX_NAMESPACE
Import
#include "onnx/common/constants.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| domain | const std::string& | Yes | A domain string to normalize or check (for NormalizeDomain and IsOnnxDomain) |
Outputs
| Name | Type | Description |
|---|---|---|
| NormalizeDomain result | std::string | Returns empty string if input is "ai.onnx", otherwise returns the input unchanged |
| IsOnnxDomain result | bool | Returns true if the domain is "" or "ai.onnx" |
Usage Examples
#include "onnx/common/constants.h"
using namespace ONNX_NAMESPACE;
// Normalize domain names for comparison
std::string d1 = NormalizeDomain("ai.onnx"); // returns ""
std::string d2 = NormalizeDomain(""); // returns ""
std::string d3 = NormalizeDomain("ai.onnx.ml"); // returns "ai.onnx.ml"
// Check if a domain is the default ONNX domain
bool is_onnx_1 = IsOnnxDomain(""); // true
bool is_onnx_2 = IsOnnxDomain("ai.onnx"); // true
bool is_onnx_3 = IsOnnxDomain("ai.onnx.ml"); // false
// Use domain constants in operator registration
if (node.domain() == ONNX_DOMAIN || node.domain() == AI_ONNX_DOMAIN) {
// handle default ONNX operators
}