CybagerAgent AI事業本部 2023年度新卒研修 MLOps 応用編で使用したスライドです。 ハンズオンで使用したGitHub Repository: https://github.com/nsakki55/aws-mlops-handson
©2023 CyberAgent Inc. Distribution prohibitedDynalyst 長江 五月MLOps応用編
View Slide
©2023 CyberAgent Inc. Distribution prohibitedToday’s Schedule10: 00 - 12: 30: Container編13: 45 - 14: 45: MLOps基礎編15: 00 - 18: 30: MLOps応用編
©2023 CyberAgent Inc. Distribution prohibitedハンズオンリポジトリ3https://github.com/nsakki55/aws-mlops-handson
©2023 CyberAgent Inc. Distribution prohibited 4皆さんに作成してもらうMLシステム学習パイプライン 推論サーバー
©2023 CyberAgent Inc. Distribution prohibited自己紹介長江五月Dynalyst Data Scientist業務MLモデリングデータ分析MLOps保有AWS認定証5@nsakki55 @nsakki55
©2023 CyberAgent Inc. Distribution prohibited本講義のゴール6持続可能なMLシステム をPortableな実験環境Scalableな推論AppContainer編 MLOps基礎編を使って作るMLOps応用編
©2023 CyberAgent Inc. Distribution prohibited本日皆さんが触れる技術スタック7MLSGDClassifierFeature HashingPython静的解析isortblackflake8mypypytestCI/CDDockerGitHub ActionsECSECRFargateS3StepFunctionsEvent BridgeAuto Scaling ALBAWS
©2023 CyberAgent Inc. Distribution prohibited目次81. 前準備2. ローカルでML基盤を開発3. CI/CD4. MLパイプラインをAWS上で実行する5. 推論サーバーをAWS上に立てる
©2023 CyberAgent Inc. Distribution prohibited目次91. 前準備2. ローカルでML基盤を開発3. CI/CD4. MLパイプラインをAWS上で実行する5. 推論サーバーをAWS上に立てる
©2023 CyberAgent Inc. Distribution prohibitedPrerequisites101. Docker Desktop2. pyenv3. poetry4. awscli (認証含む)5. hey6. direnv7. terraform
©2023 CyberAgent Inc. Distribution prohibitedハンズオンリポジトリ11$ git clone https://github.com/nsakki55/aws-mlops-handson$ cd aws-mlops-handsonハンズオンリポジトリをクローンhttps://github.com/nsakki55/aws-mlops-handson
©2023 CyberAgent Inc. Distribution prohibited環境変数の設定12direnvの設定$ cp .env.example .env$ vi .envAWS_REGION=ap-northeast-1AWS_ACCOUNT_ID=012345678901AWS_PROFILE=mlops-handsonTF_VAR_aws_region=${AWS_REGION}TF_VAR_aws_profile=${AWS_PROFILE}TF_VAR_aws_account_id=${AWS_ACCOUNT_ID}AWSリージョン名$ direnv allow .AWSアカウントID使用するAWS Profile名使用するAWS Profile名Terraform実行変数(変更しない)
©2023 CyberAgent Inc. Distribution prohibited環境変数の設定13direnvの設定$ echo $TF_VAR_aws_regionap-northeast-1$ eval "$(direnv hook zsh)" >> ~/.zshrc環境変数が設定できているか確認設定できていない場合
©2023 CyberAgent Inc. Distribution prohibitedAWSリソースの作成14$ cd infra$ terraform initTerraformでAWSリソースを作成する$ terraform apply$ cd ..
©2023 CyberAgent Inc. Distribution prohibitedPython環境構築15pyenvでPython 3.9.0の環境を用意する依存関係のインストール$ pyenv install 3.9.0$ pyenv local 3.9.0$ poetry install
©2023 CyberAgent Inc. Distribution prohibitedS316Simple Storage Serviceオブジェクトストレージサービス非構造化データを低コストで保存できる
©2023 CyberAgent Inc. Distribution prohibited学習データの準備17S3 Bucket作成S3→バケットページの「バケットを作成」をクリック
©2023 CyberAgent Inc. Distribution prohibited学習データの準備18S3 Bucket作成バケット名{name}-mlops-handson
©2023 CyberAgent Inc. Distribution prohibited学習データの準備19$ unzip train_data.zip学習データを解凍
©2023 CyberAgent Inc. Distribution prohibited学習データの準備20学習データをアップロード作成した自分のバケットページの「アップロード」をクリック
©2023 CyberAgent Inc. Distribution prohibited学習データの準備21学習データをアップロードtrain_dataをアップロード
©2023 CyberAgent Inc. Distribution prohibited環境変数の設定22direnvの設定$ vi .envAWS_BUCKET=nagae-mlops-handsonUSER_NAME=nagae 自分の名前作成したS3 Bucket名
©2023 CyberAgent Inc. Distribution prohibited目次231. 前準備2. ローカルでML基盤を開発3. CI/CD4. MLパイプラインをAWS上で実行する5. 推論サーバーをAWS上に立てる
©2023 CyberAgent Inc. Distribution prohibitedMLOpsの要件24広告クリック率をリアルタイム予測するML基盤を作成する
©2023 CyberAgent Inc. Distribution prohibitedMLOpsの要件25データkaggle avazu ctr prediction dataset
©2023 CyberAgent Inc. Distribution prohibitedモデリングを行う26jupyter notebookで分析$ poetry run jupyter lab
©2023 CyberAgent Inc. Distribution prohibitedスクリプトで管理する27/aws-mlops-handson├ Dockerfile - - - - - - - - - -MLパイプライン・推論サーバー用 Dockerfile├ Makefile - - - - - - - - - - -ローカル実行コマンド├ docker-compose.yaml - - -推論サーバー用 docker-compose.yaml├ infra - - - - - - - - - - - - - MLシステムのインフラ用Terraform├ ml - - - - - - - - - - - - - - -MLパイプラインの実装├ predictor - - - - - - - - - - 推論サーバーの実装├ pyproject.toml - - - - - - -poetry, pysen設定└ tests - - - - - - - - - - - - -テストの実装ファイル説明(全体)
©2023 CyberAgent Inc. Distribution prohibitedMLパイプラインをローカル実行する28/aws-mlops-handson└ ml├ aws - - - - - - - - - - AWSリソース操作├ dataset - - - - - - - -データ読み込み操作├ logger- - - - - - - - - ロギング├ model - - - - - - - - - MLモデル├ path - - - - - - - - - - ディレクトリ操作├ preprocessor- - - - - 前処理└ main.py - - - - - - - - MLパイプラインファイル説明(MLパイプライン)
©2023 CyberAgent Inc. Distribution prohibited学習ワークフロー概略(MLOps基礎編再掲)学習ワークフローを構成するstep● Extract step● Validate data step● Preprocess step● Train step● Evaluate step● Validate model step黄色step: ハンズオンで実行するstep
©2023 CyberAgent Inc. Distribution prohibitedMLパイプラインをローカル実行する30実行するMLパイプラインデータ取得 前処理 学習 モデル保存データ保存
©2023 CyberAgent Inc. Distribution prohibitedMLパイプラインをローカル実行する31コマンド確認$ make$ make build-mlMLパイプラインのimage buildMLパイプラインの実行$ make run-ml
©2023 CyberAgent Inc. Distribution prohibitedMLパイプラインをローカル実行する32S3にモデルがアップロードされてることを確認
©2023 CyberAgent Inc. Distribution prohibited推論サーバーをローカル実行する33推論サーバーを起動$ make up$ make logsdocker-composeのログを確認する
©2023 CyberAgent Inc. Distribution prohibitedデプロイ方法(MLOps基礎編再掲)1. モデルビルドパターンa. Dockerのビルド時にモデルを含めてしまう2. モデルロードパターンa. 外部ストレージから実行時に取得
©2023 CyberAgent Inc. Distribution prohibited推論サーバーをローカル実行する35推論サーバーにリクエストを投げる$ make predict$ make down推論サーバーを停止する
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する36Optunaでハイパラチューニングを行うモデルを追加する/3-mlops-handson└ ml└ model - - - - - - - - - MLモデル├ sgd_classifier_ctr_model.py└ sgd_classifier_ctr_optuna_model.pyMLパイプライン・推論サーバーに追加
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する37ローカル開発の流れ1. 実装2. 静的解析チェック3. テストの追加
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する38実装/3-mlops-handson/ml/model/__init__.pyfrom .base_model import BaseModelfrom .sgd_classifier_ctr_model import SGDClassifierCTRModelfrom .sgd_classifier_ctr_optuna_model import SGDClassifierCTROptunaModel__all__ = ["BaseModel", "SGDClassifierCTRModel", "SGDClassifierCTROptunaModel"]/3-mlops-handson/ml/model/models.pyclass MODELS(Enum):sgd_classifier_ctr_model = Model(….)sgd_classifier_ctr_optuna_model = Model(name="sgd_classifier_ctr_optuna_model",model=SGDClassifierCTROptunaModel,preprocessor=CTRModelPreprocessor,)オレンジ線の実装を追加
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する39静的解析チェック$ make lintpysenによって以下のチェックが実行されます・isort - - - -import文チェック・black - - - -コードスタイル整形・flake8 - - - コード品質チェック・mypy - - - - 静的型チェック$ make formatエラーが出た場合は...isort・blackによるコード整形が自動で行われます
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する40テストの追加$ make testFAILED tests/ml/model/test_models.py::test_all_models_are_tested - AssertionError: assert ['sgd_classifier_ctr_model'] == ['sgd_classif...optuna_model']追加したモデルのテストがないエラーがでますテストを実行
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する41テストの追加params = {"sgd_classifier_ctr_model": ("sgd_classifier_ctr_model", SGDClassifierCTRModel, CTRModelPreprocessor),"sgd_classifier_ctr_optuna_model": ("sgd_classifier_ctr_optuna_model", SGDClassifierCTROptunaModel, CTRModelPreprocessor),}/3-mlops-handson/ml/tests/ml/model/test_models.py$ make testテストが通るか確認オレンジ線の実装を追加
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する42追加したモデルの学習を実行する$ make build-ml$ vi .env$ make run-mlAWS_BUCKET=nagae-mlops-handsonAWS_ACCOUNT_ID=012345678901AWS_PROFILE=mlops-handsonAWS_ALB_DNS=USER_NAME=nagaeVERSION=2023-04-01MODEL=sgd_classifier_ctr_optuna_model 追加したモデル名
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する43推論サーバーに複数モデルを乗せるmodel_candidates = ["sgd_classifier_ctr_model", "sgd_classifier_ctr_optuna_model"]model_preprocessor_list = [get_model_preprocessor(bucket=BUCKET, model_name=model_name, version=VERSION) for model_name in model_candidates]/3-mlops-handson/predictor/routers.py オレンジ線の実装を追加
©2023 CyberAgent Inc. Distribution prohibitedローカル開発を体験する44推論サーバーに複数モデルを乗せる$ make up推論サーバーを起動$ make predict{"prediction":0.18150064819518077,"model":"sgd_classifier_ctr_optuna_model"}推論リクエスト複数のモデルの予測値を返す→ABテストが可能
©2023 CyberAgent Inc. Distribution prohibitedA/B基盤(MLOps基礎編再掲)何かの施策の効果検証の際、A/Bテストが使われるがA/Bの基盤パターンもいくつか存在CTR予測モデルAとBのA/Bテストをしたい時● トラフィックレベルの振り分け● アプリケーションレベルの振り分け
©2023 CyberAgent Inc. Distribution prohibited目次461. 前準備2. ローカルでML基盤を開発3. CI/CD4. MLパイプラインをAWS上で実行する5. 推論サーバーをAWS上に立てる
©2023 CyberAgent Inc. Distribution prohibitedCI/CD47静的解析・テストがない場合バグの混入可読性の低下誰も触れないMLシステムリリース自動化がない場合手作業によるリリースMLシステムの属人化
©2023 CyberAgent Inc. Distribution prohibitedCI/CD48GitHub Actionsの活用・静的解析チェック・テスト実行・image build/pushコードの品質管理・リリースの自動化
©2023 CyberAgent Inc. Distribution prohibitedコード品質管理の自動化49/aws-mlops-handson/.github/workflows/test-mlops-handson.ymlname: Run tests and linterson:push:branches:- mainjobs:tests:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v2- name: Set up Python 3.9uses: actions/setup-python@v2with:python-version: 3.9- name: Install dependenciesrun: |pip install --upgrade pippip install poetry=="1.4.2"poetry install- name: Run pysen lintrun: |poetry run pysen run lint- name: Run testsrun: |poetry run pytest実行環境の設定pysen, pytestの実行
©2023 CyberAgent Inc. Distribution prohibitedリリースの自動化50/aws-mlops-handson/.github/workflows/deploy-mlops-handson.ymlname: Run creating docker imageon:push:branches:- mainjobs:build:runs-on: ubuntu-latestpermissions:id-token: writecontents: readsteps:- uses: actions/checkout@v3- uses: aws-actions/configure-aws-credentials@v2with:role-to-assume: ${{ secrets.GTIHUB_ACTIONS_AWS_ROLE }}aws-region: ap-northeast-1- name: Login to Amazon ECRid: login-ecruses: aws-actions/amazon-ecr-login@v1実行環境の設定GitHub Action用のAWS Role名をGitHub RepositoryのSecret変数で管理
©2023 CyberAgent Inc. Distribution prohibitedリリースの自動化51/aws-mlops-handson/.github/workflows/deploy-mlops-handson.yml- name: Build, tag, and push image to Amazon ECR for mlenv:ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}ECR_REPOSITORY: mlops-handson/ml-pipelinerun: |IMAGE_TAG=$(git rev-parse --short "$GITHUB_SHA")docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --target ml .docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAGdocker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG- name: Build, tag, and push image to Amazon ECR for predict-apienv:ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}ECR_REPOSITORY: mlops-handson/predict-apirun: |IMAGE_TAG=$(git rev-parse --short "$GITHUB_SHA")docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG --target predictor ./docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAGdocker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAGimage build/push
©2023 CyberAgent Inc. Distribution prohibitedデモ52Pull RequestをマージしてGitHub Actionが実行されるのを確認
©2023 CyberAgent Inc. Distribution prohibited目次531. 前準備2. ローカルでML基盤を開発3. CI/CD4. MLパイプラインをAWS上で実行する5. 推論サーバーをAWS上に立てる
©2023 CyberAgent Inc. Distribution prohibited技術選定の前に...54○ MLを本当に使うべきかを考える○ ゴールはビジネス課題の解決であって、MLサービスを作ることではない○ MLを導入した時のコストと解決できるコストを評価する○ 小さく作り始める○ いきなり完成系を目指すのではなく、小さく作り始める■ デプロイは手動から■ 簡単な線形モデルから○ 人と向き合う○ 守備範囲が広く1人ではできないからこそ、チームメンバー(Biz、SWE、DS)と適切にコミュニケーションを取り、すり合わせる(MLOps基礎編再掲)最後に
©2023 CyberAgent Inc. Distribution prohibited技術選定55・チームにML/DSは1人・既存システムのサーバーはECSでホスティング・既存システムのオフライン処理をECS Taskで実行してる・既存システムのワークフローツールにStepFunctionsを使用してる前提状況
©2023 CyberAgent Inc. Distribution prohibited技術選定56・チームにML/DSは1人・既存システムのサーバーはECSでホスティング・既存システムのオフライン処理をECS Taskで実行してる・ワークフローツールにStepFunctionsを既に使用してる前提状況→ 他メンバーの協力が得られるように、既存システムに乗っかる→ SageMakerよりチームナレッジがあるECSを優先する→ 最初はシンプルに、コンテナを実行するだけの構成にする→ チームで面倒をみるワークフローツールは1つにする
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン構成57
©2023 CyberAgent Inc. Distribution prohibited運営が用意したネットワーク58
©2023 CyberAgent Inc. Distribution prohibitedハンズオンで作成する要素59
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン作成手順60・ECRレポジトリの作成・ECRにMLパイプラインのimage push・ECS Task定義作成・ECS Task実行・StepFunctionsの作成・EventBridgeの設定
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン作成手順61・ECRレポジトリの作成・ECRにMLパイプラインのimage push・ECS Task定義作成・ECS Task実行・StepFunctionsの作成・EventBridgeの設定
©2023 CyberAgent Inc. Distribution prohibitedECR62Elastic Container RegistoryDocker コンテナイメージを管理するマネージドサービス
©2023 CyberAgent Inc. Distribution prohibitedECRレポジトリの作成63ECRのページで「リポジトリ作成」をクリック{name}-mlops-handson/ml-pipelineで作成
©2023 CyberAgent Inc. Distribution prohibitedECRレポジトリの作成64自分のECRリポジトリがあることを確認
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン作成手順65・ECRレポジトリの作成・ECRにMLパイプラインのimage push・ECS Task定義作成・ECS Task実行・StepFunctionsの作成・EventBridgeの設定
©2023 CyberAgent Inc. Distribution prohibitedECRにimageを保存66mlパイプラインのimageをECRにpush$ make build-ml$ make push-mlECRにimageが保存されていることを確認
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン作成手順67・ECRレポジトリの作成・ECRにMLパイプラインのimage push・ECS Task定義作成・ECS Task実行・StepFunctionsの作成・EventBridgeの設定
©2023 CyberAgent Inc. Distribution prohibitedECS68Elastic Container ServiceDocker コンテナを実行するマネージド型のコンテナオーケストレーションサービス
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン作成手順69ECS Clusterの作成クラスター名mlops-handson-ecsVPCmlops-handson
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成70ECR Task定義のページで「新しいタスク定義の作成」をクリック
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成71タスク定義ファミリー{name}-ml-pipelineコンテナ: 名前ml-pipelineコンテナ: イメージURI作成した自分のECRレポジトリのURI
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成72環境変数- VERSION2023-05-11- MODELsgd_classifier_ctr_model- AWS_BUCKET作成した自分のS3バケット名
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成73アプリケーション環境AWS Fargateタスクロールmlops-handson-ecs-task-roleタスク実行ロールmlops-handson-ecs-task-execution-role
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成74ECS Task定義が作成できていることを確認
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン作成手順75・ECRレポジトリの作成・ECRにMLパイプラインのimage push・ECS Task定義作成・ECS Task実行・StepFunctionsの作成・EventBridgeの設定
©2023 CyberAgent Inc. Distribution prohibitedECS Taskを実行76自分で作成した「mlops-handson-ecs」 クラスターをクリック
©2023 CyberAgent Inc. Distribution prohibitedECS Taskを実行77タスクページの「新しいタスクの実行」をクリック
©2023 CyberAgent Inc. Distribution prohibitedECS Taskを実行78タスク定義: ファミリー作成した自分のECS Task定義{name}-ml-pipeline
©2023 CyberAgent Inc. Distribution prohibitedECS Taskを実行79VPCmlops-handsonサブネットmlops-handson-private-subnet-1aセキュリティグループmlops-handson-ml-pipeline-sgパブリックIPオフに変更
©2023 CyberAgent Inc. Distribution prohibitedECS Taskを実行80作成したECS TaskのタスクIDをクリック
©2023 CyberAgent Inc. Distribution prohibitedECS Taskを実行81ECS Taskのログページで、学習ログが出力されていることを確認
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン作成手順82・ECRレポジトリの作成・ECRにMLパイプラインのimage push・ECS Task定義作成・ECS Task実行・StepFunctionsの作成・EventBridgeの設定
©2023 CyberAgent Inc. Distribution prohibitedStepFunctions83ワークフローを作成・実行できるマネージドサービス
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを作成84StepFunctionsのページで「ステートマシンの作成」をクリック
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを作成85
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを作成86
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを作成87{"LaunchType": "FARGATE","Cluster": "arn:aws:ecs:ap-northeast-1:123456789101:cluster/mlops-handson-ecs","TaskDefinition": "arn:aws:ecs:ap-northeast-1:123456789101:task-definition/nagae-ml-pipeline:2","NetworkConfiguration": {"AwsvpcConfiguration": {"AssignPublicIp": "DISABLED","SecurityGroups": ["sg-0076849cc3bc59502"],"Subnets": ["subnet-0afd71e0a510d7b94"]}}}APIパラメータ 作成した自分のClusterのARNに変更ECS Task定義のARNに変更mlops-handson-ml-pipeline-sgセキュリティグループmlops-handson-ml-pipeline-sgのIDサブネットmlops-handson-private-subnet-1aのID
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを作成88タスク定義ページからARNを確認できる
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを作成89APIパラメータを変更
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを作成90
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを作成91名前{name}-mlops-pipeline実行ロールmlops-handson-step-functions-role設定したら画面下にある「ステートマシンの作成」をクリック
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを実行92作成したステートマシンページの「実行を開始」をクリック
©2023 CyberAgent Inc. Distribution prohibitedStepFunctionsを実行93ECS Taskが実行できていることを確認
©2023 CyberAgent Inc. Distribution prohibitedMLパイプライン作成手順94・ECRレポジトリの作成・ECRにMLパイプラインのimage push・ECS Task定義作成・ECS Task実行・StepFunctionsの作成・EventBridgeの設定
©2023 CyberAgent Inc. Distribution prohibitedEventBridge95AWSリソースのイベント実行を行うマネージドサービス
©2023 CyberAgent Inc. Distribution prohibitedEventBridgeを作成96作成したステートマシンページのアクション→「EventBridgeルールを作成」をクリック
©2023 CyberAgent Inc. Distribution prohibitedEventBridgeを作成97名前{name}-mlops-pipeline-scheduleルールタイプスケジュール
©2023 CyberAgent Inc. Distribution prohibitedEventBridgeを作成98頻度定期的なスケジュールスケジュールの種類cronベースのスケジュールcron式*/2 * * * ? *フレックスタイムウィンドウオフ
©2023 CyberAgent Inc. Distribution prohibitedEventBridgeを作成99ターゲットAPIAWS StepFunctions StartExecutionステートマシン作成した自分のステートマシンを選択{name}-ml-pipeline
©2023 CyberAgent Inc. Distribution prohibitedEventBridgeを作成100実行ロールmlops-handson-event-bridge-role他の設定は変更せず「スケジュールを作成」をクリック
©2023 CyberAgent Inc. Distribution prohibitedスケジュール実行を確認101StepFunctionsが定期実行されていることを確認します
©2023 CyberAgent Inc. Distribution prohibited目次1021. 前準備2. ローカルでML基盤を開発3. CI/CD4. MLパイプラインをAWS上で実行する5. 推論サーバーをAWS上に立てる
©2023 CyberAgent Inc. Distribution prohibited推論サーバー構成103
©2023 CyberAgent Inc. Distribution prohibited運営が用意したネットワーク104
©2023 CyberAgent Inc. Distribution prohibitedハンズオンで作成する要素105
©2023 CyberAgent Inc. Distribution prohibited推論サーバー作成手順106・ECRレポジトリの作成・ECRに推論サーバーのimage push・ECS Task定義作成・Load Balancerの作成・ECS Service作成
©2023 CyberAgent Inc. Distribution prohibited推論サーバー作成手順107・ECRレポジトリの作成・ECRに推論サーバーのimage push・ECS Task定義作成・Load Balancerの作成・ECS Service作成
©2023 CyberAgent Inc. Distribution prohibitedECRレポジトリの作成108ECRのページで「リポジトリ作成」をクリック{name}-mlops-handson/predict-apiで作成
©2023 CyberAgent Inc. Distribution prohibited推論サーバー作成手順109・ECRレポジトリの作成・ECRに推論サーバーのimage push・ECS Task定義作成・Load Balancerの作成・ECS Service作成
©2023 CyberAgent Inc. Distribution prohibitedECRにimageを保存110推論サーバーのimageをECRにpush$ make build-predictor$ make push-predictorECRにimageが保存されていることを確認
©2023 CyberAgent Inc. Distribution prohibited推論サーバー作成手順111・ECRレポジトリの作成・ECRに推論サーバーのimage push・ECS Task定義作成・Load Balancerの作成・ECS Service作成
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成112ECR Task定義のページで「新しいタスク定義の作成」をクリック
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成113タスク定義ファミリー{name}-predict-apiコンテナ: 名前prdict-apiコンテナ: イメージURI作成した自分のECRレポジトリのURIポートマッピング8080
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成114環境変数- VERSION2023-05-11- AWS_BUCKET作成した自分のS3バケット名
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成115アプリケーション環境AWS Fargateタスクロールmlops-handson-ecs-task-roleタスク実行ロールmlops-handson-ece-task-execution-role
©2023 CyberAgent Inc. Distribution prohibitedECS Task定義を作成116ECS Task定義が作成できていることを確認
©2023 CyberAgent Inc. Distribution prohibited推論サーバー作成手順117・ECRレポジトリの作成・ECRに推論サーバーのimage push・ECS Task定義作成・Load Balancerの作成・ECS Service作成
©2023 CyberAgent Inc. Distribution prohibitedALB118Application Load Balancerアプリケーションレイヤーで負荷分散を行うマネージドサービス
©2023 CyberAgent Inc. Distribution prohibitedTarget Groupの作成119EC2/ターゲットグループの「Create target group」をクリック
©2023 CyberAgent Inc. Distribution prohibitedTarget Groupの作成120Target typeIP addressesTarget group name{name}-mlops-handson-tgProtocol PortHTTP : 8080
©2023 CyberAgent Inc. Distribution prohibitedTarget Groupの作成121VPCmlops-handsonHealth check path/healthcheck他の設定は変更せず「Create Target」をクリック
©2023 CyberAgent Inc. Distribution prohibitedLoad Balancerの作成122EC2/ロードバランサーの「Create load balancer」をクリック
©2023 CyberAgent Inc. Distribution prohibitedLoad Balancerの作成123Load balancer typesApplication Load Balcanerを選択
©2023 CyberAgent Inc. Distribution prohibitedLoad Balancerの作成124Load balancer name{name}-mlops-hanson-alb
©2023 CyberAgent Inc. Distribution prohibitedLoad Balancerの作成125VPCmlops-handsonMappingsap-northeast-1a→ mlops-handson-public-subnet-1aap-northeast-1c→ mlops-handson-public-subnet-1c
©2023 CyberAgent Inc. Distribution prohibitedLoad Balancerの作成126Security Groupmlops-handson-alb-sgListeners and routingProtocol: HTTPPort: 8080Foward to:作成した自分のtaget group{name}-mlops-handson-tg他の設定は変更せず「Create Load Balancer」をクリック
©2023 CyberAgent Inc. Distribution prohibited推論サーバー作成手順127・ECRレポジトリの作成・ECRに推論サーバーのimage push・ECS Task定義作成・Load Balancerの作成・ECS Service作成
©2023 CyberAgent Inc. Distribution prohibitedECS Serviceの作成128mlops-handson-ecsクラスターのサービビスページの「作成」をクリック
©2023 CyberAgent Inc. Distribution prohibitedECS Serviceの作成129アプリケーションタイプサービスファミリー作成した自分のECS Task定義{name}-predict-apiサービス名{name}-predict-api-service必要タスク2
©2023 CyberAgent Inc. Distribution prohibitedECS Serviceの作成130VPCmlops-handsonサブネットmlops-handson-private-subnet-1amlops-handson-private-subnet-1cセキュリティグループmlops-handson-predict-api-sgパブリックIPオフ
©2023 CyberAgent Inc. Distribution prohibitedECS Serviceの作成131ロードバランサーの種類Application Load Balancerロードバランサー作成した自分のALB{name}-mlops-handson-albターゲットグループ作成した自分のTarget Group{name}-mlops-handson-tg
©2023 CyberAgent Inc. Distribution prohibitedECS Serviceの作成132サービスのオートスケーリングを使用ONに変更タスクの最小数2タスクの最大数6ポリシー名{name}-auto-scaling-policyECSサービスメトリクスALBRequestCountPerTarget他の設定は変更せず「作成」をクリック
©2023 CyberAgent Inc. Distribution prohibitedECS Serviceの作成133推論サーバーのECS Serviceを作成できているを確認
©2023 CyberAgent Inc. Distribution prohibited推論リクエストを送る134推論サーバーのECS Service→ネットワークタブのDNS名をコピー
©2023 CyberAgent Inc. Distribution prohibited推論リクエストを送る135$ vi .envAWS_ALB_DNS=nagae-mlops-handson-alb-354533381.ap-northeast-1.elb.amazonaws.comローカルからECS Serviceにリクエストを送るALBのDNS名$ make predict-ecs
©2023 CyberAgent Inc. Distribution prohibitedオートスケーリングを確認する136$ make check-scaleheyでリクエストを送る(注意) リクエストを送りすぎるとIP バンされる可能性があります
©2023 CyberAgent Inc. Distribution prohibited推論リクエストを送る137オートスケーリングを確認するタスク数が増加していることを確認
©2023 CyberAgent Inc. Distribution prohibited 138作成したMLシステムterraform: aws-mlops-handson/infra
©2023 CyberAgent Inc. Distribution prohibited 139MLOpsとしてまだできることは?推論サーバーの自動更新複数モデルの学習モデル監視データ監視インフラ監視実験管理A/Bテスト基盤インフラのコード管理レイテンシーの改善閉鎖網での推論APIの提供ドキュメント作成…
©2023 CyberAgent Inc. Distribution prohibitedMLOps実践編は以上です作成したリソースを削除しましょう140