

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 MLflow 追蹤推論建議和基準結果
<a name="generative-ai-inference-recommendations-mlflow"></a>

您可以使用 Amazon SageMaker AI 上的全受管 MLflow 來追蹤建議和基準工作的結果。當您提供 MLflow 組態時，SageMaker AI 會將任務產生的效能指標和成品記錄到 MLflow 應用程式。然後，您可以使用 MLflow UI 來檢視個別執行、比較跨任務的組態，以及與您的團隊共用結果。如需 SageMaker AI 上的 MLflow 詳細資訊，請參閱 [在 Amazon SageMaker AI 上使用受管 MLflow 加速開發生成式 AI](mlflow.md)。

MLflow 追蹤是選用的。若要啟用它，請在建立任務時提供參考 MLflow 應用程式的 MLflow 組態。

## 先決條件
<a name="generative-ai-inference-recommendations-mlflow-prereqs"></a>

使用 MLflow 追蹤結果之前，除了[先決條件](generative-ai-inference-recommendations-get-started.md#generative-ai-inference-recommendations-get-started-prereqs)建議或基準任務的 之外，還需要下列項目：
+ 操作狀態下的 MLflow 應用程式， AWS 區域 與您的任務相同。如需建立 MLflow 應用程式的資訊，請參閱 [MLflow 應用程式設定](mlflow-app-setup.md)。
+ IAM 執行角色 （您傳遞給任務的 )`RoleArn`，具有描述 MLflow 應用程式和呼叫 MLflow REST APIs許可。新增 `sagemaker:DescribeMlflowApp`動作和 任務用來記錄執行`sagemaker-mlflow`的動作。如需 MLflow 許可的詳細資訊，請參閱 [MLflow 應用程式設定](mlflow-app-setup.md)。

## MLflow 組態參數
<a name="generative-ai-inference-recommendations-mlflow-config"></a>

若要追蹤結果，請將 `MlflowConfig` 物件新增至 `CreateAIRecommendationJob`或 `CreateAIBenchmarkJob`請求`OutputConfig`的 。 `MlflowConfig`包含下列欄位：

`MlflowResourceArn`  
必要. 要記錄結果之 MLflow 應用程式的 Amazon Resource Name (ARN)，格式為 `arn:aws:sagemaker:{{region}}:{{account-id}}:mlflow-app/{{app-id}}`。

`MlflowExperimentName`  
選用。用來記錄執行的 MLflow 實驗名稱。如果實驗不存在，SageMaker AI 會建立它；如果已經存在，SageMaker AI 會重複使用它，因此您可以透過傳遞相同的名稱，將來自多個任務的執行分組在一個實驗中。

`MlflowRunName`  
選用。任務結果巢狀所在的最上層 MLflow 執行名稱。若要在一個頂層執行下將多個任務的結果分組，請將相同的 傳遞`MlflowRunName`給每個任務。

每個任務都會建立頂層 MLflow 執行，其中巢狀子執行會依基準目標或執行個體類型、部署組態和並行層級等維度細分結果。此結構可讓您深入探索個別測量，並在 MLflow UI 中比較組態。

`MlflowConfig` 您在建立請求上提供的 會在對應的`Describe`回應`OutputConfig`的 中回傳。

## 追蹤建議任務
<a name="generative-ai-inference-recommendations-mlflow-recommendation"></a>

當您建立建議任務`OutputConfig`時，將`MlflowConfig`物件新增至 。

**Python (boto3)**

```
response = client.create_ai_recommendation_job(
    AIRecommendationJobName="my-recommendation-job",
    ModelSource={
        "S3": {
            "S3Uri": "s3://DOC-EXAMPLE-BUCKET/models/my-model/",
        }
    },
    OutputConfig={
        "S3OutputLocation": "s3://DOC-EXAMPLE-BUCKET/recommendations/",
        "MlflowConfig": {
            "MlflowResourceArn": "arn:aws:sagemaker:us-west-2:111122223333:mlflow-app/app-EXAMPLE",
            "MlflowExperimentName": "my-recommendation-experiment",
            "MlflowRunName": "my-recommendation-job-run",
        },
    },
    PerformanceTarget={
        "Constraints": [
            {"Metric": "ttft-ms"}
        ]
    },
    AIWorkloadConfigIdentifier="my-recommendation-workload",
    RoleArn="arn:aws:iam::111122223333:role/ExampleRole",
)
print(response["AIRecommendationJobArn"])
```

**AWS CLI**

```
aws sagemaker create-ai-recommendation-job \
  --ai-recommendation-job-name "my-recommendation-job" \
  --model-source '{"S3": {"S3Uri": "s3://DOC-EXAMPLE-BUCKET/models/my-model/"}}' \
  --output-config '{
    "S3OutputLocation": "s3://DOC-EXAMPLE-BUCKET/recommendations/",
    "MlflowConfig": {
      "MlflowResourceArn": "arn:aws:sagemaker:us-west-2:111122223333:mlflow-app/app-EXAMPLE",
      "MlflowExperimentName": "my-recommendation-experiment",
      "MlflowRunName": "my-recommendation-job-run"
    }
  }' \
  --performance-target '{"Constraints": [{"Metric": "ttft-ms"}]}' \
  --ai-workload-config-identifier "my-recommendation-workload" \
  --role-arn "arn:aws:iam::111122223333:role/ExampleRole" \
  --region us-west-2
```

## 追蹤基準工作
<a name="generative-ai-inference-recommendations-mlflow-benchmark"></a>

`OutputConfig` 當您建立基準工作時，將`MlflowConfig`物件新增至 。

**Python (boto3)**

```
response = client.create_ai_benchmark_job(
    AIBenchmarkJobName="my-benchmark-job",
    BenchmarkTarget={
        "Endpoint": {
            "Identifier": "my-sagemaker-endpoint"
        }
    },
    OutputConfig={
        "S3OutputLocation": "s3://DOC-EXAMPLE-BUCKET/benchmark-results/",
        "MlflowConfig": {
            "MlflowResourceArn": "arn:aws:sagemaker:us-west-2:111122223333:mlflow-app/app-EXAMPLE",
            "MlflowExperimentName": "my-benchmark-experiment",
            "MlflowRunName": "my-benchmark-job-run",
        },
    },
    AIWorkloadConfigIdentifier="my-benchmark-config",
    RoleArn="arn:aws:iam::111122223333:role/ExampleRole",
)
print(response["AIBenchmarkJobArn"])
```

**AWS CLI**

```
aws sagemaker create-ai-benchmark-job \
  --ai-benchmark-job-name "my-benchmark-job" \
  --benchmark-target '{"Endpoint": {"Identifier": "my-sagemaker-endpoint"}}' \
  --output-config '{
    "S3OutputLocation": "s3://DOC-EXAMPLE-BUCKET/benchmark-results/",
    "MlflowConfig": {
      "MlflowResourceArn": "arn:aws:sagemaker:us-west-2:111122223333:mlflow-app/app-EXAMPLE",
      "MlflowExperimentName": "my-benchmark-experiment",
      "MlflowRunName": "my-benchmark-job-run"
    }
  }' \
  --ai-workload-config-identifier "my-benchmark-config" \
  --role-arn "arn:aws:iam::111122223333:role/ExampleRole" \
  --region us-west-2
```

## 在 MLflow 中檢視結果
<a name="generative-ai-inference-recommendations-mlflow-view"></a>

任務達到 `Completed` 狀態後，從 MLflow 應用程式開啟 MLflow UI 以檢查記錄的執行。如需啟動 MLflow UI 的詳細資訊，請參閱 [使用預先簽章的 URL 啟動 MLflow UI](mlflow-launch-ui.md)。

在**實驗**頁面上，選取您在 中指定的實驗`MlflowExperimentName`。

![MLflow UI 實驗頁面顯示實驗清單及其名稱和建立時間。](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/mlflow/inference-recommendations-mlflow-experiments.png)


實驗的 **Runs** 檢視會顯示頂層執行，其中包含每個任務的執行，這會擴展到每個執行個體類型、部署組態和並行層級的子執行。選取執行以檢視其參數、指標和成品。

![MLflow UI Runs 檢視顯示最上層執行，擴展到每個任務、執行個體類型、映像類型和並行層級的巢狀子執行。](http://docs.aws.amazon.com/zh_tw/sagemaker/latest/dg/images/mlflow/inference-recommendations-mlflow-runs.png)
