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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
RUN
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
| 1 |
+
# ╔══════════════════════════════════════════════════════════════════════════╗
|
| 2 |
+
# ║ MYTHICAL UNIVERSAL SYSTEM — Dockerfile [v7.0 ABSOLUTE FINAL] ║
|
| 3 |
+
# ║ [FIX-14] Ubuntu 22.04 — proven stable on Hugging Face CPU nodes ║
|
| 4 |
+
# ║ Engine: stock llama.cpp master + PGO + ThinLTO + AVX2 ║
|
| 5 |
+
# ║ Audio: whisper.cpp server (separate build, no version conflicts) ║
|
| 6 |
+
# ╚══════════════════════════════════════════════════════════════════════════╝
|
| 7 |
|
| 8 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 9 |
+
# STAGE 1: PGO INSTRUMENTATION BUILD
|
| 10 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 11 |
+
FROM ubuntu:22.04 AS pgo-instrument
|
| 12 |
+
|
| 13 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
| 14 |
+
|
| 15 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 16 |
+
clang-15 lld-15 llvm-15 cmake make git \
|
| 17 |
+
python3 python3-pip \
|
| 18 |
+
libcurl4-openssl-dev ca-certificates curl \
|
| 19 |
+
&& ln -sf /usr/bin/clang-15 /usr/local/bin/clang \
|
| 20 |
+
&& ln -sf /usr/bin/clang++-15 /usr/local/bin/clang++ \
|
| 21 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 22 |
+
|
| 23 |
+
# Download small profiling model (200MB, same transformer hot paths as Qwen3)
|
| 24 |
+
RUN pip3 install --no-cache-dir --break-system-packages huggingface_hub hf_transfer 2>/dev/null || \
|
| 25 |
+
pip3 install --no-cache-dir huggingface_hub hf_transfer && \
|
| 26 |
+
HF_HUB_ENABLE_HF_TRANSFER=1 python3 -c "\
|
| 27 |
+
from huggingface_hub import hf_hub_download; \
|
| 28 |
+
hf_hub_download( \
|
| 29 |
+
repo_id='bartowski/SmolLM2-360M-Instruct-GGUF', \
|
| 30 |
+
filename='SmolLM2-360M-Instruct-Q4_K_M.gguf', \
|
| 31 |
+
local_dir='/pgo_model' \
|
| 32 |
+
)"
|
| 33 |
+
|
| 34 |
+
WORKDIR /llama
|
| 35 |
+
RUN git clone --depth=1 https://github.com/ggml-org/llama.cpp .
|
| 36 |
+
|
| 37 |
+
# Instrumented build
|
| 38 |
+
RUN mkdir -p /pgo-data && \
|
| 39 |
+
cmake -B build-instr \
|
| 40 |
+
-DCMAKE_BUILD_TYPE=Release \
|
| 41 |
+
-DCMAKE_C_COMPILER=clang \
|
| 42 |
+
-DCMAKE_CXX_COMPILER=clang++ \
|
| 43 |
+
-DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON \
|
| 44 |
+
-DGGML_NATIVE=OFF \
|
| 45 |
+
-DBUILD_SHARED_LIBS=OFF \
|
| 46 |
+
-DLLAMA_CURL=ON \
|
| 47 |
+
-DCMAKE_C_FLAGS="-fprofile-generate=/pgo-data -O2 -march=x86-64-v3" \
|
| 48 |
+
-DCMAKE_CXX_FLAGS="-fprofile-generate=/pgo-data -O2 -march=x86-64-v3" \
|
| 49 |
+
-DCMAKE_EXE_LINKER_FLAGS="-fprofile-generate=/pgo-data" \
|
| 50 |
+
&& cmake --build build-instr --target llama-server -j$(nproc)
|
| 51 |
|
| 52 |
+
# Profile run — covers all real production use cases
|
| 53 |
+
RUN /llama/build-instr/bin/llama-server \
|
| 54 |
+
--model /pgo_model/SmolLM2-360M-Instruct-Q4_K_M.gguf \
|
| 55 |
+
--host 127.0.0.1 --port 18080 \
|
| 56 |
+
--ctx-size 2048 --threads 2 --parallel 1 \
|
| 57 |
+
--log-disable > /tmp/pgo.log 2>&1 & \
|
| 58 |
+
SERVER_PID=$! && \
|
| 59 |
+
for i in $(seq 1 30); do \
|
| 60 |
+
sleep 3; curl -sf http://127.0.0.1:18080/health >/dev/null 2>&1 && break; \
|
| 61 |
+
done && \
|
| 62 |
+
# Profile 1: Arabic text completion
|
| 63 |
+
curl -sf http://127.0.0.1:18080/v1/chat/completions \
|
| 64 |
+
-H "Content-Type: application/json" \
|
| 65 |
+
-d '{"model":"p","messages":[{"role":"system","content":"You are a universal AI assistant."},{"role":"user","content":"اشرح مفهوم التعلم الآلي بطريقة بسيطة مع أمثلة عملية."}],"max_tokens":250}' >/dev/null 2>&1 || true && \
|
| 66 |
+
# Profile 2: English reasoning
|
| 67 |
+
curl -sf http://127.0.0.1:18080/v1/chat/completions \
|
| 68 |
+
-H "Content-Type: application/json" \
|
| 69 |
+
-d '{"model":"p","messages":[{"role":"user","content":"Explain the difference between supervised and unsupervised learning."}],"max_tokens":300}' >/dev/null 2>&1 || true && \
|
| 70 |
+
# Profile 3: Tool calling (MCP)
|
| 71 |
+
curl -sf http://127.0.0.1:18080/v1/chat/completions \
|
| 72 |
+
-H "Content-Type: application/json" \
|
| 73 |
+
-d '{"model":"p","messages":[{"role":"user","content":"Search for latest AI research papers."}],"max_tokens":100,"tools":[{"type":"function","function":{"name":"web_search","description":"Search the web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}}],"tool_choice":"auto"}' >/dev/null 2>&1 || true && \
|
| 74 |
+
# Profile 4: Streaming
|
| 75 |
+
curl -sf http://127.0.0.1:18080/v1/chat/completions \
|
| 76 |
+
-H "Content-Type: application/json" \
|
| 77 |
+
-d '{"model":"p","messages":[{"role":"user","content":"Write a Python function to download files asynchronously."}],"max_tokens":200,"stream":true}' >/dev/null 2>&1 || true && \
|
| 78 |
+
# Profile 5: Code generation
|
| 79 |
+
curl -sf http://127.0.0.1:18080/v1/chat/completions \
|
| 80 |
+
-H "Content-Type: application/json" \
|
| 81 |
+
-d '{"model":"p","messages":[{"role":"user","content":"Write a complete React component for a chat interface."}],"max_tokens":400}' >/dev/null 2>&1 || true && \
|
| 82 |
+
# Repeated short requests (cache path)
|
| 83 |
+
for i in $(seq 1 8); do \
|
| 84 |
+
curl -sf http://127.0.0.1:18080/v1/chat/completions \
|
| 85 |
+
-H "Content-Type: application/json" \
|
| 86 |
+
-d '{"model":"p","messages":[{"role":"user","content":"Hello"}],"max_tokens":10}' >/dev/null 2>&1 || true; \
|
| 87 |
+
done && \
|
| 88 |
+
kill "${SERVER_PID}" && wait "${SERVER_PID}" 2>/dev/null || true && \
|
| 89 |
+
llvm-profdata-15 merge --output=/pgo-data/merged.profdata /pgo-data/ && \
|
| 90 |
+
echo "[PGO] Profile data ready: $(ls -lh /pgo-data/merged.profdata)"
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 94 |
+
# STAGE 2: PGO + ThinLTO OPTIMISED llama-server
|
| 95 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 96 |
+
FROM ubuntu:22.04 AS llama-builder
|
| 97 |
+
|
| 98 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
| 99 |
+
|
| 100 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 101 |
+
clang-15 lld-15 llvm-15 cmake make libcurl4-openssl-dev ca-certificates \
|
| 102 |
+
&& ln -sf /usr/bin/clang-15 /usr/local/bin/clang \
|
| 103 |
+
&& ln -sf /usr/bin/clang++-15 /usr/local/bin/clang++ \
|
| 104 |
&& rm -rf /var/lib/apt/lists/*
|
| 105 |
|
| 106 |
+
COPY --from=pgo-instrument /llama /llama
|
| 107 |
+
COPY --from=pgo-instrument /pgo-data /pgo-data
|
| 108 |
+
|
| 109 |
+
WORKDIR /llama
|
| 110 |
+
RUN cmake -B build-final \
|
| 111 |
+
-DCMAKE_BUILD_TYPE=Release \
|
| 112 |
+
-DCMAKE_C_COMPILER=clang \
|
| 113 |
+
-DCMAKE_CXX_COMPILER=clang++ \
|
| 114 |
+
-DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON \
|
| 115 |
+
-DGGML_NATIVE=OFF \
|
| 116 |
+
-DBUILD_SHARED_LIBS=OFF \
|
| 117 |
+
-DLLAMA_CURL=ON \
|
| 118 |
+
-DCMAKE_C_FLAGS="-fprofile-use=/pgo-data/merged.profdata \
|
| 119 |
+
-fprofile-correction -flto=thin \
|
| 120 |
+
-O3 -march=x86-64-v3 -DNDEBUG" \
|
| 121 |
+
-DCMAKE_CXX_FLAGS="-fprofile-use=/pgo-data/merged.profdata \
|
| 122 |
+
-fprofile-correction -flto=thin \
|
| 123 |
+
-O3 -march=x86-64-v3 -DNDEBUG" \
|
| 124 |
+
-DCMAKE_EXE_LINKER_FLAGS="-flto=thin -fuse-ld=lld-15" \
|
| 125 |
+
&& cmake --build build-final --target llama-server -j$(nproc) \
|
| 126 |
+
&& strip --strip-unneeded build-final/bin/llama-server \
|
| 127 |
+
&& echo "[llama-builder] Binary: $(du -sh build-final/bin/llama-server)"
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 131 |
+
# STAGE 3: WHISPER.CPP SERVER
|
| 132 |
+
# Separate stage to avoid ggml version conflicts with llama.cpp
|
| 133 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 134 |
+
FROM ubuntu:22.04 AS whisper-builder
|
| 135 |
|
| 136 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
| 137 |
+
|
| 138 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 139 |
+
clang-15 lld-15 cmake make git ca-certificates \
|
| 140 |
+
&& ln -sf /usr/bin/clang-15 /usr/local/bin/clang \
|
| 141 |
+
&& ln -sf /usr/bin/clang++-15 /usr/local/bin/clang++ \
|
| 142 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 143 |
+
|
| 144 |
+
WORKDIR /whisper
|
| 145 |
+
RUN git clone --depth=1 https://github.com/ggml-org/whisper.cpp .
|
| 146 |
+
|
| 147 |
+
RUN cmake -B build \
|
| 148 |
+
-DCMAKE_BUILD_TYPE=Release \
|
| 149 |
+
-DCMAKE_C_COMPILER=clang \
|
| 150 |
+
-DCMAKE_CXX_COMPILER=clang++ \
|
| 151 |
+
-DGGML_AVX2=ON -DGGML_FMA=ON -DGGML_F16C=ON \
|
| 152 |
+
-DGGML_NATIVE=OFF \
|
| 153 |
+
-DWHISPER_BUILD_SERVER=ON \
|
| 154 |
+
-DWHISPER_NO_OPENVINO=ON \
|
| 155 |
+
-DCMAKE_C_FLAGS="-O3 -march=x86-64-v3 -DNDEBUG" \
|
| 156 |
+
-DCMAKE_CXX_FLAGS="-O3 -march=x86-64-v3 -DNDEBUG" \
|
| 157 |
+
&& cmake --build build --target whisper-server -j$(nproc) \
|
| 158 |
+
&& strip --strip-unneeded build/bin/whisper-server \
|
| 159 |
+
&& echo "[whisper-builder] Binary: $(du -sh build/bin/whisper-server)"
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 163 |
+
# STAGE 4: RUNTIME — minimal, production-ready
|
| 164 |
+
# ─────────────────────────────────────────────────────────────────────────────
|
| 165 |
+
FROM python:3.11-slim AS runtime
|
| 166 |
+
|
| 167 |
+
ENV DEBIAN_FRONTEND=noninteractive
|
| 168 |
+
|
| 169 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 170 |
+
ffmpeg \
|
| 171 |
+
libcurl4 \
|
| 172 |
+
libjemalloc2 \
|
| 173 |
+
curl tini ca-certificates \
|
| 174 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 175 |
+
|
| 176 |
+
# Copy optimised binaries
|
| 177 |
+
COPY --from=llama-builder /llama/build-final/bin/llama-server /usr/local/bin/llama-server
|
| 178 |
+
COPY --from=whisper-builder /whisper/build/bin/whisper-server /usr/local/bin/whisper-server
|
| 179 |
+
RUN chmod +x /usr/local/bin/llama-server /usr/local/bin/whisper-server
|
| 180 |
+
|
| 181 |
+
# Python dependencies
|
| 182 |
+
RUN pip install --no-cache-dir \
|
| 183 |
+
"fastapi==0.115.12" \
|
| 184 |
+
"uvicorn[standard]==0.34.3" \
|
| 185 |
+
"uvloop==0.21.0" \
|
| 186 |
+
"httpx==0.28.1" \
|
| 187 |
+
"orjson==3.10.18" \
|
| 188 |
+
"psutil==7.0.0" \
|
| 189 |
+
"pillow==11.2.1" \
|
| 190 |
+
"pypdf==5.4.0" \
|
| 191 |
+
"beautifulsoup4==4.13.4" \
|
| 192 |
+
"youtube-transcript-api==0.6.3" \
|
| 193 |
+
"aiohttp==3.11.18" \
|
| 194 |
+
"huggingface_hub==0.31.4" \
|
| 195 |
+
"hf_transfer==0.1.9"
|
| 196 |
+
|
| 197 |
+
WORKDIR /app
|
| 198 |
+
COPY app.py /app/app.py
|
| 199 |
+
COPY model_manager.py /app/model_manager.py
|
| 200 |
+
COPY startup.sh /app/startup.sh
|
| 201 |
+
RUN chmod +x /app/startup.sh
|
| 202 |
|
| 203 |
+
RUN mkdir -p /data/models /data/slot_cache /data/whisper /tmp/media /var/log /var/run
|
| 204 |
+
VOLUME ["/data"]
|
| 205 |
|
| 206 |
+
# ── Environment defaults ──────────────────────────────────────────────────────
|
| 207 |
+
ENV \
|
| 208 |
+
# Model selection managed by model_manager.py
|
| 209 |
+
MODEL_DIR="/data/models" \
|
| 210 |
+
SLOT_CACHE_DIR="/data/slot_cache" \
|
| 211 |
+
WHISPER_DIR="/data/whisper" \
|
| 212 |
+
WHISPER_MODEL_SIZE="base" \
|
| 213 |
+
# Server
|
| 214 |
+
LLAMA_HOST="127.0.0.1" LLAMA_PORT="8080" \
|
| 215 |
+
WHISPER_HOST="127.0.0.1" WHISPER_PORT="8081" \
|
| 216 |
+
API_HOST="0.0.0.0" API_PORT="7860" \
|
| 217 |
+
# Inference
|
| 218 |
+
CTX_SIZE="16384" \
|
| 219 |
+
MAX_NEW_TOKENS="2048" \
|
| 220 |
+
MAX_CTX_TOKENS="14000" \
|
| 221 |
+
# Safety
|
| 222 |
+
RAM_WARN_GB="11.0" RAM_REJECT_GB="13.0" RAM_FLUSH_GB="14.0" \
|
| 223 |
+
REQUEST_TIMEOUT="90.0" QUEUE_TIMEOUT="30.0" \
|
| 224 |
+
ENRICH_TIMEOUT="12.0" DOWNLOAD_TIMEOUT="30.0" \
|
| 225 |
+
# API
|
| 226 |
+
API_KEY="change-this-to-a-strong-secret" \
|
| 227 |
+
RATE_LIMIT_RPM="60" CACHE_TTL="60" \
|
| 228 |
+
# Media
|
| 229 |
+
IMAGE_MAX_PX="1120" VIDEO_MAX_FRAMES="8" MAX_FILE_MB="50" \
|
| 230 |
+
# HF
|
| 231 |
+
HF_HUB_ENABLE_HF_TRANSFER="1" \
|
| 232 |
+
PYTHONUNBUFFERED="1" PYTHONDONTWRITEBYTECODE="1"
|
| 233 |
|
| 234 |
+
EXPOSE 7860
|
| 235 |
+
ENTRYPOINT ["/usr/bin/tini", "--"]
|
| 236 |
+
CMD ["/app/startup.sh"]
|