Enable or disable JIT compilation of operators within the scope. NOTE: This is an experimental feature. The compilation is a hint and only supported on a best-effort basis. Example usage: with tf.xla.experimental.jit_scope(): c = tf.matmul(a, b) # compiled with tf.xla.experim
(compile_ops=True, separate_compiled_gradients=False)
| 40 | @contextlib.contextmanager |
| 41 | @tf_export("xla.experimental.jit_scope") |
| 42 | def experimental_jit_scope(compile_ops=True, separate_compiled_gradients=False): |
| 43 | """Enable or disable JIT compilation of operators within the scope. |
| 44 | |
| 45 | NOTE: This is an experimental feature. |
| 46 | |
| 47 | The compilation is a hint and only supported on a best-effort basis. |
| 48 | |
| 49 | Example usage: |
| 50 | with tf.xla.experimental.jit_scope(): |
| 51 | c = tf.matmul(a, b) # compiled |
| 52 | with tf.xla.experimental.jit_scope(compile_ops=False): |
| 53 | d = tf.matmul(a, c) # not compiled |
| 54 | with tf.xla.experimental.jit_scope( |
| 55 | compile_ops=lambda node_def: 'matmul' in node_def.op.lower()): |
| 56 | e = tf.matmul(a, b) + d # matmul is compiled, the addition is not. |
| 57 | |
| 58 | Example of separate_compiled_gradients: |
| 59 | # In the example below, the computations for f, g and h will all be compiled |
| 60 | # in separate scopes. |
| 61 | with tf.xla.experimental.jit_scope( |
| 62 | separate_compiled_gradients=True): |
| 63 | f = tf.matmul(a, b) |
| 64 | g = tf.gradients([f], [a, b], name='mygrads1') |
| 65 | h = tf.gradients([f], [a, b], name='mygrads2') |
| 66 | |
| 67 | Args: |
| 68 | compile_ops: Whether to enable or disable compilation in the scope. |
| 69 | Either a Python bool, or a callable that accepts the parameter |
| 70 | `node_def` and returns a python bool. |
| 71 | separate_compiled_gradients: If true put each gradient subgraph into a |
| 72 | separate compilation scope. This gives fine-grained control over which |
| 73 | portions of the graph will be compiled as a single unit. Compiling |
| 74 | gradients separately may yield better performance for some graphs. |
| 75 | The scope is named based on the scope of the forward computation as well |
| 76 | as the name of the gradients. As a result, the gradients will be compiled |
| 77 | in a scope that is separate from both the forward computation, and from |
| 78 | other gradients. |
| 79 | Raises: |
| 80 | RuntimeError: if called when eager execution is enabled. |
| 81 | Yields: |
| 82 | The current scope, enabling or disabling compilation. |
| 83 | """ |
| 84 | if context.executing_eagerly(): |
| 85 | raise RuntimeError("xla.experimental.jit_scope is not supported when eager " |
| 86 | "execution is enabled. Try use it inside tf.function.") |
| 87 | |
| 88 | if callable(compile_ops): |
| 89 | def xla_compile(node_def): |
| 90 | return attr_value_pb2.AttrValue(b=compile_ops(node_def)) |
| 91 | else: |
| 92 | xla_compile = attr_value_pb2.AttrValue(b=compile_ops) |
| 93 | |
| 94 | attrs = { |
| 95 | "_XlaCompile": |
| 96 | xla_compile, |
| 97 | "_XlaSeparateCompiledGradients": |
| 98 | attr_value_pb2.AttrValue(b=bool(separate_compiled_gradients)) |
| 99 | } |
nothing calls this directly
no test coverage detected