Where#

  • Domain: ai.onnx

  • Since version: 16

Return elements, either from X or Y, depending on condition.

Inputs

  • condition (B): When True (nonzero), yield X, otherwise yield Y

  • X (T): values selected at indices where condition is True

  • Y (T): values selected at indices where condition is False

Outputs

  • output (T): Tensor of shape equal to the broadcasted shape of condition, X, and Y.

Type Constraints

  • B: Constrain to boolean tensors. Allowed types: tensor(bool).

  • T: Constrain input and output types to all tensor types (including bfloat). Allowed types: tensor(bfloat16), 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).

Examples#

test_cc_where_bool

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=bool
    [[ True,  True],
     [False, False]]
  y: shape=(2, 2), dtype=bool
    [[False, False],
     [ True,  True]]

Outputs:
  output: shape=(2, 2), dtype=bool
    [[ True, False],
     [False,  True]]

test_cc_where_double

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=float64
    [[1., 2.],
     [3., 4.]]
  y: shape=(2, 2), dtype=float64
    [[5., 6.],
     [7., 8.]]

Outputs:
  output: shape=(2, 2), dtype=float64
    [[1., 6.],
     [3., 8.]]

test_cc_where_int16

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=int16
    [[1, 2],
     [3, 4]]
  y: shape=(2, 2), dtype=int16
    [[-5, -6],
     [-7, -8]]

Outputs:
  output: shape=(2, 2), dtype=int16
    [[ 1, -6],
     [ 3, -8]]

test_cc_where_int8

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=int8
    [[1, 2],
     [3, 4]]
  y: shape=(2, 2), dtype=int8
    [[-5, -6],
     [-7, -8]]

Outputs:
  output: shape=(2, 2), dtype=int8
    [[ 1, -6],
     [ 3, -8]]

test_cc_where_nan_inf

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(6,), dtype=bool
    [ True, False,  True, False,  True, False]
  x: shape=(6,), dtype=float32
    [ nan,   1.,  inf,   2., -inf,   3.]
  y: shape=(6,), dtype=float32
    [  0.,  nan,   0.,  inf,   0., -inf]

Outputs:
  output: shape=(6,), dtype=float32
    [ nan,  nan,  inf,  inf, -inf, -inf]

test_cc_where_nan_inf_bcast

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 1), dtype=bool
    [[ True],
     [False]]
  x: shape=(2, 3), dtype=float32
    [[inf,  1., inf],
     [ 2., inf,  3.]]
  y: shape=(1, 3), dtype=float32
    [[-inf,  nan,   0.]]

Outputs:
  output: shape=(2, 3), dtype=float32
    [[ inf,   1.,  inf],
     [-inf,  nan,   0.]]

test_cc_where_string

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=object
    [['a', 'b'],
     ['c', 'd']]
  y: shape=(2, 2), dtype=object
    [['e', 'f'],
     ['g', 'h']]

Outputs:
  output: shape=(2, 2), dtype=object
    [['a', 'f'],
     ['c', 'h']]

test_cc_where_uint16

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=uint16
    [[1, 2],
     [3, 4]]
  y: shape=(2, 2), dtype=uint16
    [[5, 6],
     [7, 8]]

Outputs:
  output: shape=(2, 2), dtype=uint16
    [[1, 6],
     [3, 8]]

test_cc_where_uint32

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=uint32
    [[1, 2],
     [3, 4]]
  y: shape=(2, 2), dtype=uint32
    [[5, 6],
     [7, 8]]

Outputs:
  output: shape=(2, 2), dtype=uint32
    [[1, 6],
     [3, 8]]

test_cc_where_uint64

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=uint64
    [[1, 2],
     [3, 4]]
  y: shape=(2, 2), dtype=uint64
    [[5, 6],
     [7, 8]]

Outputs:
  output: shape=(2, 2), dtype=uint64
    [[1, 6],
     [3, 8]]

test_cc_where_uint8

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=uint8
    [[1, 2],
     [3, 4]]
  y: shape=(2, 2), dtype=uint8
    [[5, 6],
     [7, 8]]

Outputs:
  output: shape=(2, 2), dtype=uint8
    [[1, 6],
     [3, 8]]

test_where_bcast

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 1), dtype=bool
    [[ True],
     [False]]
  x: shape=(2, 3), dtype=int32
    [[1, 2, 3],
     [4, 5, 6]]
  y: shape=(1, 3), dtype=int32
    [[10, 20, 30]]

Outputs:
  output: shape=(2, 3), dtype=int32
    [[ 1,  2,  3],
     [10, 20, 30]]

test_where_example

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True, False]]
  x: shape=(2, 2), dtype=float32
    [[1., 2.],
     [3., 4.]]
  y: shape=(2, 2), dtype=float32
    [[5., 6.],
     [7., 8.]]

Outputs:
  output: shape=(2, 2), dtype=float32
    [[1., 6.],
     [3., 8.]]

test_where_long_example

Node:
  Where(condition, x, y) -> (output)
Inputs:
  condition: shape=(2, 2), dtype=bool
    [[ True, False],
     [ True,  True]]
  x: shape=(2, 2), dtype=int64
    [[1, 2],
     [3, 4]]
  y: shape=(2, 2), dtype=int64
    [[9, 8],
     [7, 6]]

Outputs:
  output: shape=(2, 2), dtype=int64
    [[1, 8],
     [3, 4]]

Differences with previous version (9)#

SchemaDiff: Where (domain 'ai.onnx')

  • old version: 9

  • new version: 16

  • breaking: no

Type constraints:

  • changed ‘T’: added types: [‘tensor(bfloat16)’]

Version History#