tensorflow XLA 不生成点文件 [英] tensorflow XLA not producing the dot file

查看:28
本文介绍了tensorflow XLA 不生成点文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习有关 XLA 和 JIT 的教程(https://www.tensorflow.org/performance/xla/jit).根据 https://www.tensorflow.org/performance/xla/jit#step_3_run_with_xla,当我运行命令

I am trying to follow the tutorial on XLA and JIT (https://www.tensorflow.org/performance/xla/jit). According to https://www.tensorflow.org/performance/xla/jit#step_3_run_with_xla, when I run the command

https://www.tensorflow.org/performance/xla/jit#step_3_run_with_xla

它应该产生一个带有 XLA 图形位置的输出.但是,我的输出不包含此信息.

It should produce an output with the location to the XLA graph. However, my output does not include this info.

Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz
Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz
Extracting /tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz
Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz
0.9172

仅生成时间线文件.

构建:Tensor Flow r1.3,带有用于 CPU 的 XLA JIT

Build: Tensor flow r1.3 with XLA JIT for CPU

推荐答案

完整的命令应该是

TF_XLA_FLAGS="--xla_hlo_graph_path=./tmp_dot --xla_generate_hlo_graph=.*" python mnist_softmax_xla.py

从源代码构建时确保有 xla 选项.以及请求设备.即

Make sure to have xla option when building from source. As well as requesting the device. ie

with tf.device("/job:localhost/replica:0/task:0/device:XLA_CPU:0"):

同样给出的例子使用了 tf.Variable,应该用 tf.get_variable 代替.

Also the example given uses tf.Variable, it should be replaced with tf.get_variable.

完整代码如下:

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at 
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
==============================================================================
"""Simple MNIST classifier example with JIT XLA and timelines.

"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.client import timeline

FLAGS = None


def main(_):
  # Import data
  with tf.device("/job:localhost/replica:0/task:0/device:XLA_CPU:0"):
    mnist = input_data.read_data_sets(FLAGS.data_dir)

    # Create the model
    x = tf.placeholder(tf.float32, [None, 784])
    w = tf.get_variable("w",initializer=tf.zeros([784, 10]),use_resource=True)
    b = tf.get_variable("b",initializer=tf.zeros([10]),use_resource=True)
    y = tf.matmul(x, w) + b

    # Define loss and optimizer
    y_ = tf.placeholder(tf.int64, [None])

    # The raw formulation of cross-entropy,
    #
    #   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
    #                                 reduction_indices=[1]))
    #
    # can be numerically unstable.
    #
    # So here we use tf.losses.sparse_softmax_cross_entropy on the raw
    # logit outputs of 'y', and then average across the batch.
    cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=y_, logits=y)
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

    config = tf.ConfigProto()
    jit_level = 0
    if FLAGS.xla:
      # Turns on XLA JIT compilation.
      jit_level = tf.OptimizerOptions.ON_1

    config.graph_options.optimizer_options.global_jit_level = jit_level
    run_metadata = tf.RunMetadata()
    sess = tf.Session(config=config)
    tf.global_variables_initializer().run(session=sess)
    # Train
    g = tf.Graph()
    print(dir(g))
    train_loops = 1000
    for i in range(train_loops):
      batch_xs, batch_ys = mnist.train.next_batch(100)

      # Create a timeline for the last loop and export to json to view with
      # chrome://tracing/.
      if i == train_loops - 1:
        sess.run(train_step,
                 feed_dict={x: batch_xs,
                            y_: batch_ys},
                 options=tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE),
                 run_metadata=run_metadata)
        trace = timeline.Timeline(step_stats=run_metadata.step_stats)
        with open('timeline.ctf.json', 'w') as trace_file:
          trace_file.write(trace.generate_chrome_trace_format())
      else:
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

    # Test trained model
    correct_prediction = tf.equal(tf.argmax(y, 1), y_)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print(sess.run(accuracy,
                   feed_dict={x: mnist.test.images,
                              y_: mnist.test.labels}))
    sess.close()


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
      '--data_dir',
      type=str,
      default='/tmp/tensorflow/mnist/input_data',
      help='Directory for storing input data')
  parser.add_argument(
      '--xla', type=bool, default=True, help='Turn xla via JIT on')
  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

这篇关于tensorflow XLA 不生成点文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆