원클릭으로
add-op
// Add a new ONNX operator or update an existing operator to a new opset version. Use when asked to define an operator schema, register an op, add inputs/outputs/attributes to an op, move an op to old.cc, or bump an op's opset version.
// Add a new ONNX operator or update an existing operator to a new opset version. Use when asked to define an operator schema, register an op, add inputs/outputs/attributes to an op, move an op to old.cc, or bump an op's opset version.
Add a function body definition to an ONNX operator, defining how it decomposes into simpler ops. Use when asked to make an op decomposable, add a FunctionBody, implement SetContextDependentFunctionBodyBuilder, or express an op in terms of other ONNX operators.
Add or update type and shape inference for an ONNX operator. Use when asked to implement TypeAndShapeInferenceFunction, propagate shapes, add shape inference tests, fix shape inference bugs, or handle broadcasting logic.
Read or write ONNX text format ("onnxtxt"). Use when authoring `.FunctionBody(R"ONNX(...)")` blocks, writing tests with `onnx.parser.parse_model` / `parse_graph`, using the C++ `OnnxParser`, debugging parser errors, or interpreting `Constant <value = ...>` and body-subgraph syntax.
| name | add-op |
| description | Add a new ONNX operator or update an existing operator to a new opset version. Use when asked to define an operator schema, register an op, add inputs/outputs/attributes to an op, move an op to old.cc, or bump an op's opset version. |
Follow the full procedure in docs/AddNewOp.md.
| Component | File |
|---|---|
| Schema definition | onnx/defs/<domain>/defs.cc |
| Operator set registration | onnx/defs/operator_sets.h |
| Type/shape inference | Inline in schema via .TypeAndShapeInferenceFunction(...) |
| Function body (if applicable) | Inline in schema via .FunctionBody(...) |
| Reference implementation | onnx/reference/ops/op_<lowercase_name>.py |
| Node tests | onnx/backend/test/case/node/<lowercase_name>.py |
| Shape inference tests | onnx/test/shape_inference_test.py |
| Version converter adapter (if behavior changed) | onnx/version_converter/adapters/<name>_<from>_<to>.h |
| Upgrade/downgrade tests | onnx/test/version_converter/automatic_upgrade_test.py and automatic_downgrade_test.py |
Domain subdirectories under onnx/defs/: math/, nn/, tensor/, logical/, reduction/, rnn/, sequence/, image/, text/, quantization/, controlflow/, optional/, traditionalml/, training/
ONNX_OPERATOR_SET_SCHEMA(
OperatorName,
OPSET_VERSION,
OpSchema()
.SetDoc(OperatorName_verN_doc)
.Input(0, "X", "Description", "T", OpSchema::Single, true, 1, OpSchema::Differentiable)
.Output(0, "Y", "Description", "T", OpSchema::Single, true, 1, OpSchema::Differentiable)
.Attr("attr_name", "Description", AttributeProto::FLOAT, default_value)
.TypeConstraint("T", {"tensor(float)", "tensor(double)", ...}, "Description")
.TypeAndShapeInferenceFunction(InferShapeForOperatorName)
// Function body uses ONNX text format — see the onnxtxt skill for syntax/conventions.
.FunctionBody(R"ONNX(
{
...
}
)ONNX", FUNCTION_OPSET_VERSION));
defs.cc to old.cc in the same domain directorydefs.cc with the new opset version numberonnx/defs/operator_sets.hWhen moving a schema to old.cc, avoid significant code/documentation duplication:
Define shape inference functions and function body builders as separate named functions rather than inline lambdas within ONNX_OPERATOR_SET_SCHEMA. The macro expansion makes setting breakpoints on inline lambdas unreliable in debuggers.
// PREFERRED: named function — easy to set breakpoints
static void InferShapeForMyOp(InferenceContext& ctx) {
propagateElemTypeFromInputToOutput(ctx, 0, 0);
// ...
}
ONNX_OPERATOR_SET_SCHEMA(
MyOp, 21,
OpSchema()
// ...
.TypeAndShapeInferenceFunction(InferShapeForMyOp));
For test fixtures, prefer the ONNX text format via onnx.parser / C++ OnnxParser. See the onnxtxt skill for the per-file recommendations table, body-subgraph idioms, the argument-order convention, and the unk__* materialization gotcha. (Empirical: PR #7962 cut ~58–70% of test LOC by switching.)
python onnx/defs/gen_doc.py
python onnx/backend/test/stat_coverage.py
python onnx/gen_proto.py # only if proto changed
lintrunner -a --output oneline