機械学習基盤の選択肢 や Amazon SageMaker での構築

アマゾンウェブ サービスジャパン株式会社 ソリューションアーキテクト 針原佳貴 AWS AI/ML Tokyo #5 (2020-07-09)

© 2020, , Inc. or its Affiliates. All rights reserved. 自己紹介

• 針原 佳貴 (はりばら よしたか) • 博士 (情報理工学) • Startup Solutions Architect • スタートアップの技術支援・機械学習導入支援 • 好きな AWS のサービスは Amazon SageMaker, Amazon Braket

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 本セッションで話すこと

機械学習基盤の技術選定における 意思決定を助けるための情報を提供する

• 機械学習基盤に求められること • Kubernetes (k8s) と Kubeflow を使った Amazon EKS での構築例 • 機械学習のマネージドサービス Amazon SageMaker で実現可能なこと • 技術選定のための総所有コスト (Total Cost of Ownership; TCO) の観点

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 機械学習基盤に求められること

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 機械学習のワークロードは反復的なプロセス

ビジネス 課題 NO

データ 特徴量 ビジネス ML の問題に データ収集 データ 可視化 エンジニア モデル学習 モデル評価 ゴールの 落とし込む ・取込 前処理 ・分析 リング 評価

YES

本番環境 デプロイ

https://d1.awsstatic.com/whitepapers/architecture/wellarchitected-Machine-Learning-Lens.pdf

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 機械学習基盤に求められること

• データレイク • 構造化・非構造化データを格納 • 高可用性・耐久性を備えたスケーラブルなストレージ • モデルの開発 • Python と深層学習フレームワーク (TensorFlow, PyTorch, Apache MXNet など) • データサイエンティストには Jupyter Notebook, JupyterLab を好む人も多い • トレーニング • 深層学習の場合は行列演算を大量に行うので GPU を使う • 複雑なモデルで大量のデータに対しては、複数 GPU で分散学習を行うケースも • デプロイ・推論 • 作ったモデルをプロダクション環境に組み込むためホスティングする必要がある • モデルが呼び出されたらいつでも推論結果を返せるよう スケーラビリティ・高可用性が求められる

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 機械学習の基盤構築における課題

• 作ったモデルを一貫性のある形で推論用にデプロイするためには 環境の統一化が必要 • 深層学習フレームワーク・バージョンなどの依存関係が 開発環境・本番環境などの複数環境で一貫している必要がある

• 複数のデータサイエンティスト・機械学習エンジニアや インフラエンジニアが関わる • 開発者に技術選定が委ねられているが、機械学習モデルを作る人と デプロイ・インフラ管理する人のスキルセットが異なる場合がある

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 機械学習アプリケーションを構成するコンポーネント

CUDA, cuDNN Python スクリプト

深層学習フレームワーク 設定・ハイパーパラメータ

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ローカルでは動いたけど、本番で動かない問題

v11.0 v10.1 v9.0

ローカルラップトップ GPU サーバー 本番

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. コンテナという解決策

トレーニングスクリプト train.py

深層学習フレームワーク

CUDA, cuDNN

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 環境をスケールさせる上での課題

AWS Cloud

Docker コンテナ

開発者 EC2 インスタンス

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Kubernetes (k8s) とは何か?

「Kubernetesは、 宣言的な構成管理と自動化を促進し、 コンテナ化されたワークロードや サービスを管理するための、 ポータブルで拡張性のある オープンソースプラットホームです。」

https://kubernetes.io/ja/docs/concepts/overview/what-is-kubernetes/

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. なぜ Kubernetes で機械学習を行うのか?

• ライブラリ・依存関係の管理をコンテナで容易に • 環境の統一化と、再現性・追跡可能性

• トレーニング・デプロイの際に 宣言的にインフラ環境をコントロールしたい • 環境のコード化とインフラの再現性

• ML パイプラインを構築するためのオープンソース エコシステム • Kubeflow Pipelines, MLflow, Metaflow,... • Custom Resource Definition (CRD) を使って自分で記述することも可能

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon EKS

Kubernetes (k8s) と Kubeflow を使った構築例

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Elastic Kubernetes Service (EKS)

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon Elastic Kubernetes Service (EKS)

Upstream Kubernetes Kubernetes certified なマネージドサービス、 コミュニティによるツールと協調。

Highly available プロダクション環境向けに構築、 複数 AZ にまたがり高い可用性。

Integrated AWS エコシステムとの統合: VPC Networking, Elastic Load Balancing, IAM Permissions, CloudWatch など。

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Kubeflow: Kubernetes のための機械学習のツールキット

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 構成例: Kubeflow on AWS

Model training Model serving / inference

Pod Pod Pod

Auto-scaling nodegroup Amazon Elastic Container Registry (ECR) Amazon Elastic Kubernetes Service Docker images for (EKS) training/serving Worker Worker Worker Worker

Amazon EKS Workshop > Advanced > Machine Learning using Kubeflow

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Kubeflow ダッシュボード

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Notebook の作成

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 数分で立ち上がり接続すると

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Jupyter Notebook の画面が開く

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Kubeflow fairing

from kubeflow.fairing import TrainJob train_job = TrainJob(HousingServe, input_files=['ames_dataset/train.csv', "requirements.txt"], docker_registry=DOCKER_REGISTRY, backend=BackendClass(build_context_source=BuildContext)) train_job.submit()

from kubeflow.fairing import PredictionEndpoint endpoint = PredictionEndpoint(HousingServe, input_files=['trained_ames_model.dat', "requirements.txt"], docker_registry=DOCKER_REGISTRY, service_type='ClusterIP', backend=BackendClass(build_context_source=BuildContext)) endpoint.create()

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Katib

• ハイパーパラメータ最適化と ニューラルアーキテクチャサーチ (NAS)

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Kubeflow Pipelines

• 機械学習パイプライン構築

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 運用時の考慮事項

• Kubernetes, Kubeflow 環境のメンテナンス • Kubernetes クラスターのバージョンアップ (3ヶ月ごと) • 参考: ドキュメント「Amazon EKS クラスターの Kubernetes バージョンの更新」 • Kubeflow のアップグレード (alpha; limited support) • 参考: ドキュメント “Upgrading a Kubeflow Deployment”

• 機械学習モデル構築とインフラ・ワークフローエンジンを扱う スキルセットの乖離 • データサイエンティスト・機械学習エンジニアはインフラ運用を必ずしも得意 としておらず、多くの場合、インフラエンジニアによる運用と継続的なメンテ ナンスが求められる。

• モデルを作ってデプロイできることが重要、必要に応じた技術選定を • 例 えば Kubeflow などのオープンソースで構築した部分をマネージドサービス で代用することは可能か?

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon SageMaker

すべての開発者とデータサイエンティストのための 機械学習マネージドサービス

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon SageMaker でできること

データ準備 モデル構 トレーニング&チューニング デプロイ&管理 築 機械学習のための Web ベース統合開発環境 (IDE)

モデルの自動構築・トレーニング

完全マネージドな クリックで コンセプト 1クリックの クリックで 可視化して 1 オートスケーリング データ処理ジョブと 1 デプロイと ドリフトの 予測結果に コラボレーションと トレーニング デバッグ・最適化 トラッキング・比較 によるコスト削減 ラベリングの ビルトインの高性能な オートスケーリング 自動検知 人手のレビュー ワークフロー アルゴリズム・モデル

101011010 010101010 000011110

モデルの トレーニング・ 機械学習 トレーニング環境の 実験管理 本番環境への モデルの データ収集・準備 デバッグ・ アルゴリズム構築 セットアップ デプロイ 監視 チューニング

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 開発環境

• Jupyter Notebook/Lab をマネージドで使える • インスタンスタイプを選んで立ち上げるだけ • DL フレームワーク等はインストール済

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon SageMaker Studio (機械学習のための IDE)

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon SageMaker

Jupyter Notebook/Lab

開発

Amazon S3

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. The Jupyter Trademark is registered with the U.S. Patent & Trademark Office. Amazon SageMaker Amazon ECR ビルド済みの Docker イメージが 予め用意されている Amazon EC2 Jupyter Notebook/Lab P3 Instances

開発 学習

Amazon S3

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. The Jupyter Trademark is registered with the U.S. Patent & Trademark Office. Amazon SageMaker

トレーニングでのメリット: Amazon EC2 • API でインスタンス起動、 Jupyter Notebook/Lab P3 Instances 学習が完了すると自動停止 • 高性能なインスタンスを 秒課金で • 簡単にスポット インスタンスでコスト削減 開発 学習 • 指定した数の インスタンスを同時起動、 分散学習も容易

Amazon S3

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. The Jupyter Trademark is registered with the U.S. Patent & Trademark Office. Amazon SageMaker

Amazon EC2 Jupyter Notebook/Lab P3 Instances

開発 学習

Amazon S3

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. The Jupyter Trademark is registered with the U.S. Patent & Trademark Office. Amazon SageMaker Amazon ECR

Endpoint/ Jupyter Notebook/Lab Batch transform

開発 推論

Amazon S3

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. The Jupyter Trademark is registered with the U.S. Patent & Trademark Office. Amazon SageMaker は見方を変えると

機械学習に特化したコンテナ オーケストレーションサービスとも

• 環境は Docker イメージとして用意 • トレーニング・デプロイ先の環境はインスタンスタイプ・台数を選び コンテナを SageMaker 側で配置 • API 経由で呼び出すため、 過去のトレーニングジョブの履歴・生成物が残り、実験管理も可能

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SageMaker Python SDK Kubeflow Fairing のようなイメージ

import sagemaker

from sagemaker. import PyTorch # 各フレームワークに対応した Estimator クラス estimator = PyTorch("train.py", # トレーニングスクリプトなどを指定して初期化 role=sagemaker.get_execution_role(), train_instance_count=1, train_instance_type="ml.p3.2xlarge", framework_version="1.5.0")

estimator.fit("s3://mybucket/data/train") # fit でトレーニング

predictor = estimator.deploy(initial_instance_count=2, # 2以上にすると Multi-AZ instance_type="ml.m5.xlarge") # deploy でエンドポイント作成

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SageMaker Managed Spot Training

スポットインスタンスで最大90%トレーニング料金を削減

• 最大待ち時間を指定 • 中断が発生する可能性があるので checkpoints に途中経過を書き出し • 事例は AWS ブログ を参照:

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SageMaker Automatic Model Tuning ハイパーパラメータ最適化

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Step Functions Data Science SDK (AWS Step Functions)

• Python でワークフローを記述し機械学習パイプラインを自動化 • AWS Step Functions Data Science SDK • Amazon SageMaker, AWS Lambda をはじめとした各サービスに対応 • マネージドサービス

git push Git webhook docker push

Data AWS CodeCommit AWS CodeBuild Amazon Scientists/ or Elastic Container Developers 3rd party Registry Git repository (ECR)

AWS Step Functions workflow

Test data Train data Endpoint

Amazon SageMaker Amazon S3 AWS Glue Amazon S3 Amazon SageMaker Amazon S3 Batch Transform / (raw data) (data) Training Job / HPO (trained model) Endpoint deploy

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Apache Airflow にも対応

• Python で記述した DAG (有向非巡回グラフ) でワークフロー管理 • SageMaker Operator も用意されている • マネージドサービスではない (EC2 + RDS は別途必要) • 参考: 「Amazon SageMaker と Apache Airflow で エンドツーエンドの機械学習ワークフローを構築する」

Test data Raw data Cleaned data

Model artifact Prediction results Train data

Amazon SageMaker Amazon SageMaker Training / HPO Batch transform

Operator PythonOperator PythonOperator SageMakerTrainOperator/ SageMakerTransformOperator PythonOperator SageMakerTuningOperator

Airflow DAG

sparse data format Analyze model performance Filter long-tailed data → RecordIO protobuf based on test data

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon SageMaker の豊富な機能

準備 構築 トレーニング・チューニング デプロイ・管理

Amazon SageMaker Studio 機械学習のための統合開発環境 (IDE)

Amazon SageMaker Autopilot One Click Deployment ML モデルを自動で構築・トレーニング リアルタイム・バッチ・複数モデル

Amazon SageMaker GroundTruth Amazon SageMaker Notebooks One Click Training Amazon SageMaker Model Monitor トレーニングデータセットの構築 1クリックで利用できるノートブック 教師あり・教師なし・強化学習 コンセプトドリフトの自動検知

Amazon SageMaker Processing AWS Marketplace Automatic Model Tuning Amazon SageMaker Neo Python, Spark でデータ前処理 構築済アルゴリズム・モデル・データ 1クリックハイパーパラメータ探索 一度のトレーニングでどこへでもデプロイ

Amazon SageMaker Experiments Amazon Elastic Inference 各ステップで取得・管理・比較 オートスケーリングで最大75%コスト削減

Amazon SageMaker Debugger Amazon Augmented AI トレーニングのデバッグ・プロファイリング 推論結果に人手のレビューを追加

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 総所有コスト

Total Cost of Ownership (TCO)

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. EKS (Kubernetes) か SageMaker か

• 総所有コスト (Total Cost of Ownership; TCO) の観点がとても大事 • インフラコスト (トレーニング・推論に使う CPU/GPU) だけでない • 基盤自体の運用負荷 • セキュリティ・コンプライアンス・ガバナンス • エンタープライズや、B-to-B で提供する企業は特に重要 • どういう観点が必要かは AWS Well-Architected Framework ML Lens のセキュリティの柱を参照

一般論として、 自由度を手に入れるためには運用負荷の増加を受け入れる必要がある

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. TCO の考え方: 4種類のコスト

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

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. インスタンスのプロビジョン管理コスト

• SageMaker • トレーニングごとにインスタンスを立ち上げ、終わったら勝手に落ちる • Kubernetes • クラスタを共有するので、無駄なコストをかけないためには Cluster Autoscaler などでジョブが流れない時にはインスタンスを落とすように する必要がある

GPU の利用率が低い時間帯がある場合、 インスタンスの上げ下げがコストに大きく効いてくる

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. TCO の考え方: Amazon SageMaker との比較

• ML インフラストラクチャ自体のコストに加え、 運用・管理コストも考慮 • 特に、 組織が小さいうちは Amazon SageMaker を使った方がお得

でのコスト削減 組織規模 Amazon SageMaker 自社運用Kubernetes (EKS) との比較 小 5 data scientists -90% 中 15 data scientists -85% 大 50 data scientists -65% 巨大 250 data scientists -54%

https://pages.awscloud.com/NAMER-ln-GC-400-machine-learning-sagemaker-tco-learn-ty.html

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SageMaker Operator for Kubernetes

Kubernetes から Amazon SageMaker を呼び モデルをトレーニング・チューニング・デプロイ • 「SageMaker 自体は便利そうだが既存 Kubernetes クラスターがあり SageMaker API や SDK の使い方を覚えるのが面倒」という方に • Kubernetes でインフラ管理を行いながらも 部分的に SageMaker でトレーニング・チューニング・デプロイ • SageMaker 側でインフラを完全にマネージ • Managed Spot Training や分散学習などの機能も利用可能

Kubernetes Amazon SageMaker © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. SageMaker Components for Kubeflow Pipelines

Kubeflow Pipelines から Amazon SageMaker を呼び モデルをトレーニング・チューニング・デプロイ • 「SageMaker 自体は便利そうだが Kubeflow Pipelines を使いたくて SageMaker API や SDK を直接呼びたくない」という方に • Kubeflow でパイプラインとワークフローの作成を行いながらも 部分的に SageMaker でトレーニング・チューニング・デプロイ • SageMaker 側でインフラを完全にマネージ • Managed Spot Training や分散学習などの機能も利用可能

Kubeflow Pipelines Amazon SageMaker © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 機械学習基盤の選択肢

1. Kubernetes (k8s) を Amazon EKS で運用 • Kubernetes に慣れている企業 • Kubeflow を使った構成例を紹介した 2. Amazon SageMaker を使う • TCO の面で多くの企業にとって合理的 • 環境は Docker コンテナで管理できる 3. Kubernetes/Kubeflow Pipelines から Amazon SageMaker を呼ぶ (mix) • 既存 Kubernetes クラスターから、機械学習部分だけを切り出し • SageMaker Operator for Kubernetes や SageMaker Components for Kubeflow Pipelinesで呼ぶ

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon EKS 事例

• メルカリ様 • 「メルカリ写真検索における Amazon EKS の活用事 例」 • https://bit.ly/eks-mercari

• ABEJA 様 • 「顧客のアプリケーションコードが動くマルチテナント 環境における課題とEKSにたどり着くまで」 • https://bit.ly/eks-abeja-20

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon SageMaker をお使いの数万ものお客様

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon SageMaker 事例

• 【開催報告】 AI/ML@Tokyo • 過去開催分のブログです • https://aws.amazon.com/jp/blogs/news/tag/ai-mltokyo/

• 【開催報告】 Amazon SageMaker 事例祭り • 2019年の事例イベントをブログにまとめています • https://aws.amazon.com/jp/blogs/news/tag/amazon- sagemaker-fes/

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

• 機械学習ワークロードをコンテナ管理するメリットは多大にある • 環境の統一化・再現性・追跡可能性

• コンテナオーケストレーションはいくつかの選択肢がある • TCO も考慮して最適なものを選択、 多くの場合は Amazon SageMaker が第一候補

• ツール選定は開発速度にも影響 • 選択肢が多すぎて迷ったら AWS の Solutions Architect に相談

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. ありがとうございました!

@_hariby

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Appendix

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Materials (Kubeflow)

• EKS Workshop: Machine Learning Using Kubeflow • https://www.eksworkshop.com/advanced/420_kubeflow/ • Kubeflow 環境の構築手順が丁寧に説明されています

• eks-kubeflow-cloudformation-quick-start • https://github.com/aws-samples/eks-kubeflow-cloudformation-quick- start • とにかく手っ取り早く Kubeflow on EKS を試したい方に

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Materials (SageMaker)

• SageMaker Examples JP • https://github.com/aws-samples/amazon-sagemaker-examples-jp • 日本語サンプルノートブック

• SageMaker Examples • https://github.com/awslabs/amazon-sagemaker-examples • サンプルノートブック

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.