# Negotiated GPU — L1 Embedding Fine-Tune Image
#
# Self-contained image that fine-tunes Qwen/Qwen3-Embedding-0.6B on exported
# (anchor, positive, negatives[]) JSONL using MultipleNegativesRankingLoss
# wrapped in MatryoshkaLoss. See README.md for the env contract and run command.
#
# Build + push:
#   docker buildx build --platform linux/amd64 --provenance=false --sbom=false \
#     -t <registry>/negotiated-l1-embed-finetune:vN --push infra/training
#
# ⚠️  MUST be a single-platform manifest. Do NOT use a bare `docker build &&
#     docker push` on Docker 23+/buildx: it emits an OCI image *index* with an
#     attestation manifest (platform unknown/unknown). RunPod's containerd
#     runtime can't resolve a runnable image from that index, so the container
#     never starts and the pod silently does nothing (no logs, 45-min timeout).
#     --provenance=false --sbom=false forces a plain single-image manifest.
#     Verify with: docker buildx imagetools inspect <ref>
#       want MediaType application/vnd.docker.distribution.manifest.v2+json
#       NOT  application/vnd.oci.image.index.v1+json
#
# Smoke-test the image (CPU, no GPU/network needed for the data path):
#   docker run --rm -e SMOKE=1 negotiated/l1-embed-finetune:v1
#
# Train (needs a CUDA GPU, e.g. a single L40S 48GB):
#   docker run --rm --gpus all \
#     -e TRAIN_DATA_PATH=/data -e OUTPUT_DIR=/out \
#     -v /host/training-data/<ts>:/data -v /host/out:/out \
#     negotiated/l1-embed-finetune:v1

# CUDA 12.4 runtime — matches the torch cu124 wheel pinned below. cudnn included
# for the transformer kernels. ubuntu22.04 for a stable glibc.
FROM nvidia/cuda:12.4.1-cudnn-runtime-ubuntu22.04

ENV DEBIAN_FRONTEND=noninteractive \
	PYTHONUNBUFFERED=1 \
	PIP_NO_CACHE_DIR=1 \
	HF_HOME=/workspace/hf-cache \
	HF_HUB_ENABLE_HF_TRANSFER=1

# python3 + pip + build basics. python3.10 is the ubuntu22.04 default.
# Data moves over HuggingFace (huggingface_hub, installed via requirements.txt):
# the entrypoint pulls the dataset with snapshot_download and train.py pushes the
# checkpoint with push_to_hub — no runpodctl/curl/tar needed at runtime.
RUN apt-get update && apt-get install -y --no-install-recommends \
		python3 python3-pip python3-dev \
		git ca-certificates \
	&& rm -rf /var/lib/apt/lists/* \
	&& ln -sf /usr/bin/python3 /usr/local/bin/python

# Pin torch to the CUDA 12.4 build explicitly (kept out of requirements.txt so
# pip never resolves a CPU/other-CUDA wheel). torch 2.5.1 is the tested pairing
# with sentence-transformers 5.x / transformers 4.51 on cu124.
RUN python3 -m pip install --upgrade pip \
	&& python3 -m pip install \
		--index-url https://download.pytorch.org/whl/cu124 \
		torch==2.5.1

# App deps (pinned).
COPY requirements.txt /app/requirements.txt
RUN python3 -m pip install -r /app/requirements.txt

# Default env contract values. All are overridable at `docker run` time.
ENV BASE_MODEL=Qwen/Qwen3-Embedding-0.6B \
	EPOCHS=2 \
	BATCH_SIZE=32 \
	LR=2e-5 \
	MATRYOSHKA_DIMS=1024,512,256,128 \
	MAX_SEQ_LENGTH=512 \
	WARMUP_RATIO=0.1 \
	SEED=42

COPY train.py /app/train.py
# Deploy-gate eval (negotiated-b4p): base-vs-candidate nDCG@10 lift gate. Shipped
# in-image so entrypoint.sh can run it post-train when RUN_DEPLOY_GATE=1.
COPY eval-gate.py /app/eval-gate.py
COPY entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

WORKDIR /app

ENTRYPOINT ["/app/entrypoint.sh"]
