在 GDC Sandbox 上部署 TimesFM

Google Research TimesFM 模型是時間序列預測的基礎模型,已根據許多實際資料集中的數十億個時間點預先訓練,因此您可以將其套用至許多領域的新預測資料集。

本教學課程指南說明如何在 GDC Sandbox 上部署 TimesFM,並達成下列目標。

  • 建立執行 TimesFM 的 Docker 容器,
  • 使用 GDC Sandbox AI 最佳化 SKU 提供的 GPU 部署容器,以及
  • 使用簡單的 HTTP 要求叫用 TimesFM 函式。

事前準備

GDC Sandbox 中的 GPU 包含在 org-infra 叢集中。

  • 如要對機構基礎架構叢集執行指令,請確認您擁有 org-1-infra 叢集的 kubeconfig,如「使用叢集」一文所述:

    • 使用 gdcloud 指令列設定及驗證,以及
    • 產生機構基礎架構叢集的 kubeconfig 檔案,並將其路徑指派給環境變數 KUBECONFIG
  • 確認使用者已獲派專案 sandbox-gpu-projectsandbox-gpu-admin 角色。 根據預設,系統會將角色指派給 platform-admin 使用者。您可以登入 platform-admin,然後執行下列指令,將角色指派給其他使用者:

    kubectl --kubeconfig ${KUBECONFIG} create rolebinding ${NAME} --role=sandbox-gpu-admin \
    --user=${USER} --namespace=sandbox-gpu-project
    
  • 請務必按照「使用 Artifact Registry」一文所述設定 Artifact Registry 存放區,並登入帳戶,以便將映像檔推送至 Artifact Registry,以及從該處提取映像檔。

部署 TimesFM 模型

部署作業是透過一組 Kubernetes 設定檔 (YAML 資訊清單) 協調處理,每個檔案都會定義特定元件或服務。

  1. 建立以 Flask 為基礎的 Python 指令碼 app.py,其中包含 predict 函式 (用於進行時間序列預測) 和 timeseries 函式 (用於根據測試資料產生視覺化內容)。

      from flask import Flask, jsonify, request
      import numpy as np
      import pandas as pd
      from sklearn.preprocessing import StandardScaler
    
      # Initialize Flask application
      app = Flask(__name__)
    
      # Sample route to display a welcome message
      @app.route('/')
      def home():
          return "Welcome to TimesFM! Use the API to interact with the app."
    
      # Example route for predictions (TimesFM might do time-series forecasting or music recommendations)
      @app.route('/predict', methods=['POST'])
      def predict():
          data = request.get_json()
    
          # Ensure the data is in the right format
          if 'features' not in data:
              return jsonify({'error': 'No features provided'}), 400
    
          # For this example, assume 'features' is a list of numbers that need to be scaled
          features = data['features']
          features = np.array(features).reshape(1, -1)
    
          # Dummy model: Apply standard scaling (you would use an actual model here)
          scaler = StandardScaler()
          scaled_features = scaler.fit_transform(features)
    
          # You would normally load your model here (e.g., using pickle or joblib)
          # For simplicity, let's just return the scaled features as a placeholder for prediction
          result = scaled_features.tolist()
    
          return jsonify({'scaled_features': result})
    
      # Example of a route for data visualization or analysis
      @app.route('/timeseries', methods=['GET'])
      def timeseries_analysis():
          # Generate a dummy time series data (replace with actual data)
          time_series_data = pd.Series(np.random.randn(100), name="Random Data")
    
          # Example analysis: compute simple moving average
          moving_avg = time_series_data.rolling(window=10).mean()
    
          return jsonify({
              'time_series': time_series_data.tolist(),
              'moving_average': moving_avg.tolist()
          })
    
      # Run the app
      if __name__ == '__main__':
          app.run(debug=True, host='0.0.0.0', port=5000)
    
  2. 建立 Dockerfile,並安裝 timesfm 來叫用應用程式。

     # Use a base image with Python installed
     FROM python:3.11-slim
     # Set the working directory inside the container
     WORKDIR /app
     # Copy the requirements.txt (if any) and install dependencies
     COPY requirements.txt .
     RUN pip install --no-cache-dir numpy pandas timesfm huggingface_hub jax pytest flask scikit-learn
    
     # Copy the rest of the code into the container
     COPY . .
    
     # Expose the necessary port (default 5000 or whatever your app uses)
     EXPOSE 5000
    
     # Define the entrypoint for the container
     CMD ["python", "app.py"] # Replace with the correct entry script for TimesFM
    
  3. 建構 Docker 映像檔,並上傳至 Artifact Registry 存放區。

    docker build -t timesfm .
    docker tag timesfm "REGISTRY_REPOSITORY_URL"/timesfm:latest
    docker push "REGISTRY_REPOSITORY_URL"/timesfm:latest
    

    更改下列內容:

    • REGISTRY_REPOSITORY_URL:存放區網址。
  4. 建立密鑰來儲存 Docker 憑證。

    
    export SECRET="DOCKER_REGISTRY_SECRET"
    export DOCKER_TEST_CONFIG=~/.docker/config.json 
    kubectl --kubeconfig ${KUBECONFIG} create secret docker-registry ${SECRET} --from-file=.dockerconfigjson=${DOCKER_TEST_CONFIG} -n sandbox-gpu-project
    

    更改下列內容:

    • DOCKER_REGISTRY_SECRET 密鑰名稱。
  5. 建立 timesfm-deployment.yaml 檔案,以部署 timesfm

    timesfm 伺服器部署作業會要求一個 GPU。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: timesfm-deployment
      namespace: sandbox-gpu-project
      labels:
        app: timesfm
    spec:
      replicas: 1 # You can scale up depending on your needs
      selector:
        matchLabels:
          app: timesfm
      template:
        metadata:
          labels:
            app: timesfm
        spec:
          containers:
          - name: timesfm
            image: REGISTRY_REPOSITORY_URL/timesfm:latest
            ports:
            - containerPort: 5000
            resources:
              requests:
                nvidia.com/gpu-pod-NVIDIA_H100_80GB_HBM3: 1  # Request 1 GPU
              limits:
                nvidia.com/gpu-pod-NVIDIA_H100_80GB_HBM3: 1  # Limit to 1 GPU
            env:
            - name: ENV
              value: "production"
          imagePullSecrets:
          - name: docker-registry-secret
    

    更改下列內容:

    • REGISTRY_REPOSITORY_URL:存放區網址。
    • DOCKER_REGISTRY_SECRET:Docker 密鑰的名稱。
  6. 建立 timesfm-service.yaml 檔案,在內部公開 timesfm 伺服器。

    apiVersion: v1
    kind: Service
    metadata:
      name: timesfm-service
    spec:
      selector:
        app: timesfm
      ports:
        - protocol: TCP
          port: 80 # External port exposed
          targetPort: 5000 # Internal container port for Flask
      type: LoadBalancer # Use NodePort for internal access
    
  7. 套用資訊清單。

    kubectl --kubeconfig ${KUBECONFIG} apply -f timesfm-deployment.yaml
    kubectl --kubeconfig ${KUBECONFIG} apply -f timesfm-service.yaml
    
  8. 確認 TimesFM Pod 正在執行。

    kubectl --kubeconfig ${KUBECONFIG} get deployments timesfm-deployment -n sandbox-gpu-project
    kubectl --kubeconfig ${KUBECONFIG} get service timesfm-service -n sandbox-gpu-project
    
  9. 建立專案網路政策,允許來自外部 IP 位址的傳入流量。

    kubectl --kubeconfig ${KUBECONFIG} apply -f - <<EOF
    apiVersion: networking.global.gdc.goog/v1
    kind: ProjectNetworkPolicy
    metadata:
      namespace: sandbox-gpu-project
      name: allow-inbound-traffic-from-external
    spec:
      policyType: Ingress
      subject:
        subjectType: UserWorkload
      ingress:
      - from:
        - ipBlock:
            cidr: 0.0.0.0/0
    EOF
    
  10. 執行下列指令,找出 TimesFM 服務的外部 IP。請記下這個值,後續步驟會將這個值代入 TIMESFM_END_POINT

      kubectl --kubeconfig ${KUBECONFIG} get service timesfm-service \
            -n sandbox-gpu-project -o jsonpath='{.status.loadBalancer.ingress[*].ip}'
    

測試服務。

  1. 如要取得預測結果,請使用 curl 指令將資料傳送至服務,並將 TIMESFM_END_POINT 替換為服務的實際地址和特徵的輸入值。這會叫用 app.py 中定義的 predict 函式,對輸入資料執行一些操作,並以 JSON 格式傳回資料。

    curl -X POST http://TIMESFM_END_POINT/predict -H "Content-Type: application/json" -d '{"features": [1.2, 3.4, 5.6]}'
    
  2. 將 curl 要求傳送至 /timeseries,即可查看使用隨機產生資料的資料視覺化範例。這會叫用 app.py 中定義的時間序列函式,產生隨機時間序列,並對其執行移動平均分析。

    curl http://TIMESFM_END_POINT/timeseries