Slide 1

Slide 1 text

© 2020, Amazon Web Services, Inc. or its Affiliates. Amazon Web Services Japan Startup Solutions Architect Yoshitaka Haribara, Ph.D. 2020-08-27 AWS Startup.fm #8. 機械学習のコスト最適化

Slide 2

Slide 2 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Startup.fm #8. 機械学習のコスト最適化 • 設計原則 (15分) • ベストプラクティス (15分) • まとめ (10分) • アンケート・クレジット (10分) 質問は Sli.do #startup-fm に書き込んで頂ければ適宜拾います

Slide 3

Slide 3 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Well-Architected Framework 5本の柱 Operational Excellence セキュリティ 信頼性 パフォーマンス コスト 最適化

Slide 4

Slide 4 text

© 2020, Amazon Web Services, Inc. or its Affiliates. 設計原則

Slide 5

Slide 5 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Well-Architected Framework Machine Learning Lens コスト最適化の柱 設計原則 • 総所有コスト (TCO) を削減するためマネージドサービスを使う • 小さいデータセットで実験する • 正しいタイプ・サイズのインスタンスを選ぶ • 消費パターンに基づいた推論アーキテクチャ設計 • 全体の費用対効果 (ROI) と機会費用を定義 https://d1.awsstatic.com/whitepapers/architecture/wellarchitected-Machine- Learning-Lens.pdf

Slide 6

Slide 6 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS の機械学習スタック 幅広く完全な機械学習のサービス群 コンピュータ ビジョン 音声 テキスト 検索 チャットボット パーソナライズ 予測 不正検知 開発 コンタクトセンター Deep Learning AMIs & Containers GPUs & CPUs Elastic Inference Inferentia FPGA Amazon Rekognition Amazon Polly Amazon Transcribe +Medical Amazon Comprehend +Medical Amazon Translate Amazon Lex Amazon Personalize Amazon Forecast Amazon Fraud Detector Amazon CodeGuru AI サービス ML サービス 深層学習フレームワーク & インフラ Amazon Textract Amazon Kendra Contact Lens For Amazon Connect DeepGraphLibrary Ground Truth AWS Marketplace for ML Neo Augmented AI Built-in algorithms Notebooks Experiments Processing Model training & tuning Debugger Autopilot Model hosting Model Monitor SageMaker Studio IDE Amazon SageMaker

Slide 7

Slide 7 text

© 2020, Amazon Web Services, Inc. or its Affiliates. Amazon SageMaker で 54% 低い TCO を3年間で実現 Build Train Deploy ML インフラストラクチャ 運用 セキュリティ&コンプライアンス Amazon SageMaker で開発者の生産性を10x に

Slide 8

Slide 8 text

© 2020, Amazon Web Services, Inc. or its Affiliates. TCO の考え方: 3種類のコスト Capability 考慮事項 インフラストラクチャ • インスタンス単価 • スポットインスタンスなどの活用によるコスト削減 運用 • インスタンスのプロビジョン、環境構築 • パッチ適用等のメンテナンス • パフォーマンス最適化 • トレーニングジョブ毎の独立したパフォーマンス • 分散学習に適したストレージやネットワークのチューニング • 推論のための最適なインフラ選択とチューニング • データやモデル成果物の可用性・耐久性 • トレーニング・推論環境のモニタリング、ログの取得や管理 • 推論環境の可用性 (Multi-AZ 対応やオートスケーリング) セキュリティ & コンプライアンス • 転送時・保管時のデータ暗号化 • 権限管理と証跡管理 • SOC, PCI, ISO, FedRAMP, HIPAA などのコンプライアンス対応

Slide 9

Slide 9 text

© 2020, Amazon Web Services, Inc. or its Affiliates. 小さいデータセットで実験する 始めは 目処が立ったら P3/P3dn インスタンス P3 インスタンス 一部のデータ 全データ

Slide 10

Slide 10 text

© 2020, Amazon Web Services, Inc. or its Affiliates. トレーニング・推論それぞれに適切なインスタンスを選ぶ トレーニング 推論 P3/P3dn インスタンス (NVIDIA V100 GPU) G4dn インスタンス (NVIDIA T4 GPU) G4 Inf1 インスタンス Inf1 C5 インスタンス (Intel Xeon CPU)

Slide 11

Slide 11 text

© 2020, Amazon Web Services, Inc. or its Affiliates. リアルタイム推論が必要なければバッチ変換ジョブを使う Amazon S3

Slide 12

Slide 12 text

© 2020, Amazon Web Services, Inc. or its Affiliates. 全体の費用対効果 (ROI) と機会費用 • 費用対効果 (ROI) • 費用は各サービスの料金ページから試算。 • 例えば「SageMaker 料金」や「Personalize 料金」などと検索。 • 料金の例が載っているので参考にする。 • ビジネスインパクトは何の指標を向上させるかで考える。 • 機会費用 • そもそも ML を利用すべきか否か、最適な選択肢を選ぶ。

Slide 13

Slide 13 text

© 2020, Amazon Web Services, Inc. or its Affiliates. ベストプラクティス

Slide 14

Slide 14 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Well-Architected Framework Machine Learning Lens コスト最適化の柱 ベストプラクティス • 費用対効果の高いリソース • 需要と供給を合わせる • 支出意識 • 継続的な最適化 https://d1.awsstatic.com/whitepapers/architecture/wellarchitected-Machine- Learning-Lens.pdf

Slide 15

Slide 15 text

© 2020, Amazon Web Services, Inc. or its Affiliates. Managed Spot Training • オンデマンドに比べて最大90%のコスト削減 • 中断が発生する可能性があるので checkpoints に途中経過を書き出し • 最大で待てる時間を指定 呼び出し方: estimator = PyTorch("train.py", role=sagemaker.get_execution_role(), train_instance_count=1, train_instance_type="ml.p3.2xlarge", framework_version="1.5.0", train_use_spot_instances=True, train_max_wait=2*24*60*60, # >= train_max_run (デフォルト1日) checkpoint_s3_uri="s3://mybucket/checkpoints", checkpoint_local_path="/opt/ml/checkpoints/" ) estimator.fit("s3://mybucket/data/train") # fit でトレーニング

Slide 16

Slide 16 text

© 2020, Amazon Web Services, Inc. or its Affiliates. プロダクション環境 の課題は推論コスト 90% Inference (Prediction) 10% Training

Slide 17

Slide 17 text

© 2020, Amazon Web Services, Inc. or its Affiliates. 推論コストを抑えるための選択肢 1. Amazon Elastic Inference • 推論に必要な分だけ適切な量の計算リソースをアタッチ 2. Multi-Model Endpoint • 一つのエンドポイントの裏に複数モデルをデプロイ 3. AWS Inferentia • 推論のための専用チップ 4. Amazon SageMaker Neo • モデルのコンパイルによる軽量化と高速化

Slide 18

Slide 18 text

© 2020, Amazon Web Services, Inc. or its Affiliates. Amazon Elastic Inference 低い推論コスト アクセラレーターごと 1 - 32 TFLOPS 需要に合わせた キャパシティ TensorFlow, Apache MXNet, PyTorch をサポート Amazon EC2, Amazon SageMaker と統合 単精度・混合精度演算 深層学習の推論コストを最大75%削減

Slide 19

Slide 19 text

© 2020, Amazon Web Services, Inc. or its Affiliates. Elastic Inference PyTorch Elastic Inference G4 インスタンス (T4 GPU) 安い https://aws.amazon.com/blogs/machine-learning/reduce-inference-costs-on-amazon-ec2-for-pytorch-models-with-amazon-elastic-inference/

Slide 20

Slide 20 text

© 2020, Amazon Web Services, Inc. or its Affiliates. Multi-Model Endpoint • 単一のエンドポイントの裏に複数のモデルを置くことができる • 動的にモデルを追加することも可能 S3 model storage tokyo.tar.gz kanagawa.tar.gz osaka.tar.gz aichi.tar.gz S3://bucket/our-endpoint-models/ S3://bucket/our-endpoint-models Load

Slide 21

Slide 21 text

© 2020, Amazon Web Services, Inc. or its Affiliates. 機械学習 推論用プロセッサ AWS Inferentia • AWS による独自設計推論プロセッサ • 4 Neuron コア / チップ • チップ当たり最大128 TOPS • (2,000 TOPS @inf1.24xlarge) • 2ステージ メモリ階層 • 大容量オンチップ キャッシュと DRAM メモリ • FP16, BF16, INT8 データタイプをサポート • FP32 で構築された学習モデルを BF16 で実行可能 • 高速なチップ間通信 Inferentia Neuron コア cache Neuron コア cache メモリ Neuron コア cache Neuron コア cache メモリ メモリ メモリ

Slide 22

Slide 22 text

© 2020, Amazon Web Services, Inc. or its Affiliates. Neuronコア パイプライン - 大規模モデルを低遅延で推論 • 大規模モデルを低遅延で推論 • Neuron コア間、チップ間をパイプラインモードで接続することによ り、大規模モデルを各オンチップキャッシュメモリ上に展開し、 高スループット・低レイテンシを実現 CACHE Memory CACHE Memory CACHE Memory CACHE Memory Neuron コア パイプライン

Slide 23

Slide 23 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Neuron SDK https://github.com/aws/aws-neuron-sdk コンパイル Neuron コンパイラ (NCC) NEFF を出⼒ Neuron バイナリ (NEFF) デプロイ Neuron ランタイム (NRT) プロファイル Neuron ツール C:\>code --version 1.1.1

Slide 24

Slide 24 text

© 2020, Amazon Web Services, Inc. or its Affiliates. 一度のトレーニングで、どこでも 2x のパフォーマンス Amazon SageMaker Neo Neo Broad framework support Broad hardware support Open-source Neo-AI device runtime and compiler 1/10th the size of original frameworks https://github.com/neo-ai/ Inf1 Inferentia

Slide 25

Slide 25 text

© 2020, Amazon Web Services, Inc. or its Affiliates. Parse Model Optimize Tensors Generate Code Optimize Graph TensorFlow, MXNet, PyTorch, XGBoost のモデルを共通 フォーマットに変換 ML モデル (NN) 中のパターンを 認識し、実行コストを削減する ようグラフ構造の最適化 入力データの shape から パターンを抽出し、 効率的にメモリを割り当てる ターゲットデバイス用に 低レベルコンパイラを 用いて機械語を生成 Neo コンパイラ (TVM / treelite) Pruning Operator fusion Nested loop tiling Vectorization / Tensorization Data layout transform

Slide 26

Slide 26 text

© 2020, Amazon Web Services, Inc. or its Affiliates. 需要と供給を合わせる ⇨ Auto Scaling Amazon Application Auto Scaling

Slide 27

Slide 27 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Cost Explorer で支出の確認

Slide 28

Slide 28 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Cost Explorer (例: サービスごと)

Slide 29

Slide 29 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Cost Explorer (例: SageMaker, Usage Type ごと)

Slide 30

Slide 30 text

© 2020, Amazon Web Services, Inc. or its Affiliates. まとめ

Slide 31

Slide 31 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Well-Architected Framework Machine Learning Lens コスト最適化の柱 設計原則 • 総所有コスト (TCO) を削減するためマネージドサービスを使う • 小さいデータセットで実験する • 正しいタイプ・サイズのインスタンスを選ぶ • 消費パターンに基づいた推論アーキテクチャ設計 • 全体の費用対効果 (ROI) と機会費用を定義 https://d1.awsstatic.com/whitepapers/architecture/wellarchitected-Machine- Learning-Lens.pdf

Slide 32

Slide 32 text

© 2020, Amazon Web Services, Inc. or its Affiliates. AWS Well-Architected Framework Machine Learning Lens コスト最適化の柱 ベストプラクティス • 費用対効果の高いリソース • 需要と供給を合わせる • 支出意識 • 継続的な最適化 https://d1.awsstatic.com/whitepapers/architecture/wellarchitected-Machine- Learning-Lens.pdf

Slide 33

Slide 33 text

© 2020, Amazon Web Services, Inc. or its Affiliates. 費用対効果の高いリソース トレーニング 1. Managed Spot Training 推論 1. Amazon Elastic Inference • 推論に必要な分だけ適切な量の計算リソースをアタッチ 2. Multi-Model Endpoint • 一つのエンドポイントの裏に複数モデルをデプロイ 3. AWS Inferentia • 推論のための専用チップ 4. Amazon SageMaker Neo • モデルのコンパイルによる軽量化と高速化