<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/profiler/trace.py#L27-L123) |

Context manager that generates a trace event in the profiler.

    tf.profiler.experimental.Trace(
        name, **kwargs
    )

A trace event will start when entering the context, and stop and save the
result to the profiler when exiting the context. Open TensorBoard Profile tab
and choose trace viewer to view the trace event in the timeline.

Trace events are created only when the profiler is enabled. More information
on how to use the profiler can be found at
<https://tensorflow.org/guide/profiler>

#### Example usage:

    tf.profiler.experimental.start('logdir')
    for step in range(num_steps):
      # Creates a trace event for each training step with the step number.
      with tf.profiler.experimental.Trace("Train", step_num=step, _r=1):
        train_fn()
    tf.profiler.experimental.stop()

<br />

<br />

<br />

| ## Args ||
|---|---|
| `name` | The name of the trace event. |
| `**kwargs` | Keyword arguments added to the trace event. Both the key and value are of types that can be converted to strings, which will be interpreted by the profiler according to the traceme name. <br /> Example usage: tf.profiler.experimental.start('logdir') for step in range(num_steps): # Creates a trace event for each training step with the # step number. with tf.profiler.experimental.Trace("Train", step_num=step): train_fn() tf.profiler.experimental.stop() The example above uses the keyword argument "step_num" to specify the training step being traced. |

<br />

## Methods

### `set_metadata`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/profiler/trace.py#L87-L119)

    set_metadata(
        **kwargs
    )

Sets metadata in this trace event.

<br />

<br />

<br />

| Args ||
|---|---|
| `**kwargs` | metadata in key-value pairs. |

<br />

This method enables setting metadata in a trace event after it is
created.

#### Example usage:


      def call(function):
        with tf.profiler.experimental.Trace("call",
             function_name=function.name) as tm:
          binary, in_cache = jit_compile(function)
          tm.set_metadata(in_cache=in_cache)
          execute(binary)

In this example, we want to trace how much time spent on
calling a function, which includes compilation and execution.
The compilation can be either getting a cached copy of the
binary or actually generating the binary, which is indicated
by the boolean "in_cache" returned by jit_compile(). We need
to use set_metadata() to pass in_cache because we did not know
the in_cache value when the trace was created (and we cannot
create the trace after jit_compile(), because we want
to measure the entire duration of call()).

### `__enter__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/profiler/trace.py#L83-L85)

    __enter__()

### `__exit__`

[View source](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/profiler/trace.py#L121-L123)

    __exit__(
        exc_type, exc_val, exc_tb
    )