SlideShare a Scribd company logo
2021.08.30
Mana Murakami, Solution Architect , NVIDIA
NVIDIA プロファイラを用いた
PYTORCH 学習最適化手法のご紹介
2
1. プロファイリングの重要性について
2. DLProf & Nsight Systems
3. まとめ
AGENDA
3
よくあるご質問
• GPU を学習に使用したら速くなったが、これ以上速くなるか分からない
• GPU を学習にしようしているが、GPU がどの程度使われているのかよく分からない
• そもそも最適化のステップが分からない
プロファイリングの重要性について
4
よくあるご質問
• GPU を学習に使用したら速くなったが、これ以上速くなるか分からない
• GPU を学習にしようしているが、GPU がどの程度使われているのかよく分からない
• そもそも最適化のステップが分からない
プロファイリングの重要性について
ボトルネック解析の為の便利なツールが
いくつか存在します
5
パフォーマンス最適化の限界
アムダ―ルの法則: トレーニングセッションの一部 (GPU で動作) を高速化すると、残りの部分 (CPU で動
作) が性能ボトルネックになる
プロファイリングの重要性について
Mixed Precision
(TF32/FP16/BF16)
MATH
(linear, conv, matmul)
MEMORY
(pointwise,
reductions)
OTHER
(data pipeline,
communication)
8-16x
1-2x 1x ~2x overall faster
training session time
Single Precision
FP32
GPU CPU
NVIDIA プロファイリング スタック
用途毎に使い分け可能な階層型プロファイル スタック
DLProf Viewer
Deep Learning Profiler (DLProf)
Nsight Systems
NVTX for
Tensorflow NVTX Plugins NVTX for PyTorch
NGC Optimized Framework Containers
NVIDIA COMPUTING PLATFORM
• Nsight Systems と Nsight Compute は CUPTI
(Profiling Tools Interface) ベースの GPU アプリケー
ションの為のプロファイラ
• NVTX (NVIDIA Tools Extension Library) は
ソースコードにアノテーションをする為の CUDA ライブラ
リ
• DLProf は内部で Nsight Systems を実行してプロファイ
ルデータを収集し、データサイエンティストが分かりやすい形
に整形して可視化
NEW
V1.0.0
6
7
性能最適化の為の便利なツール
DL Prof と Nsight Systems
データサイエンティストと
応用研究者
DLProf
<Nsight Systems w/ NVTX>
研究者と開発者
NVTX
Nsight Systems
Nsight Compute
アルゴリズム開発者 特定のドメイン向けのモデル開発や
アプリケーション開発者
8
性能最適化の為の便利なツール
DL Prof と Nsight Systems
データサイエンティストと
応用研究者
DLProf
<Nsight Systems w/ NVTX>
研究者と開発者
NVTX
Nsight Systems
Nsight Compute
アルゴリズム開発者 特定のドメイン向けのモデル開発や
アプリケーション開発者
アルゴリズムやフレームワーク開発者の為のプロファイルツール
オーバーヘッドも低く軽量でCUDA処理の流れを
細かく把握する事ができる
解析結果をデータサイエンティストが理解しやすい形に
整形・可視化して学習コードの最適化を支援
42
DLProf
10
DLProf とは?
DLProf :ディープラーニングモデルの為のプロファイリングツール
解析結果や最適化のアドバイスを表示
TensorFlow, PyTorch, TensorRT をサポート
11
DLProf とは?
ダッシュボード
• GPU 使用率チャート
• wall clock time のうち GPU がアクティブになっている割合の表示、複数 GPU の場合すべての GPU の平均利用率を示す
• オペレーション GPU 時間チャート :
• すべてのオペレーションを「Tensor コアを使用した処理」「Tensor コア使用できたが使用しなかった処理」「Tensor コアを使用する事が出来ない
処理」の 3つに分類してチャートを表示
CUDA カーネルの GPU 時間チャート:
•
• 全 CUDA カーネル実行時間を「カーネル内で Tensor コアを使用した時間」「カーネル内でメモリ処理を行っていた時間」「カーネル内のその他すべての
処理」の 3つに分類してチャートを表示
•
Tensor コア使用率チャート
• Tensor コアを使用した処理の全 GPU 時間に対する割合をチャートで表示
12
DLProf とは?
ダッシュボード
• 性能 サマリー:
• 実行時に重要な主要指標を一覧として表示 (実行時間、Tensor コア使用率、GPU 使用率など)
• イテレーション サマリー:
• 実行中に各イテレーションでかかった時間を示す棒グラフ。Tensor コアを使用した時間、Tensor コア以外でGPUを使用した時間、GPU を使用し
ていない時間のインテレーション毎の内訳が表示される。
• トップ 10 GPU オペレーション :
• 実行時間がかかっている上位10オペレーションをソートして表示。ボトルネックになっている箇所の特定に有効
13
DLProf のインストール
DLProf を使うには?
1. NGC 上で配布されている TensorFlow および PyTorch コンテナに同梱されている DLProf を使う (PyTorch と TensorFlow (1.x/2.x ))
•TensorFlow https://ngc.nvidia.com/catalog/containers/nvidia:tensorflow
•PyTorch https://ngc.nvidia.com/catalog/containers/nvidia:pytorch
2. Python pip 経由のインストール (PyTorch と TF2.x のみ)
• PyTorch の例: (py Index、DLProf および依存パッケージ、 DLProf Viewer Plugin for TensorBoard のインストール)
$ pip installnvidia-pyindex
$ pip installnvidia-dlprof[pytorch]
$ pip installnvidia-tensorboard-plugin-dlprof
14
DLProf のインストール
DLProf を使うには?
1. NGC 上で配布されている TensorFlow および PyTorch コンテナに同梱されている DLProf を使う (PyTorch と TensorFlow (1.x/2.x ))
•TensorFlow https://ngc.nvidia.com/catalog/containers/nvidia:tensorflow
•PyTorch https://ngc.nvidia.com/catalog/containers/nvidia:pytorch
2. Python pip 経由のインストール (PyTorch と TF2.x のみ)
• PyTorch の例: (py Index、DLProf および依存パッケージ、 DLProf Viewer Plugin for TensorBoard のインストール)
$ pip installnvidia-pyindex
$ pip installnvidia-dlprof[pytorch]
$ pip installnvidia-tensorboard-plugin-dlprof
NOTE:
NGCで配布されているDeep LearningコンテナをSingularityで動かす方法はAppendix.のブログを参照のこと
各コンテナに同梱されている DLProf のバージョンは以下のドキュメントで確認可能
https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/
15
DLProf のインストール
DLProf を使うには?
1. NGC 上で配布されている TensorFlow および PyTorch コンテナに同梱されている DLProf を使う (PyTorch と TensorFlow (1.x/2.x ))
•TensorFlow https://ngc.nvidia.com/catalog/containers/nvidia:tensorflow
•PyTorch https://ngc.nvidia.com/catalog/containers/nvidia:pytorch
2. Python pip 経由のインストール (PyTorch と TF1.x のみ)
• PyTorch の例: (py Index、DLProf および依存パッケージ、 DLProf Viewer Plugin for TensorBoard のインストール)
$ pip installnvidia-pyindex
$ pip installnvidia-dlprof[pytorch]
$ pip installnvidia-tensorboard-plugin-dlprof
NOTE:
CUDA toolkit および driver と依存関係がある為、構築環境の CUDA バージョンと互換性があるバー
ジョンを入れる必要がある
(参考)
https://docs.nvidia.com/deeplearning/frameworks/dlprof-release-notes/index.html
16
PyTorch スクリプトのプロファイル手順
DLProf を使うには?
1. プロファイル対象の PyTorch コードに以下を追加
2. DLProf の実行
3. DLProfViewer による結果の可視化
import nvidia_dlprof_pytorch_nvtx as nvtx
nvtx.init(enable_function_stack=True)
with torch.autograd.profiler.emit_nvtx():
for iter in range(iters):
#forward
#backward
$ dlprof --mode=pytorch python main.py
$ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite
References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
17
PyTorch スクリプトのプロファイル手順
DLProf を使うには?
1. プロファイル対象の PyTorch コードに以下を追加
2. DLProf の実行
3. DLProfViewer による結果の可視化
import nvidia_dlprof_pytorch_nvtx as nvtx
nvtx.init(enable_function_stack=True)
with torch.autograd.profiler.emit_nvtx():
for iter in range(iters):
#forward
#backward
$ dlprof --mode=pytorch python main.py
$ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite
DLProfの解析は時間がかかる為、イテレーション数を少なくするのが良い (10~20 mini-batchくらい)
--delayオプションを付けてwarmup部をスキップしてプロファイルする事も可能
References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
18
PyTorch スクリプトのプロファイル手順
DLProf を使うには?
1. プロファイル対象の PyTorch コードに以下を追加
2. DLProf の実行
3. DLProfViewer による結果の可視化
import nvidia_dlprof_pytorch_nvtx as nvtx
nvtx.init(enable_function_stack=True)
with torch.autograd.profiler.emit_nvtx():
for iter in range(iters):
#forward
#backward
$ dlprof --mode=pytorch python main.py
$ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite
3行追加するだけ
References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
19
PyTorch スクリプトのプロファイル手順
DLProf を使うには?
1. プロファイル対象の PyTorch コードに以下を追加
2. DLProf の実行
3. DLProfViewer による結果の可視化
import nvidia_dlprof_pytorch_nvtx as nvtx
nvtx.init(enable_function_stack=True)
with torch.autograd.profiler.emit_nvtx():
for iter in range(iters):
#forward
#backward
$ dlprof --mode=pytorch python main.py
$ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite
特にファイル名を指定せずに実行した場合、「dlprof_dldb.sqlite」と「nsys_profile.sqlite」が
出力される
Dlprofviewerには「dlprof_dldb.sqlite」を指定する必要がある
References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
20
PyTorch スクリプトのプロファイル手順
DLProf を使うには?
1. プロファイル対象の PyTorch コードに以下を追加
2. DLProf の実行
3. DLProfViewer による結果の可視化
import nvidia_dlprof_pytorch_nvtx as nvtx
nvtx.init(enable_function_stack=True)
with torch.autograd.profiler.emit_nvtx():
for iter in range(iters):
#forward
#backward
$ dlprof --mode=pytorch python main.py
$ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite
特にファイル名を指定せずに実行した場合、「dlprof_dldb.sqlite」と「nsys_profile.sqlite」が
出力される
Dlprofviewerには「dlprof_dldb.sqlite」を指定
References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
21
例: DLProf + DLProfViewer によるプロファイル結果
GPU 最適化前 (AMP未使用/バッチサイズ小)
全実行時間の殆どがCPU処理
なのが一目で分かる
22
例: DLProf + DLProfViewer によるプロファイル結果
GPU 最適化前 (AMP未使用/バッチサイズ小)
全実行時間の殆どがCPU処理
なのが一目で分かる
“Problem detected:”と”Recommended Change:”
が表示され、問題点が分かる
23
42
NVIDIA NSIGHT
SYSTEMS
24
NSIGHT ツールワークフロー
新しくなった CUDA プロファイルツール群
Nsight Systems
包括的なシステムレベルの性能確認
Nsight Compute
CUDA カーネル詳細性能確認用
Nsight Graphics
フレーム/レンダー詳細性能確認
メトリック/カウンタを用いた
CUDAカーネル単位の詳細
性能確認
グラフィックフレーム単位の
詳細プロファイル
スタート
全体的なパフォーマンスを
再確認
全体的なパフォーマンス
を再確認
https://developer.nvidia.com/nsight-systems
25
Nsight Systems
主な機能:
• システム全体のアルゴリズム最適化
• マルチプロセスのアプリケーション解析のサポート
• アプリケーション内のボトルネックを探しに有効
• 非常に高速なGUIタイムライン上で何百万ものイベントを視覚化
• コマンドライン、IDE(統合型開発環境)の両方に対応
OS: Linux (x86, Power, Arm SBSA, Tegra), Windows, MacOSX (host)
GPUs: Pascal+
新しくなった CUDA プロファイルツール群
$ nsys profile –t cuda,osrt,nvtx,cudnn,cublas –o inference_result.qdstrm –w true
python inference.py
https://developer.nvidia.com/nsight-systems
26
CPU
utilization
Processes &
threads
OS runtime
APIs
CUDA &
cuBLAS APIs
GPU CUDA
Kernels & memory
transfers
CPU IP &
backtrace
sample data
NVTX
annotations
NVTX projected on
GPU CUDA streams
27
開発環境に NSIGHT SYSTEMS がインストールされていない場合
Setting Up and Using Nsight Systems Inside Containers
CUDA 11.4: install
CUDA 11.3: install
CUDA 11.2: install
Mapping an Nsight Systems Host Installation into a Container
NSIGHT SYTEMS
$ apt-get update –y
$ apt-get install -y cuda-nsight-systems-11-3 nsight-systems-2021.1.3
$ apt-get update –y
$ apt-get install -y cuda-nsight-systems-11-2 nsight-systems-2020.4.3
$ docker run --rm -it --network=host --gpus=all -v /opt/nvidia/nsight-systems/2021.1.3:/opt/nvidia/nsight-systems/2021.1.3 
nvcr.io/nvidia/pytorch:21.08-py3 bash
$ apt-get update –y
$ apt-get install -y cuda-nsight-systems-11-4 nsight-systems-2021.2.4
28
NSIGHT SYSTEMS を使うには?
Example
cuda – GPU kernel
osrt – OS runtime
nvtx – NVIDIA Tools Extension
cublas – CUDA BLAS library
https://docs.nvidia.com/nsight-systems/2020.3/profiling/index.html#cli-options
NSIGHT SYTEMS
$ nsys profile -t nvtx,cuda,osrt,cublas 
--stats=true 
-f true 
-o pusch_result 
python main.py
APIs to be traced
Outputs profiling information similar to nvprof
Overwrite the output
Output filename
29
NSIGHT SYSTEMS を使うには?
Example
cuda – GPU kernel
osrt – OS runtime
nvtx – NVIDIA Tools Extension
cublas – CUDA BLAS library
https://docs.nvidia.com/nsight-systems/2020.3/profiling/index.html#cli-options
NSIGHT SYTEMS
$ nsys profile -t nvtx,cuda,osrt,cublas 
--stats=true 
-f true 
-o pusch_result 
python main.py
APIs to be traced
Outputs profiling information similar to nvprof
Overwrite the output
Output filename
Other Userful Options
• --delay (-y) : Collection start delay in seconds
• --duration(-d): Collection duration in seconds.
• --capture-range(-c): none/cudaProfilerApi/nvtx
etc..
30
例: Nsight Systems + NVTX
Nsight Systems プロファイル結果(NVTX あり)
前処理
11.07sec 推論処理(10iteration) 28.924sec
1iteration
アノテーションする事で
タイムライン上で処理を把握しやすくなる!
31
Appendix. 技術ブログ・関連セッション
Deep Learning Examples
• https://github.com/NVIDIA/DeepLearningExamples/
How to Run NGC Deep Learning Containers with Singularity
• https://developer.nvidia.com/blog/how-to-run-ngc-deep-learning-containers-with-singularity/
Profiling and Optimizing Deep Neural Networks with DLProf and PyProf (TensorFlow)
• https://developer.nvidia.com/blog/profiling-and-optimizing-deep-neural-networks-with-dlprof-and-pyprof/
Deep Learning Performance Optimization with Profiling Tools
• https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31228/
Profiling and Optimizing Deep Neural Networks with DLProf and PyProf
• https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31341/
PyTorch Performance Tuning Guide
• https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31831/
NVIDIA プロファイラを用いた Pytorch 学習最適化手法のご紹介
THANK YOU!

More Related Content

PDF
Neural networks for Graph Data NeurIPS2018読み会@PFN
PDF
[DL輪読会] Spectral Norm Regularization for Improving the Generalizability of De...
PDF
Transformerを多層にする際の勾配消失問題と解決法について
PPTX
【DL輪読会】SimCSE: Simple Contrastive Learning of Sentence Embeddings (EMNLP 2021)
PDF
【DL輪読会】DINOv2: Learning Robust Visual Features without Supervision
PDF
t-SNE Explained
PDF
【DL輪読会】Patches Are All You Need? (ConvMixer)
PDF
【DL輪読会】Domain Generalization by Learning and Removing Domainspecific Features
Neural networks for Graph Data NeurIPS2018読み会@PFN
[DL輪読会] Spectral Norm Regularization for Improving the Generalizability of De...
Transformerを多層にする際の勾配消失問題と解決法について
【DL輪読会】SimCSE: Simple Contrastive Learning of Sentence Embeddings (EMNLP 2021)
【DL輪読会】DINOv2: Learning Robust Visual Features without Supervision
t-SNE Explained
【DL輪読会】Patches Are All You Need? (ConvMixer)
【DL輪読会】Domain Generalization by Learning and Removing Domainspecific Features

What's hot (20)

PPTX
[DL輪読会]GQNと関連研究,世界モデルとの関係について
PPTX
物体検出フレームワークMMDetectionで快適な開発
PDF
「NVIDIA プロファイラを用いたPyTorch学習最適化手法のご紹介(修正版)」
PDF
自己教師学習(Self-Supervised Learning)
PDF
【メタサーベイ】数式ドリブン教師あり学習
PDF
【チュートリアル】コンピュータビジョンによる動画認識
PPTX
[DL輪読会]Pay Attention to MLPs (gMLP)
PDF
[DL輪読会]Glow: Generative Flow with Invertible 1×1 Convolutions
PDF
SSII2019TS: Shall We GANs?​ ~GANの基礎から最近の研究まで~
PPTX
CNNの構造最適化手法について
PPTX
【DL輪読会】Llama 2: Open Foundation and Fine-Tuned Chat Models
PPTX
近年のHierarchical Vision Transformer
PPTX
Swin Transformer (ICCV'21 Best Paper) を完璧に理解する資料
PDF
【DL輪読会】Foundation Models for Decision Making: Problems, Methods, and Opportun...
PPTX
【DL輪読会】言語以外でのTransformerのまとめ (ViT, Perceiver, Frozen Pretrained Transformer etc)
PPTX
CVPR2018 pix2pixHD論文紹介 (CV勉強会@関東)
PDF
時系列予測にTransformerを使うのは有効か?
PDF
[DL輪読会]An Image is Worth 16x16 Words: Transformers for Image Recognition at S...
PPTX
Tensor コアを使った PyTorch の高速化
PDF
【メタサーベイ】基盤モデル / Foundation Models
[DL輪読会]GQNと関連研究,世界モデルとの関係について
物体検出フレームワークMMDetectionで快適な開発
「NVIDIA プロファイラを用いたPyTorch学習最適化手法のご紹介(修正版)」
自己教師学習(Self-Supervised Learning)
【メタサーベイ】数式ドリブン教師あり学習
【チュートリアル】コンピュータビジョンによる動画認識
[DL輪読会]Pay Attention to MLPs (gMLP)
[DL輪読会]Glow: Generative Flow with Invertible 1×1 Convolutions
SSII2019TS: Shall We GANs?​ ~GANの基礎から最近の研究まで~
CNNの構造最適化手法について
【DL輪読会】Llama 2: Open Foundation and Fine-Tuned Chat Models
近年のHierarchical Vision Transformer
Swin Transformer (ICCV'21 Best Paper) を完璧に理解する資料
【DL輪読会】Foundation Models for Decision Making: Problems, Methods, and Opportun...
【DL輪読会】言語以外でのTransformerのまとめ (ViT, Perceiver, Frozen Pretrained Transformer etc)
CVPR2018 pix2pixHD論文紹介 (CV勉強会@関東)
時系列予測にTransformerを使うのは有効か?
[DL輪読会]An Image is Worth 16x16 Words: Transformers for Image Recognition at S...
Tensor コアを使った PyTorch の高速化
【メタサーベイ】基盤モデル / Foundation Models
Ad

Similar to 「NVIDIA プロファイラを用いたPyTorch学習最適化手法のご紹介(修正前 typoあり)」 (20)

PDF
ディープラーニングイメージで構築する快適・高速な機械学習環境
PDF
20170421 tensor flowusergroup
PDF
[GTCJ2018] Optimizing Deep Learning with Chainer PFN得居誠也
PDF
エヌビディアが加速するディープラーニング ~進化するニューラルネットワークとその開発方法について~
PDF
機械学習プロジェクトにおける Cloud AI Platform の使い方 (2018-11-19)
PDF
EnrootとPyxisで快適コンテナ生活
PDF
Anaconda & NumbaPro 使ってみた
PDF
続・PFN のオンプレML基盤の取り組み / オンプレML基盤 on Kubernetes 〜PFN、ヤフー〜 #2
PPTX
OpenStackを使用したGPU仮想化IaaS環境 事例紹介
PDF
What's new in open shift container platform 4.7 japan_20210318
PDF
Deep Learning Implementations: pylearn2 and torch7 (JNNS 2015)
PDF
How to run P4 BMv2
PDF
20170726 py data.tokyo
PDF
PFNのML/DL基盤を支えるKubernetesにおける自動化 / DevOpsDays Tokyo 2021
PDF
はてなにおける継続的デプロイメントの現状と Docker の導入
PDF
Unity2018/2019における最適化事情
PDF
20160720 aws development-tools-and_hybrid_cdp
PPTX
機械学習 / Deep Learning 大全 (6) Library編
PDF
【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例
PDF
実践! Argo cd &amp; rollouts による canary release(cndt2021)
ディープラーニングイメージで構築する快適・高速な機械学習環境
20170421 tensor flowusergroup
[GTCJ2018] Optimizing Deep Learning with Chainer PFN得居誠也
エヌビディアが加速するディープラーニング ~進化するニューラルネットワークとその開発方法について~
機械学習プロジェクトにおける Cloud AI Platform の使い方 (2018-11-19)
EnrootとPyxisで快適コンテナ生活
Anaconda & NumbaPro 使ってみた
続・PFN のオンプレML基盤の取り組み / オンプレML基盤 on Kubernetes 〜PFN、ヤフー〜 #2
OpenStackを使用したGPU仮想化IaaS環境 事例紹介
What's new in open shift container platform 4.7 japan_20210318
Deep Learning Implementations: pylearn2 and torch7 (JNNS 2015)
How to run P4 BMv2
20170726 py data.tokyo
PFNのML/DL基盤を支えるKubernetesにおける自動化 / DevOpsDays Tokyo 2021
はてなにおける継続的デプロイメントの現状と Docker の導入
Unity2018/2019における最適化事情
20160720 aws development-tools-and_hybrid_cdp
機械学習 / Deep Learning 大全 (6) Library編
【Unite Tokyo 2019】運用中超大規模タイトルにおけるUnityアップデート課題の解決手法と事例
実践! Argo cd &amp; rollouts による canary release(cndt2021)
Ad

More from ManaMurakami1 (7)

PDF
20170518 eureka dli
PDF
20161210 jawsai
PDF
20161122 gpu deep_learningcommunity#02
PDF
20161121 open hyperscale#6
PDF
20160902 hyperscale #04
PDF
20160728 hyperscale #03
PDF
OHS#2 GREでディープラーニング学習REST APIを作る
20170518 eureka dli
20161210 jawsai
20161122 gpu deep_learningcommunity#02
20161121 open hyperscale#6
20160902 hyperscale #04
20160728 hyperscale #03
OHS#2 GREでディープラーニング学習REST APIを作る

「NVIDIA プロファイラを用いたPyTorch学習最適化手法のご紹介(修正前 typoあり)」

  • 1. 2021.08.30 Mana Murakami, Solution Architect , NVIDIA NVIDIA プロファイラを用いた PYTORCH 学習最適化手法のご紹介
  • 3. 3 よくあるご質問 • GPU を学習に使用したら速くなったが、これ以上速くなるか分からない • GPU を学習にしようしているが、GPU がどの程度使われているのかよく分からない • そもそも最適化のステップが分からない プロファイリングの重要性について
  • 4. 4 よくあるご質問 • GPU を学習に使用したら速くなったが、これ以上速くなるか分からない • GPU を学習にしようしているが、GPU がどの程度使われているのかよく分からない • そもそも最適化のステップが分からない プロファイリングの重要性について ボトルネック解析の為の便利なツールが いくつか存在します
  • 5. 5 パフォーマンス最適化の限界 アムダ―ルの法則: トレーニングセッションの一部 (GPU で動作) を高速化すると、残りの部分 (CPU で動 作) が性能ボトルネックになる プロファイリングの重要性について Mixed Precision (TF32/FP16/BF16) MATH (linear, conv, matmul) MEMORY (pointwise, reductions) OTHER (data pipeline, communication) 8-16x 1-2x 1x ~2x overall faster training session time Single Precision FP32 GPU CPU
  • 6. NVIDIA プロファイリング スタック 用途毎に使い分け可能な階層型プロファイル スタック DLProf Viewer Deep Learning Profiler (DLProf) Nsight Systems NVTX for Tensorflow NVTX Plugins NVTX for PyTorch NGC Optimized Framework Containers NVIDIA COMPUTING PLATFORM • Nsight Systems と Nsight Compute は CUPTI (Profiling Tools Interface) ベースの GPU アプリケー ションの為のプロファイラ • NVTX (NVIDIA Tools Extension Library) は ソースコードにアノテーションをする為の CUDA ライブラ リ • DLProf は内部で Nsight Systems を実行してプロファイ ルデータを収集し、データサイエンティストが分かりやすい形 に整形して可視化 NEW V1.0.0 6
  • 7. 7 性能最適化の為の便利なツール DL Prof と Nsight Systems データサイエンティストと 応用研究者 DLProf <Nsight Systems w/ NVTX> 研究者と開発者 NVTX Nsight Systems Nsight Compute アルゴリズム開発者 特定のドメイン向けのモデル開発や アプリケーション開発者
  • 8. 8 性能最適化の為の便利なツール DL Prof と Nsight Systems データサイエンティストと 応用研究者 DLProf <Nsight Systems w/ NVTX> 研究者と開発者 NVTX Nsight Systems Nsight Compute アルゴリズム開発者 特定のドメイン向けのモデル開発や アプリケーション開発者 アルゴリズムやフレームワーク開発者の為のプロファイルツール オーバーヘッドも低く軽量でCUDA処理の流れを 細かく把握する事ができる 解析結果をデータサイエンティストが理解しやすい形に 整形・可視化して学習コードの最適化を支援
  • 11. 11 DLProf とは? ダッシュボード • GPU 使用率チャート • wall clock time のうち GPU がアクティブになっている割合の表示、複数 GPU の場合すべての GPU の平均利用率を示す • オペレーション GPU 時間チャート : • すべてのオペレーションを「Tensor コアを使用した処理」「Tensor コア使用できたが使用しなかった処理」「Tensor コアを使用する事が出来ない 処理」の 3つに分類してチャートを表示 CUDA カーネルの GPU 時間チャート: • • 全 CUDA カーネル実行時間を「カーネル内で Tensor コアを使用した時間」「カーネル内でメモリ処理を行っていた時間」「カーネル内のその他すべての 処理」の 3つに分類してチャートを表示 • Tensor コア使用率チャート • Tensor コアを使用した処理の全 GPU 時間に対する割合をチャートで表示
  • 12. 12 DLProf とは? ダッシュボード • 性能 サマリー: • 実行時に重要な主要指標を一覧として表示 (実行時間、Tensor コア使用率、GPU 使用率など) • イテレーション サマリー: • 実行中に各イテレーションでかかった時間を示す棒グラフ。Tensor コアを使用した時間、Tensor コア以外でGPUを使用した時間、GPU を使用し ていない時間のインテレーション毎の内訳が表示される。 • トップ 10 GPU オペレーション : • 実行時間がかかっている上位10オペレーションをソートして表示。ボトルネックになっている箇所の特定に有効
  • 13. 13 DLProf のインストール DLProf を使うには? 1. NGC 上で配布されている TensorFlow および PyTorch コンテナに同梱されている DLProf を使う (PyTorch と TensorFlow (1.x/2.x )) •TensorFlow https://ngc.nvidia.com/catalog/containers/nvidia:tensorflow •PyTorch https://ngc.nvidia.com/catalog/containers/nvidia:pytorch 2. Python pip 経由のインストール (PyTorch と TF2.x のみ) • PyTorch の例: (py Index、DLProf および依存パッケージ、 DLProf Viewer Plugin for TensorBoard のインストール) $ pip installnvidia-pyindex $ pip installnvidia-dlprof[pytorch] $ pip installnvidia-tensorboard-plugin-dlprof
  • 14. 14 DLProf のインストール DLProf を使うには? 1. NGC 上で配布されている TensorFlow および PyTorch コンテナに同梱されている DLProf を使う (PyTorch と TensorFlow (1.x/2.x )) •TensorFlow https://ngc.nvidia.com/catalog/containers/nvidia:tensorflow •PyTorch https://ngc.nvidia.com/catalog/containers/nvidia:pytorch 2. Python pip 経由のインストール (PyTorch と TF2.x のみ) • PyTorch の例: (py Index、DLProf および依存パッケージ、 DLProf Viewer Plugin for TensorBoard のインストール) $ pip installnvidia-pyindex $ pip installnvidia-dlprof[pytorch] $ pip installnvidia-tensorboard-plugin-dlprof NOTE: NGCで配布されているDeep LearningコンテナをSingularityで動かす方法はAppendix.のブログを参照のこと 各コンテナに同梱されている DLProf のバージョンは以下のドキュメントで確認可能 https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/
  • 15. 15 DLProf のインストール DLProf を使うには? 1. NGC 上で配布されている TensorFlow および PyTorch コンテナに同梱されている DLProf を使う (PyTorch と TensorFlow (1.x/2.x )) •TensorFlow https://ngc.nvidia.com/catalog/containers/nvidia:tensorflow •PyTorch https://ngc.nvidia.com/catalog/containers/nvidia:pytorch 2. Python pip 経由のインストール (PyTorch と TF1.x のみ) • PyTorch の例: (py Index、DLProf および依存パッケージ、 DLProf Viewer Plugin for TensorBoard のインストール) $ pip installnvidia-pyindex $ pip installnvidia-dlprof[pytorch] $ pip installnvidia-tensorboard-plugin-dlprof NOTE: CUDA toolkit および driver と依存関係がある為、構築環境の CUDA バージョンと互換性があるバー ジョンを入れる必要がある (参考) https://docs.nvidia.com/deeplearning/frameworks/dlprof-release-notes/index.html
  • 16. 16 PyTorch スクリプトのプロファイル手順 DLProf を使うには? 1. プロファイル対象の PyTorch コードに以下を追加 2. DLProf の実行 3. DLProfViewer による結果の可視化 import nvidia_dlprof_pytorch_nvtx as nvtx nvtx.init(enable_function_stack=True) with torch.autograd.profiler.emit_nvtx(): for iter in range(iters): #forward #backward $ dlprof --mode=pytorch python main.py $ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
  • 17. 17 PyTorch スクリプトのプロファイル手順 DLProf を使うには? 1. プロファイル対象の PyTorch コードに以下を追加 2. DLProf の実行 3. DLProfViewer による結果の可視化 import nvidia_dlprof_pytorch_nvtx as nvtx nvtx.init(enable_function_stack=True) with torch.autograd.profiler.emit_nvtx(): for iter in range(iters): #forward #backward $ dlprof --mode=pytorch python main.py $ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite DLProfの解析は時間がかかる為、イテレーション数を少なくするのが良い (10~20 mini-batchくらい) --delayオプションを付けてwarmup部をスキップしてプロファイルする事も可能 References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
  • 18. 18 PyTorch スクリプトのプロファイル手順 DLProf を使うには? 1. プロファイル対象の PyTorch コードに以下を追加 2. DLProf の実行 3. DLProfViewer による結果の可視化 import nvidia_dlprof_pytorch_nvtx as nvtx nvtx.init(enable_function_stack=True) with torch.autograd.profiler.emit_nvtx(): for iter in range(iters): #forward #backward $ dlprof --mode=pytorch python main.py $ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite 3行追加するだけ References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
  • 19. 19 PyTorch スクリプトのプロファイル手順 DLProf を使うには? 1. プロファイル対象の PyTorch コードに以下を追加 2. DLProf の実行 3. DLProfViewer による結果の可視化 import nvidia_dlprof_pytorch_nvtx as nvtx nvtx.init(enable_function_stack=True) with torch.autograd.profiler.emit_nvtx(): for iter in range(iters): #forward #backward $ dlprof --mode=pytorch python main.py $ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite 特にファイル名を指定せずに実行した場合、「dlprof_dldb.sqlite」と「nsys_profile.sqlite」が 出力される Dlprofviewerには「dlprof_dldb.sqlite」を指定する必要がある References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
  • 20. 20 PyTorch スクリプトのプロファイル手順 DLProf を使うには? 1. プロファイル対象の PyTorch コードに以下を追加 2. DLProf の実行 3. DLProfViewer による結果の可視化 import nvidia_dlprof_pytorch_nvtx as nvtx nvtx.init(enable_function_stack=True) with torch.autograd.profiler.emit_nvtx(): for iter in range(iters): #forward #backward $ dlprof --mode=pytorch python main.py $ dlprofviewer –b 0.0.0.0 –p 8000 dlprof_dldb.sqlite 特にファイル名を指定せずに実行した場合、「dlprof_dldb.sqlite」と「nsys_profile.sqlite」が 出力される Dlprofviewerには「dlprof_dldb.sqlite」を指定 References:https://docs.nvidia.com/deeplearning/frameworks/dlprof-user-guide/index.html#quickstart_topic
  • 21. 21 例: DLProf + DLProfViewer によるプロファイル結果 GPU 最適化前 (AMP未使用/バッチサイズ小) 全実行時間の殆どがCPU処理 なのが一目で分かる
  • 22. 22 例: DLProf + DLProfViewer によるプロファイル結果 GPU 最適化前 (AMP未使用/バッチサイズ小) 全実行時間の殆どがCPU処理 なのが一目で分かる “Problem detected:”と”Recommended Change:” が表示され、問題点が分かる
  • 24. 24 NSIGHT ツールワークフロー 新しくなった CUDA プロファイルツール群 Nsight Systems 包括的なシステムレベルの性能確認 Nsight Compute CUDA カーネル詳細性能確認用 Nsight Graphics フレーム/レンダー詳細性能確認 メトリック/カウンタを用いた CUDAカーネル単位の詳細 性能確認 グラフィックフレーム単位の 詳細プロファイル スタート 全体的なパフォーマンスを 再確認 全体的なパフォーマンス を再確認 https://developer.nvidia.com/nsight-systems
  • 25. 25 Nsight Systems 主な機能: • システム全体のアルゴリズム最適化 • マルチプロセスのアプリケーション解析のサポート • アプリケーション内のボトルネックを探しに有効 • 非常に高速なGUIタイムライン上で何百万ものイベントを視覚化 • コマンドライン、IDE(統合型開発環境)の両方に対応 OS: Linux (x86, Power, Arm SBSA, Tegra), Windows, MacOSX (host) GPUs: Pascal+ 新しくなった CUDA プロファイルツール群 $ nsys profile –t cuda,osrt,nvtx,cudnn,cublas –o inference_result.qdstrm –w true python inference.py https://developer.nvidia.com/nsight-systems
  • 26. 26 CPU utilization Processes & threads OS runtime APIs CUDA & cuBLAS APIs GPU CUDA Kernels & memory transfers CPU IP & backtrace sample data NVTX annotations NVTX projected on GPU CUDA streams
  • 27. 27 開発環境に NSIGHT SYSTEMS がインストールされていない場合 Setting Up and Using Nsight Systems Inside Containers CUDA 11.4: install CUDA 11.3: install CUDA 11.2: install Mapping an Nsight Systems Host Installation into a Container NSIGHT SYTEMS $ apt-get update –y $ apt-get install -y cuda-nsight-systems-11-3 nsight-systems-2021.1.3 $ apt-get update –y $ apt-get install -y cuda-nsight-systems-11-2 nsight-systems-2020.4.3 $ docker run --rm -it --network=host --gpus=all -v /opt/nvidia/nsight-systems/2021.1.3:/opt/nvidia/nsight-systems/2021.1.3 nvcr.io/nvidia/pytorch:21.08-py3 bash $ apt-get update –y $ apt-get install -y cuda-nsight-systems-11-4 nsight-systems-2021.2.4
  • 28. 28 NSIGHT SYSTEMS を使うには? Example cuda – GPU kernel osrt – OS runtime nvtx – NVIDIA Tools Extension cublas – CUDA BLAS library https://docs.nvidia.com/nsight-systems/2020.3/profiling/index.html#cli-options NSIGHT SYTEMS $ nsys profile -t nvtx,cuda,osrt,cublas --stats=true -f true -o pusch_result python main.py APIs to be traced Outputs profiling information similar to nvprof Overwrite the output Output filename
  • 29. 29 NSIGHT SYSTEMS を使うには? Example cuda – GPU kernel osrt – OS runtime nvtx – NVIDIA Tools Extension cublas – CUDA BLAS library https://docs.nvidia.com/nsight-systems/2020.3/profiling/index.html#cli-options NSIGHT SYTEMS $ nsys profile -t nvtx,cuda,osrt,cublas --stats=true -f true -o pusch_result python main.py APIs to be traced Outputs profiling information similar to nvprof Overwrite the output Output filename Other Userful Options • --delay (-y) : Collection start delay in seconds • --duration(-d): Collection duration in seconds. • --capture-range(-c): none/cudaProfilerApi/nvtx etc..
  • 30. 30 例: Nsight Systems + NVTX Nsight Systems プロファイル結果(NVTX あり) 前処理 11.07sec 推論処理(10iteration) 28.924sec 1iteration アノテーションする事で タイムライン上で処理を把握しやすくなる!
  • 31. 31 Appendix. 技術ブログ・関連セッション Deep Learning Examples • https://github.com/NVIDIA/DeepLearningExamples/ How to Run NGC Deep Learning Containers with Singularity • https://developer.nvidia.com/blog/how-to-run-ngc-deep-learning-containers-with-singularity/ Profiling and Optimizing Deep Neural Networks with DLProf and PyProf (TensorFlow) • https://developer.nvidia.com/blog/profiling-and-optimizing-deep-neural-networks-with-dlprof-and-pyprof/ Deep Learning Performance Optimization with Profiling Tools • https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31228/ Profiling and Optimizing Deep Neural Networks with DLProf and PyProf • https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31341/ PyTorch Performance Tuning Guide • https://www.nvidia.com/en-us/on-demand/session/gtcspring21-s31831/ NVIDIA プロファイラを用いた Pytorch 学習最適化手法のご紹介