Skip to content
fn

where_op

→TensorImplPtr
int where_op(const int & cond, const int & x, const int & y)

Python wrappers

Public Python APIs implemented by this engine symbol.

Element-wise conditional selection between two tensors.

Returns x at positions where cond is true, otherwise y. This is the tensor analogue of the ternary expression cond ? x : y applied element-wise.

Math

y_i & \text{otherwise}\end{cases}$$ **Shape** On the GPU stream all three operands are broadcast by the MLX backend. On the CPU stream all three must share the same shape.

Parameters

condTensorImplPtr
Boolean (or 0/1-valued) condition tensor.
xTensorImplPtr
Values selected at positions where cond is true.
yTensorImplPtr
Values selected at positions where cond is false.

Returns

TensorImplPtr

Tensor of the broadcast shape and dtype of x/y.

Notes

Backward propagates grad_out masked by cond to x and masked by ~cond to y. cond itself receives no gradient.