GLM-OCR / SERVING.md
changh95's picture
tt-model push glm-ocr (container)
aa204f5 verified
|
Raw History Blame Contribute Delete
5.99 kB

Serving GLM-OCR on a Tenstorrent p150 (changh95/GLM-OCR)

Start

tt serve changh95/GLM-OCR
# or with tt-model directly (options BEFORE the target):
tt-model serve --port 20000 changh95/GLM-OCR

The first start downloads the checkpoint (about 2.7 GB) into the Hugging Face cache, JIT-compiles the device programs and captures one trace per vision bucket and per prefill bucket (9 + 9 with the defaults) plus the decode traces; later starts reuse the caches. The server logs Opening device, Loading weights, Warming up and Warmup complete: ready in N s; GET /health answers 200 once ready (503 before). Measured on the p150 (container tt-model/glm-ocr:51e311fd8091, 2026-12-01): first start with empty caches 24.5 min (megakernel weight streams packed in 43 s; the rest is the kernel JIT on a busy 8-core host), restart 1.9 min (Warmup complete: ready in 110.7 s).

Routes

route what
POST /v1/chat/completions OpenAI-compatible chat completions (one image_url part per request; SSE streaming)
POST /ocr native OCR: multipart file + fields, JSON {"image": base64, ...}, or the raw image as the body (Content-Type: image/*, fields as query parameters)
GET /health, GET /v1/health readiness (200 ok / 503 starting or failed), backend, device and the queue
GET /info weights repo and commit, device (dispatch, grid), engines, precision, buckets, limits, defaults, task prompts, configuration, the timings of the last request
GET /v1/models the served model (glm-ocr)

POST /v1/chat/completions

Request: messages (the LAST user message is the request: its text parts joined are the prompt, empty = Text Recognition:; at most one image_url part, {"url": "data:image/png;base64,..."} or a bare base64 string; http(s) URLs only with GLMOCR_ALLOW_URLS=1; system and earlier messages are ignored: single-turn OCR), max_tokens / max_completion_tokens, temperature (default 0 = greedy, the official decoding, NOT OpenAI's 1), top_p, seed, stream, stream_options.include_usage, and the extensions top_k, repetition_penalty, max_pixels / max_image_tokens, min_pixels. Refused with 400: n other than 1, stop, logprobs, tools, non-zero presence_penalty / frequency_penalty, a response_format other than text. Other fields (model, user, metadata, ...) are ignored.

Response: {"id": "chatcmpl-...", "object": "chat.completion", "created", "model": "glm-ocr", "choices": [{"index": 0, "message": {"role": "assistant", "content"}, "finish_reason": "stop" | "length"}], "usage": {"prompt_tokens", "completion_tokens", "total_tokens", "prompt_tokens_details": {"image_tokens"}}, "timings", "image"}. The content is the raw model output (tables as HTML, formulas as $$ ... $$). completion_tokens counts the stop token.

stream=true: text/event-stream of chat.completion.chunk objects: the first with delta: {"role": "assistant", "content": ""}, then delta: {"content": "..."} pieces (whole UTF-8 characters), then an empty delta with the finish_reason (and timings), then with include_usage a chunk with choices: [] and usage, then data: [DONE].

POST /ocr

Fields: task (text | table | formula | extract), schema (extract: a JSON object of the fields to fill), prompt (replaces the task's prompt), format (raw | markdown | json), max_new_tokens (or max_tokens), temperature, top_k, top_p, repetition_penalty, seed, max_pixels / max_image_tokens, min_pixels, stream, model (ignored). Unknown fields are refused with 400.

Response: {"model", "text", "raw_text", "finish_reason", "task", "format", "json", "json_error", "usage": {"prompt_tokens", "image_tokens", "completion_tokens", "total_tokens"}, "image": {"width", "height", "resized_width", "resized_height", "image_tokens", "patches", "grid", "min_pixels", "max_pixels"}, "sampling", "timings": {"preprocess_ms", "first_token_ms", "generate_ms", "total_ms", "vision_ms", "prefill_ms", "ttft_ms", "decode_ms", "decode_tok_s", "n_tokens", "vision_bucket", "prefill_bucket", ...}}.

stream=true: data: {"type": "delta", "text": "..."} events while tokens are decoded, then data: {"type": "done", ...the response above...} and data: [DONE]. The deltas join to raw_text.

Errors

{"detail": "...", "error": {"message", "type", "code"}}: 400 invalid input (the reason), 413 body larger than GLMOCR_MAX_BODY_MB or image larger than GLMOCR_MAX_INPUT_MEGAPIXELS, 415 content type, 422 malformed JSON / multipart, 429 queue full (Retry-After), 503 starting / shutting down / waited longer than GLMOCR_QUEUE_TIMEOUT_S, 500 inference failed: .... Errors after a stream started arrive as an SSE event ({"error": ...} on the chat route, {"type": "error", ...} on /ocr) followed by data: [DONE].

Configuration (environment, read once at start)

See the card's configuration table; GET /info reports the active values (configuration). A bad value refuses to start (the log names the variable). The server also reads TT_MODEL_WEIGHTS_REVISION (then TT_WEIGHTS_REVISION; the weights commit), TT_DEVICE_ID, TT_MESH_SHAPE (1x1 only), GLMOCR_DISPATCH (eth; worker only for A/B), GLMOCR_BACKEND (device; cpu and fake for development and tests), GLMOCR_MODEL_DIR (a local checkpoint instead of the Hugging Face cache), GLMOCR_TORCH_THREADS, GLMOCR_TRACE_REGION_MB, GLMOCR_URL_TIMEOUT_S.

Client and smoke test

code/tt_glmocr/server/client.py (standard library) sends an image to /ocr (--task, --schema, --format, --stream) or to the chat route (--chat); code/tt_glmocr/server/smoke_test.py checks a running server (--allow-fake for a GLMOCR_BACKEND=fake server); code/scripts/container_smoke.sh <staged dir> [port] serves a built package, runs the smoke test and always stops it. examples/quickstart.py streams the OCR of one image.