XLA編譯器用於JIT加速

最近遇到了模型性能方面的問題,調研中關於JIT(just in time)即時編譯一些知識點進行介紹:

概述

       XLA(加速線性代數)是用於優化TensorFlow計算的線性代數的域特定編譯器。代碼位置在tensorflow/compiler.

       在XLA技術之前,TensorFlow中計算圖的執行是由runtime(運行時)代碼驅動的:runtime負責加載計算圖定義、創建計算圖、計算圖分區、計算圖優化、分配設備、管理節點間的依賴並調度節點kernel的執行;計算圖是數據部分,runtime是代碼部分。在XLA出現之後,我們有了另一個選擇,計算圖現在可以直接被編譯成目標平臺的可執行代碼,可以直接執行,不需要runtime代碼的參與了。

       XLA 利用 JIT 編譯技術分析用戶在運行時創建的 TensorFlow 圖表,根據實際運行時維度和類型將其專門化,將多個運算融合在一起併爲它們生成高效的本機代碼——適用於 CPU、GPU 之類的設備和自定義加速器(例如,Google 的 TPU)。

       目前XLA是實驗性的。大多數使用情況在性能(加快速度或減少內存使用)方面都沒有改進。

代碼示例

代碼來自tenorflow源碼下的tensorflow\examples\tutorials\mnist\mnist_softmax_xla.py

這份代碼原理和前面幾篇博客類似,相通的知識點就不特別說了。

開啓JIT編譯

在會話級別打開JIT方法如下:

方式一,通過Session設置

# Config to turn on JIT compilation
config = tf.ConfigProto()
config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1

sess = tf.Session(config=config)

方式二,通過tf.contrib.compiler.jit.experimental_jit_scope()

jit_scope = tf.contrib.compiler.jit.experimental_jit_scope

x = tf.placeholder(np.float32)
with jit_scope():
   y = tf.add(x, x)  # The "add" will be compiled with XLA.

方式三,通過設置device:

with tf.device("/job:localhost/replica:0/task:0/device:XLA_GPU:0"):
  output = tf.add(input1, input2)

記錄元數據和timeline文件

元數據用於記錄運行過程的時間和內存消耗。把這些信息導出來,可以保存爲timeline文件,用chrome瀏覽器查看。

run_metadata = tf.RunMetadata()
    sess = tf.Session(config=config)

    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)

前面寫過博客把元數據寫到tensorboard事件日誌裏了。

這裏寫到磁盤上timeline文件裏。這個文件是jason格式的,可以使用chrome可視化。在chrome瀏覽器打開"chrome://tracing",把文件拖到頁面上打開,可以看到運行的時間。這個和android用於分析性能的界面類似。

完整代碼

我增加了把計算圖結構寫入tensorboard文件的代碼。其它基本未變。

"""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
  mnist = input_data.read_data_sets(FLAGS.data_dir)

  # Create the model
  x = tf.placeholder(tf.float32, [None, 784])
  w = tf.Variable(tf.zeros([784, 10]))
  b = tf.Variable(tf.zeros([10]))
  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)


  writer = tf.summary.FileWriter( FLAGS.log_dir + '/train', sess.graph )
  writer.close()

  # Train
  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='./data',
      help='Directory for storing input data')
  parser.add_argument(
      '--xla', type=bool, default=True, help='Turn xla via JIT on')
  parser.add_argument(
      '--log_dir',
      type=str,
      default='./logs',
      help='Directory to put the log data.'
  )
  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章