如何使用模型版本別名

模型別名是可變動的具名參照,指向模型資源中唯一的模型版本。別名是「可變動」的,因為別名可以從一個模型版本移至另一個版本;別名也是「具名」的,因為別名是使用者定義的任意字串。模型別名有助於透過參照擷取或部署特定模型版本,而不必知道特定版本的 ID。因此,這些參照的運作方式與 Docker 標記或 Git 中的分支參照類似。

在模型登錄中建立新模型時,系統會自動將預設別名指派給第一個版本。如果使用者在模型上執行指令時未指定特定版本,預設別名會參照使用的模型版本。模型必須隨時有一個版本帶有預設別名。否則,預設別名會與其他使用者定義的別名相同。

從 Google Cloud 控制台,別名標記可協助利害關係人一目瞭然,瞭解哪個模型是可供部署的穩定版本。除了預設別名,您也可以在模型登錄中建立及指派自訂別名給模型。

在模型登錄中,您可以查看別名欄,快速瞭解哪個模型版本具有預設別名。

「別名」欄和「預設別名」標記。

如果決定將別名重新指派給其他模型版本,可以將別名移至其他版本。

使用別名時請注意以下事項:

  • 版本別名不得重複,且每個模型一次只能將別名指派給單一版本。
  • 版本別名不得為數字。
  • 如果您未指定正式版模型版本,系統會使用預設模型。
  • 別名與標籤不同,進一步瞭解模型標籤
  • 如果套用的是其他模型版本的現有別名,系統會從該版本移除別名。

將模型版本設為預設版本

  1. 前往 Google Cloud 控制台的 Vertex AI「Model Registry」頁面。

    前往「Model Registry」頁面

  2. 在「模型登錄」中,選取要編輯的模型名稱。模型詳細資料視窗隨即開啟。系統會列出所有模型版本。其中一個模型版本具有 default 別名。

  3. 選取要指派預設版本的模型版本「動作」按鈕。

  4. 按一下「設成預設值」

為模型版本新增別名

  1. 前往 Google Cloud 控制台的 Vertex AI「Model Registry」頁面。

    前往「Model Registry」頁面

  2. 在「模型登錄」中,選取要編輯的模型名稱。模型詳細資料視窗隨即開啟。

  3. 在詳細資料頁面中選取所需模型版本,然後按一下「更多」

  4. 按一下「編輯別名」。 選取「新增」

  5. 編輯別名:按一下「新增別名」,然後輸入要新增至模型版本的別名名稱。

  6. 按一下 [儲存]

上傳新的別名模型版本

API

Python


from typing import List

from google.cloud import aiplatform


def upload_new_aliased_model_version_sample(
    parent_name: str,
    artifact_uri: str,
    serving_container_image: str,
    is_default_version: bool,
    version_aliases: List[str],
    version_description: str,
    project: str,
    location: str,
):
    """
    Uploads a new aliased version of a model with ID 'model_id'.
    Args:
        parent_name: The parent resource name of an existing model.
        artifact_uri: The URI of the model artifact to upload.
        serving_container_image: The name of the serving container image to use.
        is_default_version: Whether this version is the default version of the model.
        version_aliases: The aliases of the model version.
        version_description: The description of the model version.
        project: The project ID.
        location: The region name.
    Returns:
        The new version of the model.
    """
    # Initialize the client.
    aiplatform.init(project=project, location=location)

    # Upload a new aliased version of the Model resource with the ID 'model_id'. The parent_name of Model resource can be also
    # 'projects/<your-project-id>/locations/<your-region>/models/<your-model-id>'
    model = aiplatform.Model.upload(
        artifact_uri=artifact_uri,
        serving_container_image=serving_container_image,
        parent_name=parent_name,
        is_default_version=is_default_version,
        version_aliases=version_aliases,
        version_description=version_description,
    )

    return model

刪除模型別名

刪除已指派預設別名的模型版本時,系統會自動將別名指派給下一個最新版本。

控制台

  1. 前往 Google Cloud 控制台的 Vertex AI「Model Registry」頁面。

    前往「Model Registry」頁面

  2. 在「模型登錄」中,選取要編輯的模型名稱。模型詳細資料視窗隨即開啟。

  3. 在詳細資料頁面中,按一下模型版本的「動作」按鈕。

  4. 按一下「編輯別名」

  5. 系統會顯示附加至模型版本的別名清單。將游標懸停在標籤的「別名」欄位右側,即可顯示刪除圖示。

  6. 找出要刪除的別名,然後按一下相對應的刪除圖示。

  7. 按一下 [儲存]

API

Python


from typing import List

from google.cloud import aiplatform


def delete_aliases_model_version_sample(
    model_id: str,
    version_aliases: List[str],
    version_id: str,
    project: str,
    location: str,
):
    """
    Delete aliases to a model version.
    Args:
        model_id: The ID of the model.
        version_aliases: The version aliases to assign.
        version_id: The version ID of the model to assign the aliases to.
        project: The project ID.
        location: The region name.
    Returns
        None.
    """
    # Initialize the client.
    aiplatform.init(project=project, location=location)

    # Initialize the Model Registry resource with the ID 'model_id'.The parent_name of Model resource can be also
    # 'projects/<your-project-id>/locations/<your-region>/models/<your-model-id>'
    model_registry = aiplatform.models.ModelRegistry(model=model_id)

    # Remove the version aliases to the model version with the version 'version'.
    model_registry.remove_version_aliases(
        target_aliases=version_aliases, version=version_id
    )