运行 Google Colab 环境中的云 TPU 分析器

Running cloud TPU profiler in Google Colab environment

我正在 运行使用 Google Colab 笔记本并尝试捕获 TPU 分析数据以用于 TensorBoard,但是我无法获得 capture_tpu_profile 到 运行 在后台 运行 运行我的 TensorFlow 代码。

到目前为止,我尝试 运行 在后台捕获进程:

!capture_tpu_profile --logdir=gs://<my_logdir> --tpu=$COLAB_TPU_ADDR &

!bg capture_tpu_profile --logdir=gs://<my_logdir> --tpu=$COLAB_TPU_ADDR

原来一个方法是像这样直接从 python 开始进程(我还必须将参数从 --tpu 修改为 --service_addr):

import subprocess
subprocess.Popen(["capture_tpu_profile","--logdir=gs://<my_logdir>", "--service_addr={}".format(os.environ['COLAB_TPU_ADDR'])])

check=True 使命令在失败时引发异常。

一种方法是使用 TPUProfilerHook

https://github.com/tensorflow/tpu/blob/master/models/common/tpu_profiler_hook.py

它将探查器作为会话挂钩运行。

此处示例https://github.com/tensorflow/tpu/blob/5d838047af0163bdf7b97b9404648dc2961c4b63/models/official/resnet/resnet_main.py#L699