PaddleOCR-VL / SERVING.md
changh95's picture
tt-model push paddleocr-vl (container)
22e0325 verified
|
Raw History Blame Contribute Delete
12.4 kB

Serving PaddleOCR-VL on a Tenstorrent p150 (changh95/PaddleOCR-VL)

Start

tt serve changh95/PaddleOCR-VL                 # default profile 1.6
tt serve changh95/PaddleOCR-VL --profile 1.5   # PaddleOCR-VL-1.5
tt serve changh95/PaddleOCR-VL --profile vl    # the first PaddleOCR-VL (larger images: up to 2.8 megapixels)
# or with tt-model directly (options BEFORE the target):
tt-model serve --port 20000 --profile 1.6 changh95/PaddleOCR-VL

The first start downloads the checkpoints (1.9 GB each) into the Hugging Face cache, packs the decoder megakernel's weight streams into the persistent weight cache (/weight-cache), JIT-compiles the device programs and captures one trace per vision bucket and per prefill bucket; later starts reuse the caches. The server logs Starting paddleocr-vl ... (profile, engines, buckets) and Warmup complete: ready in N s; GET /health answers 200 once ready (503 before).

Routes

route what
POST /v1/chat/completions OpenAI-compatible chat completions with one image (below)
POST /ocr (and /v1/ocr) native recognition: task presets, all decoding controls, task views (table HTML, spotting boxes, LaTeX), timings
GET /health, GET /v1/health readiness (200 ok / 503 starting or failed) and the queue
GET /info profile, tasks and their prompts, weights repo and commit, device (dispatch, grid), engines, precision, configuration, buckets, limits
GET /v1/models the served model

Tasks

task prompt output profiles
ocr OCR: text (a line, a paragraph or a whole page) vl, 1.5, 1.6
table Table Recognition: OTSL cell sequence: <fcel> cell, <ecel> empty cell, <lcel> / <ucel> / <xcel> merged left / up / both, <nl> end of row vl, 1.5, 1.6
formula Formula Recognition: LaTeX (with \[ \]) vl, 1.5, 1.6
chart Chart Recognition: the chart's data as a pipe-separated table vl, 1.5, 1.6
spotting Spotting: text lines, each with location tokens <|LOC_n|> (n = 0..1000, normalized to the image) 1.5, 1.6
seal Seal Recognition: the text of a seal / stamp 1.5, 1.6

Task presets (from the model cards): spotting upscales the image 2x (LANCZOS) when both sides are below 1500 pixels and allows up to 2048 x 28 x 28 = 1605632 pixels; every other task uses the profile's processor limits (1.5 / 1.6: 112896 .. 1003520 pixels, vl: 147384 .. 2822400). Any other prompt text is sent as is (task custom); the model is trained on the six prompts above.

POST /v1/chat/completions

One user message with exactly one image part and an optional text part:

{"model": "paddleocr-vl",
 "messages": [{"role": "user", "content": [
   {"type": "image_url", "image_url": {"url": "data:image/png;base64,iVBORw0..."}},
   {"type": "text", "text": "Table Recognition:"}]}],
 "max_tokens": 1024, "temperature": 0, "stream": false}
  • The text selects the task when it is a task prompt (OCR:, ...) or a task name (ocr, table, ...); empty text = ocr; other text = a custom prompt. The extension field task overrides it.
  • image_url.url: a data: URL (base64). http(s) URLs are fetched only with PADDLEOCR_ALLOW_URLS=1.
  • Fields: max_tokens / max_completion_tokens (default 512 for 1.5 / 1.6, 1024 for vl; up to PADDLEOCR_MAX_NEW_TOKENS), temperature (0 = greedy = the official decoding; up to 2), top_p, seed, stream, stream_options.include_usage, n (1 only); extensions top_k (0 .. PADDLEOCR_K_MAX), repetition_penalty (1 .. 2), task, max_pixels, min_pixels, skip_special_tokens (default true). Refused with 400: n > 1, stop, non-zero frequency_penalty / presence_penalty, logprobs, tools, non-empty system messages, assistant turns, zero or several images, unknown fields.
  • The answer: choices[0].message.content = the model's text (special tokens skipped, as the model cards decode), finish_reason stop (end of sequence) or length, usage, and paddleocr = {task, profile, result, timings, image, stop} (the same result as /ocr).
  • stream=true: text/event-stream of chat.completion.chunk objects (first the role, then content deltas, then a chunk with finish_reason and the paddleocr block, then the usage chunk if requested) and data: [DONE].

POST /ocr

Body: JSON {"image": base64 | data URL | URL, ...}, multipart/form-data (file + fields) or the raw image as the body (Content-Type: image/*, fields in the query string). Fields:

field default values
task ocr ocr, table, formula, chart (+ spotting, seal on 1.5 / 1.6), custom (with prompt)
prompt the task's prompt any text (custom prompt)
max_new_tokens 512 (1.5 / 1.6), 1024 (vl) 1 .. PADDLEOCR_MAX_NEW_TOKENS, clipped silently to the KV capacity (/info max_positions): on 1.5 / 1.6 prompt + output <= 4,096 (the decoder megakernel's KV; the answer then ends with stop length); on vl 7,808 (beyond 4,096 on the ttnn decode engine)
temperature 0 0 .. 2 (0 = greedy)
top_k 0 (off) 0 .. PADDLEOCR_K_MAX
top_p 1 (0, 1]
repetition_penalty 1 (off) 1 .. 2 (HF's rule over prompt + generated ids). Every profile: the decoder megakernel produces the logits of each step and the host applies the penalty and chooses (engine mk-host in timings.engine_dec; about 2.5-3 ms per token greedy instead of ~1.5, about 20-27 ms per token when also sampling with temperature > 0 because the host then samples over the full vocabulary; measured on 1.6, tokens identical to the ttnn decode engine on 5 test images); requests beyond the megakernel's KV run on the ttnn decode engine when it is built
seed random 0 .. 2^63 - 1 (sampling is reproducible with a seed)
max_pixels the task preset 3136 .. PADDLEOCR_MAX_PIXELS
min_pixels the profile's 3136 .. max_pixels
skip_special_tokens true false keeps every special token in text
return_tokens false true adds the generated token ids
stream false SSE: {"type": "delta", "delta"} events, then {"type": "done", ...the answer} and [DONE]

The answer: text, raw_text (special tokens kept), task, prompt, profile, stop (eos | length), result (table: html, otsl, rows; spotting: items = [{text, norm, points, box}] with points / box in pixels of the image you sent; formula: latex; chart: rows when the output is a table), usage (prompt_tokens, image_tokens, completion_tokens, total_tokens), timings (ms: preprocess, vision, prefill, first_token, decode, per_token, total, request), image (size, model_input_size, resized, grid, patches), params.

Configuration (environment, read once at start)

The device programs, buckets and traces are built for one configuration; a bad value refuses to start (the log names the variable). GET /info reports the active values (configuration).

variable values default what it fixes
PADDLEOCR_PROFILE 1.6, 1.5, vl 1.6 checkpoint, pixel limits, chat template, task set (the serve profiles set it)
PADDLEOCR_ENGINE vis=ttnn,pre=ttnn,dec=mk or dec=ttnn vis=ttnn,pre=ttnn,dec=mk device engine per stage (vision, prefill, decode)
PADDLEOCR_PRECISION vis, vis_mlp, pre_attn, pre_mlp, pre_head = bf16 / bfp8; mk = bf16 / bfp8 / bfp8h8; vis_fid, pre_fid = HiFi4 / HiFi2 / LoFi vision bf16 + HiFi2, prefill bf16 (MLP bfp8) + HiFi4, megakernel bfp8h8 weight formats and math fidelities
PADDLEOCR_MAX_PIXELS 3136 .. 16777216 1605632 (1.5 / 1.6, the spotting preset), 2822400 (vl) the largest image after the resize = the vision buckets
PADDLEOCR_MIN_PIXELS 3136 .. max 112896 (1.5 / 1.6), 147384 (vl) the default lower bound of the resize
PADDLEOCR_VIS_BUCKETS multiples of 128 640, 768, 1024, 1536, 2048, 3072, 4096, 5120, 6144, 8192 (vl: from 768, + 10240, 12288, 14464) patch counts with a vision trace (the vision time follows the bucket)
PADDLEOCR_PRE_BUCKETS multiples of 32 one per vision bucket (image tokens + PADDLEOCR_MAX_PROMPT_TOKENS, rounded up to 128) prompt lengths with a prefill trace
PADDLEOCR_MAX_PROMPT_TOKENS 16 .. 1024 64 text tokens of the template + prompt (custom prompts)
PADDLEOCR_MAX_NEW_TOKENS 16 .. 16384 4096 cap of the per-request max_tokens
PADDLEOCR_MAX_POSITIONS >= the largest prefill bucket + 16 largest prefill bucket + PADDLEOCR_MAX_NEW_TOKENS KV-cache capacity
PADDLEOCR_K_MAX 1 .. 64 64 largest top_k of the device sampler
PADDLEOCR_POLL_TOKENS 1 .. 64 8 decode steps per host poll (streaming granularity of the megakernel)
PADDLEOCR_TRACE 0, 1 1 capture traces
PADDLEOCR_TTNN_FALLBACK auto, 0, 1 auto also build the ttnn decode engine (~6 ms per token): requests whose prompt + max_new_tokens exceed the megakernel's KV capacity (4096 positions) run on it, up to PADDLEOCR_MAX_POSITIONS. auto (tt/model.py): built only when the profile's default request at its largest prompt does not fit 4096 positions, i.e. on vl; on 1.5 / 1.6 set 1 to get longer outputs (more start-up time and device memory). repetition_penalty does not need it
PADDLEOCR_WARMUP all, none, a list of vision buckets all buckets compiled and captured before READY
PADDLEOCR_QUEUE_DEPTH, PADDLEOCR_QUEUE_TIMEOUT_S 0 .. 1024, seconds 8, 300 waiting requests (batch 1)
PADDLEOCR_MAX_BODY_MB, PADDLEOCR_MAX_IMAGE_MPIX MB, megapixels 64, 64 upload limits (the pixel count is checked before decoding)
PADDLEOCR_ALLOW_URLS 0, 1 0 fetch http(s) image URLs
PADDLEOCR_DISPATCH eth, worker eth dispatch cores (eth: the 12 x 10 grid)
PADDLEOCR_BACKEND tt, cpu, fake tt device / the clean-room CPU reference (slow) / a test stand-in
PADDLEOCR_WEIGHTS_DIR a path the pinned checkpoint in the HF cache a local checkpoint instead

Examples

IMG=$(base64 -w0 code/tt_paddleocr/samples/table_simple.png)
curl -s localhost:20000/ocr -H 'Content-Type: application/json' -d "{\"image\": \"$IMG\", \"task\": \"table\"}"
curl -s localhost:20000/ocr -F file=@code/tt_paddleocr/samples/formula_1.png -F task=formula
curl -s 'localhost:20000/ocr?task=ocr' -H 'Content-Type: image/png' --data-binary @code/tt_paddleocr/samples/text_zh_line.png
curl -sN localhost:20000/ocr -F file=@page.png -F task=spotting -F max_new_tokens=2048 -F stream=true
curl -s localhost:20000/v1/chat/completions -H 'Content-Type: application/json' -d '{"messages": [{"role": "user",
  "content": [{"type": "image_url", "image_url": {"url": "data:image/png;base64,'"$IMG"'"}},
              {"type": "text", "text": "Table Recognition:"}]}]}'

Client and smoke test

code/tt_paddleocr/server/client.py (standard library) recognizes an image (--task, --prompt, --chat, --stream, --html, --json); code/tt_paddleocr/server/smoke_test.py checks a running server (--allow-fake for a PADDLEOCR_BACKEND=fake server); code/scripts/container_smoke.sh <staged dir> [port] --profile <p> serves a built package, runs the smoke test and always stops it.

Stopping the server

A stop (tt-model stop, SIGTERM) refuses new requests at once, but the request being decoded is allowed to finish before the lifespan shutdown releases the model and closes the chip (measured: 9 s on 1.6, 24 s on vl, both clean). The slowest request is a long sampled one on the host-sampling paths: on vl, 4,096 sampled tokens on the ttnn decode engine take about 110 s (~26 ms per token: the host sampler sorts the whole vocabulary each step), close to tt-model stop's 120 s grace before SIGKILL; avoid stopping a server in that state, or cap max_tokens for sampled vl requests. The same applies on 1.5 and 1.6 to a request that both samples and uses repetition_penalty (the mk-host path, about 20-33 ms per token): near 4,096 tokens it runs 80-130 s, which can overrun the 120 s stop grace and end in a SIGKILL (and a card reset). Cap max_tokens for such requests, or wait for them to finish before stopping.