ConstantOfShape - version 20#

This page documents version 20 of operator ConstantOfShape. See ConstantOfShape for the latest version (since version 25).

  • Domain: ai.onnx

  • Since version: 20

Generate a tensor with given value and shape.

Inputs

  • input (T1): 1D tensor. The shape of the expected output tensor. If empty tensor is given, the output would be a scalar. All values must be >= 0.

Outputs

  • output (T2): Output tensor of shape specified by ‘input’.If attribute ‘value’ is specified, the value and datatype of the output tensor is taken from ‘value’.If attribute ‘value’ is not specified, the value in the output defaults to 0, and the datatype defaults to float32.

Attributes

  • value (tensor): (Optional) The value of the output elements.Should be a one-element tensor. If not specified, it defaults to a tensor of value 0 and datatype float32

Type Constraints

  • T1: Constrain input types. Allowed types: tensor(int64).

  • T2: Constrain output types to be numerics. Allowed types: tensor(bfloat16), tensor(bool), tensor(double), tensor(float), tensor(float16), tensor(float8e4m3fn), tensor(float8e4m3fnuz), tensor(float8e5m2), tensor(float8e5m2fnuz), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

Examples#

test_cc_constantofshape_int64_fortytwo

Node:
  ConstantOfShape(x) -> (y)
  Attributes:
    value = <tensor>
Inputs:
  x: shape=(2,), dtype=int64
    [2, 3]

Outputs:
  y: shape=(2, 3), dtype=int64
    [[42, 42, 42],
     [42, 42, 42]]

test_constantofshape_float_ones

Node:
  ConstantOfShape(x) -> (y)
  Attributes:
    value = <tensor>
Inputs:
  x: shape=(3,), dtype=int64
    [4, 3, 2]

Outputs:
  y: shape=(4, 3, 2), dtype=float32
    [[[1., 1.],
      [1., 1.],
      [1., 1.]],

     [[1., 1.],
      [1., 1.],
      [1., 1.]],

     [[1., 1.],
      [1., 1.],
      [1., 1.]],

     [[1., 1.],
      [1., 1.],
      [1., 1.]]]

test_constantofshape_int_shape_zero

Node:
  ConstantOfShape(x) -> (y)
  Attributes:
    value = <tensor>
Inputs:
  x: shape=(1,), dtype=int64
    [0]

Outputs:
  y: shape=(0,), dtype=int32
    []

test_constantofshape_int_zeros

Node:
  ConstantOfShape(x) -> (y)
  Attributes:
    value = <tensor>
Inputs:
  x: shape=(2,), dtype=int64
    [10,  6]

Outputs:
  y: shape=(10, 6), dtype=int32
    [[0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0]]

Differences with previous version (9)#

SchemaDiff: ConstantOfShape (domain 'ai.onnx')

  • old version: 9

  • new version: 20

  • breaking: no

Type constraints:

  • changed ‘T2’: added types: [‘tensor(bfloat16)’, ‘tensor(float8e4m3fn)’, ‘tensor(float8e4m3fnuz)’, ‘tensor(float8e5m2)’, ‘tensor(float8e5m2fnuz)’]