Implementation:Onnx Onnx Data Propagators
| Knowledge Sources | |
|---|---|
| Domains | Shape Inference, Data Propagation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for propagating concrete shape and dimension values during ONNX shape inference provided by the ONNX library.
Description
The onnx/defs/data_propagators.h header provides data propagation utilities used during ONNX shape inference. These functions enable operators to propagate known concrete dimension values through the computation graph, going beyond symbolic shape inference to track actual numeric values when available.
The header defines four inline functions:
appendDimToTensorShapeProto(tsp, input_data, index) appends a dimension from an input shape proto at a given index to a target TensorShapeProto. It handles negative indices (wrapping from the end) and validates that the index is within the valid range [-rank, rank-1].
axisIsZero(ctx, defaultZero) checks whether the "axis" attribute of the current operator is zero. It examines the "axis" attribute from the DataPropagationContext. For negative axis values, it resolves them using the input tensor's rank. If the attribute is missing and defaultZero is true, it returns true; otherwise it raises an inference error. This is critical because data propagation from Shape operators is only supported along axis 0.
PropagateShapeDataFromInputToOutput(ctx, idx) copies known shape data from a specified input directly to output 0. If the input has known dimension data, it creates a copy and adds it as output data via the DataPropagationContext.
GatherOp13DataPropagator(ctx) is a specialized propagator for the Gather operator (opset 13+). It handles the case where axis is 0, both the input data and indices are known, and it constructs the output shape by indexing into the input shape data using the known indices. Each index dimension value is used to select the corresponding dimension from the input data.
Usage
Use these propagators when implementing data propagation for ONNX operators that produce shape-related outputs. They are primarily used with operators like Shape, Gather, and others that produce or manipulate dimension values. Register them as data propagation functions in operator schema definitions.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/defs/data_propagators.h
Signature
namespace ONNX_NAMESPACE {
inline void appendDimToTensorShapeProto(
TensorShapeProto& tsp,
const TensorShapeProto* input_data,
int index);
inline bool axisIsZero(
DataPropagationContext& ctx,
bool defaultZero = false);
inline void PropagateShapeDataFromInputToOutput(
DataPropagationContext& ctx,
int idx);
inline void GatherOp13DataPropagator(
DataPropagationContext& ctx);
} // namespace ONNX_NAMESPACE
Import
#include "onnx/defs/data_propagators.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ctx | DataPropagationContext& | Yes | Context providing access to input data, input types, attributes, and output data registration |
| tsp | TensorShapeProto& | Yes (for appendDimToTensorShapeProto) | Target shape proto to append dimension to |
| input_data | const TensorShapeProto* | Yes (for appendDimToTensorShapeProto) | Source shape proto to extract dimension from |
| index | int | Yes (for appendDimToTensorShapeProto) | Index of the dimension to extract (supports negative indexing) |
| idx | int | Yes (for PropagateShapeDataFromInputToOutput) | Input index to propagate from |
| defaultZero | bool | No | If true, treat missing axis attribute as 0 (defaults to false) |
Outputs
| Name | Type | Description |
|---|---|---|
| (side effect) | void | Functions modify the DataPropagationContext by adding output data via ctx.addOutputData() |
| axisIsZero result | bool | Returns true if the resolved axis is 0, false otherwise |
Usage Examples
#include "onnx/defs/data_propagators.h"
using namespace ONNX_NAMESPACE;
// Register data propagation for an Identity-like operator
ONNX_OPERATOR_SET_SCHEMA(
MyIdentity, 1,
OpSchema()
.SetDataPropagationFunction([](DataPropagationContext& ctx) {
PropagateShapeDataFromInputToOutput(ctx, 0);
})
);
// Register data propagation for the Gather operator
ONNX_OPERATOR_SET_SCHEMA(
Gather, 13,
OpSchema()
.SetDataPropagationFunction(GatherOp13DataPropagator)
);
// Custom data propagation that checks axis
void MyDataPropagator(DataPropagationContext& ctx) {
if (!axisIsZero(ctx, /*defaultZero=*/true)) {
return; // only propagate for axis=0
}
PropagateShapeDataFromInputToOutput(ctx, 0);
}