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-project
프로젝트에sandbox-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 매니페스트)을 통해 오케스트레이션됩니다.
시계열 예측을 실행하는
predict
함수와 테스트 데이터를 기반으로 시각화를 생성하는timeseries
함수가 있는 플라스크 기반 Python 스크립트app.py
를 만드세요.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)
앱을 호출하여 설치된
timesfm
로 Dockerfile을 만듭니다.# 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
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
: 저장소 URL입니다.
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
보안 비밀 이름
timesfm
을 배포하기 위해timesfm-deployment.yaml
파일을 만듭니다.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
: 저장소 URL입니다.DOCKER_REGISTRY_SECRET
: Docker 보안 비밀의 이름입니다.
timesfm
서버를 내부적으로 노출하는timesfm-service.yaml
파일을 만듭니다.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
매니페스트를 적용합니다.
kubectl --kubeconfig ${KUBECONFIG} apply -f timesfm-deployment.yaml kubectl --kubeconfig ${KUBECONFIG} apply -f timesfm-service.yaml
TimesFM
포드가 실행 중인지 확인합니다.kubectl --kubeconfig ${KUBECONFIG} get deployments timesfm-deployment -n sandbox-gpu-project kubectl --kubeconfig ${KUBECONFIG} get service timesfm-service -n sandbox-gpu-project
외부 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
다음 명령어를 실행하여 TimesFM 서비스의 외부 IP를 식별합니다. 이 값을 TIMESFM_END_POINT로 대체하는 이후 단계에서 사용할 수 있도록 기록해 둡니다.
kubectl --kubeconfig ${KUBECONFIG} get service timesfm-service \ -n sandbox-gpu-project -o jsonpath='{.status.loadBalancer.ingress[*].ip}'
서비스를 테스트합니다.
예측을 받으려면
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]}'
/timeseries에 curl 요청을 보내 무작위로 생성된 데이터를 사용한 데이터 시각화의 예를 확인합니다. 이렇게 하면 app.py에 정의된 시계열 함수가 호출되어 임의의 시계열이 생성되고 이동 평균 분석이 실행됩니다.
curl http://TIMESFM_END_POINT/timeseries