shape_tensor.h#

Shape-inference functions for ONNX operators in the tensor family.

namespace ONNX_LIGHT_NAMESPACE
namespace onnx_optim
namespace shapes
namespace tensor#

Functions

void ComputeShapeConcat(ShapesContext &ctx, const NodeProto &node)#

Computes the output :cpp:class:OptimTensor of a Concat node and stores it in ctx.

Concat concatenates a variadic list of input tensors along the axis specified by the axis attribute. All inputs must share the same rank and the same dimension sizes on every axis other than the concatenation axis. The output dtype always matches the dtype of the first input (type constraint T); the output shape is:

  • on the concatenation axis: the sum of all input dimensions when every input dimension on that axis is a concrete integer; otherwise a fresh symbolic dimension;

  • on every other axis: the merged dimension between all inputs (concrete dimensions must match across inputs, otherwise an exception is thrown; a concrete value overrides a symbolic one).

The axis attribute can be negative, in which case it is interpreted as axis + rank. When the attribute is missing the default of 1 (the opset 1 default) is used.

Parameters:
  • ctx – In/out context. Must already contain an entry for every name in node.input. On return it also contains an entry for node.output(0).

  • node – The Concat NodeProto whose output should be described. node.op_type() must be "Concat", node must declare at least one input and at least one output.

Throws:
  • std::invalid_argument – if node.op_type() is not "Concat", if node has no input or output, if the inputs have different ranks, if their non-concat dimensions disagree, if the resolved axis is out of range, or if the input dtypes differ.

  • std::out_of_range – if any input name is missing from ctx.

void ComputeShapeCast(ShapesContext &ctx, const NodeProto &node)#

Computes the output :cpp:class:OptimTensor of a Cast node and stores it in ctx.

Cast produces an output whose shape is identical to the shape of its single input and whose element type is given by the required integer attribute to (a TensorProto::DataType value). The other optional attributes (saturate, round_mode) do not affect the output shape or dtype and are therefore not inspected by this function.

Parameters:
  • ctx – In/out context. Must already contain an entry for node.input(0). On return it also contains an entry for node.output(0).

  • node – The Cast NodeProto whose output should be described. node.op_type() must be "Cast", node must declare at least one input and at least one output and must carry the required to attribute.

Throws:
  • std::invalid_argument – if node.op_type() is not "Cast", if node has no input or output, if the to attribute is missing, or if its value does not map to a supported :cpp:enum:TensorType.

  • std::out_of_range – if the input name is missing from ctx.

void ComputeShapeReshape(ShapesContext &ctx, const NodeProto &node)#

Computes the output :cpp:class:OptimTensor of a Reshape node and stores it in ctx.

Reshape takes a data tensor and a 1-D int64 shape tensor whose values describe the desired output shape. The output dtype is the dtype of data (type constraint T). The output shape is derived element-by-element from the target shape:

  • a positive value is used verbatim;

  • 0

    means “copy from the input ``data`` shape at the same

    index”, unless the

    allowzero attribute is set to 1 (in which case 0 is honoured literally);

  • exactly one -1 is allowed; the corresponding dimension is inferred so that the total number of elements is preserved (when data is fully known and the other dims are concrete);

  • symbolic target dims are forwarded as symbolic output dims.

Shape values are read from the shape input’s :cpp:func:OptimTensor::ValueAsShape annotation (populated for small constants, e.g. by :cpp:func:ComputeShapeConstant). When that annotation is missing the output rank is taken from the static shape of the shape input (its single dimension, when concrete) and every output dim is left symbolic. When the rank itself is unknown the output is left as a fully-symbolic rank-1 tensor.

Parameters:
  • ctx – In/out context. Must contain entries for node.input(0) (data) and node.input(1) (shape). On return it also contains an entry for node.output(0).

  • node – The Reshape NodeProto whose output should be described. node.op_type() must be "Reshape", node must declare two inputs and at least one output.

Throws:
  • std::invalid_argument – if node.op_type() is not "Reshape", if node has fewer than two inputs or no output, if the target shape contains more than one -1, contains a value strictly less than -1, if a 0 entry (with allowzero == 0) refers to a position outside the input rank, or if a -1 cannot be reconciled with the input’s element count.

  • std::out_of_range – if any input name is missing from ctx.