使用 Google Cloud 管道元件

使用 Google Cloud Pipeline Components (GCPC) 時,您可以運用下列 Vertex AI 和 Google Cloud 功能,確保元件和構件安全無虞。

為元件指定服務帳戶

使用元件時,您可以選擇指定服務帳戶。您的元件會啟動並以這個服務帳戶的權限運作。 舉例來說,您可以使用下列程式碼指定 ModelDeploy 元件的服務帳戶:

model_deploy_op = ModelDeployOp(model=training_job_run_op.outputs["model"],
    endpoint=endpoint_op.outputs["endpoint"],
    automatic_resources_min_replica_count=1,
    automatic_resources_max_replica_count=1,
    service_account="SERVICE_ACCOUNT_ID@PROJECT_ID.iam.gserviceaccount.com")

更改下列內容:

  • SERVICE_ACCOUNT_ID:服務帳戶的 ID。
  • PROJECT_ID:專案 ID。

進一步瞭解如何使用自訂服務帳戶,以及設定服務帳戶以搭配使用 Vertex AI Pipelines。

使用 VPC Service Controls 防止資料外洩

VPC Service Controls 有助於降低 Vertex AI Pipelines 資料遭竊的風險。使用 VPC Service Controls 建立服務範圍時,系統會自動保護 Vertex AI Pipelines 和 Google Cloud Pipeline Components 建立的資源和資料。舉例來說,當您使用 VPC Service Controls 保護管道時,下列構件無法離開服務範圍:

  • AutoML 模型的訓練資料
  • 您建立的模型
  • 批次預測要求結果

進一步瞭解 Vertex AI 的 VPC Service Controls

設定虛擬私有雲網路對等互連

您可以提供額外參數,設定 Google Cloud 管道元件與虛擬私有雲對等互連。舉例來說,您可以使用下列程式碼,為 EndpointCreate 元件指定虛擬私有雲網路:

endpoint_create_op = EndpointCreateOp(
    project="PROJECT_ID",
    location="REGION",
    display_name="endpoint-display-name",
    network="NETWORK")

更改下列內容:

  • PROJECT_ID:專案 ID。
  • REGION:您使用 Vertex AI 的區域。
  • NETWORK:虛擬私有雲網路,例如 "projects/12345/global/networks/myVPC"

進一步瞭解 Vertex AI 中的虛擬私有雲網路對等互連

使用客戶自行管理的加密金鑰 (CMEK)

根據預設, Google Cloud 會使用 Google 代管的加密金鑰,在靜態時自動加密資料。如果您在保護資料的金鑰方面有特定的法規遵循或監管要求,可以為資源使用客戶自行管理的加密金鑰 (CMEK)。開始使用客戶自行管理的加密金鑰前,請先瞭解 Vertex AI 的 CMEK 優點,以及目前支援 CMEK 的資源

使用 CMEK 設定元件

Cloud Key Management Service 中建立金鑰環和金鑰,並授予 Vertex AI 金鑰的加密者和解密者權限後,您就可以指定金鑰做為其中一個建立參數,建立支援 CMEK 的新元件。舉例來說,您可以使用下列程式碼為 ModelBatchPredict 元件指定鍵:

model_batch_predict_op = ModelBatchPredictOp(project="PROJECT_ID",
    model=model_upload_op.outputs["model"],
    encryption_spec_key_name="projects/PROJECT_ID/locations/LOCATION_ID/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME")

更改下列內容:

  • PROJECT_ID:您的 Google Cloud 專案 ID。
  • LOCATION_ID:有效的地點或區域 ID,例如 us-central1
  • KEY_RING_NAME:CMEK 的金鑰環名稱。如要進一步瞭解金鑰環,請參閱 Cloud KMS 資源
  • KEY_NAME:CMEK 金鑰名稱。

注意:不屬於 Vertex AI 的 Google Cloud 元件可能需要額外權限。舉例來說,BigQuery 元件可能需要加密和解密權限。此外,CMEK 金鑰的位置必須與元件位置相同。舉例來說,如果 BigQuery 元件從美國多地區位置的資料集載入資料,CMEK 金鑰也必須位於美國多地區位置。

在元件中取用或產生構件

Google Cloud SDK 定義了一組機器學習中繼資料構件類型,做為元件的輸入和輸出。部分 Google Cloud 管道元件會將這些構件做為輸入內容,或產生這些構件做為輸出內容。

本頁說明如何使用及產生這些構件。

使用機器學習構件

在元件 YAML 中使用構件

構件的中繼資料可做為元件的輸入內容。如要準備要當做輸入內容使用的構件,請先擷取構件,然後將其放入元件 YAML 檔案。

舉例來說,ModelUploadOp 元件會產生 google.VertexModel Artifact,而 ModelDeployOp 元件可使用該 Artifact。在元件 YAML 檔案中使用下列程式碼,從輸入內容 (參照) 擷取 Vertex AI Model 資源:

"model": "',"{{$.inputs.artifacts['model'].metadata['resourceName']}}", '"'

如要查看構件中繼資料的完整結構定義,請參閱 Kubeflow GitHub 存放區中的 artifact_types.py 檔案

在輕量型 Python 元件中取用構件

from kfp.dsl import Artifact, Input

@dsl.component
def classification_model_eval_metrics(
    project: str,
    location: str,  # "us-central1",
    model: Input[Artifact],
) :
   # Consumes the `resourceName` metadata
   model_resource_path = model.metadata["resourceName"]

如要查看如何使用 Vertex ML Metadata 構件型別的範例,請參閱「使用表格型資料和 Vertex AI AutoML 訓練分類模型」。

建立機器學習構件

以下程式碼範例說明如何建立 Vertex ML Metadata 構件,供 GCPC 元件做為輸入內容。

使用匯入器節點

以下範例會建立 Importer 節點,向 Vertex 機器學習中繼資料註冊新的構件項目。匯入器節點會將構件的 URI 和中繼資料做為基本型別,並將其封裝至構件中。

from google_cloud_pipeline_components import v1
from google_cloud_pipeline_components.types import artifact_types
from kfp.components import importer_node
from kfp import dsl

@dsl.pipeline(name=_PIPELINE_NAME)
def pipeline():
  # Using importer and UnmanagedContainerModel artifact for model upload
  # component.
  importer_spec = importer_node.importer(
      artifact_uri='gs://managed-pipeline-gcpc-e2e-test/automl-tabular/model',
      artifact_class=artifact_types.UnmanagedContainerModel,
      metadata={
          'containerSpec': {
              'imageUri':
                  'us-docker.pkg.dev/vertex-ai/automl-tabular/prediction-server:prod'
          }
      })

  # Consuming the UnmanagedContainerModel artifact for the previous step
  model_upload_with_artifact_op = v1.model.ModelUploadOp(
      project=_GCP_PROJECT_ID,
      location=_GCP_REGION,
      display_name=_MODEL_DISPLAY_NAME,
      unmanaged_container_model=importer_spec.outputs['artifact'])

使用以 Python 函式為基礎的元件

以下範例說明如何直接從 Python 元件輸出 Vertex ML 中繼資料構件。

from google_cloud_pipeline_components import v1
from kfp.components import importer_node
from kfp import dsl

@dsl.component(
    base_image='python:3.9',
    packages_to_install=['google-cloud-aiplatform'],
)
# Note currently KFP SDK doesn't support outputting artifacts in `google` namespace.
# Use the base type dsl.Artifact instead.
def return_unmanaged_model(model: dsl.Output[dsl.Artifact]):
  model.metadata['containerSpec'] = {
      'imageUri':
          'us-docker.pkg.dev/vertex-ai/automl-tabular/prediction-server:prod'
  }
  model.uri = f'gs://automl-tabular-pipeline/automl-tabular/model'

@dsl.pipeline(name=_PIPELINE_NAME)
def pipeline():

  unmanaged_model_op = return_unmanaged_model()

  # Consuming the UnmanagedContainerModel artifact for the previous step
  model_upload_with_artifact_op = v1.model.ModelUploadOp(
      project=_GCP_PROJECT_ID,
      location=_GCP_REGION,
      display_name=_MODEL_DISPLAY_NAME,
      unmanaged_container_model=unmanaged_model_op.outputs['model'])

使用自己的容器型元件

以下範例說明如何使用 artifact_types.py 公用程式類別,從以容器為基礎的元件產生 VertexBatchPredictionJob 構件做為輸出內容。

bp_job_artifact = VertexBatchPredictionJob(
    'batchpredictionjob', vertex_uri_prefix + get_job_response.name,
    get_job_response.name, get_job_response.output_info.bigquery_output_table,
    get_job_response.output_info.bigquery_output_dataset,
    get_job_response.output_info.gcs_output_directory)

    output_artifacts = executor_input_json.get('outputs', {}).get('artifacts', {})
    executor_output['artifacts'] = bp_job_artifact.to_executor_output_artifact(output_artifacts[bp_job_artifact.name])