Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

build(docker): add the Compose HTTP service laya-serve (#234)

PerryLink983da0387842
diff --git a/Dockerfile b/Dockerfile
index 940e6a5e..7cc08fb1 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -13,7 +13,10 @@ RUN pip install torch --index-url https://download.pytorch.org/whl/${TORCH_INDEX
WORKDIR /src
COPY pyproject.toml setup.py README.md LICENSE ./
COPY laya/ ./laya/
-RUN pip install . && pip check
+# The `serve` extra puts `laya-serve` (POST /v1/systemone, GET /health) in the image, so
+# the same image can run a one-shot request or serve the Jev-compatible API. It adds
+# fastapi and uvicorn only; torch was installed above.
+RUN pip install ".[serve]" && pip check
FROM python:3.11-slim-bookworm AS runtime
diff --git a/compose.cuda.yaml b/compose.cuda.yaml
index f2857819..dfc0710e 100644
--- a/compose.cuda.yaml
+++ b/compose.cuda.yaml
@@ -1,4 +1,7 @@
-# Use with compose.yaml, or with compose.http.yaml on the HTTP preview branch.
+# Use with compose.yaml, and with compose.http.yaml when serving over HTTP. The
+# `laya-serve` override repeats the CUDA build args and device reservation, because a
+# Compose override replaces a service's `build` block whole rather than merging it, so
+# adding this file without repeating them would silently serve on CPU.
services:
laya:
build:
@@ -13,3 +16,18 @@ services:
- driver: nvidia
device_ids: ["${LAYA_GPU_ID:-0}"]
capabilities: [gpu]
+
+ laya-serve:
+ build:
+ context: .
+ args:
+ TORCH_INDEX: "${LAYA_TORCH_INDEX:-cu128}"
+ environment:
+ LAYA_DEVICE: "${LAYA_DEVICE:-cuda}"
+ deploy:
+ resources:
+ reservations:
+ devices:
+ - driver: nvidia
+ device_ids: ["${LAYA_GPU_ID:-0}"]
+ capabilities: [gpu]
diff --git a/compose.http.yaml b/compose.http.yaml
new file mode 100644
index 00000000..cb062316
--- /dev/null
+++ b/compose.http.yaml
@@ -0,0 +1,60 @@
+# Serve Laya over HTTP from the same image, so a Jev client can point its `baseUrl` at it:
+#
+# docker compose -f compose.yaml -f compose.http.yaml up --build laya-serve
+# curl -s localhost:8000/health
+# curl -s localhost:8000/v1/systemone -H 'content-type: application/json' \
+# --data @examples/docker/request.json
+#
+# For NVIDIA, append -f compose.cuda.yaml before "up":
+#
+# docker compose -f compose.yaml -f compose.http.yaml -f compose.cuda.yaml up --build laya-serve
+#
+# This file is additive. `laya` in compose.yaml still runs the one-shot SDK quickstart and
+# still publishes no ports, so `docker compose run --rm laya` behaves exactly as before.
+# Weights share the same named cache volume as that service.
+#
+# Set LAYA_API_KEY (or LAYA_API_KEY_FILE) to require `Authorization: Bearer <key>`.
+# LAYA_PRELOAD=1 builds every checkpoint at startup so the first request does not pay for
+# it; the default here is 0 because it makes the container download the whole family on
+# first boot, which is a surprise in a quickstart.
+services:
+ laya-serve:
+ build:
+ context: .
+ args:
+ TORCH_INDEX: "${LAYA_TORCH_INDEX:-cpu}"
+ # Overrides the image's one-shot quickstart CMD; the ENTRYPOINT that loads *_FILE
+ # secrets still runs first.
+ command: ["laya-serve"]
+ ports:
+ # Host port first, container port second. Both come from LAYA_PORT, which
+ # `laya-serve` itself reads, so they cannot drift apart.
+ - "${LAYA_PORT:-8000}:${LAYA_PORT:-8000}"
+ environment:
+ LAYA_HOST: "${LAYA_HOST:-0.0.0.0}"
+ LAYA_PORT: "${LAYA_PORT:-8000}"
+ LAYA_DEVICE: "${LAYA_DEVICE:-cpu}"
+ LAYA_PRELOAD: "${LAYA_PRELOAD:-0}"
+ LAYA_MODELS: "${LAYA_MODELS:-}"
+ LAYA_THREADS: "${LAYA_THREADS:-${OMP_NUM_THREADS:-4}}"
+ LAYA_AUTO_TASK: "${LAYA_AUTO_TASK:-0}"
+ LAYA_LOG_LEVEL: "${LAYA_LOG_LEVEL:-info}"
+ LAYA_API_KEY: "${LAYA_API_KEY:-}"
+ LAYA_API_KEY_FILE: "${LAYA_API_KEY_FILE:-}"
+ HF_HUB_OFFLINE: "${HF_HUB_OFFLINE:-0}"
+ HF_TOKEN: "${HF_TOKEN:-}"
+ HF_TOKEN_FILE: "${HF_TOKEN_FILE:-}"
+ OMP_NUM_THREADS: "${OMP_NUM_THREADS:-4}"
+ volumes:
+ - model-cache:/home/laya/.cache/huggingface
+ # Optional token files, readable by UID 10001. Never put a secret value in the
+ # environment; these paths are read once at startup and the *_FILE variable is
+ # then removed before the server execs.
+ # - ${HF_TOKEN_PATH:?Set HF_TOKEN_PATH}:/run/secrets/hf_token:ro
+ # - ${LAYA_API_KEY_PATH:?Set LAYA_API_KEY_PATH}:/run/secrets/laya_api_key:ro
+ init: true
+ restart: unless-stopped
+
+volumes:
+ model-cache:
+ name: "${LAYA_CACHE_VOLUME:-${COMPOSE_PROJECT_NAME}_model-cache}"
diff --git a/docker/entrypoint.py b/docker/entrypoint.py
index ca871747..b2cc8a82 100644
--- a/docker/entrypoint.py
+++ b/docker/entrypoint.py
@@ -3,7 +3,9 @@ import os
from pathlib import Path
import sys
-SECRET_NAMES = ("HF_TOKEN",)
+# Both names are read as `<NAME>_FILE` and then moved into `<NAME>`, so a secret can be
+# mounted as a file instead of passed in the environment.
+SECRET_NAMES = ("HF_TOKEN", "LAYA_API_KEY")
def main():
diff --git a/docs/docker.md b/docs/docker.md
index b273795a..4d084d3a 100644
--- a/docs/docker.md
+++ b/docs/docker.md
@@ -64,6 +64,8 @@ work with `docker run -e`; Compose-only settings are identified below.
| `LAYA_REQUEST_FILE` | bundled request | JSON request path inside the container |
| `OMP_NUM_THREADS` | `4` | CPU threads; keep within available cores |
| `HF_TOKEN` / `HF_TOKEN_FILE` | unset | Optional Hugging Face credential |
+| `LAYA_API_KEY` / `LAYA_API_KEY_FILE` | unset | **`laya-serve` only:** require `Authorization: Bearer <key>` |
+| `LAYA_PORT` | `8000` | **`laya-serve` only:** container port, and the host port published for it |
| `HF_HUB_OFFLINE` | `0` | `1` uses only cached checkpoints |
| `HF_HOME` | `/home/laya/.cache/huggingface` | Cache path; see mount requirement below |
| `LAYA_CACHE_VOLUME` | project model cache | **Compose only:** named cache volume |
@@ -165,7 +167,66 @@ them again; don't remove a cache shared with another project.
## HTTP serving
-The base quickstart runs the SDK and publishes no ports. HTTP serving is pending
-[#3](https://github.com/NandhaKishorM/laya/pull/3) or
-[#31](https://github.com/NandhaKishorM/laya/pull/31). The server command and health
-probe can follow the interface accepted upstream.
+The image ships `laya-serve`, so the same build that runs the one-shot quickstart can
+serve the Jev-compatible API. `compose.http.yaml` adds it as a second service and leaves
+`laya` alone:
+
+```bash
+docker compose -f compose.yaml -f compose.http.yaml up --build laya-serve
+curl -s localhost:8000/health
+curl -s localhost:8000/v1/systemone -H 'content-type: application/json' \
+ --data @examples/docker/request.json
+```
+
+For NVIDIA, add the CUDA override. It repeats the build args and the device reservation
+for `laya-serve`, because a Compose override replaces a service's `build` block whole
+rather than merging it:
+
+```bash
+docker compose -f compose.yaml -f compose.http.yaml -f compose.cuda.yaml up --build laya-serve
+```
+
+`up` keeps the service running in the foreground; `-d` detaches. Weights go to the same
+named `model-cache` volume as the quickstart, so serving after a quickstart run starts
+with the checkpoints already on disk. Stop with `docker compose ... down`, using the same
+Compose files.
+
+### Server configuration
+
+These apply to the `laya-serve` service only.
+
+| variable | default | effect |
+|---|---|---|
+| `LAYA_HOST` | `0.0.0.0` | bind address inside the container |
+| `LAYA_PORT` | `8000` | container port, and the host port published for it |
+| `LAYA_PRELOAD` | `0` | `1` builds every checkpoint at startup instead of on first request |
+| `LAYA_MODELS` | (all) | comma list to preload: `english,multilingual,typed-decisions` |
+| `LAYA_THREADS` | `OMP_NUM_THREADS` | caps torch intra-op threads; keep at or below physical cores |
+| `LAYA_AUTO_TASK` | `0` | `1` lets the router reach `typed-decisions` automatically |
+| `LAYA_LOG_LEVEL` | `info` | uvicorn log level |
+| `LAYA_API_KEY` | (none) | when set, requires `Authorization: Bearer <key>` |
+
+`LAYA_PRELOAD` defaults to `0` here rather than the package default of `1`, because
+preloading makes the first boot download all three checkpoints. Set it to `1` for a
+long-running deployment so the first request does not pay for the build.
+
+`LAYA_PORT` sets both the published host port and the port the server binds, so the two
+cannot drift. Change one place to move the service:
+
+```bash
+LAYA_PORT=9000 docker compose -f compose.yaml -f compose.http.yaml up --build laya-serve
+```
+
+### Bearer token from a file
+
+`LAYA_API_KEY_FILE` is read once at startup, moved into `LAYA_API_KEY`, and the `_FILE`
+variable is removed before the server execs. Prefer this to putting the key in the
+environment:
+
+```bash
+docker compose -f compose.yaml -f compose.http.yaml run --rm \
+ --volume "$PWD/laya_api_key:/run/secrets/laya_api_key:ro" \
+ -e LAYA_API_KEY_FILE=/run/secrets/laya_api_key \
+ --service-ports laya-serve
+```
+
diff --git a/tests/test_packaging.py b/tests/test_packaging.py
index 9854b075..585124b2 100644
--- a/tests/test_packaging.py
+++ b/tests/test_packaging.py
@@ -145,6 +145,56 @@ for _path in _md:
check("md/no link to a file that does not exist", _broken_files, [])
check("md/no anchor that matches no heading", _broken_anchors, [])
+# ---------------------------------------------------------------- Compose layout
+# `compose.http.yaml` is an override, so it is merged onto `compose.yaml` rather than
+# read on its own. These checks are textual because the suite takes no third-party
+# dependency and PyYAML is not one; the real validation is `docker compose config`,
+# which the Docker workflow runs for every file combination.
+http = read("compose.http.yaml")
+base = read("compose.yaml")
+cuda = read("compose.cuda.yaml")
+dockerfile = read("Dockerfile")
+
+check_true("compose.http/declares laya-serve", "laya-serve:" in http, http[:200])
+check_true("compose.http/runs the server command",
+ 'command: ["laya-serve"]' in http, "laya-serve is not the container command")
+check_true("compose.http/publishes a port", re.search(r"^\s*ports:", http, re.M) is not None)
+# Host and container port must come from the same variable, or they drift apart and the
+# published port stops reaching the server.
+port_map = re.search(r'-\s*"(\$\{[A-Z_]+:-(\d+)\}):(\$\{[A-Z_]+:-(\d+)\})"', http)
+check_true("compose.http/port mapping is present", port_map is not None, http[:300])
+if port_map:
+ check("compose.http/host port equals container port", port_map.group(2), port_map.group(4))
+ check("compose.http/both sides use the same variable", port_map.group(1), port_map.group(3))
+check_true("compose.http/the server reads the same variable",
+ 'LAYA_PORT: "${LAYA_PORT:-8000}"' in http)
+check_true("compose.http/shares the model cache",
+ "model-cache:/home/laya/.cache/huggingface" in http)
+# The base service is what `docker compose run --rm laya` uses; publishing it a port or
+# changing its command would be a breaking change to the quickstart.
+check_true("compose.http/leaves the quickstart service alone",
+ "laya:" not in http, "compose.http.yaml overrides the base `laya` service")
+
+# `pip install .` alone puts no `laya-serve` in the image, so the extra is load-bearing.
+check_true("Dockerfile/installs the serve extra", '".[serve]"' in dockerfile, dockerfile[:400])
+check_true("Dockerfile/still runs pip check", "pip check" in dockerfile)
+
+# A Compose override replaces a service's `build` block whole. If the CUDA override does
+# not repeat the args for laya-serve, that service silently serves on CPU.
+check_true("compose.cuda/covers laya-serve too",
+ re.search(r"^\s{2}laya-serve:", cuda, re.M) is not None,
+ "compose.cuda.yaml does not mention laya-serve, so GPU serving would be CPU")
+check("compose.cuda/repeats the torch index for the base service",
+ len(re.findall(r'TORCH_INDEX: "\$\{LAYA_TORCH_INDEX:-cu128\}"', cuda)), 2)
+check("compose.cuda/repeats the device reservation for both services",
+ len(re.findall(r"driver: nvidia", cuda)), 2)
+check_true("compose.cuda/no stale reference to a missing file",
+ "compose.http.yaml on the HTTP preview branch" not in cuda,
+ "compose.cuda.yaml still describes compose.http.yaml as living on another branch")
+
+# Every file the Docker workflow validates must exist.
+for name in ("compose.yaml", "compose.example.yml", "compose.cuda.yaml", "compose.http.yaml"):
+ check_true("compose/%s exists" % name, os.path.exists(name))
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL: