次の方法で共有


サーバーレス コンピューティングでのモデル トレーニング

適用対象:Azure CLI ml 拡張機能 v2 (現行)Python SDK azure-ai-ml v2 (現行)

スケーラブルな方法でモデルをトレーニングするためにコンピューティングを作成して管理する必要はなくなりました。 代わりに、サーバーレス コンピューティングと呼ばれる新しいコンピューティング ターゲットの種類にジョブを送信できます。 サーバーレス コンピューティングは、Azure Machine Learning でトレーニング ジョブを実行する最も簡単な方法です。 サーバーレス コンピューティングは、フル マネージドのオンデマンド コンピューティングです。 Azure Machine Learning では、コンピューティングの作成、スケーリング、管理が行われます。 サーバーレス コンピューティングによるモデル トレーニングを通じて、機械学習の専門家は機械学習モデルの構築に関する専門知識に集中でき、コンピューティング インフラストラクチャやその設定について学ぶ必要がありません。

機械学習の専門家は、ジョブに必要なリソースを指定できます。 Azure Machine Learning はコンピューティング インフラストラクチャを管理し、マネージド ネットワークの分離を提供し、負担を軽減します。

また、各ジョブに最適なリソースを指定することで、企業はコストを削減できます。 IT 管理者は、サブスクリプションとワークスペース レベルでコア クォータを指定し、Azure ポリシーを適用することで、引き続き制御を適用できます。

サーバーレス コンピューティングを使用して、モデル カタログ内のモデルを微調整できます。 サーバーレス コンピューティングを使用して、Azure Machine Learning Studio、SDK、CLI からすべての種類のジョブを実行できます。 サーバーレス コンピューティングは、環境イメージの構築や責任ある AI ダッシュボードのシナリオにも使用できます。 サーバーレス ジョブは、Azure Machine Learning コンピューティング クォータと同じクォータを使います。 標準 (専用) レベルまたはスポット (低優先度) VM を選択できます。 マネージド ID とユーザー ID は、サーバーレス ジョブでサポートされています。 課金モデルは、Azure Machine Learning コンピューティングと同じです。

サーバーレス コンピューティングの利点

  • Azure Machine Learning は、コンピューティング インフラストラクチャの作成、設定、スケーリング、削除、修正プログラムの適用を管理し、管理オーバーヘッドを削減します
  • コンピューティング、さまざまなコンピューティングの種類、および関連するプロパティについて学習する必要はありません
  • 必要な VM サイズごとにクラスターを繰り返し作成し、同じ設定を使用して、各ワークスペースにレプリケートする必要はありません
  • インスタンスの種類 (VM サイズ) とインスタンス数の観点から、各ジョブが必要とするリソースを実行時に正確に指定することで、コストを最適化できます。 ジョブの使用率メトリックを監視して、ジョブに必要なリソースを最適化できます
  • ジョブの実行に必要なステップの削減
  • ジョブの送信をさらに簡略化するために、リソースを完全にスキップできます。 Azure Machine Learning では既定でインスタンス数が設定され、クォータ、コスト、パフォーマンス、ディスク サイズなどの要因に基づいてインスタンスの種類 (VM サイズ) が選択されます
  • ジョブの実行が開始されるまでの待機時間が短くなる場合があります
  • ジョブの送信では、ユーザー ID とワークスペースのユーザー割り当てマネージド ID がサポートされます
  • マネージド ネットワーク分離を使用すると、ネットワーク分離構成を合理化および自動化できます。 お客様の仮想ネットワークもサポートされています
  • クォータと Azure ポリシーによる管理制御

サーバーレス コンピューティングの使用方法

  • 独自のコンピューティング クラスターを作成する場合、compute="cpu-cluster" のように、コマンド ジョブでその名前を使います。 サーバーレスでは、コンピューティング クラスターの作成をスキップし、compute パラメーターを省略することで、代わりにサーバーレス コンピューティングを使用できます。 ジョブに compute が指定されていない場合、ジョブはサーバーレス コンピューティングで実行されます。 CLI または SDK のジョブでコンピューティング名を省略すると、次のジョブの種類でサーバーレス コンピューティングが使われ、必要に応じてジョブが必要とするリソースをインスタンス数およびインスタンスの種類で指定できます。

    • 対話型ジョブや分散トレーニングを含むコマンド ジョブ
    • AutoML ジョブ
    • スイープ ジョブ
    • 並列ジョブ
  • CLI を介したパイプライン ジョブの場合、パイプライン レベルの既定のコンピューティングには default_compute: azureml:serverless を使います。 SDK を使用したパイプライン ジョブの場合は、 default_compute="serverless"を使用します。 例については、「パイプライン ジョブ」を参照してください。

  • スタジオ (プレビュー) でトレーニング ジョブを送信する場合、コンピューティングの種類として [サーバーレス] を選びます。

  • Azure Machine Learning デザイナーを使う場合、既定のコンピューティングとして [サーバーレス] を選びます。

パフォーマンスに関する考慮事項

サーバーレス コンピューティングを使うと、次のようにトレーニングの高速化に役立ちます。

クォータの不足: 独自のコンピューティング クラスターを作成する場合、作成する VM サイズとノード数を把握する必要があります。 ジョブの実行時に、クラスターのクォータが十分でない場合、ジョブは失敗します。 サーバーレス コンピューティングでは、クォータに関する情報を使って、既定で適切な VM サイズを選びます。

スケール ダウンの最適化: コンピューティング クラスターをスケールダウンする場合、新しいジョブはスケール ダウンが発生するのを待ってから、ジョブを実行する前にスケールアップする必要があります。 サーバーレス コンピューティングでは、スケールダウンを待つ必要がなく、ジョブは別のクラスター/ノードで実行を開始できます (クォータがある場合)。

クラスタ使用率最適化: コンピューティング クラスターでジョブが実行中の際、別のジョブを送信すると現在のジョブの後ろに順番待ちされます。 サーバーレス コンピューティングでは、別のノードや別のクラスターでジョブの実行を開始できます (クォータがある場合)。

Quota

ジョブを送信する場合でも、続行するのに十分な Azure Machine Learning コンピューティング クォータ (ワークスペースとサブスクリプション レベルのクォータの両方) が必要です。 サーバーレス ジョブの既定の VM サイズは、このクォータに基づいて選ばれます。 独自の VM サイズ/ファミリを指定する場合:

  • VM サイズ/ファミリのクォータはあるが、インスタンス数に対するクォータが十分でない場合、エラーが表示されます。 このエラーでは、クォータ制限に基づいてインスタンスの数を有効な数に減らすか、この VM ファミリのクォータの引き上げを要求するか、VM サイズを変更することをお勧めします。
  • 指定した VM サイズのクォータがない場合は、エラーが表示されます。 このエラーでは、使用可能なクォータを持つ別の VM サイズの選択や、この VM ファミリのクォータをリクエストすることをお勧めします。
  • VM ファミリがサーバーレス ジョブを実行するのに十分なクォータがあるが、他のジョブでクォータを使用している場合は、クォータが使用可能になるまでジョブがキューで待機する必要があることを示すメッセージが表示されます。

Azure portal で使用量とクォータを表示すると、"サーバーレス" という名前が表示され、サーバーレス ジョブで使ったすべてのクォータが表示されます。

ID のサポートと資格情報のパススルー

  • ユーザー資格情報パススルー: サーバーレス コンピューティングでは、ユーザー資格情報パススルーが完全にサポートされます。 ジョブを送信するユーザーのユーザー トークンは、ストレージ アクセスに使われます。 これらの資格情報は、Microsoft Entra ID から取得されます。

    from azure.ai.ml import command
    from azure.ai.ml import MLClient     # Handle to the workspace
    from azure.identity import DefaultAzureCredential     # Authentication package
    from azure.ai.ml.entities import ResourceConfiguration
    from azure.ai.ml.entities import UserIdentityConfiguration 
    
    credential = DefaultAzureCredential()
    # Get a handle to the workspace. You can find the info on the workspace tab on ml.azure.com
    ml_client = MLClient(
        credential=credential,
        subscription_id="<Azure subscription id>", 
        resource_group_name="<Azure resource group>",
        workspace_name="<Azure Machine Learning Workspace>",
    )
    job = command(
        command="echo 'hello world'",
        environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest",
            identity=UserIdentityConfiguration(),
    )
    # submit the command job
    ml_client.create_or_update(job)
    
  • ユーザー割り当てマネージド ID: ユーザー割り当てマネージド ID で構成されたワークスペースがある場合は、その ID をサーバーレス ジョブと共にストレージ アクセスに使用できます。 シークレットにアクセスするには、「Azure Machine Learning ジョブで認証資格情報シークレットを使用する」を参照してください。

  1. ワークスペース ID の構成を確認します。

    from azure.ai.ml import MLClient
    from azure.identity import DefaultAzureCredential
    
    subscription_id = "<your-subscription-id>"
    resource_group = "<your-resource-group>"
    workspace = "<your-workspace-name>"
    
    ml_client = MLClient(
        DefaultAzureCredential(),
        subscription_id,
        resource_group,
        workspace
    )
    
    # Get workspace details
    ws = ml_client.workspaces.get(name=workspace)
    print(ws)
    
    

    出力でユーザー割り当て ID を探します。 不足している場合は、「 Azure Machine Learning と他のサービス間の認証を設定する」の手順に従って、ユーザー割り当てマネージド ID を持つ新しいワークスペースを作成します。

  2. あなたの作業でユーザー割り当てマネージド ID を使用します。

    from azure.ai.ml import command
    from azure.ai.ml import MLClient     # Handle to the workspace
    from azure.identity import DefaultAzureCredential    # Authentication package
    from azure.ai.ml.entities import ResourceConfiguration
    from azure.ai.ml.entities import ManagedIdentityConfiguration
    
    credential = DefaultAzureCredential()
    # Get a handle to the workspace. You can find the info on the workspace tab on ml.azure.com
    ml_client = MLClient(
        credential=credential,
        subscription_id="<Azure subscription id>", 
        resource_group_name="<Azure resource group>",
        workspace_name="<Azure Machine Learning Workspace>",
    )
    job = command(
        command="echo 'hello world'",
        environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest",
        identity= ManagedIdentityConfiguration(client_id="<workspace-uami-client-id>"),
    )
    # submit the command job
    ml_client.create_or_update(job)
    

コマンド ジョブのプロパティを構成する

コマンド、スイープ、および AutoML ジョブにコンピューティング 先が指定されていない場合、コンピューティングは既定でサーバーレス コンピューティングになります。 たとえば、このコマンド ジョブの場合:

from azure.ai.ml import command
from azure.ai.ml import command 
from azure.ai.ml import MLClient # Handle to the workspace
from azure.identity import DefaultAzureCredential # Authentication package

credential = DefaultAzureCredential()
# Get a handle to the workspace. You can find the info on the workspace tab on ml.azure.com
ml_client = MLClient(
    credential=credential,
    subscription_id="<Azure subscription id>", 
    resource_group_name="<Azure resource group>",
    workspace_name="<Azure Machine Learning Workspace>",
)
job = command(
    command="echo 'hello world'",
    environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest",
)
# submit the command job
ml_client.create_or_update(job)

コンピューティングの既定値は、次のサーバーレス コンピューティングです。

  • このジョブの単一ノード。 既定のノード数は、ジョブの種類に基づきます。 他のジョブの種類については、次のセクションを参照してください。
  • CPU 仮想マシン。クォータ、パフォーマンス、コスト、ディスク サイズに基づいて決定されます。
  • 専用仮想マシン
  • ワークスペースの場所

これらの既定値はオーバーライドできます。 サーバーレス コンピューティングの VM 型やノード数を指定する場合は、ジョブに resources を追加します。

  • instance_type で特定の VM を選びます。 特定の CPU/GPU の VM サイズが必要な場合は、このパラメーターを使います

  • ノード数を指定するには instance_count を使います。

    from azure.ai.ml import command 
    from azure.ai.ml import MLClient # Handle to the workspace
    from azure.identity import DefaultAzureCredential # Authentication package
    from azure.ai.ml.entities import JobResourceConfiguration 
    
    credential = DefaultAzureCredential()
    # Get a handle to the workspace. You can find the info on the workspace tab on ml.azure.com
    ml_client = MLClient(
        credential=credential,
        subscription_id="<Azure subscription id>", 
        resource_group_name="<Azure resource group>",
        workspace_name="<Azure Machine Learning Workspace>",
    )
    job = command(
        command="echo 'hello world'",
        environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest",
        resources = JobResourceConfiguration(instance_type="Standard_NC24", instance_count=4)
    )
    # submit the command job
    ml_client.create_or_update(job)
    
  • ジョブレベルを変更するには、 queue_settings を使用して、専用 VM (job_tier: Standard) と低優先度 (job_tier: Spot) を選択します。

    from azure.ai.ml import command
    from azure.ai.ml import MLClient    # Handle to the workspace
    from azure.identity import DefaultAzureCredential    # Authentication package
    credential = DefaultAzureCredential()
    # Get a handle to the workspace. You can find the info on the workspace tab on ml.azure.com
    ml_client = MLClient(
        credential=credential,
        subscription_id="<Azure subscription id>", 
        resource_group_name="<Azure resource group>",
        workspace_name="<Azure Machine Learning Workspace>",
    )
    job = command(
        command="echo 'hello world'",
        environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest",
        queue_settings={
          "job_tier": "spot"  
        }
    )
    # submit the command job
    ml_client.create_or_update(job)
    

コマンド ジョブですべてのフィールドを指定した場合の例

ID などのジョブが使用する必要があるすべてのフィールドを指定した例をこちらに示します。 ワークスペース レベルのマネージド ネットワーク分離が自動的に使われるため、仮想ネットワーク設定を指定する必要はありません。

from azure.ai.ml import command
from azure.ai.ml import MLClient      # Handle to the workspace
from azure.identity import DefaultAzureCredential     # Authentication package
from azure.ai.ml.entities import ResourceConfiguration
from azure.ai.ml.entities import UserIdentityConfiguration 

credential = DefaultAzureCredential()
# Get a handle to the workspace. You can find the info on the workspace tab on ml.azure.com
ml_client = MLClient(
    credential=credential,
    subscription_id="<Azure subscription id>", 
    resource_group_name="<Azure resource group>",
    workspace_name="<Azure Machine Learning Workspace>",
)
job = command(
    command="echo 'hello world'",
    environment="azureml://registries/azureml/environments/sklearn-1.5/labels/latest",
         identity=UserIdentityConfiguration(),
    queue_settings={
      "job_tier": "Standard"  
    }
)
job.resources = ResourceConfiguration(instance_type="Standard_E4s_v3", instance_count=1)
# submit the command job
ml_client.create_or_update(job)

サーバーレス コンピューティングを使用したトレーニングのその他の例を表示します。

AutoML ジョブ

AutoML ジョブの場合、コンピューティングを指定する必要はありません。 リソースは必要に応じて指定できます。 インスタンス数が指定されていない場合は、max_concurrent_trials と max_nodes パラメーターに基づき既定値で設定されます。 インスタンスの種類を指定せずに AutoML の画像分類または NLP タスクを送信した場合、GPU VM のサイズが自動的に選ばれます。 AUTOML ジョブは、CLI、SDK、または Studio を使用して送信できます。 Studio でサーバーレス コンピューティングを使用して AutoML ジョブを送信するには、まずプレビュー パネル でスタジオ (プレビュー) 機能でトレーニング ジョブの送信 を有効にします。

種類やインスタンス数を指定する場合は、ResourceConfiguration クラスを使います。

# Create the AutoML classification job with the related factory-function.
from azure.ai.ml.entities import ResourceConfiguration 

classification_job = automl.classification(
    experiment_name=exp_name,
    training_data=my_training_data_input,
    target_column_name="y",
    primary_metric="accuracy",
    n_cross_validations=5,
    enable_model_explainability=True,
    tags={"my_custom_tag": "My custom value"},
)

# Limits are all optional
classification_job.set_limits(
    timeout_minutes=600,
    trial_timeout_minutes=20,
    max_trials=max_trials,
    # max_concurrent_trials = 4,
    # max_cores_per_trial: -1,
    enable_early_termination=True,
)

# Training properties are optional
classification_job.set_training(
    blocked_training_algorithms=[ClassificationModels.LOGISTIC_REGRESSION],
    enable_onnx_compatible_models=True,
)

# Serverless compute resources used to run the job
classification_job.resources = 
ResourceConfiguration(instance_type="Standard_E4s_v3", instance_count=6)

パイプライン ジョブ

パイプライン ジョブの場合、既定のコンピューティングの種類に "serverless" を指定すると、サーバーレス コンピューティングが使われます。

# Construct pipeline
@pipeline()
def pipeline_with_components_from_yaml(
    training_input,
    test_input,
    training_max_epochs=20,
    training_learning_rate=1.8,
    learning_rate_schedule="time-based",
):
    """E2E dummy train-score-eval pipeline with components defined via yaml."""
    # Call component obj as function: apply given inputs & parameters to create a node in pipeline
    train_with_sample_data = train_model(
        training_data=training_input,
        max_epochs=training_max_epochs,
        learning_rate=training_learning_rate,
        learning_rate_schedule=learning_rate_schedule,
    )

    score_with_sample_data = score_data(
        model_input=train_with_sample_data.outputs.model_output, test_data=test_input
    )
    score_with_sample_data.outputs.score_output.mode = "upload"

    eval_with_sample_data = eval_model(
        scoring_result=score_with_sample_data.outputs.score_output
    )

    # Return: pipeline outputs
    return {
        "trained_model": train_with_sample_data.outputs.model_output,
        "scored_data": score_with_sample_data.outputs.score_output,
        "evaluation_report": eval_with_sample_data.outputs.eval_output,
    }


pipeline_job = pipeline_with_components_from_yaml(
    training_input=Input(type="uri_folder", path=parent_dir + "/data/"),
    test_input=Input(type="uri_folder", path=parent_dir + "/data/"),
    training_max_epochs=20,
    training_learning_rate=1.8,
    learning_rate_schedule="time-based",
)

# set pipeline to use serverless compute
pipeline_job.settings.default_compute = "serverless"

デザイナーで、サーバーレス コンピューティングを既定のコンピューティングとして設定することもできます。

User-Assigned マネージド ID を使用してサーバーレス パイプライン ジョブを構成する

パイプライン ジョブでサーバーレス コンピューティングを使用する場合は、ルート パイプライン レベルではなく、コンピューティングで実行される個々のステップ レベルでユーザー ID を設定することをお勧めします。 (ID 設定はルート パイプラインとステップ レベルの両方でサポートされていますが、両方が設定されている場合は、ステップ レベルの設定が優先されます。ただし、パイプライン コンポーネントを含むパイプラインの場合は、実行される個々のステップで ID を設定する必要があります。ルート パイプラインまたはパイプライン コンポーネント レベルで設定された ID は機能しません。そのため、わかりやすくするために、個々のステップ レベルで ID を設定することをお勧めします)。

def my_pipeline():
    train_job = train_component(
        training_data=Input(type="uri_folder", path="./data")
    )
    # Set managed identity for the job
    train_job.identity = {"type": "user_identity"}
    return {"train_output": train_job.outputs}

pipeline_job = my_pipeline()
# set pipeline to use serverless compute
pipeline_job.settings.default_compute = "serverless"

次のステップ

サーバーレス コンピューティングを使用したトレーニングのその他の例を表示します。