Using the Tensorflow profiler with tf.Estimator
Asked Answered
L

3

6

I need to use the Tensorflow profiler to profile some code that is running slowly for some reason. Unfortunately, the code in question uses tf.Estimator, so I can't figure out how to inject the run metadata object into the session run() call in order to get the information that the profiler needs.

What should I do?

Latex answered 10/11, 2017 at 1:15 Comment(0)
C
4

tf.estimator use tf.train.ProfilerHook works!

just add a ProfilerHook in TrainSpec hooks!

hook = tf.train.ProfilerHook(
    save_steps=20,
    output_dir=os.path.join(args.model_dir, "tracing"),
    show_dataflow=True,
    show_memory=True)
hooks = [hook]
train_spec = tf.estimator.TrainSpec(
    hooks=hooks,
    input_fn=lambda: input_fn())

then, you could get the tracing file like timeline-{}.json in model_dir/tracing, and open chrome chrome://tracing to visual!

refs: https://mcmap.net/q/916316/-how-to-display-runtime-statistics-in-tensorboard-using-estimator-api-in-a-distributed-environment

Counterstatement answered 11/12, 2019 at 7:28 Comment(0)
D
2

with tf.contrib.tfprof.ProfileContext('/tmp/train_dir', dump_steps=[10]) as pctx: estimator.train() # any thing you want to profile

Then you will get a file at /tmp/train_dir/profile_10

Arguments are defined in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/profiler/profile_context.py

Dynamite answered 24/10, 2018 at 23:33 Comment(0)
L
0

Use ProfileContext, as described here: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler . This allows you to profile without needing to get access to the session.

Latex answered 10/11, 2017 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.