feat(ml): fuse two embedding models and protect the ML service with a token

This commit is contained in:
shira committed 2026-10-11 14:40:52 +03:00
1 parent bc00d6901f
commit b24e5ff906
63 files changed
+2287 -276

No files matched your search

+12 -6
View File
@@ -4,12 +4,17 @@ services:
context: ../..
dockerfile: ml/Dockerfile
environment:
# giga loads the pinned model baked into the image; fake is the hash
# encoder for stands without the model; none keeps the service not ready.
ML_EMBEDDER: ${ML_EMBEDDER:-giga}
# A comma list of the pinned models baked into the image (giga, qwen3),
# the first one is the default; fake is the hash encoder for stands
# without the models; none keeps the service not ready. The ensemble
# policy needs giga,qwen3.
ML_EMBEDDER: ${ML_EMBEDDER:-giga,qwen3}
ML_TORCH_THREADS: ${ML_TORCH_THREADS:-4}
ML_MICRO_BATCH: ${ML_MICRO_BATCH:-1}
ML_VERIFY_WEIGHTS: ${ML_VERIFY_WEIGHTS:-true}
# The container listens on all its interfaces, so the service refuses
# to start without the shared bearer token that the backend sends.
ML_SERVICE_TOKEN: ${ML_SERVICE_TOKEN:-}
# Loopback only: the backend reaches the service over the app network by
# the alias below, never through a published port.
ports:
@@ -35,13 +40,14 @@ services:
options:
max-size: 10m
max-file: "3"
# The float32 model takes about 3.6 GiB resident with four torch threads
# (research CPU benchmark); the limits leave headroom for a request batch.
# The float32 Giga model takes about 3.6 GiB resident with four torch
# threads (research CPU benchmark) and Qwen3-Embedding about 2.5 GiB more;
# the limits leave headroom for a request batch.
deploy:
resources:
limits:
cpus: "4.0"
memory: 5G
memory: 8G
networks:
# The core project network; start the core project first (make -C deploy up).
+3
View File
@@ -247,6 +247,9 @@ services:
# the separate ML project, which joins this network as "ml".
SEMANTIC_MATCHING_ENABLED: ${SEMANTIC_MATCHING_ENABLED:-false}
ML_SERVICE_URL: ${ML_SERVICE_URL:-http://ml:8000}
# Shared bearer token of the ML service (a secret, at least 32
# characters); required while SEMANTIC_MATCHING_ENABLED is true.
ML_SERVICE_TOKEN: ${ML_SERVICE_TOKEN:-}
ML_REQUEST_TIMEOUT: ${ML_REQUEST_TIMEOUT:-30s}
ML_CIRCUIT_FAILURES: ${ML_CIRCUIT_FAILURES:-3}
ML_CIRCUIT_OPEN: ${ML_CIRCUIT_OPEN:-30s}
+42
View File
@@ -0,0 +1,42 @@
# systemd user unit of the ML service on a GPU host without Docker. Install it
# as ~/.config/systemd/user/lincodex-ml.service of the service user (with
# lingering enabled) and keep the settings and the token in the environment
# file, mode 600; see docs/en/DEPLOY.md#ml-service-on-a-gpu-host.
#
# %h is the home directory of the user: the layout below is
# %h/lincodex-ml/app/ml the ml/ directory of the release
# %h/lincodex-ml/venv the virtualenv (make -C ml cuda-requirements)
[Unit]
Description=Lincodex ML embedding service
StartLimitIntervalSec=600
StartLimitBurst=10
[Service]
Type=exec
WorkingDirectory=%h/lincodex-ml/app/ml
EnvironmentFile=%h/.config/lincodex-ml/ml.env
Environment=PYTHONPATH=%h/lincodex-ml/app/ml/src
Environment=PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
# The models are read from local files only; nothing is downloaded.
Environment=HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 HF_HUB_DISABLE_TELEMETRY=1 HF_HUB_DISABLE_PROGRESS_BARS=1
# Thread pools of the CPU work around the GPU (tokenizer, pooling).
Environment=OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 TOKENIZERS_PARALLELISM=false
# A permanent load failure ends the process, so systemd restarts it later.
Environment=ML_EXIT_ON_LOAD_FAILURE=true
ExecStart=%h/lincodex-ml/venv/bin/python -m lincodex_ml.serve
Restart=on-failure
RestartSec=15
TimeoutStopSec=30
# The process with both models on CUDA keeps about 2.6 GiB resident (peak
# 2.9 GiB); the limits keep a fault from starving the other host services.
MemoryHigh=5G
MemoryMax=6G
CPUQuota=400%
TasksMax=512
LimitNOFILE=4096
Nice=5
NoNewPrivileges=yes
UMask=0077
[Install]
WantedBy=default.target
+25
View File
@@ -0,0 +1,25 @@
# Settings of the ML service on a GPU host; copy to
# ~/.config/lincodex-ml/ml.env (mode 600) and replace the placeholders. The
# token is a secret: generate it on the host, for example
# python3 -c 'import secrets; print(secrets.token_hex(32))'
# and give the same value to the backend as ML_SERVICE_TOKEN.
ML_EMBEDDER=giga,qwen3
ML_DEVICE=cuda
# float16 fits both models in about 2.4 GiB of VRAM; see the precision note in
# docs/en/DEPLOY.md#ml-service-on-a-gpu-host before changing it.
ML_DTYPE=float16
# Allocator cap of the process; the CUDA context adds about 130 MiB.
ML_CUDA_MEMORY_LIMIT_MB=2300
ML_TORCH_THREADS=4
ML_MICRO_BATCH=1
ML_VERIFY_WEIGHTS=true
# A private address of the host, never 0.0.0.0.
ML_LISTEN_HOST=<private-ip>
ML_LISTEN_PORT=<port>
# Directories with the pinned files (a Hugging Face snapshot directory works)
# and the lock files of the release that pin them.
ML_GIGA_MODEL_DIR=<directory of ai-sage/Giga-Embeddings-instruct-480M-0826 at the pinned revision>
ML_GIGA_MODEL_LOCK=<release>/ml/model/giga-embeddings-480m.lock.json
ML_QWEN3_MODEL_DIR=<directory of Qwen/Qwen3-Embedding-0.6B at the pinned revision>
ML_QWEN3_MODEL_LOCK=<release>/ml/model/qwen3-embedding-0.6b.lock.json
ML_SERVICE_TOKEN=<generated on the host>