Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dojo ++ 「GPUなしでもここまでできるAI構築シリーズ Part 2」インテルがチューンしたPythonとTensorFlow

kkojima
October 28, 2020

Dojo ++ 「GPUなしでもここまでできるAI構築シリーズ Part 2」インテルがチューンしたPythonとTensorFlow

Dojo ++ 「GPUなしでもここまでできるAI構築シリーズ Part 2」
https://connpass.com/event/190151/
インテルがチューンしたPythonとTensorFlowをつかってみる

インテル 大内山さんが話す当日の資料です。

手元で動作を確認するための環境構築手順書が別にあります。
ぜひそちらもご参照ください。

kkojima

October 28, 2020
Tweet

More Decks by kkojima

Other Decks in Technology

Transcript

  1. インテル株式会社 1 【重要】最初にこちらを実行ください! ※Dockerがインストールされていることが前提です。 § コマンドプロンプトまたはターミナルを起動(サーバーを使われる方はSSHでログイン) § AnacondaがプリインストールされたDockerコンテナーを起動 • docker

    run -i -t --privileged con1nuumio/anaconda /bin/bash § コンテナ内で下記環境セットアップ用スクリプトをダウンロード&実行 • wget h9ps://raw.githubusercontent.com/hiouchiy/intel_ai_basic_handson/main/install.sh • source ./install.sh <物理コア数> ※お使いのデバイスの物理コア数をご確認ください。 • Linux: ターミナルからlscpu • Win10: タスクマネージャからパフォーマンスタブ • MacOS: ターミナルからsystem_profiler SPHardwareDataType
  2. インテル株式会社 5 インテル® CPUは ディープラーニング推論性能を向上させるために進化 インテル® Xeon® プロセッサー E3/E5/E7 インテル®

    Xeon® プロセッサー E3/E5/E7 v2 インテル® Xeon® プロセッサー E3/E5/E7 v3 (コードネーム:Haswell) インテル® Xeon® プロセッサー E3/E5/E7 v4 (コードネーム:Broadwell) インテル® Xeon® スケーラブル・プロセッサー (コードネーム:Skylake) 第 2 世代インテル® Xeon® スケーラブル・プロセッサー (コードネーム:Cascade Lake) 第5世代 インテル® Core™ プロセッサー 第6世代 インテル® Core™ プロセッサー 第7世代 インテル® Core™ プロセッサー 第8世代 インテル® Core™ プロセッサー 第9世代 インテル® Core™ プロセッサー 第10世代 インテル® Core™ プロセッサー (コードネーム:Ice Lake / Comet Lake) 第 3 世代インテル® Xeon® スケーラブル・プロセッサー (コードネーム:Cooper Lake) 第11世代 インテル® Core™ プロセッサー (コードネーム:Tiger Lake) Xeon Core 旧 新 薦 薦 ・ ・ ・ ・ ・ ・ AVX-512 AVX-512 AVX2 AVX2 AVX AVX DL Boost AVX-512 DL Boost AVX2 AVX2 AVX2 AVX2 AVX2 ※ Comet Lakeは、AVX512/VNNIは含まれず、AVX2のまま AVX-512, DL Boostが入ってる世代がおすすめ AVX-512 DL Boost BF16 AVX-512 DL Boost AVX-512 → https://www.intel.co.jp/content/www/jp/ja/architecture-and-technology/avx-512-overview.html IBM Cloud に搭載
  3. インテル株式会社 6 インテル® AI ソフトウェア: マシンラーニングとディープラーニング Red font products are

    the most broadly applicable SW products for AI users Developer TOols App Developers SW Platform Developer Machine learning Deep LEarning Architect & DevOps Topologies & Models Data Scientist Frameworks Data Scientist Graph ML Performance Engineer Kernel ML Performance Engineer § Intel Data Analytics Acceleration Library (Intel DAAL) § Intel Math Kernel Library (Intel MKL) § Intel Machine Learning Scaling Library (Intel MLSL) § Intel® Deep Neural Network Library (DNNL) Deep Learning Reference Stack Data Analytics Reference Stack § Intel Distribu-on for Python (SKlearn, Pandas) Management Tools CPU CPU ▪ GPU ▪ FPGA ▪ 専用 Containers
  4. インテル株式会社 7 Intel® Distribution for Python* • Numpy • Pandas

    • Scipy • Scikit-learn • XGBoost • Daal4py • etc.. インテルが実装、かつ、最適化した Python、および、周辺ライブラリ h4ps://so9ware.intel.com/en-us/distribuAon-for-python/benchmarks Public Cloud 回帰分析 学習処理 on AVX512 & 72cores 423 倍(OSS実装との⽐較) Public Cloud ⾏列のコレスキー分解 on AVX512 & 72cores 9 倍 (OSS実装との⽐較) M L学 習 / 推 論
  5. インテル株式会社 8 How to install Intel® Distribu7on for Python* §

    前提 • Linux(Ubuntu, CentOS) • Anaconda 3 § Full • conda create -n intel python=3.7 • conda activate intel • conda install -c intel intelpython3_full § Core • conda create -n intel python=3.7 • conda activate intel • conda install -c intel intelpython3_core § ライブラリごと • conda create –n intel –c intel python=3.7 scikit-learn pandas
  6. インテル株式会社 9 インテルによるディープラーニング・フレームワークの最適化 for インストール・ガイドはこちら↓ ai.intel.com/framework-optimizations/ スケール § ロード・バランシン グ向上

    § 同期イベント、all- to-all 通信の削減 全コアの有効活⽤ § OpenMP、MPI § 同期イベント、直列 コードの削減 § ロード・バランシン グ向上 ベクトル演算 / SIMD § SIMD レーンごとの ユニット・ストライ ド・アクセス § ⾼いベクトル効率 § データ・アライメン ト 効率的なメモリー とキャッシュ利⽤ § ブロッキング § データ再利⽤ § プリフェッチ § メモリー・ アロケーション 更なるフレームワークの 最適化が進⾏中 (例、 PaddlePaddle* 、CNTK* な ど) SEE ALSO: Machine Learning Libraries for Python (Scikit-learn, Pandas, NumPy), R (Cart, randomForest, e1071), Distributed (MlLib on Spark, Mahout) *Limited availability today Optimization Notice
  7. インテル株式会社 10 How to install Tensorflow with intel optimization •IDP

    Fullà conda create –n IDP intelpython3_full •Main trunkà conda install tensorflow –c anaconda •Intel Channelà conda install tensorflow –c intel Conda Package •$ pip install intel-tensorflow Python Wheels •$ docker pull docker.io/intelaipg/intel-opSmized-tensorflow:<tag> •$ docker run -it -p 8888:8888 intelaipg/intel-opSmized-tensorflow Docker Images •https://github.com/tensorflow/tensorflow •Refer to installation guide for more details Build from Source OS: Linux TensorFlow: ~2.3 Python: 3.5 | 3.6 | 3.7 | 3.8 https://software.intel.com/en-us/articles/intel-optimization-for-tensorflow-installation-guide • Intel optimizations have already been merged into mainstream source code.
  8. インテル株式会社 11 How to install PyTorch § Intel optimization has

    already been accepted into source code of PyTorch. § User enjoys Intel acceleration by default with PyTorch.
  9. インテル株式会社 12 ここから一部はハンズオンになります。 § おすすめ構成 • CPU:第 2 世代インテル® Xeon®

    スケーラブル・プロセッサー(Cascade Lake) • OS:Ubuntu 18.04 • Docker • Anaconda 3 (OpJon) § ※講師の環境はIBM Cloud上のVM (Cascade Lake, 4 core)を利用
  10. インテル株式会社 13 ハンズオン環境イメージ図 インテルCPU(Xeon or Core) OS(Linux / Win10 /

    MacOS) Docker Ubuntu 18.04 Anaconda 3 仮想環境#1 OSS Python 仮想環境#2 Intel Python 仮想環境#3 OSS TensorFlow 仮想環境#4 Intel TensorFlow コ ン テ ナ
  11. インテル株式会社 16 インストール § インテル版Python • conda create -n intel

    -c intel python=3.7 scikit-learn=0.22.1 pandas -y § OSS版Python • conda create -n oss -c conda-forge python=3.7 scikit-learn=0.22.1 pandas -y § 環境変数の設定 • export USE_DAAL4PY_SKLEARN=YES
  12. インテル株式会社 17 回帰分析 § プログラムのダウンロード • wget https://raw.githubusercontent.com/hiouchiy/intel_ai_basic_handson/main/ml/linear.py § OSS版で実行してみる

    • conda activate oss • python linear.py § Intel版で実行してみる • conda activate intel • python linear.py § どの程度性能が変わるかを各環境で確認ください。
  13. インテル株式会社 18 クラスタリング § プログラムとデータのダウンロード • wget https://hiouchiystorage.blob.core.windows.net/share/kmeans.zip • unzip

    kmeans.zip • wget https://raw.githubusercontent.com/hiouchiy/intel_ai_basic_handson/main/ml/kmeans.py § OSS版で実行してみる • conda activate oss • python kmeans.py § Intel版で実行してみる • conda activate intel • python kmeans.py § どの程度性能が変わるかを各環境で確認ください。
  14. インテル株式会社 21 インストール § インテル版TensorFlow • conda create -n intel_tf

    -c intel python=3.7 -y • conda activate intel_tf • pip install intel-tensorflow==2.2.0 pillow opencv-python pandas § OSS版TensorFlow • conda create -n oss_tf -c conda-forge python=3.7 -y • conda activate oss_tf • pip install tensorflow==2.2.0 pillow opencv-python pandas § サンプルデータのダウンロード • wget https://hiouchiystorage.blob.core.windows.net/share/data.zip • mkdir train_data • mv data.zip train_data/ • unzip train_data/data.zip -d train_data/
  15. インテル株式会社 22 MobileNet v1 の学習 § 学習用スクリプトをダウンロード • wget https://raw.githubusercontent.com/hiouchiy/intel_ai_basic_handson/main/dl/train.py

    § printenvコマンドにて下記4つの環境変数がセットされていることを確認(無い場合はセット) • KMP_AFFINITY=granularity=fine,compact,1,0 • KMP_BLOCKTIME=1 • KMP_SETTINGS=1 • OMP_NUM_THREADS=物理コア数 § 学習開始 • python train.py
  16. インテル株式会社 23 Mobilenet v1の推論(FP32) § 学習済みモデルと推論用スクリプトをダウンロード • wget https://storage.googleapis.com/intel-optimized-tensorflow/models/v1_8/mobilenet_v1_1.0_224_frozen.pb •

    wget https://raw.githubusercontent.com/hiouchiy/intel_ai_basic_handson/main/dl/infer_script.py § printenvコマンドを叩いて下記4つの環境変数がセットされていることを確認(無い場合はセット) • KMP_AFFINITY=granularity=fine,compact,1,0 • KMP_BLOCKTIME=1 • KMP_SETTINGS=1 • OMP_NUM_THREADS=物理コア数 § OSS TensorFlowとIntel TensorFlowで環境を切り替えて、次ステップの推論実行をご実施ください。 • OSS TensorFlow: conda activate oss_tf • Intel TensorFlow: conda activate intel_tf § 推論実行 • python infer_script.py --input_graph mobilenet_v1_1.0_224_frozen.pb --num_images 100
  17. インテル株式会社 24 Mobilenet v1の推論(INT8) § 学習済みモデル(INT8)をダウンロード(推論スクリプトはFP32にものを流用) • wget https://storage.googleapis.com/intel-optimized-tensorflow/models/v1_8/mobilenetv1_int8_pretrained_model.pb §

    printenvコマンドを叩いて下記4つの環境変数がセットされていることを確認(無い場合はセット) • KMP_AFFINITY=granularity=fine,compact,1,0 • KMP_BLOCKTIME=1 • KMP_SETTINGS=1 • OMP_NUM_THREADS=物理コア数 § OSS TensorFlowとIntel TensorFlowで環境を切り替えて、次ステップの推論実行をご実施ください。 • OSS TensorFlow: conda activate oss_tf • Intel TensorFlow: conda activate intel_tf § 推論実行 • python infer_script.py --input_graph mobilenetv1_int8_pretrained_model.pb --num_images 100
  18. インテル株式会社 25 5 解説:謎の環境変数 § 4つの環境変数はoneDNNの中で動いているOpenMP用です。 CPU (Intel Xeon) oneDNN

    (by Intel) TensorFlow-CPU (Intel版) CPU (Intel Xeon) Eigen (by Google) TensorFlow-CPU (Official版) train_model.py GPU CUDA (by NVIDIA) TensorFlow-GPU (Official版) train_model.py train_model.py KMP_AFFINITY=granularity=fine,compact,1,0 KMP_BLOCKTIME=1 KMP_SETTINGS=1 OMP_NUM_THREADS=物理コアの数 https://software.intel.com/content/www/us/en/develop/articles/maximize-tensorflow-performance-on-cpu-considerations-and-recommendations-for-inference.html 0QFO.1 パラメーターの推奨設定
  19. インテル株式会社 26 ディープラーニング推論 - FP32 vs INT8 モデル (FP32) モデル

    (INT8) Intel TensorFlow Intel TensorFlow Xeon (Cascade Lake) Xeon (Cascade Lake) AVX-512 AVX-512 DL Boost 量子化 次々回やります DL Boost
  20. インテル株式会社 28 インテル® Xeon® プロセッサー上での 分散学習における技術考慮点 DL学 習 物理ノード #2

    物理ノード #1 CPU CPU Main Mem Main Mem UPI CPU CPU Main Mem Main Mem UPI Ethernet (25Gb~)/ IB/OPA Worker #1 Worker #2 Worker #3 Worker #4 Worker #5 Worker #6 Worker #7 Worker #8 1 2 3 • Parameter Server • Ring All-Reduce • Butterfly All-Reduce • Tree All-Reduce, etc.. 集団通信戦略 • oneCCL • MPI • Gloo etc.. 集団通信ライブラリー • Horovod • Distributed TensorFlow* • Ray etc.. 分散学習フレームワーク • XenServer* / KVM • Docker* / Singularity • Kubernetes* / Swarm etc.. 仮想化/コンテナー
  21. 31

  22. インテル株式会社 32 事前準備① Dockerインストール (Linux) § sudo apt update §

    sudo apt install apt-transport-https ca-certificates curl software-properties-common § curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - § sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable" § sudo apt update § apt-cache policy docker-ce § sudo apt install docker-ce § sudo usermod -aG docker ${USER} § su - ${USER} § id -nG
  23. インテル株式会社 33 事前準備① Dockerインストール (Windows & macOS) § 下記手順に従ってください §

    Windows • https://docs.docker.jp/docker-for-windows/install.html § macOS • https://docs.docker.jp/docker-for-mac/install.html
  24. インテル株式会社 34 事前準備② Anaconda のDocker イメージを取得&起動 § Dockerイメージ取得 • docker

    pull continuumio/anaconda § コンテナ起動 • docker run -i -t --privileged continuumio/anaconda /bin/bash