57
Deep Learning Acceleration 勉強会 2017/9/3 TensorFlow XLAの可能性 TensorFlow r1.3で導入さ れたpluginにて、いろいろな ハードウェアへの対応がで きるようになる! @Vengineer

TensorFlow XLAの可能性

Embed Size (px)

Citation preview

Page 1: TensorFlow XLAの可能性

Deep Learning Acceleration 勉強会2017/9/3

TensorFlow XLAの可能性

TensorFlow r1.3で導入されたpluginにて、いろいろなハードウェアへの対応ができるようになる! @Vengineer

Page 2: TensorFlow XLAの可能性

CQ出版社:インターフェース 8月号、9月号にTensorFlow XLAのAOTについての記事を書きました。

8月号:衝撃的な性能UPの可能性を秘めた注目テクノロジ速報

AIをサクサク動かすGoogle新機能TensorFlow「XLA」を探る9月号:最新テクノロジ・マニアの挑戦 ...AIサクサク用TensorFlow XLA AOTコンパイラ探訪

初めてのGoogleソースコード!AI用コンパイラの可能性を探る

ブログ : Vengineerの戯言http://blogs.yahoo.co.jp/verification_engineer

Twitter : @Vengineer自己紹介

Page 3: TensorFlow XLAの可能性

Design Solution Forum2017年10月13日(金)開催@新横浜

今年で4年目、毎年500名を越える来場者

絶賛、申込受付中

http://www.dsforum.jp/

「ディーブラーニングトラック」

「RISC-Vトラック」

それぞれ5講演の予定

Page 4: TensorFlow XLAの可能性

TensorFlow XLAとはhttps://www.tensorflow.org/performance/xla/

XLA(Accelerated Linear Algebra)は、TensorFlow計算を最適化する線形代数のドメイン固有のコンパイラです。 結果として、サーバーおよびモバイルプラットフォームでの速度、メモリ使用率、移植性が向上します。 当初、ほとんどのユーザーはXLAの大きなメリットは見られませんが、JIT(Just-In-Time)コンパイルやAOT(Ahead-Of-Time)コンパイルを使用してXLAを使用することで実験を開始できます。 新しいハードウェアアクセラレータをターゲットとする開発者は、XLAを試すことを特にお勧めします。

原文(英語)をそのまま、Google翻訳にお願いしました。

Page 5: TensorFlow XLAの可能性

TensorFlow w/XLA: TensorFlow, Compiled! Expressiveness with performancehttps://autodiff-workshop.github.io/slides/JeffDean.pdf

XLA対応のデバイス

Page 6: TensorFlow XLAの可能性

ブログにも書きました

TensorFlow XLAの衝撃2017年2月20日

http://blogs.yahoo.co.jp/verification_engineer/71016304.html

Page 7: TensorFlow XLAの可能性

TensorFlow User Group ハード部 #2https://tfug-tokyo.connpass.com/event/54426/

TensorFlow XLAは、

中で何をやっているのか?2017年4月21日

https://blogs.yahoo.co.jp/verification_engineer/71103781.html

Page 8: TensorFlow XLAの可能性

CQ出版社インターフェース 8月号、9月号に

TensorFlow XLAのAOTについての記事を書きまし

8月号:衝撃的な性能UPの可能性を秘めた注目テクノロジ速報

AIをサクサク動かすGoogle新機能TensorFlow「XLA」を探る

9月号:最新テクノロジ・マニアの挑戦 ...AIサクサク用TensorFlow XLA AOTコンパイラ探訪

初めてのGoogleソースコード!AI用コンパイラの可能性を探る

Page 9: TensorFlow XLAの可能性

また、ブログにも書きました

TensorFlow XLAに動きあり2017年7月3日

https://blogs.yahoo.co.jp/verification_engineer/71198977.html

Page 10: TensorFlow XLAの可能性

日経エレクトロニクス、2017年9月号の記事、ソニーも参戦、深層学習ソフト 組み込み向けの開発環境で競う

日経エレクトロニクスの

    取材を受けましたよ2017年8月19日

https://blogs.yahoo.co.jp/verification_engineer/71267571.html

Page 11: TensorFlow XLAの可能性
Page 12: TensorFlow XLAの可能性

これからお話する内容

0)、Pythonの式からTensorFlowグラフが  どう変形されるかを見ていきます

1)、JIT (Just-In-Time) コンパイルただし、単一マシンのみで、GPUは1つ

2)、AOT (Ahead-Of-Time) コンパイルCPUのみx86-64/ARM/AARCH64/PowerPCCQ出版インターフェースを見てね!

Page 13: TensorFlow XLAの可能性

Using JIT Compilationhttps://www.tensorflow.org/performance/xla/jit

TensorFlow/XLA JITコンパイラは、XLAを使用してTensorFlowグラフの一部をコンパイルして実行します。 この標準的なTensorFlow実装の利点は、XLAが複数の演算子(カーネル融合)を少数のコンパイル済みカーネルに融合できることです。 TensorFlow Executorsが実行するように、演算子を融合させることで、メモリ帯域幅の要件を減らし、演算子を1つずつ実行するよりもパフォーマンスを向上させることができます。

原文(英語)をそのまま、Google翻訳にお願いしました。

Page 14: TensorFlow XLAの可能性

サンプルコードで確認してみよう

Page 15: TensorFlow XLAの可能性

デバイスを gpu にすると

def test_gpu(self):

with tf.Session() as sess: x = tf.placeholder(tf.float32, [2], name="x") with tf.device("gpu"): y = x * 2 result = sess.run(y, {x: [1.5, 0.5]})

Page 16: TensorFlow XLAの可能性

Session.runの動きpython/client/session.py

SessionInterface => BaseSession => Sessiondef run( self, fetches, feed_dict=None, options=None, run_metadata=None );

_run _do_run  tf_session.TF_Run

 ここからC++の世界

c/c_api.ccのTF_Run関数

   c/c_api.ccのTF_Run_Helper関数

      Session::run (core/public/session.h) DirectSession::Run

 

Page 17: TensorFlow XLAの可能性

C++のDirectSession::RunDirectSession::Run (core/common_runtime/direct_session.cc)

Executorを生成する

GetOrCreateExecutors(pool, input_tensor_names, output_names, target_nodes, &executors_and_keys, &run_state_args));

Executorは複数あり

各Executorが独立して実行し

各Executor間の通信は非同期に行われる 

Page 18: TensorFlow XLAの可能性

C++のDirectSession::Runの続き

DirectSession::Run (core/common_runtime/direct_session.cc)

実行部分のところ

for (const auto& item : executors_and_keys->items) { item.executor->RunAsync(args, barrier->Get()); }  Executorが非同期に実行される

すべてExecutorの実行が終了するまで待つ

WaitForNotification(&run_state, &step_cancellation_manager, run_options.timeout_in_ms() > 0 ? run_options.timeout_in_ms() : operation_timeout_in_ms_);

 

Page 19: TensorFlow XLAの可能性

executor->RunAsync

Executor::RunAync (core/common_runtime/executor.h) ExecuteImple::RunAsync ExecuteState::RunAsync ExecuteState::ScheduleReady ExecuteState::Process (core/common_runtime/executor.cc)

 ・device->Compute 

ここ、後で出てくるので覚えておいてね!

Page 20: TensorFlow XLAの可能性

0)、最初

Mul

Const

Feed(x)

Fetch(y)

Page 21: TensorFlow XLAの可能性

1)、Feed/Fetchノードの追加

Mul

_Recv

Const

_Send

Feed(x)

Fetch(y)

Page 22: TensorFlow XLAの可能性

2)、Placement

Mul

_Recv

Const

_Send

cpu : Feed(x)

cpu : Fetch(y)

gpu

gpu

Page 23: TensorFlow XLAの可能性

3)、グラフの分割

_Recv

_Send

_Send _Recv _Send

gpu

Feed(x) Fetch(y)cpu

MulConst

_Recv

Page 24: TensorFlow XLAの可能性

gpu を XLA_GPU に変更

def testXLA_JIT(self):

with tf.Session() as sess:

x = tf.placeholder(tf.float32, [2], name="x") with tf.device("device:XLA_GPU:0"): y = x * 2 result = sess.run(y, {x: [1.5, 0.5]})

Page 25: TensorFlow XLAの可能性

0)、最初

Mul

Const

Feed(x)

Fetch(y)

Page 26: TensorFlow XLAの可能性

1)、Feed/Fetchノードの追加

Mul

_Recv

Const

_Send

Feed(x)

Fetch(y)

Page 27: TensorFlow XLAの可能性

2)、Placement

Mul

_Recv

Const

_Send

cpu : Feed(x)

cpu : Fetch(y)

XLA_GPU

XLA_GPU

Page 28: TensorFlow XLAの可能性

3)、グラフの分割

_Recv

_Send

_Send _Recv _Send

XLA_GPU

Feed(x) Fetch(y)cpu

MulConst

_Recv

Page 29: TensorFlow XLAの可能性

3)、グラフの分割

_XlaLaunch

_Recv

_Recv _Send

_Send _Recv _Send

XLA_GPU

Feed(x) Fetch(y)cpu

Page 30: TensorFlow XLAの可能性

複数Opsを_XlaLaunch Opに変換

_XlaLaunch

XLA_GPU

MulConst

gpu

Page 31: TensorFlow XLAの可能性

ええええ、なんで、_XlaLaunch

になっちゃうの?

どうして?

Page 32: TensorFlow XLAの可能性

_XlaLaunch Op って?

Page 33: TensorFlow XLAの可能性

TensorFlow XLA : JITでは!

同じデバイス内で実行できるSubgraph単位のノードをギュギュッと1つにまとめて、

_XlaLaunch Op

内で実行する_XlaLaunchは、TensorFlow XLA専用のOpとして実装

Page 34: TensorFlow XLAの可能性

Adding a New Ophttps://www.tensorflow.org/versions/master/how_tos/adding_an_op/

必要なものは、

 ・Register the new Op in a C++ file ・Implement the Op in C++

 ・Optionally, create a Python wrapper ・Optionally, write a function to compute gradients for the Op ・Test the Op, typically in Python

Page 35: TensorFlow XLAの可能性

_XlaLaunch Opで実装は?

・Register the new Op in a C++ file・Implement the Op in C++

compiler/jit/kernels/xla_local_launch_op.h compiler/jit/kernels/xla_local_launch_op.cc

Page 36: TensorFlow XLAの可能性

_XlaLaunch Op の登録

REGISTER_OP("_XlaLaunch") .Input("constants: Tconstants") .Attr("Tconstants: list(type) >= 0") .Input("args: Targs") .Attr("Targs: list(type) >= 0") .Output("results: Tresults") .Attr("Tresults: list(type) >= 0") .Attr("function: func") .Doc("XLA Launch Op. For use by the XLA JIT only.");

Page 37: TensorFlow XLAの可能性

_XlaLaunch Op の実装

class XlaDeviceLaunchOp : public OpKernel { public: explicit XlaDeviceLaunchOp(OpKernelConstruction* ctx); ~XlaDeviceLaunchOp() override; void Compute(OpKernelContext* ctx) override;

覚えていましたか? device->Compute ですよ!

private: .... TF_DISALLOW_COPY_AND_ASSIGN(XlaDevicelLaunchOp);};

Page 38: TensorFlow XLAの可能性

XlaDeviceLaunchOp::Compute ・XlaCompilationCacheクラスのインスタンス(compiler)を生成

 ・_XlaLaunch Op内で実行する一連の関数群をコンパイル

cache>Compile( …. );

 ・各種パラメータ&入力リストをXLA用データに変換

 ・キャッシュの生成&実行

cache->client()->Execute(.....); 

 ・XLA用データを出力リストに変換

Page 39: TensorFlow XLAの可能性

TensorFlow w/XLA: TensorFlow, Compiled! Expressiveness with performancehttps://autodiff-workshop.github.io/slides/JeffDean.pdf

XlaLocalLaunchOp::Computeの処理

ここに LLVM を使っている

cache->Compile cache->cilent()->Execute

Page 40: TensorFlow XLAの可能性

Plugin

Intel NervanaGraphcore

もXLAをサポートするかも?

Page 41: TensorFlow XLAの可能性

Intel® Nervana™ Graph Beta2017-06-22

TensorFlow/XLA Support

https://www.intelnervana.com/intel-nervana-graph-and-neon-3-0-updates/

Intel® Nervana™ Graph: A Universal Tensor JIT Compiler Webinar

https://software.seek.intel.com/IntelNervanaGraphWebinar_Reg詳細は、このWebinarの資料を見てね!

Page 42: TensorFlow XLAの可能性

Graphcore

https://www.nextplatform.com/2017/05/08/dive-deep-learning-chip-startup-graphcores-software-stack/

TensorFlow XLAのPluginのオリジナルコードは、Graphcore

TensorFlow:Remove copyright on non-poplar fileshttps://github.com/tensorflow/tensorflow/commit/679152e2c13229db9386fe5c3a267e63d0093889

Page 43: TensorFlow XLAの可能性

TensorFlow XLA Google Group

https://groups.google.com/forum/m/#!forum/xla-dev

GraphcoreIntel Nervana

Knuedgeから投稿あり

当然、Googleの中の人も

Page 44: TensorFlow XLAの可能性

compiler/plugin/executor

  ・BUILD ・device.cc

・compiler.{cc, h}・executable.{cc, h}・executor.{cc, h}・platform.{cc, h}・platform_id.h・transfer_manager.{cc, h}

Page 45: TensorFlow XLAの可能性

TensorFlow w/XLA: TensorFlow, Compiled! Expressiveness with performancehttps://autodiff-workshop.github.io/slides/JeffDean.pdf

XlaDeviceLaunchOp::Computeの処理

compiler.cc executable.{h,cc}executor.{h,cc}

Page 46: TensorFlow XLAの可能性

XLA_EXECの登録 (device.cc)const char* const DEVICE_XLA_EXEC = "XLA_EXEC";const char* const DEVICE_EXEC_XLA_JIT = "XLA_EXEC_JIT";constexpr std::array<DataType, 5> kExecAllTypes = { {DT_INT32, DT_FLOAT, DT_BOOL, DT_DOUBLE, DT_INT64}};class XlaExaDeviceFactory : public DeviceFactory { public: Status CreateDevices(const SessionOptions& options, const string& name_prefix, std::vector<Device*>* devices) override;

Page 47: TensorFlow XLAの可能性

XLA_EXECの登録 (device.cc)REGISTER_LOCAL_DEVICE_FACTORY(  DEVICE_XLA_EXEC, XlaExaDeviceFactory, 40);

constexpr std::array<DataType, 5> kAllXlaCpuTypes = {{  DT_INT32, DT_INT64, DT_FLOAT,   DT_DOUBLE, DT_BOOL}};

REGISTER_XLA_LAUNCH_KERNEL(  DEVICE_XLA_EXEC, XlaDeviceLaunchOp, kExecAllTypes);REGISTER_XLA_DEVICE_KERNELS(  DEVICE_XLA_EXEC, kExecAllTypes);

Page 48: TensorFlow XLAの可能性

デバイスの登録core/common_runtime/device_factory.{h,c}

// The default priority values for built-in devices is: // GPU: 210 // SYCL: 200 // GPUCompatibleCPU: 70 // ThreadPoolDevice: 60 // Default: 50

REGISTER_LOCAL_DEVICE_FACTORYマクロで設定する

Page 49: TensorFlow XLAの可能性

XLA_EXECの登録 (device.cc)REGISTER_XLA_BACKEND( DEVICE_EXEC_XLA_JIT, kExecAllTypes, OpFilter);

tf2xla/xla_op_registry.h に r1.2で追加された

// REGISTER_XLA_BACKEND() registers an XLA backend. Example usage:// REGISTER_XLA_BACKEND(DEVICE_GPU_XLA_JIT, kGpuAllTypes, GpuOpFilter);#define REGISTER_XLA_BACKEND(NAME, ...) \ REGISTER_XLA_BACKEND_UNIQ_HELPER(__COUNTER__, NAME, __VA_ARGS__)

Page 50: TensorFlow XLAの可能性

Compile plugin/executor/compiler.{h,c}

RunHloOptimization : HLOの最適化

// Typically you would visit the HLO graph, building up a compiled equivalent // In this case we are using an Hlo evaluator at execution time, so we don't // need to compile anything

// ここでPluginに対応したコード生成を行う

ExecutorExecutableの生成

Page 51: TensorFlow XLAの可能性

引用:https://raw.githubusercontent.com/aws/aws-fpga/master/hdk/docs/images/AWS_FPGA_Software_Overview.jpg

AWS EC2 F1 でもできるかな?

Page 52: TensorFlow XLAの可能性

https://www.nextplatform.com/2017/08/23/first-depth-view-wave-computings-dpu-architecture-systems/

Wave Computing

Page 53: TensorFlow XLAの可能性

http://tvmlang.org/2017/08/17/tvm-release-announcement.html

MXnet-NVVM-TVM

LLVMはCPU、CUDAは別

Page 54: TensorFlow XLAの可能性

https://www.nextplatform.com/2017/08/24/drilling-microsofts-brainwave-soft-deep-leaning-chip/

Microsoft BrainWave

推論で、バッチサイズは、1

Page 55: TensorFlow XLAの可能性

TensorFlow XLAの可能性

以上、説明してきたように、

TensorFlow r1.3で導入されたpluginにて、

他のフレームワークではできない

いろいろなハードウェアへの対応が

できるようになる!

そこに注目しました!

Page 56: TensorFlow XLAの可能性

SlideShareで公開しています

https://www.slideshare.net/ssuser479fa3

TensroFlow XLA : JIT編 (r1.3版)https://www.slideshare.net/ssuser479fa3/tensroflow-xla-jit

Intel Nervana Graph とは?https://www.slideshare.net/ssuser479fa3/intel-nervana-graph-compiler

DSPでディープラーニングhttps://www.slideshare.net/ssuser479fa3/dsp-75659146

Page 57: TensorFlow XLAの可能性

ありがとうございました

ブログ : Vengineerの戯言http://blogs.yahoo.co.jp/verification_engineer

Twitter : @Vengineer

FPGAエクストリーム・コンピューティング

第9回        2017年9月24日