# tf.experimental.dispatch_for_binary_elementwise_apis

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.11.1/tensorflow/python/util/dispatch.py#L794-L849) |

Decorator to override default implementation for binary elementwise APIs.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.experimental.dispatch_for_binary_elementwise_apis`](https://www.tensorflow.org/api_docs/python/tf/experimental/dispatch_for_binary_elementwise_apis)

<br />

    tf.experimental.dispatch_for_binary_elementwise_apis(
        x_type, y_type
    )

The decorated function (known as the "elementwise api handler") overrides
the default implementation for any binary elementwise API whenever the value
for the first two arguments (typically named `x` and `y`) match the specified
type annotations. The elementwise api handler is called with two arguments:

`elementwise_api_handler(api_func, x, y)`

Where `x` and `y` are the first two arguments to the elementwise api, and
`api_func` is a TensorFlow function that takes two parameters and performs the
elementwise operation (e.g., [`tf.add`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/add)).

The following example shows how this decorator can be used to update all
binary elementwise operations to handle a `MaskedTensor` type:

    class MaskedTensor(tf.experimental.ExtensionType):
      values: tf.Tensor
      mask: tf.Tensor
    @dispatch_for_binary_elementwise_apis(MaskedTensor, MaskedTensor)
    def binary_elementwise_api_handler(api_func, x, y):
      return MaskedTensor(api_func(x.values, y.values), x.mask & y.mask)
    a = MaskedTensor([1, 2, 3, 4, 5], [True, True, True, True, False])
    b = MaskedTensor([2, 4, 6, 8, 0], [True, True, True, False, True])
    c = tf.add(a, b)
    print(f"values={c.values.numpy()}, mask={c.mask.numpy()}")
    values=[ 3 6 9 12 5], mask=[ True True True False False]

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x_type` | A type annotation indicating when the api handler should be called. |
| `y_type` | A type annotation indicating when the api handler should be called. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A decorator. ||

<br />

#### Registered APIs

The binary elementwise APIs are:

- [`tf.bitwise.bitwise_and(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/bitwise/bitwise_and)
- [`tf.bitwise.bitwise_or(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/bitwise/bitwise_or)
- [`tf.bitwise.bitwise_xor(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/bitwise/bitwise_xor)
- [`tf.bitwise.left_shift(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/bitwise/left_shift)
- [`tf.bitwise.right_shift(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/bitwise/right_shift)
- [`tf.compat.v1.div(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/compat/v1/div)
- [`tf.compat.v1.floor_div(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/compat/v1/floor_div)
- [`tf.compat.v1.math.scalar_mul(scalar, x, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/compat/v1/scalar_mul)
- [`tf.dtypes.complex(real, imag, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/dtypes/complex)
- [`tf.math.add(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/add)
- [`tf.math.atan2(y, x, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/atan2)
- [`tf.math.divide(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/divide)
- [`tf.math.divide_no_nan(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/divide_no_nan)
- [`tf.math.equal(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/equal)
- [`tf.math.floordiv(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/floordiv)
- [`tf.math.floormod(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/floormod)
- [`tf.math.greater(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/greater)
- [`tf.math.greater_equal(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/greater_equal)
- [`tf.math.less(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/less)
- [`tf.math.less_equal(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/less_equal)
- [`tf.math.logical_and(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/logical_and)
- [`tf.math.logical_or(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/logical_or)
- [`tf.math.logical_xor(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/logical_xor)
- [`tf.math.maximum(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/maximum)
- [`tf.math.minimum(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/minimum)
- [`tf.math.multiply(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/multiply)
- [`tf.math.multiply_no_nan(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/multiply_no_nan)
- [`tf.math.not_equal(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/not_equal)
- [`tf.math.pow(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/pow)
- [`tf.math.scalar_mul(scalar, x, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/scalar_mul)
- [`tf.math.squared_difference(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/squared_difference)
- [`tf.math.subtract(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/subtract)
- [`tf.math.truediv(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/truediv)
- [`tf.math.xdivy(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/xdivy)
- [`tf.math.xlog1py(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/xlog1py)
- [`tf.math.xlogy(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/xlogy)
- [`tf.math.zeta(x, q, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/math/zeta)
- [`tf.nn.sigmoid_cross_entropy_with_logits(labels, logits, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits)
- [`tf.realdiv(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/realdiv)
- [`tf.truncatediv(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/truncatediv)
- [`tf.truncatemod(x, y, name)`](https://www.tensorflow.org/versions/r2.11/api_docs/python/tf/truncatemod)