次の方法で共有


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

適用対象: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、Python SDK、Azure CLI を使用して、すべての種類のジョブを実行できます。 サーバーレス コンピューティングを使用して、環境イメージと責任ある AI ダッシュボード シナリオを構築することもできます。 サーバーレス ジョブは、Azure Machine Learning コンピューティング クォータと同じクォータを使います。 標準 (専用) レベルまたはスポット (低優先度) VM を選択できます。 マネージド ID とユーザー ID は、サーバーレス ジョブでサポートされています。 課金モデルは、Azure Machine Learning コンピューティングのモデルと同じです。

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

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

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

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

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

  • Studio でトレーニング ジョブを送信する場合は、コンピューティングの種類として [サーバーレス] を選択します。

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

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

サーバーレス コンピューティングでは、次の方法でトレーニングの速度を上げることができます。

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

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

クラスタービジーの最適化。 ジョブがコンピューティング クラスターで実行されていて、別のジョブが送信されると、ジョブは現在実行中のジョブの背後にキューに入れられます。 サーバーレス コンピューティングを使用すると、ジョブを別のノード/クラスター (クォータがある場合) で実行を開始できます。

Quota

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

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

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

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

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

    サーバーレス コンピューティングでは、システム割り当て 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 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 は、クォータ、パフォーマンス、コスト、ディスク サイズに基づいて決定されます。
  • 専用仮想マシン。
  • ワークスペースの場所。

これらの既定値はオーバーライドできます。 サーバーレス コンピューティングの 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) と優先順位の低い VM (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)

トレーニングにサーバーレス コンピューティングを使用する 2 つの例を次に示します。

AutoML ジョブ

AutoML ジョブのコンピューティングを指定する必要はありません。 必要に応じて、リソースを指定できます。 インスタンス数が指定されていない場合は、 max_concurrent_trials パラメーターと max_nodes パラメーターに基づいて既定値になります。 インスタンスの種類を指定せずに AutoML イメージ分類または NLP タスクを送信すると、GPU VM サイズが自動的に選択されます。 CLI、Python SDK、または 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"

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

ユーザー割り当てマネージド 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": "managed"}
    return {"train_output": train_job.outputs}

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

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