# tf.xla.experimental.jit_scope

<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/compiler/xla/jit.py#L36-L156) |

Enable or disable JIT compilation of operators within the scope.

#### View aliases


**Compat aliases for migration**

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

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

<br />

    @contextlib.contextmanager
    tf.xla.experimental.jit_scope(
        compile_ops=True, separate_compiled_gradients=False
    )

> [!NOTE]
> **Note:** This is an experimental feature.

The compilation is a hint and only supported on a best-effort basis.

<br />

<br />

<br />

| ## Example usage ||
|---|---|
| <br /> with tf.xla.experimental.jit_scope(): c = tf.matmul(a, b)  # compiled with tf.xla.experimental.jit_scope(compile_ops=False): d = tf.matmul(a, c)  # not compiled with tf.xla.experimental.jit_scope( compile_ops=lambda node_def: 'matmul' in node_def.op.lower()): e = tf.matmul(a, b) + d  # matmul is compiled, the addition is not. <br /> ||

<br />

Example of `separate_compiled_gradients`:

      # In the example below, the computations for f, g and h will all be compiled
      # in separate scopes.
      with tf.xla.experimental.jit_scope(
          separate_compiled_gradients=True):
        f = tf.matmul(a, b)
      g = tf.gradients([f], [a, b], name='mygrads1')
      h = tf.gradients([f], [a, b], name='mygrads2')

Ops that are not in the scope may be clustered and compiled with ops in
the scope with `compile_ops=True`, while the ops in the scope with
`compile_ops=False` will never be compiled.

<br />

<br />

<br />

| ## For example ||
|---|---|
| <br /> # In the example below, x and loss may be clustered and compiled together, # while y will not be compiled. with tf.xla.experimental.jit_scope(): x = tf.matmul(a, b) with tf.xla.experimental.jit_scope(compile_ops=False): y = tf.matmul(c, d) loss = x + y <br /> ||

<br />

If you want to only compile the ops in the scope with `compile_ops=True`,
consider adding an outer `jit_scope(compile_ops=False)`:

      # In the example below, only x will be compiled.
      with tf.xla.experimental.jit_scope(compile_ops=False):
        with tf.xla.experimental.jit_scope():
          x = tf.matmul(a, b)
        y = tf.matmul(c, d)
        loss = x + y

<br />

<br />

<br />

| ## Args ||
|---|---|
| `compile_ops` | Whether to enable or disable compilation in the scope. Either a Python bool, or a callable that accepts the parameter `node_def` and returns a python bool. |
| `separate_compiled_gradients` | If true put each gradient subgraph into a separate compilation scope. This gives fine-grained control over which portions of the graph will be compiled as a single unit. Compiling gradients separately may yield better performance for some graphs. The scope is named based on the scope of the forward computation as well as the name of the gradients. As a result, the gradients will be compiled in a scope that is separate from both the forward computation, and from other gradients. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `RuntimeError` | if called when eager execution is enabled. |

<br />

<br />

<br />

<br />

| ## Yields ||
|---|---|
| The current scope, enabling or disabling compilation. ||

<br />