shape_generator.h#
Shape-inference functions for ONNX operators in the generator 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 generator#
Functions
-
void ComputeShapeConstant(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aConstantnode and stores it inctx.Constantdeclares its output as the value of exactly one of the attributesvalue,sparse_value,value_int,value_ints,value_float,value_floats,value_stringorvalue_strings(which one is allowed depends on the schema revision, but for shape inference the union of every revision is accepted). The output dtype and shape are taken from the present attribute.When the resulting tensor carries at most :cpp:var:
kConstantValueAsShapeMaxElementsinteger elements and has rank at most one — i.e. it is small enough to plausibly be used later as a shape input of operators such asReshape,ExpandorConstantOfShape— its integer values are also recorded via :cpp:func:OptimTensor::SetValueAsShape. This mirrors the upstream ONNX shape-inference data-propagation behaviour for small integer constants.- Parameters:
ctx – In/out context. On return contains an entry for
node.output(0)describing the constant output.node – The
ConstantNodeProtowhose output should be described.node.op_type()must be"Constant"andnodemust declare at least one output.
- Throws:
std::invalid_argument – if
node.op_type()is not"Constant", ifnodehas no output, or if the attributes do not specify exactly one of the allowed value forms.
-
void ComputeShapeConstantOfShape(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aConstantOfShapenode and stores it inctx.ConstantOfShapeproduces a tensor whose shape is given by the single 1-Dint64input and whose element type and fill value are taken from the optionalvalueattribute (defaults to a singlefloat32zero).The output element type is inferred from the
valueattribute when present, otherwise defaults to :cpp:enumerator:TensorType::kFloat. The output shape is taken from the input’s :cpp:func:OptimTensor::ValueAsShapeannotation when available. When the input value has not been data-propagated but its static shape is a 1-D tensor whose single dim is a known integer, the corresponding output rank is reconstructed with symbolic dims.- Parameters:
ctx – In/out context. On return contains an entry for
node.output(0)describing the output.node – The
ConstantOfShapeNodeProtowhose output should be described.node.op_type()must be"ConstantOfShape",nodemust declare exactly one input and one output.
- Throws:
std::invalid_argument – if
node.op_type()is not"ConstantOfShape", ifnodehas no input or output, or if thevalueattribute is present but does not carry a tensor value.
-
void ComputeShapeEyeLike(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof anEyeLikenode and stores it inctx.EyeLikeoutputs a tensor with the same 2-D shape as its input. The output dtype is set from the optionaldtypeattribute when present, otherwise it defaults to the input dtype.- Throws:
std::invalid_argument – if
node.op_type()is not"EyeLike", ifnodehas no input or output, if the input rank is not 2, or ifdtypeis present but unsupported.
-
void ComputeShapeBlackmanWindow(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aBlackmanWindownode and stores it inctx.BlackmanWindowproduces a 1-D tensor of lengthsize(its single scalar integer input) holding the Blackman window coefficients. The output element type is given by the optionaloutput_datatypeattribute (a :cpp:class:TensorProto::DataTypevalue, defaults toFLOAT). Theperiodicattribute does not affect the output shape or dtype.The output shape is taken from the input’s :cpp:func:
OptimTensor::ValueAsShapeannotation when available (i.e. whensizeis a known constant): the output is then a 1-D tensor with a concrete dim. Otherwise the output is a 1-D tensor with a single symbolic dim.- Parameters:
ctx – In/out context. On return contains an entry for
node.output(0)describing the output.node – The
BlackmanWindowNodeProtowhose output should be described.node.op_type()must be"BlackmanWindow",nodemust declare exactly one input and one output.
- Throws:
std::invalid_argument – if
node.op_type()is not"BlackmanWindow"or ifnodehas no output.
-
void ComputeShapeHannWindow(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aHannWindownode and stores it inctx. Same semantics as :cpp:func:ComputeShapeBlackmanWindowbut for theHannWindowoperator.- Throws:
std::invalid_argument – if
node.op_type()is not"HannWindow"or ifnodehas no output.
-
void ComputeShapeHammingWindow(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aHammingWindownode and stores it inctx. Same semantics as :cpp:func:ComputeShapeBlackmanWindowbut for theHammingWindowoperator.- Throws:
std::invalid_argument – if
node.op_type()is not"HammingWindow"or ifnodehas no output.
-
void ComputeShapeMelWeightMatrix(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aMelWeightMatrixnode and stores it inctx.MelWeightMatrixproduces a 2-D tensor of shape[floor(dft_length/2) + 1, num_mel_bins]holding the triangular Mel filter-bank weights. The output element type is given by the optionaloutput_datatypeattribute (a :cpp:class:TensorProto::DataTypevalue, defaults toFLOAT).The output shape is taken from the input’s :cpp:func:
OptimTensor::ValueAsShapeannotation when available (i.e. whennum_mel_binsanddft_lengthare known constants). Otherwise the corresponding output dim is symbolic.- Throws:
std::invalid_argument – if
node.op_type()is not"MelWeightMatrix"or ifnodehas no output.
-
void ComputeShapeBernoulli(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aBernoullinode and stores it inctx.Bernoullidraws binary samples from a Bernoulli distribution whose probabilities are given by the single input tensor. The output has the same shape as the input. Its element type is given by the optionaldtypeattribute (a :cpp:class:TensorProto::DataTypevalue); when the attribute is absent, the output dtype matches the input dtype.- Parameters:
ctx – In/out context. On return contains an entry for
node.output(0)describing the output.node – The
BernoulliNodeProtowhose output should be described.node.op_type()must be"Bernoulli",nodemust declare exactly one input and one output.
- Throws:
std::invalid_argument – if
node.op_type()is not"Bernoulli", ifnodehas no input or output, or if thedtypeattribute is present but holds an unsupported value.
-
void ComputeShapeRandomNormal(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aRandomNormalnode and stores it inctx.RandomNormalproduces a tensor whose shape is given by the requiredshapeattribute (astd::vector<int64_t>) and whose element type is given by the optionaldtypeattribute (a :cpp:class:TensorProto::DataTypevalue, defaults toFLOAT).- Throws:
std::invalid_argument – if
node.op_type()is not"RandomNormal", ifnodehas no output, if theshapeattribute is missing, contains negative dims, or ifdtypeis present but holds an unsupported value.
-
void ComputeShapeRandomUniform(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aRandomUniformnode and stores it inctx. Same shape/dtype semantics as :cpp:func:ComputeShapeRandomNormal.- Throws:
std::invalid_argument – if
node.op_type()is not"RandomUniform"or as documented in :cpp:func:ComputeShapeRandomNormal.
-
void ComputeShapeRandomNormalLike(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aRandomNormalLikenode and stores it inctx.RandomNormalLikecopies its single input tensor’s shape. The output element type is given by the optionaldtypeattribute (a :cpp:class:TensorProto::DataTypevalue); when the attribute is absent, the output dtype matches the input dtype.- Throws:
std::invalid_argument – if
node.op_type()is not"RandomNormalLike", ifnodehas no input or output, or ifdtypeis present but holds an unsupported value.
-
void ComputeShapeRandomUniformLike(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aRandomUniformLikenode and stores it inctx. Same shape/dtype semantics as :cpp:func:ComputeShapeRandomNormalLike.- Throws:
std::invalid_argument – if
node.op_type()is not"RandomUniformLike"or as documented in :cpp:func:ComputeShapeRandomNormalLike.
-
void ComputeShapeMultinomial(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aMultinomialnode and stores it inctx.Multinomialdrawssample_sizesamples per row from a multinomial distribution whose unnormalized log-probabilities are given by the 2-D input tensor of shape[batch_size, class_size]. The output is a 2-D tensor of shape[batch_size, sample_size]whose element type defaults toINT32(and may also beINT64).- Throws:
std::invalid_argument – if
node.op_type()is not"Multinomial", ifnodehas no input or output, if the input rank is statically known to be different from 2, or if thedtypeattribute is present but is neitherINT32norINT64.
-
void ComputeShapeRange(ShapesContext &ctx, const NodeProto &node)#
Computes the output :cpp:class:
OptimTensorof aRangenode and stores it inctx.Rangeproduces a 1-D tensor whose element type matches the three scalar inputsstart,limitanddelta(which must all share the same dtype, per the schema’sTconstraint).When the values of all three inputs have been data-propagated as scalars, the output dimension is explicitly computed as
max(ceil((limit - start) / delta), 0). Otherwise the output is described as a 1-D tensor with a single symbolic dim of unknown size.- Parameters:
ctx – In/out context. On return contains an entry for
node.output(0)describing the output.node – The
RangeNodeProtowhose output should be described.node.op_type()must be"Range"andnodemust declare exactly three inputs and one output.
- Throws:
std::invalid_argument – if
node.op_type()is not"Range", ifnodehas fewer than three inputs or no output.
Variables
-
constexpr int64_t kConstantValueAsShapeMaxElements = kOptimValueAsShapeMaxElements#
Maximum element count of a
Constantvalue tensor for which :cpp:func:ComputeShapeConstantpopulates the output :cpp:func:OptimTensor::ValueAsShapeannotation. Constants beyond this threshold are not data-propagated (the output dtype and shape are still inferred normally). Kept in sync with :cpp:var:kOptimValueAsShapeMaxElements.
-
void ComputeShapeConstant(ShapesContext &ctx, const NodeProto &node)#
-
namespace generator#
-
namespace shapes
-
namespace onnx_optim