shape_sequence.h#
Shape-inference functions for ONNX operators in the sequence family.
-
namespace onnx_light
Alias that makes onnx-light headers compatible with code that references
ONNX_LIGHT_NAMESPACE(the macro used in the standard onnx package).Set to
ONNX_LIGHT_NAMESPACEso both names resolve to the same namespace.Symbol-visibility attribute for the public onnx-light C++ API.
Defined as empty because onnx-light does not require explicit
__declspec(dllexport)or__attribute__((visibility("default")))annotations — visibility is controlled at the shared-library level. The macro is provided so that vendored ONNX headers that decorate their declarations withONNX_APIcompile without modification.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_light(via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs fromonnx.-
namespace onnx_optim
-
namespace shapes
-
namespace sequence#
Functions
-
void ComputeShapeSequenceConstruct(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimSequenceof aSequenceConstructnode and stores it inctx.SequenceConstruct(since opset 11 in theai.onnxdomain) takesN >= 1tensor inputs that share the same element type and produces a single tensor-sequence output of lengthN. The element dtype of the output sequence is the common dtype of the inputs; the ONNX schema does not require the inputs to share a common shape, so the output :cpp:class:OptimSequencerecords one :cpp:class:OptimShapeper input verbatim (see :cpp:func:OptimSequence::ElemShapes).When called with zero inputs, the output sequence has length
0, an unknown element dtype (:cpp:enumerator:TensorType::kUndefined) and an empty per-element shapes vector.- Parameters:
ctx – In/out context. Must already contain an :cpp:class:
OptimTensorentry for every named input ofnode; on return it also contains an :cpp:class:OptimSequenceentry fornode.output(0).node – The
SequenceConstructNodeProtowhose output should be described.node.op_type()must be"SequenceConstruct"andnodemust declare at least one output.
- Throws:
-
void ComputeShapeConcatFromSequence(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aConcatFromSequencenode and stores it inctx.ConcatFromSequence(since opset 11 in theai.onnxdomain) takes a single tensor-sequence input and produces a single tensor output by concatenating (whennew_axis == 0, the default) or stacking (whennew_axis == 1) the input tensors alongaxis. The element type of the output is the element type of the input sequence.The per-element shapes of the input sequence must all share the same rank
r. Whennew_axis == 0,axisranges in[-r, r - 1]and the output has rankr; theaxisdimension is the sum of the per-element dimensions alongaxis(or symbolic when any per-element dimension alongaxisis symbolic), and every other dimension is merged across elements (concrete values win over symbolic, mismatched concrete values throw). Whennew_axis == 1,axisranges in[-r - 1, r]and the output has rankr + 1; the new dimension ataxisis the sequence length and every other dimension is merged across elements.When the per-element shapes of the input sequence are unknown (:cpp:func:
OptimSequence::HasElemShapesisfalse), only the element dtype is recorded on the output and the shape is left empty.- Parameters:
ctx – In/out context. Must already contain an :cpp:class:
OptimSequenceentry fornode.input(0); on return it also contains an :cpp:class:OptimTensorentry fornode.output(0).node – The
ConcatFromSequenceNodeProtowhose output should be described.
- Throws:
std::invalid_argument – if
node.op_type()is not"ConcatFromSequence", ifnodehas no input or no output, ifnew_axisis not0or1, ifaxisis out of range, or if the per-element shapes of the input sequence have inconsistent ranks or conflicting concrete dimensions on a non-concat axis.std::out_of_range – if the named input sequence is missing from
ctx.
-
void ComputeShapeSequenceLength(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aSequenceLengthnode and stores it inctx.SequenceLengthtakes one sequence input and produces one scalar INT64 tensor output. The output shape is always empty (rank 0).- Parameters:
ctx – In/out context. Must already contain an :cpp:class:
OptimSequenceentry fornode.input(0); on return it also contains an :cpp:class:OptimTensorentry fornode.output(0).node – The
SequenceLengthNodeProtowhose output should be described.
- Throws:
-
void ComputeShapeSequenceEmpty(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimSequenceof aSequenceEmptynode and stores it inctx.SequenceEmpty(since opset 11 in theai.onnxdomain) takes no inputs and produces an empty sequence whose element dtype is taken from the optionaldtypeattribute (an INT-valuedonnx::TensorProto::DataType). Whendtypeis absent the schema default isFLOAT. The output sequence length is always0and the per-element shapes vector is empty.- Parameters:
ctx – In/out context. On return it contains an :cpp:class:
OptimSequenceentry fornode.output(0).node – The
SequenceEmptyNodeProtowhose output should be described.node.op_type()must be"SequenceEmpty"andnodemust declare at least one output.
- Throws:
std::invalid_argument – if
node.op_type()is not"SequenceEmpty", ifnodehas no output, or if thedtypeattribute is present but is not an INT.
-
void ComputeShapeSequenceErase(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimSequenceof aSequenceErasenode and stores it inctx.SequenceErase(since opset 11 in theai.onnxdomain) takes a sequence input and an optional scalar position input, and produces a sequence output with one element removed. The element dtype of the output sequence matches the input sequence. When the input sequence has known per-element shapes, the output sequence records the shapes of all elements except the one at the erased position. Because the position is a runtime value the erased index is generally unknown at shape-inference time, so the output per-element shapes are dropped and only the element dtype is forwarded together with a symbolic sequence length.- Parameters:
ctx – In/out context. Must already contain an :cpp:class:
OptimSequenceentry fornode.input(0); on return it also contains an :cpp:class:OptimSequenceentry fornode.output(0).node – The
SequenceEraseNodeProtowhose output should be described.node.op_type()must be"SequenceErase"andnodemust declare at least one output.
- Throws:
-
void ComputeShapeSequenceAt(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aSequenceAtnode and stores it inctx.SequenceAt(since opset 11 in theai.onnxdomain) takes a sequence input and a required scalar position input, and produces a tensor output equal to the sequence element at the given position. The output element dtype matches the input sequence element dtype. Because the position is a runtime value, the output shape is generally unknown; the only exception is when the input sequence records per-element shapes and all of them are equal, in which case the shared shape is forwarded as the output shape.- Parameters:
ctx – In/out context. Must already contain an :cpp:class:
OptimSequenceentry fornode.input(0); on return it also contains an :cpp:class:OptimTensorentry fornode.output(0).node – The
SequenceAtNodeProtowhose output should be described.node.op_type()must be"SequenceAt"andnodemust declare at least one output.
- Throws:
-
void ComputeShapeSequenceInsert(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimSequenceof aSequenceInsertnode and stores it inctx.SequenceInsert(since opset 11 in theai.onnxdomain) takes a sequence input, a tensor to insert, and an optional scalar position input. The output sequence element dtype matches the input sequence element dtype (or the inserted tensor dtype when the input sequence dtype is unknown) and the output length is the input length plus one. Because the insertion position is generally a runtime value, per-element output shapes are not inferred.- Parameters:
ctx – In/out context. Must already contain an :cpp:class:
OptimSequenceentry fornode.input(0)and an :cpp:class:OptimTensorentry fornode.input(1); on return it also contains an :cpp:class:OptimSequenceentry fornode.output(0).node – The
SequenceInsertNodeProtowhose output should be described.node.op_type()must be"SequenceInsert"andnodemust declare at least one output.
- Throws:
-
void ComputeShapeSequenceMap(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimSequence(s) of aSequenceMapnode and stores them inctx.SequenceMap(since opset 17 in theai.onnxdomain) takes a required sequence input (input_sequence) and zero or more additional tensor or sequence inputs, plus a requiredbodygraph attribute. The body subgraph is applied to each element ofinput_sequencetogether with the additional inputs and producesM >= 1output tensors per iteration.SequenceMapthen producesMoutput sequences, each of length equal to the length ofinput_sequence.Shape inference walks the body subgraph in a child context seeded with:
body input
0(the per-iteration element ofinput_sequence): a tensor descriptor whose dtype matches the input sequence element dtype and whose shape is the common per-element shape of the input sequence (or empty when per-element shapes are not recorded);body inputs
1..K(the additional inputs): inherited verbatim from the matching outer-scopenodeinputs (either tensor or sequence).
Each output sequence then records the body output dtype as its element dtype; the sequence length is the input sequence length (concrete when the input length is known, otherwise symbolic). Per-element shapes are not recorded on the output sequence (the body may vary the per-element shape across iterations and we forward only the dtype).
- Parameters:
ctx – In/out context. Must already contain an :cpp:class:
OptimSequenceentry fornode.input(0)and the matching :cpp:class:OptimTensor/ :cpp:class:OptimSequenceentries for the remainingnodeinputs; on return it also contains one :cpp:class:OptimSequenceentry per declarednode.output.node – The
SequenceMapNodeProtowhose outputs should be described.node.op_type()must be"SequenceMap",nodemust declare at least one input and one output, and must carry a graph attribute named"body"whose outputs matchnode.output_size().
- Throws:
-
void ComputeShapeSplitToSequence(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimSequenceof aSplitToSequencenode and stores it inctx.SplitToSequence(since opset 11 in theai.onnxdomain) takes a tensor input and an optionalsplittensor input, plus the attributesaxis(default0) andkeepdims(default1, ignored whensplitis provided), and produces a single tensor-sequence output. The output element dtype matches the input tensor dtype.Per-element shapes are inferred when possible:
when
splitis omitted, the input axis dimension is split into chunks of size1; withkeepdims == 1each element keeps the input rank with axis dim1, withkeepdims == 0the axis is squeezed away;when
splitis a 1-D tensor whose value is known at shape inference time, each entry gives the corresponding element’s size alongaxis;when
splitis a scalar whose value is known, equal chunks of that size are produced (the last chunk possibly being smaller).
When the axis dimension or the
splitvalue are unknown the sequence length and per-element shapes are dropped and only the element dtype is forwarded together with a symbolic length.- Parameters:
ctx – In/out context. Must already contain an :cpp:class:
OptimTensorentry fornode.input(0)and, when present, fornode.input(1); on return it also contains an :cpp:class:OptimSequenceentry fornode.output(0).node – The
SplitToSequenceNodeProtowhose output should be described.node.op_type()must be"SplitToSequence"andnodemust declare at least one input and one output.
- Throws:
-
void ComputeShapeSequenceConstruct(ShapesContext &ctx, const NodeProto &node)#
-
namespace sequence#
-
namespace shapes
-
namespace onnx_optim