$30 off During Our Annual Pro Sale. View Details »

SageMaker Dive Deep Workshop 動かして理解する SageMaker Training Job の仕組み

SageMaker Dive Deep Workshop 動かして理解する SageMaker Training Job の仕組み

2022年4月26日に実施したワークショップのスライドです。

kmotohas

July 14, 2022
Tweet

More Decks by kmotohas

Other Decks in Technology

Transcript

  1. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker Dive Deep Workshop
    動かして理解する SageMaker Training Job の仕組み
    本橋 和貴
    機械学習パートナー ソリューション アーキテクト
    呉 和仁
    機械学習ソリューション アーキテクト
    アマゾン ウェブ サービス ジャパン合同会社

    View Slide

  2. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    本ワークショップの対象者
    • これまで SageMaker の概要の説明を聞いたりハンズオンイベント
    に参加したものの、⾃前のモデルをどう SageMaker 上で学習する
    かイメージがつかなかった⽅々
    • これから SageMaker を活⽤していきたい⽅々
    前提条件
    • 【必須】Python の基礎知識
    • 【望ましい】TensorFlow/PyTorch での実装経験
    *SageMaker Training Job 以外の機能にはほぼ触れません
    2

    View Slide

  3. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    Agenda
    • SageMaker Training Job 座学
    • SageMaker の全体像
    • SageMaker Notebooks と SageMaker Studio
    • SageMaker Training Job と SageMaker Python SDK
    • SageMaker Training Job ハンズオン
    • Toy コードを⽤いた SageMaker Training Job の動作の理解
    • TensorFlow / PyTorch の学習コードを SageMaker ⽤に書き換える
    3

    View Slide

  4. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    機械学習を取り巻く状況
    AI による変⾰
    2025年までに新しいエン
    タープライズ アプリケー
    ションの 90% 以上に AI
    が組み込まれる
    –IDC
    2024年までに、⼈⼯知能に
    対する投資は1100億ドルに
    到達する
    –IDC
    加速する投資
    機械学習と
    DevOps の統合
    機械学習はすでにメイン
    ストリームの DevOps
    プロセスの⼀部であり、
    独⽴したプロジェクトで
    はない
    ̶Gartner
    あらゆるものが「賢く」 なる時代

    View Slide

  5. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    お客様にとって、AI/ML はすでに最重要課題
    お客様は急速なペースで ML を導⼊しています。2024年には75%のお客様が ML
    を運⽤するようになります(出典︓Gartner)
    より多くのビジネスグループにリーチを広げる
    ML は、マーケティング、法務、サプライチェーン、⼈事、財務、さらには IT など、
    ビジネスライン全体で使⽤されています。ML のワークロードは、お客様のアカウント
    での活動範囲を増やすための⾜がかりとなります。
    お客様には⽀援が必要
    お客様は ML を重視していますが、ほとんどの場合、実⾏するための専⾨的なスキルを
    社内に持ち合わせていません。お客様は、ML プラットフォームの構築からビジネス
    ユースケースの解決、技術⼒の拡張まで、ML のライフサイクル全体にわたって⽀援を
    必要としています。
    AI & 機械学習
    なぜパートナーに
    とって重要か

    View Slide

  6. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    お客様の ML の障壁を SageMaker で克服
    ଟذʹΘͨΔσʔλαΠΤϯε
    πʔϧ
    ୯ҰΠϯλʔϑΣʔε಺ͷ౷߹ .-πʔϧ
    *%&Λར༻ͯ͠ϞσϧΛߏஙɺֶशɺσϓϩΠ
    .-ͷͨΊͷେྔͷσʔλ
    ͷॲཧͱϥϕϦϯά
    ઐ༻ͷσʔλ४උπʔϧ
    .- ͷͨΊͷσʔλͷϥϕϦϯάͱ४උ
    .-։ൃऀͷෆ଍
    ϊʔίʔυ .-πʔϧ
    .-ͷઐ໳஌͕ࣝͳͯ͘΋༧ଌΛੜ੒
    ୀ۶ͳखಈͷ
    .-ΦϖϨʔγϣϯ
    ૊ΈࠐΈͷ .-0QT ػೳ
    .-0QT ϓϥΫςΟεΛ࣮૷ͯ͠ .-ϥΠϑαΠΫϧΛޮ཰Խ

    View Slide

  7. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates. 7
    https://gitlab.com/juliensimon/awsmlmap/-/blob/master/map-sagemaker.png
    データ準備
    モデル構築
    モデル学習
    最適化
    ⾃動化
    モニタリング
    デプロイ

    View Slide

  8. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    クラウド上に機械学習開発
    プラットフォームを構築
    SageMaker Training Job
    を活⽤して学習コストの最適化
    ⾃動ハイパーパラメータ
    最適化や分散学習を⽤いて
    ⼤規模に学習を実⾏
    MLOps パイプラインを構築して
    モデルの再学習プロセスを⾃動化
    移⾏
    最適化
    ⼤規模化
    ⾃動化
    モデル学習の Modernization
    SageMaker
    Experiments
    Managed
    Spot Training
    SageMaker
    Debugger
    SageMaker
    Autopilot
    SageMaker
    Studio
    SageMaker
    Pipelines
    SageMaker
    Clarify
    SageMaker
    Data Wrangler

    View Slide

  9. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    クラウド上に機械学習開発
    プラットフォームを構築
    SageMaker Training Job
    を活⽤して学習コストの最適化
    ⾃動ハイパーパラメータ
    最適化や分散学習を⽤いて
    ⼤規模に学習を実⾏
    MLOps パイプラインを構築して
    モデルの再学習プロセスを⾃動化
    移⾏
    最適化
    ⼤規模化
    ⾃動化
    モデル学習の Modernization
    SageMaker
    Experiments
    Managed
    Spot Training
    SageMaker
    Debugger
    SageMaker
    Autopilot
    SageMaker
    Studio
    SageMaker
    Pipelines
    SageMaker
    Clarify
    SageMaker
    Data Wrangler

    View Slide

  10. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    Amazon SageMaker の基本構成要素
    学習スクリプト
    ML 実⾏環境
    学習データ

    Amazon SageMaker Amazon ECR
    Amazon S3
    SageMaker
    Python SDK

    View Slide

  11. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    Jupyter ノートブック環境でモデルを構築
    The Jupyter Trademark is registered with the U.S. Patent &
    Trademark Office.

    View Slide

  12. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    Amazon SageMaker Notebooks
    Jupyter Notebook JupyterLab

    View Slide

  13. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    Amazon SageMaker Studio

    View Slide

  14. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker Studio / Notebooks
    14

    View Slide

  15. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    EBS Volume
    SageMaker Notebooks のアーキテクチャー
    15
    🧑💻 Browser EC2 instance
    Notebook Server
    Kernel
    Kernel
    HTTPS/
    WSS 0MQ
    Jupyter /
    JupyterLab UI
    User
    機械学習開発⽤にセットアップされた
    シンプルなマネージド Jupyter 環境
    組み込みのカーネル

    View Slide

  16. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker Studio のアーキテクチャー
    16
    Amazon Elastic File System (EFS)
    👩💻
    User 1
    🧑💻
    User 2
    User Profile
    User Profile
    Studio
    UI
    Studio
    UI
    EC2 instance
    Notebook
    Server
    JupyterServer App
    System
    Terminal
    docker container
    KernelGateway
    Kernel
    Kernel Image Terminal
    docker container
    KernelGateway App
    KernelGateway
    KernelGateway
    KernelGateway
    ...
    ...
    ... ... ...

    View Slide

  17. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker Studio のアーキテクチャー
    17
    Amazon Elastic File System (EFS)
    👩💻
    User 1
    🧑💻
    User 2
    User Profile
    User Profile
    Studio
    UI
    Studio
    UI
    EC2 instance
    Notebook
    Server
    JupyterServer App
    System
    Terminal
    docker container
    KernelGateway
    Kernel
    Kernel Image Terminal
    docker container
    KernelGateway App
    KernelGateway
    KernelGateway
    KernelGateway
    ...
    ...
    ... ... ...

    View Slide

  18. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker Studio のアーキテクチャー
    18
    Amazon Elastic File System (EFS)
    👩💻
    User 1
    🧑💻
    User 2
    User Profile
    User Profile
    Studio
    UI
    Studio
    UI
    EC2 instance
    Notebook
    Server
    JupyterServer App
    System
    Terminal
    docker container
    KernelGateway
    Kernel
    Kernel Image Terminal
    docker container
    KernelGateway App
    KernelGateway
    KernelGateway
    KernelGateway
    ...
    ...
    ... ... ...

    View Slide

  19. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker Notebooks と SageMaker Studio の機能⽐較
    19
    SageMaker Notebooks SageMaker Studio
    UI Jupyter / JupyterLab
    JupyterLab を拡張した
    SageMaker IDE
    ストレージ EBS EFS
    環境分離 Anaconda Docker
    インスタンスの変更
    インスタンス⾃体の
    停⽌ → 切り替え → 起動
    Studio UI 上で切り替え
    ローカルモード 使⽤可能 不可
    AWS SSO 連携不可 連携可能

    View Slide

  20. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    Cloud9 に EFS をマウントするという選択肢
    20
    Amazon Elastic File System AWS Cloud9
    Amazon SageMaker

    View Slide

  21. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker Training Job
    21

    View Slide

  22. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    ノートブック環境と学習環境の分離
    The Jupyter Trademark is registered with the U.S. Patent &
    Trademark Office.

    View Slide

  23. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    ノートブック環境と学習環境の分離
    The Jupyter Trademark is registered with the U.S. Patent &
    Trademark Office.

    View Slide

  24. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    ノートブック環境と学習環境の分離
    The Jupyter Trademark is registered with the U.S. Patent &
    Trademark Office.
    学習が終わると削除
    学習時間のみ秒単位で課⾦

    View Slide

  25. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    推論環境をコンテナで素早く構築
    The Jupyter Trademark is registered with the U.S. Patent &
    Trademark Office.
    エンドポイント

    View Slide

  26. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker Python SDK
    • SageMaker でのモデル学習とかデプロイを簡素化する OSS の SDK
    26
    👩💻
    Amazon SageMaker
    AWS SDK for Python
    (Boto3)
    SageMaker
    Python SDK
    ⼤変なことは
    SageMaker に
    やらせよう
    Estimator
    学習コード
    ハイパーパラメータ
    コンテナ URI

    Estimator.fit(学習データ)
    create_training_job(...)
    インスタンスタイプ
    わかる
    Estimator.deploy(...)
    create_model(...)
    create_endpoint_config(...)
    create_endpoint(...)

    View Slide

  27. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    SageMaker でモデルをトレーニングする⽅法
    • Jupyter Notebook 上で直接実⾏
    § 任意のライブラリ
    • SageMaker Traning Job で実⾏
    § 組み込みアルゴリズム
    – Linear Learner, XGBoost, k-NN, Random Cut Forest, ResNet, BrazingText, ...
    § スクリプトモード
    – TensorFlow, MXNet, PyTorch, HuggingFace, SKLearn, etc
    § 独⾃コンテナの持ち込み (Bring Your Own Container)
    § AWS Marketplace アルゴリズム
    27

    View Slide

  28. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    Estimator – SageMaker Python SDK
    • エンドツーエンドの学習とモデルのデプロイを制御するクラス
    28
    EstimatorBase
    Estimator
    Framework
    MXNet
    XGBoost
    SKLearn
    TensorFlow
    HuggingFace
    PyTorch
    Chainer
    汎⽤的な Estimator クラス
    • 組み込みアルゴリズム
    • コンテナ持ち込み (BYOC)
    各フレームワーク⽤にカスタマイズされた Estimator クラス
    • スクリプトモード
    AlgorithmEstimator
    • AWS Marketplace アルゴリズム

    View Slide

  29. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    TensorFlow Estimator の使⽤例
    29
    from sagemaker.tensorflow import TensorFlow
    tensorflow_estimator = TensorFlow(
    entry_point='train.py',
    hyperparameters={
    'epochs': 20,
    'batch-size': 64,
    'learning-rate': 0.1
    },
    framework_version='2.6',
    py_version='py38',
    instance_type='ml.p3.2xlarge',
    instance_count=1,
    )
    tensorflow_estimator.fit({
    'train': 's3://my-bucket/path/to/training/data',
    'test': 's3://my-bucket/path/to/test/data'
    })
    学習コードの指定
    学習コードに渡すハイパーパラメータの設定
    学習コンテナの指定
    学習インスタンスの指定
    学習データの指定
    e.g.) 763104351884.dkr.ecr.us-east-1.amazonaws.com/tensorflow-training:2.6-gpu-py38

    View Slide

  30. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    ⼀般的な学習コードの構成
    SageMaker Training Job
    § [ジョブ開始時]: 学習データは S3 から⾃動ダウンロード
    § [ジョブ終了時]: 学習済みモデルは S3 に⾃動アップロード
    コードの書き換えが最低限必要なのは
    § データをどのパスからロードするか
    § モデルをどのパスに保存するか
    § (*ハイパーパラメータは引数として argparse で取得可能)
    § (*分散学習する場合はトレーニング部分も)
    30
    データのロード
    モデルの定義
    トレーニング*
    モデルの保存



    View Slide

  31. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    コード × コンテナ × SageMaker Python SDK
    31
    データのロード
    モデルの定義
    トレーニング*
    モデルの保存



    /opt/ml
    ├──
    │ ├── data
    │ │ ├──
    │ │ │ └──
    │ │ ├──
    │ │ │ └──
    │ │ └──
    │ │ └──
    │ └── config
    │ ├── hyperparameters.json
    │ └── resourceConfig.json

    ├── code
    │ ├──
    │ └── .py
    ├── output
    │ └──
    └── model
    └──
    データ
    モデル
    コード
    fit( ={
    'train': '’,
    'validation': '’,
    'test': '’,
    })
    hyperparameters={
    'epochs': 20,
    'batch-size': 64,
    'learning-late': 0.1
    }
    Estimator
    学習コンテナのファイルシステム
    [source_dir]
    entry_point

    View Slide

  32. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    学習コンテナ(学習コード)で使える便利な環境変数*
    • SM_CHANNEL_{channel_name}
    § Estimator.fit() の引数で指定したチャンネルのデータが配置されるディレクトリ
    – SM_CHANNEL_TRAIN=/opt/ml/input/data/train
    – SM_CHANNEL_TEST=/opt/ml/input/data/test
    • SM_HPS
    § Estimator に渡したハイパーパラメータを JSON エンコードした辞書
    – SM_HPS=‘{“epochs”: “20”, “batch-size”: “64”, “learning-rate”: “0.1”}’
    • SM_NUM_GPUS / SM_NUM_CPUS
    § コンテナで利⽤可能な GPU / CPU の数
    • SM_MODEL_DIR
    § S3 にアップロードされることになるモデルの置き場所
    – SM_MODEL_DIR=/opt/ml/model
    32
    *その他多数あり
    https://zenn.dev/kmotohas/articles/7bfe313eab01ea

    View Slide

  33. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    (おまけ)学習済みモデルのデプロイ
    • fit を呼び出すと Estimator の self.latest_training_job にジョブ
    の情報が格納され、その情報をもとにモデルをデプロイできる
    33
    predictor = estimator.deploy(
    instance_type="ml.c5.xlarge",
    initial_instance_count=1,
    )
    result = predictor.predict(data)
    create_model()
    create_endpoint_config()
    create_endpoint()
    invoke_endpoint()

    View Slide

  34. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    (おまけ)Estimator.deploy() がやっていること
    34
    Inference Container Image
    Model Artifact
    Advanced Configurations
    SageMaker Model
    IAM Role
    SageMaker
    Endpoint
    インスタンスタイプ / 数 /
    リクエストの割合 / ...
    invoke_endpoint()
    create_model
    create_endpoint_config
    create_endpoint



    View Slide

  35. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    (おまけ)A/B テストの仕組みも簡単に作れる
    35
    SageMaker
    Endpoint
    invoke_endpoint()
    95%
    5%
    Variant 1
    Variant 2

    View Slide

  36. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    まとめ
    • SageMaker は機械学習ライフサイクル全体をエンドツーエンドで
    サポートすることができる
    • まずは、SageMaker Training Job を利⽤してモデル学習を
    modernize していくところから始めるのがおすすめ
    • SageMaker Python SDK を⽤いるとモデルの学習からデプロイまで
    の⼿順を簡素化できる
    • スクリプトモードを⽤いる場合も学習コードの書き換えは最⼩限
    36

    View Slide

  37. AWS パートナー様限定 SageMaker Dive Deep Workshop 〜 動かして理解する SageMaker Training Job の仕組み
    © 2022, Amazon Web Services, Inc. or its affiliates.
    Thank you!
    © 2022, Amazon Web Services, Inc. or its affiliates.

    View Slide