分析模型以決定資源使用率

適用於:Azure CLI ml 延伸模組第 1 版Python SDK azureml 第 1 版

本文說明如何分析機器學習模型,以判斷當您將模型部署為 Web 服務時,需要配置多少 CPU 和記憶體。

重要

本文適用於 CLI 第 1 版和 SDK 第 1 版。 此分析技術不適用於 CLI 或 SDK 的第 2 版。

重要

本文中的 Azure CLI 命令使用 azure-cli-ml 或 v1 (Azure Machine Learning 的擴充功能)。 v1 擴充功能的支援將於 2025 年 9 月 30 日終止。 您將能安裝並使用 v1 擴充功能,直到該日期為止。

建議您在 2025 年 9 月 30 日之前轉換至 ml 或 v2 擴充功能。 如需有關 v2 擴充功能的詳細資訊,請參閱 Azure ML CLI 擴充功能和 Python SDK v2

必要條件

本文假設您已使用 Azure Machine Learning 來定型和註冊模型。 請參閱這裡的範例教學課程,其中提供使用 Azure Machine Learning 來定型和註冊 scikit-learn 模型的範例。

限制

  • 當工作區的 Azure Container Registry (ACR) 位於虛擬網路後方時,分析將無法運作。

執行分析工具

註冊模型並備妥其部署所需的其他元件之後,您就可以決定部署的服務所需的 CPU 和記憶體。 分析會測試負責執行模型的服務,並傳回 CPU 使用量、記憶體使用量和回應延遲等資訊。 也會根據資源使用量,提供 CPU 和記憶體的建議。

為了分析模型,您需要有:

  • 已註冊的模型。
  • 以輸入腳本和推斷環境定義為基礎的推斷設定。
  • 單一資料行表格式資料集,其中每個資料列都包含字串來代表樣本要求資料。

重要

此時,我們僅支援分析預期其要求資料為字串的服務,例如:字串序列化 json、文字、字串序列化映像等。資料集 (字串) 每個資料列的內容都會放入 HTTP 要求的主體中,並傳送至封裝模型以進行評分的服務。

重要

在 ChinaEast2 和 USGovArizona 區域,我們只支援分析最多 2 個 CPU。

以下範例說明如何建構輸入資料集來分析服務,而此服務預期傳入要求資料包含序列化 json。 在此案例中,我們以相同要求資料內容的 100 個執行個體為基礎建立資料集。 在真實情節中,建議您使用包含各種輸入的更大資料集,特別是當模型資源使用量/行為取決於輸入時。

適用於:Python SDK azureml 第 1 版

import json
from azureml.core import Datastore
from azureml.core.dataset import Dataset
from azureml.data import dataset_type_definitions

input_json = {'data': [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]]}
# create a string that can be utf-8 encoded and
# put in the body of the request
serialized_input_json = json.dumps(input_json)
dataset_content = []
for i in range(100):
    dataset_content.append(serialized_input_json)
dataset_content = '\n'.join(dataset_content)
file_name = 'sample_request_data.txt'
f = open(file_name, 'w')
f.write(dataset_content)
f.close()

# upload the txt file created above to the Datastore and create a dataset from it
data_store = Datastore.get_default(ws)
data_store.upload_files(['./' + file_name], target_path='sample_request_data')
datastore_path = [(data_store, 'sample_request_data' +'/' + file_name)]
sample_request_data = Dataset.Tabular.from_delimited_files(
    datastore_path, separator='\n',
    infer_column_types=True,
    header=dataset_type_definitions.PromoteHeadersBehavior.NO_HEADERS)
sample_request_data = sample_request_data.register(workspace=ws,
                                                   name='sample_request_data',
                                                   create_new_version=True)

備妥包含樣本要求資料的資料集之後,請建立推斷設定。 推斷設定以 score.py 和環境定義為基礎。 下列範例示範如何建立推斷設定並執行分析:

from azureml.core.model import InferenceConfig, Model
from azureml.core.dataset import Dataset


model = Model(ws, id=model_id)
inference_config = InferenceConfig(entry_script='path-to-score.py',
                                   environment=myenv)
input_dataset = Dataset.get_by_name(workspace=ws, name='sample_request_data')
profile = Model.profile(ws,
            'unique_name',
            [model],
            inference_config,
            input_dataset=input_dataset)

profile.wait_for_completion(True)

# see the result
details = profile.get_details()

適用於:Azure CLI ml 延伸模組第 1 版

下列命令示範如何使用 CLI 來分析模型:

az ml model profile -g <resource-group-name> -w <workspace-name> --inference-config-file <path-to-inf-config.json> -m <model-id> --idi <input-dataset-id> -n <unique-name>

提示

若要保存分析所傳回的資訊,請使用模型的標籤或屬性。 使用標籤或屬性會將資料和模型一起儲存在模型登錄中。 下列範例示範新增包含 requestedCpurequestedMemoryInGb 資訊的新標籤:

model.add_tags({'requestedCpu': details['requestedCpu'],
                'requestedMemoryInGb': details['requestedMemoryInGb']})
az ml model profile -g <resource-group-name> -w <workspace-name> --i <model-id> --add-tag requestedCpu=1 --add-tag requestedMemoryInGb=0.5

下一步