使用 kubectl 從 Amazon S3 和 Amazon FSx 部署自訂微調模型 - Amazon SageMaker AI

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

使用 kubectl 從 Amazon S3 和 Amazon FSx 部署自訂微調模型

下列步驟說明如何使用 kubectl 將存放在 Amazon S3 或 Amazon FSx 上的模型部署至 Amazon SageMaker HyperPod 叢集。

下列指示包含在終端機中執行的程式碼儲存格和命令。執行這些命令之前,請確定您已使用 AWS 登入資料設定環境。

先決條件

開始之前,請確認您已:

設定和組態

將所有預留位置值取代為您的實際資源識別符。

  1. 選取您環境中的 區域。

    export REGION=<region>
  2. 初始化您的叢集名稱。這可識別要部署模型的 HyperPod 叢集。

    注意

    請洽詢您的叢集管理員,以確保已授與此角色或使用者的許可。您可以執行 !aws sts get-caller-identity --query "Arn"來檢查您在終端機中使用的角色或使用者。

    # Specify your hyperpod cluster name here HYPERPOD_CLUSTER_NAME="<Hyperpod_cluster_name>" # NOTE: For sample deployment, we use g5.8xlarge for deepseek-r1 1.5b model which has sufficient memory and GPU instance_type="ml.g5.8xlarge"
  3. 初始化叢集命名空間。您的叢集管理員應該已在命名空間中建立 Hyperpod-inference 服務帳戶。

    cluster_namespace="<namespace>"
  4. 使用下列其中一個選項建立 CRD:

    Using Amazon FSx as the model source
    1. 設定 SageMaker 端點名稱。

      export SAGEMAKER_ENDPOINT_NAME="deepseek15b-fsx"
    2. 設定要使用的 Amazon FSx 檔案系統 ID。

      export FSX_FILE_SYSTEM_ID="fs-1234abcd"
    3. 以下是使用 Amazon FSx 和 DeepSeek 模型建立端點的範例 yaml 檔案。

      cat <<EOF> deploy_fsx_cluster_inference.yaml
      ---
      apiVersion: inference.sagemaker.aws.amazon.com/v1alpha1
      kind: InferenceEndpointConfig
      metadata:
        name: $SAGEMAKER_ENDPOINT_NAME
        namespace: $CLUSTER_NAMESPACE
      spec:
        endpointName: $SAGEMAKER_ENDPOINT_NAME
        instanceType: $INSTANCE_TYPE
        invocationEndpoint: invocations
        modelName: deepseek15b
        modelSourceConfig:
          fsxStorage:
            fileSystemId: $FSX_FILE_SYSTEM_ID
          modelLocation: deepseek-1-5b
          modelSourceType: fsx
        worker:
          environmentVariables:
          - name: HF_MODEL_ID
            value: /opt/ml/model
          - name: SAGEMAKER_PROGRAM
            value: inference.py
          - name: SAGEMAKER_SUBMIT_DIRECTORY
            value: /opt/ml/model/code
          - name: MODEL_CACHE_ROOT
            value: /opt/ml/model
          - name: SAGEMAKER_ENV
            value: '1'
          image: 763104351884.dkr.ecr.us-east-2.amazonaws.com/huggingface-pytorch-tgi-inference:2.4.0-tgi2.3.1-gpu-py311-cu124-ubuntu22.04-v2.0
          modelInvocationPort:
            containerPort: 8080
            name: http
          modelVolumeMount:
            mountPath: /opt/ml/model
            name: model-weights
          resources:
            limits:
              nvidia.com/gpu: 1
            requests:
              cpu: 30000m
              memory: 100Gi
              nvidia.com/gpu: 1
      EOF
    Using Amazon S3 as the model source
    1. 設定 SageMaker 端點名稱。

      export SAGEMAKER_ENDPOINT_NAME="deepseek15b-s3"
    2. 設定模型所在的 Amazon S3 儲存貯體位置。

      export S3_MODEL_LOCATION="deepseek-qwen-1-5b"
    3. 以下是使用 Amazon S3 和 DeepSeek 模型建立端點的範例 yaml 檔案。

      cat <<EOF> deploy_s3_inference.yaml
      ---
      apiVersion: inference.sagemaker.aws.amazon.com/v1alpha1
      kind: InferenceEndpointConfig
      metadata:
        name: $SAGEMAKER_ENDPOINT_NAME
        namespace: $CLUSTER_NAMESPACE
      spec:
        modelName: deepseek15b
        endpointName: $SAGEMAKER_ENDPOINT_NAME
        instanceType: ml.g5.8xlarge
        invocationEndpoint: invocations
        modelSourceConfig:
          modelSourceType: s3
          s3Storage:
            bucketName: $S3_MODEL_LOCATION
            region: $REGION
          modelLocation: deepseek15b
          prefetchEnabled: true
        worker:
          resources:
            limits:
              nvidia.com/gpu: 1
            requests:
              nvidia.com/gpu: 1
              cpu: 25600m
              memory: 102Gi
          image: 763104351884.dkr.ecr.us-east-2.amazonaws.com/djl-inference:0.32.0-lmi14.0.0-cu124
          modelInvocationPort:
            containerPort: 8080
            name: http
          modelVolumeMount:
            name: model-weights
            mountPath: /opt/ml/model
          environmentVariables:
            - name: OPTION_ROLLING_BATCH
              value: "vllm"
            - name: SERVING_CHUNKED_READ_TIMEOUT
              value: "480"
            - name: DJL_OFFLINE
              value: "true"
            - name: NUM_SHARD
              value: "1"
            - name: SAGEMAKER_PROGRAM
              value: "inference.py"
            - name: SAGEMAKER_SUBMIT_DIRECTORY
              value: "/opt/ml/model/code"
            - name: MODEL_CACHE_ROOT
              value: "/opt/ml/model"
            - name: SAGEMAKER_MODEL_SERVER_WORKERS
              value: "1"
            - name: SAGEMAKER_MODEL_SERVER_TIMEOUT
              value: "3600"
            - name: OPTION_TRUST_REMOTE_CODE
              value: "true"
            - name: OPTION_ENABLE_REASONING
              value: "true"
            - name: OPTION_REASONING_PARSER
              value: "deepseek_r1"
            - name: SAGEMAKER_CONTAINER_LOG_LEVEL
              value: "20"
            - name: SAGEMAKER_ENV
              value: "1"
      EOF

從 Amazon S3 或 Amazon FSx 部署您的模型

  1. 從 HyperPod 叢集 ARN 取得 Amazon EKS 叢集名稱,以進行 kubectl 身分驗證。

    export EKS_CLUSTER_NAME=$(aws --region $REGION sagemaker describe-cluster --cluster-name $HYPERPOD_CLUSTER_NAME \ --query 'Orchestrator.Eks.ClusterArn' --output text | \ cut -d'/' -f2) aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region $REGION
  2. 使用下列其中一個選項部署 InferenceEndpointConfig 模型:

    Deploy with Amazon FSx as a source
    kubectl apply -f deploy_fsx_luster_inference.yaml
    Deploy with Amazon S3 as a source
    kubectl apply -f deploy_s3_inference.yaml

驗證部署的狀態

  1. 檢查模型是否已成功部署。

    kubectl describe InferenceEndpointConfig $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
  2. 檢查端點是否已成功建立。

    kubectl describe SageMakerEndpointRegistration $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
  3. 測試部署的端點,以確認其正常運作。此步驟確認您的模型已成功部署,並且可以處理推論請求。

    aws sagemaker-runtime invoke-endpoint \ --endpoint-name $SAGEMAKER_ENDPOINT_NAME \ --content-type "application/json" \ --body '{"inputs": "What is AWS SageMaker?"}' \ --region $REGION \ --cli-binary-format raw-in-base64-out \ /dev/stdout

管理您的部署

完成部署測試後,請使用下列命令來清理資源。

注意

在繼續之前,請確認您不再需要部署的模型或儲存的資料。

清除您的資源
  1. 刪除推論部署和相關聯的 Kubernetes 資源。這會停止執行中的模型容器,並移除 SageMaker 端點。

    kubectl delete inferenceendpointconfig $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
  2. 確認已成功完成清除。

    # # Check that Kubernetes resources are removed kubectl get pods,svc,deployment,InferenceEndpointConfig,sagemakerendpointregistration -n $CLUSTER_NAMESPACE
    # Verify SageMaker endpoint is deleted (should return error or empty) aws sagemaker describe-endpoint --endpoint-name $SAGEMAKER_ENDPOINT_NAME --region $REGION
故障診斷

如果您的部署未如預期般運作,請使用這些除錯命令。

  1. 檢查 Kubernetes 部署狀態。

    kubectl describe deployment $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
  2. 檢查 InferenceEndpointConfig 狀態,以查看高階部署狀態和任何組態問題。

    kubectl describe InferenceEndpointConfig $SAGEMAKER_ENDPOINT_NAME -n $CLUSTER_NAMESPACE
  3. 檢查所有 Kubernetes 物件的狀態。全面檢視命名空間中所有相關 Kubernetes 資源。這可讓您快速概觀正在執行的項目,以及可能遺漏的項目。

    kubectl get pods,svc,deployment,InferenceEndpointConfig,sagemakerendpointregistration -n $CLUSTER_NAMESPACE