Hikaru's notes

A collection of notes and resources

View the Project on GitHub HikaruMizoe/notes

3.1 既存のアーキテクチャと要件に基づいてデプロイインフラストラクチャを選択する

タスクステートメント

既存のアーキテクチャと要件に基づいてデプロイインフラストラクチャを選択する

対象知識

デプロイのベストプラクティス

バージョニング

ロールバック戦略

AWS のデプロイサービス

Amazon SageMaker

その他のデプロイオプション

ML モデルをリアルタイムにバッチ処理する方法

リアルタイム推論

バッチ推論

本番環境とテスト環境にコンピューティングリソースをプロビジョニングする方法

CPU vs GPU

インスタンスタイプ

デプロイエンドポイントのモデルとエンドポイントの要件

サーバーレスエンドポイント

リアルタイムエンドポイント

非同期エンドポイント

バッチ推論(Batch Transform)

適切なコンテナの選択方法

提供されたコンテナ

カスタマイズされたコンテナ

エッジデバイス上のモデルを最適化する方法

SageMaker Neo

対象スキル

1. パフォーマンス、コスト、レイテンシーのトレードオフを評価する

エンドポイントタイプ パフォーマンス コスト レイテンシー ユースケース
リアルタイム 高 高 低(ms) Webアプリ、リアルタイム推論
サーバーレス 中 低~中 中(秒) 不定期なトラフィック
非同期 中 中 高(分) 大きなペイロード、長時間処理
バッチ 高(一括) 低 N/A 定期的な大量処理

2. 要件に基づき、トレーニングと推論に適したコンピューティング環境を選択する

# GPU インスタンスの選択例
estimator = PyTorch(
    entry_point='train.py',
    role=role,
    instance_count=4,
    instance_type='ml.p4d.24xlarge',  # 8x A100 GPU
    framework_version='2.0'
)

# 推論最適化インスタンス
predictor = model.deploy(
    initial_instance_count=1,
    instance_type='ml.inf2.xlarge'  # AWS Inferentia
)

3. 適切なデプロイオーケストレーターを選択する

Apache Airflow (Amazon MWAA)

SageMaker Pipelines

from sagemaker.workflow.pipeline import Pipeline
from sagemaker.workflow.steps import ProcessingStep, TrainingStep

# パイプラインステップの定義
pipeline = Pipeline(
    name='ml-pipeline',
    steps=[
        processing_step,
        training_step,
        evaluation_step
    ]
)

pipeline.upsert(role_arn=role)

4. マルチモデルデプロイまたはマルチコンテナデプロイを選択する

マルチモデルエンドポイント

from sagemaker.multidatamodel import MultiDataModel

# マルチモデルエンドポイント
mme = MultiDataModel(
    name='multi-model',
    model_data_prefix=f's3://{bucket}/models/',
    image_uri=container,
    role=role
)

predictor = mme.deploy(
    initial_instance_count=1,
    instance_type='ml.m5.xlarge'
)

# 特定モデルで推論
predictions = predictor.predict(data, target_model='model1.tar.gz')

マルチコンテナエンドポイント

from sagemaker.multidatamodel import MultiDataModel
from sagemaker.pipeline import PipelineModel

# 前処理 + モデル推論のパイプライン
pipeline_model = PipelineModel(
    name='pipeline-model',
    role=role,
    models=[
        preprocessing_model,
        inference_model
    ]
)

predictor = pipeline_model.deploy(
    initial_instance_count=1,
    instance_type='ml.m5.xlarge'
)

5. 適切なデプロイターゲットを選択する

SageMaker エンドポイント

Kubernetes (Amazon EKS)

Amazon ECS

AWS Lambda

6. モデルデプロイ戦略を選択する

リアルタイムデプロイ例

# ブルー/グリーングデプロイ
predictor = model.deploy(
    initial_instance_count=1,
    instance_type='ml.m5.xlarge',
    endpoint_name='production-endpoint'
)

# 新バージョンを追加
predictor.update_endpoint(
    initial_instance_count=1,
    instance_type='ml.m5.xlarge',
    variant_name='green-variant',
    initial_variant_weight=0  # 最初は0%
)

# トラフィックをシフト
predictor.update_endpoint_weights_and_capacities(
    desired_weights_and_capacities=[
        {'VariantName': 'blue-variant', 'DesiredWeight': 50},
        {'VariantName': 'green-variant', 'DesiredWeight': 50}
    ]
)

バッチデプロイ例

from sagemaker.transformer import Transformer

transformer = Transformer(
    model_name='my-model',
    instance_count=1,
    instance_type='ml.m5.xlarge',
    output_path=f's3://{bucket}/batch-output/'
)

transformer.transform(
    data=f's3://{bucket}/batch-input/',
    content_type='text/csv',
    split_type='Line'
)

エンドポイント選択のフローチャート

トラフィックパターン
├─ 常時トラフィックあり?
│  ├─ Yes → レイテンシー要件は?
│  │  ├─ < 100ms → リアルタイムエンドポイント(常時稼働)
│  │  └─ > 1秒 → 非同期エンドポイント
│  └─ No → トラフィック予測可能?
│     ├─ Yes → サーバーレスエンドポイント
│     └─ No → バッチ推論
└─ 大量データの一括処理?
   └─ Yes → バッチ Transform

重要なポイント

デプロイ戦略の選択基準

  1. レイテンシー要件: リアルタイム vs バッチ
  2. トラフィックパターン: 予測可能性、変動幅
  3. コスト: 常時稼働 vs オンデマンド
  4. ペイロードサイズ: 小 vs 大
  5. 処理時間: 短 vs 長

Neo コンパイルの利点

関連リンク