TensorFusion Docs

预加载模型

预加载模型,加速模型部署启动速度

TensorFusion 负责 GPU / NPU 资源池化、调度和虚拟化。模型文件预加载属于业务运行时和存储层能力,当前没有独立的 TensorFusion ModelPreload CRD。生产环境建议用 Kubernetes 原生能力预热镜像、模型缓存和 GPU 池容量。

warmResources 只表示资源池保留的热 GPU / NPU 容量,不会下载模型文件。模型文件仍需要通过镜像、PVC、对象存储缓存或 initContainer 处理。

预热内容

通常需要分别处理三类冷启动开销:

类型处理方式
容器镜像提前拉取推理镜像,设置合适的 imagePullPolicy
模型文件使用 PVC、对象存储缓存、节点本地盘或分布式文件系统
GPU 池容量使用 warmResources 保留热资源,避免完全缩到 0

预拉取推理镜像

如果镜像很大,可以用 DaemonSet 在 GPU 节点上提前拉取镜像。下面示例会让每个 GPU 节点保留一个轻量 Pod,从而把镜像缓存到节点容器运行时中。

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: inference-image-cache
  namespace: tensor-fusion-sys
spec:
  selector:
    matchLabels:
      app: inference-image-cache
  template:
    metadata:
      labels:
        app: inference-image-cache
    spec:
      nodeSelector:
        nvidia.com/gpu.present: "true"
      tolerations:
        - operator: Exists
      containers:
        - name: cache
          image: pytorch/pytorch:2.6.0-cuda12.4-cudnn9-runtime
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
            - sleep 3600

确认镜像缓存完成后,可以删除该 DaemonSet,或保留它用于持续预热新节点。

使用 PVC 预下载模型

对于多副本推理服务,推荐把模型下载到共享 PVC 或节点本地缓存目录,再让业务容器直接读取。

示例:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: model-cache
  namespace: default
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 200Gi
---
apiVersion: batch/v1
kind: Job
metadata:
  name: preload-qwen-model
  namespace: default
spec:
  template:
    spec:
      restartPolicy: OnFailure
      containers:
        - name: preload
          image: python:3.11-slim
          command:
            - sh
            - -c
            - |
              pip install -U modelscope
              python - <<'PY'
              from modelscope import snapshot_download
              snapshot_download("Qwen/Qwen3-0.6B", cache_dir="/models")
              PY
              touch /models/.qwen3-0.6b-ready
          volumeMounts:
            - name: model-cache
              mountPath: /models
      volumes:
        - name: model-cache
          persistentVolumeClaim:
            claimName: model-cache

业务 Deployment 复用同一个 PVC:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: qwen-inference
  namespace: default
  labels:
    tensor-fusion.ai/enabled: "true"
spec:
  replicas: 2
  selector:
    matchLabels:
      app: qwen-inference
  template:
    metadata:
      labels:
        app: qwen-inference
        tensor-fusion.ai/enabled: "true"
      annotations:
        tensor-fusion.ai/inject-container: python
        tensor-fusion.ai/tflops-request: "20"
        tensor-fusion.ai/tflops-limit: "20"
        tensor-fusion.ai/vram-request: 8Gi
        tensor-fusion.ai/vram-limit: 8Gi
    spec:
      containers:
        - name: python
          image: pytorch/pytorch:2.6.0-cuda12.4-cudnn9-runtime
          env:
            - name: MODELSCOPE_CACHE
              value: /models
            - name: HF_HOME
              value: /models/huggingface
          volumeMounts:
            - name: model-cache
              mountPath: /models
      volumes:
        - name: model-cache
          persistentVolumeClaim:
            claimName: model-cache

使用 initContainer 按 Pod 预热

如果每个 Pod 使用独立缓存,可以在业务 Pod 中增加 initContainer。这种方式简单,但每个副本都会执行一次下载,适合小模型或节点本地盘缓存。

spec:
  template:
    spec:
      initContainers:
        - name: download-model
          image: python:3.11-slim
          command:
            - sh
            - -c
            - |
              test -f /models/.ready && exit 0
              pip install -U modelscope
              python - <<'PY'
              from modelscope import snapshot_download
              snapshot_download("Qwen/Qwen3-0.6B", cache_dir="/models")
              PY
              touch /models/.ready
          volumeMounts:
            - name: model-cache
              mountPath: /models
      containers:
        - name: python
          volumeMounts:
            - name: model-cache
              mountPath: /models
      volumes:
        - name: model-cache
          emptyDir: {}

保留热资源

如果资源池会自动缩容,可以配置 warmResources 保留一部分热容量,避免推理服务扩容时等待新 GPU 节点创建。

apiVersion: tensor-fusion.ai/v1
kind: TensorFusionCluster
metadata:
  name: tensor-fusion
spec:
  gpuPools:
    - name: shared
      specTemplate:
        capacityConfig:
          warmResources:
            tflops: "100"
            vram: 80Gi

验收

kubectl get pod -n tensor-fusion-sys -l app=inference-image-cache
kubectl get job preload-qwen-model
kubectl logs job/preload-qwen-model
kubectl rollout status deployment/qwen-inference

如果业务仍然启动慢,优先检查模型下载耗时、镜像拉取耗时、PVC 吞吐、节点本地盘容量和业务框架首次加载编译耗时。

目录