optim_sequence.h#

Lightweight description of an ONNX tensor sequence value used by onnx_optim shape-inference passes.

The onnx_optim stack already exposes :cpp:class:OptimTensor to describe tensor-typed values flowing through a graph. Operators such as SequenceConstruct, SequenceInsert, SplitToSequence or SequenceEmpty produce sequence values rather than tensors, so a matching descriptor is needed.

:cpp:class:OptimSequence is the analogue of :cpp:class:OptimTensor for sequence values. It records the common element dtype and one :cpp:class:OptimShape per element of the sequence (the ONNX SequenceConstruct operator only requires that elements share the same dtype, not the same shape). The sequence length is itself an :cpp:class:OptimDim so it can be either a concrete integer (e.g. for a SequenceConstruct node with N known inputs) or a symbolic expression (e.g. for the output of SplitToSequence whose length depends on a runtime split value). When the per-element shapes are known, the length is implicitly :cpp:func:ElemShapes().size().

Like :cpp:class:OptimTensor it is a small, value-typed, non-owning descriptor: it never allocates the underlying data.

namespace ONNX_LIGHT_NAMESPACE
namespace onnx_optim#
class OptimSequence#
#include <optim_sequence.h>

Descriptor for an ONNX tensor-sequence value. A sequence carries a common element :cpp:type:TensorType and one :cpp:class:OptimShape per element. The sequence length is itself an :cpp:class:OptimDim so it can be either a concrete integer (e.g. for a SequenceConstruct node with N known inputs) or a symbolic expression (e.g. for the output of SplitToSequence whose length depends on a runtime split value).

When the element dtype is not known (e.g. for the output of SequenceEmpty whose dtype is set by an attribute or defaults to FLOAT, or for SequenceConstruct with zero inputs), the descriptor stores :cpp:enumerator:TensorType::kUndefined for the dtype. When the per-element shapes are unknown (typically for sequences whose length is itself symbolic), the descriptor stores no shapes at all and :cpp:func:HasElemShapes returns false. Callers can use :cpp:func:HasElemDtype and :cpp:func:HasElemShapes to distinguish “unknown” from “empty sequence” (length 0).

Public Functions

OptimSequence() = default#

Default constructs an empty sequence descriptor with unknown element dtype, no per-element shapes recorded, and a zero length.

inline OptimSequence(TensorType elem_dtype, std::vector<OptimShape> elem_shapes)#

Constructs a sequence descriptor from a known element dtype and a vector of per-element shapes. The sequence length is set to the number of supplied shapes.

inline OptimSequence(TensorType elem_dtype, OptimDim length)#

Constructs a sequence descriptor from a known element dtype and a (possibly symbolic) length. No per-element shape is recorded.

inline TensorType ElemDtype() const noexcept#

Element dtype shared by every tensor in the sequence. Returns :cpp:enumerator:TensorType::kUndefined when the dtype is unknown (see :cpp:func:HasElemDtype).

inline const std::vector<OptimShape> &ElemShapes() const noexcept#

Per-element shapes of the sequence. The returned vector is empty when :cpp:func:HasElemShapes is false; otherwise its size is the (concrete) sequence length and ElemShapes()[i] is the shape of the i-th tensor in the sequence.

inline std::vector<OptimShape> &ElemShapes() noexcept#
inline const OptimDim &Length() const noexcept#

Sequence length, possibly symbolic. When :cpp:func:HasElemShapes is true, the length equals ElemShapes().size().

inline OptimDim &Length() noexcept#
inline bool HasElemDtype() const noexcept#

true when an element dtype has been recorded for this sequence.

inline bool HasElemShapes() const noexcept#

true when per-element shapes have been recorded for this sequence. An empty vector still returns true and denotes a sequence of length 0.

inline void SetElemDtype(TensorType dtype) noexcept#

Replaces the recorded element dtype. Passing :cpp:enumerator:TensorType::kUndefined clears the dtype.

inline void SetElemShapes(std::vector<OptimShape> shapes)#

Replaces the recorded per-element shapes. Also synchronises the sequence length with the number of supplied shapes.

inline void ClearElemShapes() noexcept#

Clears the recorded per-element shapes. The sequence length is left untouched.

inline void SetLength(OptimDim length) noexcept#

Replaces the sequence length. Callers are responsible for keeping the length consistent with :cpp:func:ElemShapes when both are recorded.

inline bool operator==(const OptimSequence &other) const noexcept#

Equality compares the element dtype, the per-element shapes, the sequence length, and the “known” flags.

inline bool operator!=(const OptimSequence &other) const noexcept#

Private Members

TensorType elem_dtype_ = TensorType::kUndefined#
std::vector<OptimShape> elem_shapes_ = {}#
OptimDim length_ = {static_cast<int64_t>(0)}#
bool has_elem_dtype_ = false#
bool has_elem_shapes_ = false#