Files
parsing-service/docker-compose.yaml
2026-09-29 05:39:45 +00:00

159 lines
5.7 KiB
YAML

# Toàn bộ service trên một host, một card GPU: `cp .env.example .env`, sửa .env, rồi
# docker compose up -d --build
#
# chandra DLA/OCR từng trang, vLLM cổng ${PORT:-9222}, served name "chandra"
# kie bóc trường Qwen3.5-4B, vLLM cổng ${KIE_PORT:-9224}, served name "qwen3.5-4b"
# api HTTP API cổng ${IDP_PORT:-8001}
# worker hàng đợi trang, KIE, finalize, callback; tải PDF từ HDFS
#
# api/worker chạy network_mode host nên .env giữ nguyên như khi chạy run-api.sh/run-worker.sh:
# IDP_OCR_URL=http://127.0.0.1:9222/..., IDP_KIE_URL mặc định http://localhost:9224/...,
# namenode/KDC phân giải theo /etc/hosts của host.
# kie chỉ khởi động sau khi chandra healthy để hai vLLM không đo bộ nhớ GPU cùng lúc;
# worker đợi cả hai model healthy mới nhận trang.
# Chandra đọc weight nhúng sẵn trong image tại /model. Qwen đọc weight đã giải nén
# từ qwen35-4b-weights.tar tại KIE_MODEL_DIR, dùng cùng image làm runtime vLLM.
x-gpu: &gpu
resources:
reservations:
devices:
- driver: nvidia
device_ids: ["${GPU_DEVICE:-0}"] # như `docker run --gpus device=0`
capabilities: [gpu]
x-vllm-image: &vllm-image ${IMAGE_TAG:-chandra-dla-api:v2-20260906-024126}
x-vllm-env: &vllm-env
HF_HUB_OFFLINE: "1"
TRANSFORMERS_OFFLINE: "1"
VLLM_LOGGING_LEVEL: "${VLLM_LOG_LEVEL:-INFO}"
x-health: &health
test: ["CMD-SHELL", "python3 -c \"import urllib.request; urllib.request.urlopen('http://localhost:8000/health')\""]
interval: 30s
timeout: 5s
retries: 20
start_period: 600s
x-app: &app
image: ${APP_IMAGE:-idp-parsing-service:0.2.0}
build: .
restart: unless-stopped
network_mode: host
env_file: .env
services:
chandra:
image: *vllm-image
pull_policy: never
container_name: chandra-ocr
restart: unless-stopped
deploy: *gpu
ipc: host
shm_size: "16gb"
ports:
- "${PORT:-9222}:8000"
environment: *vllm-env
command:
- --model=/model
- --served-model-name=chandra
- --host=0.0.0.0
- --port=8000
- --dtype=bfloat16
- --max-model-len=${MAX_MODEL_LEN:-32768}
- --max-num-seqs=${MAX_NUM_SEQS:-128}
- --max-num-batched-tokens=${MAX_BATCHED_TOKENS:-65536}
- --gpu-memory-utilization=${GPU_UTIL:-0.55}
- --enable-prefix-caching
- --seed=42
# PHẢI khớp lúc train (IMAGE_MAX_TOKEN_NUM=1792) — đổi là bbox lệch.
- --mm-processor-kwargs={"min_pixels":65536,"max_pixels":1835008}
healthcheck: *health
kie:
# Dùng runtime vLLM đã có trong image Chandra; mount Qwen đè lên /model.
image: *vllm-image
pull_policy: never
container_name: qwen-kie
restart: unless-stopped
depends_on:
chandra:
condition: service_healthy
deploy: *gpu
ipc: host
shm_size: "8gb"
ports:
- "${KIE_PORT:-9224}:8000"
environment: *vllm-env
volumes:
- type: bind
source: ${KIE_MODEL_DIR:?dat KIE_MODEL_DIR trong .env}
target: /model
read_only: true
bind:
create_host_path: false
command:
- --model=/model
- --served-model-name=qwen3.5-4b
- --host=0.0.0.0
- --port=8000
- --dtype=bfloat16
- --max-model-len=32768
- --max-num-seqs=16
- --max-num-batched-tokens=8192
- --gpu-memory-utilization=${KIE_GPU_UTIL:-0.3}
- --enable-prefix-caching
- --seed=42
# Service cần JSON ngay, không cần khối <think>.
- --default-chat-template-kwargs={"enable_thinking":false}
# Mỗi lượt KIE gửi tới 5 trang; giữ ~1.8k token/ảnh như lúc đo kết quả.
- --mm-processor-kwargs={"min_pixels":65536,"max_pixels":1835008}
healthcheck: *health
api:
<<: *app
container_name: idp-api
environment:
IDP_DATA_DIR: ${IDP_DATA_DIR:-/data/idp-runtime}
volumes:
# Cùng đường dẫn trong và ngoài container; SQLite phải nằm trên đĩa local, không NFS.
- ${IDP_DATA_DIR:-/data/idp-runtime}:${IDP_DATA_DIR:-/data/idp-runtime}
command: ["uvicorn", "idp_service.api:app", "--host=${IDP_HOST:-0.0.0.0}", "--port=${IDP_PORT:-8001}"]
healthcheck:
test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://127.0.0.1:${IDP_PORT:-8001}/readyz', timeout=3)\""]
interval: 30s
timeout: 5s
retries: 3
start_period: 30s
worker:
<<: *app
container_name: idp-worker
depends_on:
chandra:
condition: service_healthy
kie:
condition: service_healthy
environment:
IDP_DATA_DIR: ${IDP_DATA_DIR:-/data/idp-runtime}
# Như run-worker.sh: client HDFS native (Java, Hadoop, conf, keytab) lấy từ gói portable.
PATH: /opt/native-hdfs/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KRB5_CONFIG: /opt/native-hdfs/conf/krb5.conf
HDFS_KEYTAB: /opt/native-hdfs/conf/searchengine.headless.keytab
volumes:
- ${IDP_DATA_DIR:-/data/idp-runtime}:${IDP_DATA_DIR:-/data/idp-runtime}
- type: bind
source: ${HDFS_NATIVE_DIR:?dat HDFS_NATIVE_DIR trong .env}
target: /opt/native-hdfs
read_only: true
bind:
create_host_path: false
command: ["python", "-m", "idp_service.worker"]
healthcheck:
test: ["CMD-SHELL", "python -c \"import os, shutil, sqlite3; from idp_service.config import Settings; s=Settings(); db=sqlite3.connect(str(s.db_path), timeout=3); db.execute('SELECT 1'); db.close(); assert s.storage != 'hdfs_cli' or (shutil.which('hdfs') and os.access(os.environ.get('HDFS_KEYTAB', ''), os.R_OK))\""]
interval: 30s
timeout: 5s
retries: 3
start_period: 30s