If#

  • Domain: ai.onnx

  • Since version: 13

If conditional

Inputs

  • cond (B): Condition for the if. The tensor must contain a single element.

Outputs

  • outputs (V): Values that are live-out to the enclosing scope. The return values in the then_branch and else_branch must be of the same data type. The then_branch and else_branch may produce tensors with the same element type and different shapes. If corresponding outputs from the then-branch and the else-branch have static shapes S1 and S2, then the shape of the corresponding output variable of the if-node (if present) must be compatible with both S1 and S2 as it represents the union of both possible shapes.For example, if in a model file, the first output of then_branch is typed float tensor with shape [2] and the first output of else_branch is another float tensor with shape [3], If’s first output should have (a) no shape set, or (b) a shape of rank 1 with neither dim_value nor dim_param set, or (c) a shape of rank 1 with a unique dim_param. In contrast, the first output cannot have the shape [2] since [2] and [3] are not compatible.

Attributes

  • else_branch (graph): Graph to run if condition is false. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the then_branch.

  • then_branch (graph): Graph to run if condition is true. Has N outputs: values you wish to be live-out to the enclosing scope. The number of outputs must match the number of outputs in the else_branch.

Type Constraints

  • V: All Tensor and Sequence types Allowed types: seq(tensor(bool)), seq(tensor(complex128)), seq(tensor(complex64)), seq(tensor(double)), seq(tensor(float)), seq(tensor(float16)), seq(tensor(int16)), seq(tensor(int32)), seq(tensor(int64)), seq(tensor(int8)), seq(tensor(string)), seq(tensor(uint16)), seq(tensor(uint32)), seq(tensor(uint64)), seq(tensor(uint8)), tensor(bool), tensor(complex128), tensor(complex64), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(string), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

  • B: Only bool Allowed types: tensor(bool).

Examples#

test_cc_if

Node:
  If(cond) -> (res)
  Attributes:
    then_branch = <subgraph>
    else_branch = <subgraph>
Inputs:
  cond: shape=(), dtype=bool
    True

Outputs:
  res: shape=(2,), dtype=float32
    [1., 2.]

test_cc_if_else

Node:
  If(cond) -> (res)
  Attributes:
    then_branch = <subgraph>
    else_branch = <subgraph>
Inputs:
  cond: shape=(), dtype=bool
    False

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

test_cc_if_multi_output

Node:
  If(cond) -> (res_a, res_b)
  Attributes:
    then_branch = <subgraph>
    else_branch = <subgraph>
Inputs:
  cond: shape=(), dtype=bool
    True

Outputs:
  res_a: shape=(3,), dtype=int64
    [1, 2, 3]
  res_b: shape=(2, 2), dtype=float32
    [[0.1, 0.2],
     [0.3, 0.4]]

test_cc_if_seq

Node:
  If(cond) -> (seq_res)
  Attributes:
    then_branch = <subgraph>
    else_branch = <subgraph>
Inputs:
  cond: shape=(), dtype=bool
    True

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

test_cc_shape_inference_if_symbolic_shapes

Node:
  If(cond) -> (I1, I2)
  Attributes:
    then_branch = <subgraph>
    else_branch = <subgraph>
Inputs:
  cond: shape=(), dtype=bool
    True
  input_1: shape=(3, 4), dtype=float32
    [[1.       , 1.1      , 1.2      , 1.3      ],
     [1.4      , 1.5      , 1.6      , 1.7      ],
     [1.8      , 1.9000001, 2.       , 2.1      ]]
  input_2: shape=(5, 4), dtype=float32
    [[-1.        , -0.9       , -0.8       , -0.7       ],
     [-0.6       , -0.5       , -0.39999998, -0.3       ],
     [-0.19999999, -0.09999996,  0.        ,  0.10000002],
     [ 0.20000005,  0.30000007,  0.39999998,  0.5       ],
     [ 0.6       ,  0.70000005,  0.8000001 ,  0.9       ]]
  input_3: shape=(5,), dtype=bool
    [ True, False,  True, False,  True]
  input_4: shape=(3,), dtype=int64
    [1, 2, 3]
  input_5: shape=(5,), dtype=int64
    [-1, -2, -3, -4, -5]

Outputs:
  I1: shape=(3, 4), dtype=float32
    [[1.       , 1.1      , 1.2      , 1.3      ],
     [1.4      , 1.5      , 1.6      , 1.7      ],
     [1.8      , 1.9000001, 2.       , 2.1      ]]
  I2: shape=(3,), dtype=int64
    [-1, -2, -3]

Differences with previous version (11)#

SchemaDiff: If (domain 'ai.onnx')

  • old version: 11

  • new version: 13

  • breaking: no

Type constraints:

  • changed ‘V’: added types: [‘seq(tensor(bool))’, ‘seq(tensor(complex128))’, ‘seq(tensor(complex64))’, ‘seq(tensor(double))’, ‘seq(tensor(float))’, ‘seq(tensor(float16))’, ‘seq(tensor(int16))’, ‘seq(tensor(int32))’, ‘seq(tensor(int64))’, ‘seq(tensor(int8))’, ‘seq(tensor(string))’, ‘seq(tensor(uint16))’, ‘seq(tensor(uint32))’, ‘seq(tensor(uint64))’, ‘seq(tensor(uint8))’]

Version History#