# 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.7.4/tensorflow/python/util/dispatch.py#L736-L801) |

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.7/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:

    from tensorflow.python.framework import extension_type
    class MaskedTensor(extension_type.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=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/bitwise/invert)
- [`tf.cast(x, dtype, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/cast)
- [`tf.clip_by_value(t, clip_value_min, clip_value_max, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/clip_by_value)
- [`tf.compat.v1.ones_like(tensor, dtype=None, name=None, optimize=True)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/compat/v1/ones_like)
- `tf.compat.v1.strings.length(input, name=None, unit='BYTE')`
- `tf.compat.v1.strings.substr(input, pos, len, name=None, unit='BYTE')`
- [`tf.compat.v1.strings.to_hash_bucket(string_tensor=None, num_buckets=None, name=None, input=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/compat/v1/string_to_hash_bucket)
- `tf.compat.v1.substr(input, pos, len, name=None, unit='BYTE')`
- [`tf.compat.v1.zeros_like(tensor, dtype=None, name=None, optimize=True)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/compat/v1/zeros_like)
- [`tf.debugging.check_numerics(tensor, message, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/debugging/check_numerics)
- [`tf.dtypes.saturate_cast(value, dtype, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/dtypes/saturate_cast)
- [`tf.io.decode_base64(input, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/io/decode_base64)
- `tf.io.decode_compressed(bytes, compression_type='', name=None)`
- [`tf.io.encode_base64(input, pad=False, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/io/encode_base64)
- [`tf.math.abs(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/abs)
- [`tf.math.acos(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/acos)
- [`tf.math.acosh(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/acosh)
- [`tf.math.angle(input, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/angle)
- [`tf.math.asin(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/asin)
- [`tf.math.asinh(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/asinh)
- [`tf.math.atan(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/atan)
- [`tf.math.atanh(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/atanh)
- [`tf.math.bessel_i0(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/bessel_i0)
- [`tf.math.bessel_i0e(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/bessel_i0e)
- [`tf.math.bessel_i1(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/bessel_i1)
- [`tf.math.bessel_i1e(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/bessel_i1e)
- [`tf.math.ceil(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/ceil)
- [`tf.math.conj(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/conj)
- [`tf.math.cos(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/cos)
- [`tf.math.cosh(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/cosh)
- [`tf.math.digamma(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/digamma)
- [`tf.math.erf(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/erf)
- [`tf.math.erfc(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/erfc)
- [`tf.math.erfcinv(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/erfcinv)
- [`tf.math.erfinv(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/erfinv)
- [`tf.math.exp(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/exp)
- [`tf.math.expm1(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/expm1)
- [`tf.math.floor(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/floor)
- [`tf.math.imag(input, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/imag)
- [`tf.math.is_finite(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/is_finite)
- [`tf.math.is_inf(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/is_inf)
- [`tf.math.is_nan(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/is_nan)
- [`tf.math.lgamma(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/lgamma)
- [`tf.math.log(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/log)
- [`tf.math.log1p(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/log1p)
- [`tf.math.log_sigmoid(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/log_sigmoid)
- [`tf.math.logical_not(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/logical_not)
- [`tf.math.ndtri(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/ndtri)
- [`tf.math.negative(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/negative)
- [`tf.math.nextafter(x1, x2, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/nextafter)
- [`tf.math.real(input, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/real)
- [`tf.math.reciprocal(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/reciprocal)
- [`tf.math.reciprocal_no_nan(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/reciprocal_no_nan)
- [`tf.math.rint(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/rint)
- [`tf.math.round(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/round)
- [`tf.math.rsqrt(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/rsqrt)
- [`tf.math.sigmoid(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/sigmoid)
- [`tf.math.sign(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/sign)
- [`tf.math.sin(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/sin)
- [`tf.math.sinh(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/sinh)
- [`tf.math.softplus(features, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/softplus)
- [`tf.math.special.bessel_j0(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/bessel_j0)
- [`tf.math.special.bessel_j1(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/bessel_j1)
- [`tf.math.special.bessel_k0(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/bessel_k0)
- [`tf.math.special.bessel_k0e(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/bessel_k0e)
- [`tf.math.special.bessel_k1(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/bessel_k1)
- [`tf.math.special.bessel_k1e(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/bessel_k1e)
- [`tf.math.special.bessel_y0(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/bessel_y0)
- [`tf.math.special.bessel_y1(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/bessel_y1)
- [`tf.math.special.dawsn(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/dawsn)
- [`tf.math.special.expint(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/expint)
- [`tf.math.special.fresnel_cos(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/fresnel_cos)
- [`tf.math.special.fresnel_sin(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/fresnel_sin)
- [`tf.math.special.spence(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/special/spence)
- [`tf.math.sqrt(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/sqrt)
- [`tf.math.square(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/square)
- [`tf.math.tan(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/tan)
- [`tf.math.tanh(x, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/math/tanh)
- [`tf.ones_like(input, dtype=None, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/ones_like)
- `tf.strings.as_string(input, precision=-1, scientific=False, shortest=False, width=-1, fill='', name=None)`
- `tf.strings.length(input, unit='BYTE', name=None)`
- [`tf.strings.regex_full_match(input, pattern, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/strings/regex_full_match)
- [`tf.strings.regex_replace(input, pattern, rewrite, replace_global=True, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/strings/regex_replace)
- [`tf.strings.strip(input, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/strings/strip)
- `tf.strings.substr(input, pos, len, unit='BYTE', name=None)`
- [`tf.strings.to_hash_bucket(input, num_buckets, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/strings/to_hash_bucket)
- [`tf.strings.to_hash_bucket_fast(input, num_buckets, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/strings/to_hash_bucket_fast)
- [`tf.strings.to_hash_bucket_strong(input, num_buckets, key, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/strings/to_hash_bucket_strong)
- [`tf.strings.to_number(input, out_type=tf.float32, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/strings/to_number)
- [`tf.strings.unicode_script(input, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/strings/unicode_script)
- [`tf.zeros_like(input, dtype=None, name=None)`](https://www.tensorflow.org/versions/r2.7/api_docs/python/tf/zeros_like)