Skip to content

Commit

Permalink
Shape returns an empty tensor with shape {0} for a scalar which is in…
Browse files Browse the repository at this point in the history
…correct as there is a single data element.

Expected might be for it to return a shape of `{}`, but ONNX shape inferencing doesn't support scalar either as it calls add_dim on the output_shape at the start so result will always be rank 1 or higher.

https://github.com/onnx/onnx/blob/b60f69412abb5393ab819b936b473f83867f6c87/onnx/defs/tensor/defs.cc#L496

The spec doesn't say scalars aren't supported.
  • Loading branch information
skottmckay committed Nov 19, 2023
1 parent 53917a3 commit ea224b1
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions onnxruntime/core/providers/cpu/tensor/shape_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Shape final : public OpKernel {
const TensorShape& input_shape = input->Shape();

int64_t rank = gsl::narrow_cast<int64_t>(input_shape.NumDimensions());
// ONNX shape inferencing doesn't work with a scalar. Spec does not say it's unsupported.
ORT_ENFORCE(rank != 0, "Shape of a scalar is not supported");

if (!needs_slicing_) { // vanilla use of Shape (no slicing)
Tensor* output = context->Output(0, {rank});
Expand Down

0 comments on commit ea224b1

Please sign in to comment.