optim_tensor.h#
Lightweight, non-owning tensor description used by ONNX graph optimisation passes.
The onnx_optim namespace exposes three small value types:
:cpp:class:
OptimDim— a single shape dimension, either a concreteint64_tor a symbolic string expression.:cpp:class:
OptimShape— an ordered, bounded-rank collection of :cpp:class:OptimDim.:cpp:class:
OptimTensor— a non-owning view over a contiguous buffer carrying a :cpp:type:TensorType, an :cpp:class:OptimShape, and an optional shape annotation when the tensor itself represents a shape (e.g. theshapeinput ofReshape).
These types are intentionally header-only friendly and never allocate the tensor data they describe; callers are responsible for the lifetime of the underlying buffer.
-
namespace ONNX_LIGHT_NAMESPACE
-
namespace onnx_optim#
Typedefs
-
using TensorType = ONNX_LIGHT_NAMESPACE::onnx_op::TensorType#
Reuse the TensorType enumeration defined by
onnx_opso thatonnx_optimis fully aligned with the rest of the operator stack.
Variables
-
constexpr std::size_t kMaxOptimRank = 8#
Maximum number of dimensions an
OptimShapecan describe inline. Shapes in practice rarely exceed this rank, so storing the dimensions in a small container keeps the structure compact and cache-friendly.
-
class OptimDim#
- #include <optim_tensor.h>
A single shape dimension that is either a concrete non-negative integer or a symbolic expression represented as a string.
OptimDimis used byOptimShapeto describe both fully-known and partially-symbolic shapes.Public Functions
-
inline OptimDim()#
Default constructs a zero-valued integer dimension.
-
inline OptimDim(int64_t value)#
Constructs an integer-valued dimension.
-
inline OptimDim(std::string expr)#
Constructs a symbolic dimension expressed as a string expression.
-
inline OptimDim(const char *expr)#
Convenience overload for C string literals.
-
inline bool IsInt() const noexcept#
Returns
truewhen the dimension holds a concrete integer value.
-
inline bool IsExpr() const noexcept#
Returns
truewhen the dimension holds a symbolic expression string.
-
inline int64_t AsInt() const#
Returns the integer value. Throws
std::bad_variant_accessif the dimension is not an integer.
-
inline const std::string &AsExpr() const#
Returns the symbolic expression. Throws
std::bad_variant_accessif the dimension is not a string expression.
-
inline OptimDim()#
-
class OptimShape#
- #include <optim_tensor.h>
A short, value-typed shape composed of
OptimDimentries. The rank is bounded bykMaxOptimRankso that the structure fits comfortably on the stack. Symbolic and concrete dimensions can be mixed freely.Public Functions
-
OptimShape() = default#
-
OptimShape(std::initializer_list<OptimDim> dims)#
Constructs a shape from an initializer list of dimensions.
-
explicit OptimShape(const std::vector<OptimDim> &dims)#
Constructs a shape from any iterable container of
OptimDim.
-
inline bool Empty() const noexcept#
truewhen the shape contains no dimensions (scalar / rank-0).
-
inline const OptimDim &operator[](std::size_t i) const#
Access a dimension by index. Throws
std::out_of_rangeif invalid.
-
void PushBack(OptimDim dim)#
Appends a new dimension. Throws
std::length_errorwhen the maximum rankkMaxOptimRankwould be exceeded.
-
bool IsFullyKnown() const noexcept#
Returns
truewhen every dimension is a concrete integer.
-
int64_t NumElements() const#
Computes the product of all integer dimensions. Returns
1for a rank-0 (empty) shape, matching the standard scalar-element-count semantic. Throwsstd::runtime_errorif any dimension is symbolic.
-
inline bool operator==(const OptimShape &other) const noexcept#
Equality compares the dimensions element-wise.
-
inline bool operator!=(const OptimShape &other) const noexcept#
-
OptimShape() = default#
-
class OptimTensor#
- #include <optim_tensor.h>
A non-owning view over a contiguous tensor buffer.
OptimTensornever allocates: the caller is responsible for the lifetime of the underlying memory referenced bydata. The shape may contain symbolic dimensions which is useful for representing intermediate values during optimisation passes where the concrete shape is not yet known.Public Functions
-
OptimTensor() = default#
Constructs an empty (null) tensor.
-
inline OptimTensor(void *data, TensorType dtype, OptimShape shape)#
Constructs an
OptimTensorreferencing an external buffer.- Parameters:
data – Pointer to the first element of the externally-owned buffer. May be
nullptronly whenshapeis empty or all integer dimensions are zero.dtype – Element type of the data referenced by
data.shape – Shape describing the layout of the data.
-
inline void *Data() const noexcept#
Pointer to the externally-owned buffer. The view itself is
constbut the buffer it references is not, mirroringstd::spansemantics.
-
inline TensorType Dtype() const noexcept#
Element type of the referenced buffer.
-
inline const OptimShape &Shape() const noexcept#
Shape of the tensor (may contain symbolic dimensions).
-
inline OptimShape &Shape() noexcept#
-
inline bool IsNull() const noexcept#
truewhen the tensor has no associated data pointer.
-
inline void SetValueAsShape(OptimShape shape)#
Tags the tensor as carrying a shape value (e.g. the
shapeinput of aReshapenode) and stores that shape. An emptyOptimShapeis permitted: it denotes a rank-0 / scalar shape value.
-
inline void ClearValueAsShape() noexcept#
Clears the value-as-shape annotation so that
HasValueAsShapebecomesfalseagain.
-
inline bool HasValueAsShape() const noexcept#
truewhen the tensor’s value is interpreted as a shape. An empty stored shape still returnstrue— useValueAsShape().Empty()to distinguish the empty case.
-
inline const OptimShape &ValueAsShape() const#
Returns the shape value carried by this tensor. Throws
std::bad_optional_accessifHasValueAsShape()isfalse.
-
inline OptimShape &ValueAsShape()#
-
inline bool operator==(const OptimTensor &other) const noexcept#
Equality compares the data pointer, dtype, shape, and the optional value-as-shape annotation. Because :cpp:class:
OptimTensoris a non-owning view, two tensors are considered equal only when they refer to the same external buffer.
-
inline bool operator!=(const OptimTensor &other) const noexcept#
Private Members
-
void *data_ = nullptr#
-
TensorType dtype_ = TensorType::kUndefined#
-
OptimShape shape_ = {}#
-
std::optional<OptimShape> value_as_shape_ = {}#
-
OptimTensor() = default#
-
using TensorType = ONNX_LIGHT_NAMESPACE::onnx_op::TensorType#
-
namespace onnx_optim#