# tf.experimental.dispatch_for_unary_elementwise_apis

<br />

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

Decorator to override default implementation for unary 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_unary_elementwise_apis`](https://www.tensorflow.org/api_docs/python/tf/experimental/dispatch_for_unary_elementwise_apis)

<br />

    tf.experimental.dispatch_for_unary_elementwise_apis(
        x_type
    )

The decorated function (known as the "elementwise api handler") overrides
the default implementation for any unary elementwise API whenever the value
for the first argument (typically named `x`) matches the type annotation
`x_type`. The elementwise api handler is called with two arguments:

`elementwise_api_handler(api_func, x)`

Where `api_func` is a function that takes a single parameter and performs the
elementwise operation (e.g., [`tf.abs`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/abs)), and `x` is the first argument to the
elementwise api.

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

    class MaskedTensor(tf.experimental.ExtensionType):
      values: tf.Tensor
      mask: tf.Tensor
    @dispatch_for_unary_elementwise_apis(MaskedTensor)
    def unary_elementwise_api_handler(api_func, x):
      return MaskedTensor(api_func(x.values), x.mask)
    mt = MaskedTensor([1, -2, -3], [True, False, True])
    abs_mt = tf.abs(mt)
    print(f"values={abs_mt.values.numpy()}, mask={abs_mt.mask.numpy()}")
    values=[1 2 3], mask=[ True False True]

For unary elementwise operations that take extra arguments beyond `x`, those
arguments are *not* passed to the elementwise api handler, but are
automatically added when `api_func` is called. E.g., in the following
example, the `dtype` parameter is not passed to
`unary_elementwise_api_handler`, but is added by `api_func`.

    ones_mt = tf.ones_like(mt, dtype=tf.float32)
    print(f"values={ones_mt.values.numpy()}, mask={ones_mt.mask.numpy()}")
    values=[1.0 1.0 1.0], mask=[ True False True]

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x_type` | A type annotation indicating when the api handler should be called. See `dispatch_for_api` for a list of supported annotation types. |

<br />

<br />

<br />

<br />

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

<br />

#### Registered APIs

The unary elementwise APIs are:

- [`tf.bitwise.invert(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/bitwise/invert)
- [`tf.cast(x, dtype, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/cast)
- [`tf.clip_by_value(t, clip_value_min, clip_value_max, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/clip_by_value)
- [`tf.compat.v1.nn.log_softmax(logits, axis, name, dim)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/math/log_softmax)
- [`tf.compat.v1.ones_like(tensor, dtype, name, optimize)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/ones_like)
- [`tf.compat.v1.strings.length(input, name, unit)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/strings/length)
- [`tf.compat.v1.strings.substr(input, pos, len, name, unit)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/strings/substr)
- [`tf.compat.v1.strings.to_hash_bucket(string_tensor, num_buckets, name, input)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/string_to_hash_bucket)
- [`tf.compat.v1.substr(input, pos, len, name, unit)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/substr)
- [`tf.compat.v1.to_bfloat16(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/to_bfloat16)
- [`tf.compat.v1.to_complex128(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/to_complex128)
- [`tf.compat.v1.to_complex64(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/to_complex64)
- [`tf.compat.v1.to_double(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/to_double)
- [`tf.compat.v1.to_float(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/to_float)
- [`tf.compat.v1.to_int32(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/to_int32)
- [`tf.compat.v1.to_int64(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/to_int64)
- [`tf.compat.v1.zeros_like(tensor, dtype, name, optimize)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/zeros_like)
- [`tf.debugging.check_numerics(tensor, message, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/debugging/check_numerics)
- [`tf.dtypes.saturate_cast(value, dtype, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/dtypes/saturate_cast)
- [`tf.image.adjust_brightness(image, delta)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/image/adjust_brightness)
- [`tf.image.adjust_gamma(image, gamma, gain)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/image/adjust_gamma)
- [`tf.image.convert_image_dtype(image, dtype, saturate, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/image/convert_image_dtype)
- [`tf.image.random_brightness(image, max_delta, seed)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/image/random_brightness)
- [`tf.image.stateless_random_brightness(image, max_delta, seed)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/image/stateless_random_brightness)
- [`tf.io.decode_base64(input, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/io/decode_base64)
- [`tf.io.decode_compressed(bytes, compression_type, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/io/decode_compressed)
- [`tf.io.encode_base64(input, pad, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/io/encode_base64)
- [`tf.math.abs(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/abs)
- [`tf.math.acos(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/acos)
- [`tf.math.acosh(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/acosh)
- [`tf.math.angle(input, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/angle)
- [`tf.math.asin(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/asin)
- [`tf.math.asinh(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/asinh)
- [`tf.math.atan(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/atan)
- [`tf.math.atanh(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/atanh)
- [`tf.math.bessel_i0(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/bessel_i0)
- [`tf.math.bessel_i0e(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/bessel_i0e)
- [`tf.math.bessel_i1(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/bessel_i1)
- [`tf.math.bessel_i1e(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/bessel_i1e)
- [`tf.math.ceil(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/ceil)
- [`tf.math.conj(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/conj)
- [`tf.math.cos(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/cos)
- [`tf.math.cosh(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/cosh)
- [`tf.math.digamma(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/digamma)
- [`tf.math.erf(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/erf)
- [`tf.math.erfc(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/erfc)
- [`tf.math.erfcinv(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/erfcinv)
- [`tf.math.erfinv(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/erfinv)
- [`tf.math.exp(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/exp)
- [`tf.math.expm1(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/expm1)
- [`tf.math.floor(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/floor)
- [`tf.math.imag(input, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/imag)
- [`tf.math.is_finite(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/is_finite)
- [`tf.math.is_inf(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/is_inf)
- [`tf.math.is_nan(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/is_nan)
- [`tf.math.lgamma(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/lgamma)
- [`tf.math.log(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/log)
- [`tf.math.log1p(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/log1p)
- [`tf.math.log_sigmoid(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/log_sigmoid)
- [`tf.math.logical_not(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/logical_not)
- [`tf.math.ndtri(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/ndtri)
- [`tf.math.negative(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/negative)
- [`tf.math.nextafter(x1, x2, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/nextafter)
- [`tf.math.real(input, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/real)
- [`tf.math.reciprocal(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/reciprocal)
- [`tf.math.reciprocal_no_nan(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/reciprocal_no_nan)
- [`tf.math.rint(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/rint)
- [`tf.math.round(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/round)
- [`tf.math.rsqrt(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/rsqrt)
- [`tf.math.sigmoid(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/sigmoid)
- [`tf.math.sign(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/sign)
- [`tf.math.sin(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/sin)
- [`tf.math.sinh(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/sinh)
- [`tf.math.softplus(features, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/softplus)
- [`tf.math.special.bessel_j0(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/bessel_j0)
- [`tf.math.special.bessel_j1(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/bessel_j1)
- [`tf.math.special.bessel_k0(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/bessel_k0)
- [`tf.math.special.bessel_k0e(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/bessel_k0e)
- [`tf.math.special.bessel_k1(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/bessel_k1)
- [`tf.math.special.bessel_k1e(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/bessel_k1e)
- [`tf.math.special.bessel_y0(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/bessel_y0)
- [`tf.math.special.bessel_y1(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/bessel_y1)
- [`tf.math.special.dawsn(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/dawsn)
- [`tf.math.special.expint(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/expint)
- [`tf.math.special.fresnel_cos(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/fresnel_cos)
- [`tf.math.special.fresnel_sin(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/fresnel_sin)
- [`tf.math.special.spence(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/special/spence)
- [`tf.math.sqrt(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/sqrt)
- [`tf.math.square(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/square)
- [`tf.math.tan(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/tan)
- [`tf.math.tanh(x, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/math/tanh)
- [`tf.nn.elu(features, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/nn/elu)
- [`tf.nn.gelu(features, approximate, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/nn/gelu)
- [`tf.nn.leaky_relu(features, alpha, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/nn/leaky_relu)
- [`tf.nn.relu(features, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/nn/relu)
- [`tf.nn.relu6(features, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/nn/relu6)
- [`tf.nn.selu(features, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/nn/selu)
- [`tf.nn.silu(features, beta)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/nn/silu)
- [`tf.nn.softsign(features, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/nn/softsign)
- [`tf.ones_like(input, dtype, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/ones_like)
- [`tf.strings.as_string(input, precision, scientific, shortest, width, fill, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/as_string)
- [`tf.strings.length(input, unit, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/length)
- [`tf.strings.lower(input, encoding, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/lower)
- [`tf.strings.regex_full_match(input, pattern, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/regex_full_match)
- [`tf.strings.regex_replace(input, pattern, rewrite, replace_global, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/regex_replace)
- [`tf.strings.strip(input, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/strip)
- [`tf.strings.substr(input, pos, len, unit, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/substr)
- [`tf.strings.to_hash_bucket(input, num_buckets, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/to_hash_bucket)
- [`tf.strings.to_hash_bucket_fast(input, num_buckets, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/to_hash_bucket_fast)
- [`tf.strings.to_hash_bucket_strong(input, num_buckets, key, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/to_hash_bucket_strong)
- [`tf.strings.to_number(input, out_type, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/to_number)
- [`tf.strings.unicode_script(input, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/unicode_script)
- [`tf.strings.unicode_transcode(input, input_encoding, output_encoding, errors, replacement_char, replace_control_characters, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/unicode_transcode)
- [`tf.strings.upper(input, encoding, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/strings/upper)
- [`tf.zeros_like(input, dtype, name)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/zeros_like)