File size: 3,307 Bytes
22e0325 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | # PaddleOCR-VL on Tenstorrent p150: Python API
```python
from tt_paddleocr import serve_config as S
from tt_paddleocr.api import PaddleOCRVL
ocr = PaddleOCRVL.from_config(S.resolve_config({"PADDLEOCR_PROFILE": "1.6"})) # opens the device, builds programs
r = ocr.recognize("crop.png") # path, bytes, PIL image or HxWx3 uint8 array; task "ocr"
print(r["text"]) # the model's text (special tokens skipped, as the model card)
r = ocr.recognize("table.png", task="table") # r["result"]["html"]: the table as HTML (+ "otsl", "rows")
r = ocr.recognize("formula.png", task="formula") # r["result"]["latex"]
r = ocr.recognize("page.png", task="spotting") # 1.5 / 1.6: r["result"]["items"] = [{text, box, points}]
r = ocr.recognize(img, prompt="Chart Recognition:", temperature=0.7, top_p=0.9, seed=1)
for delta in ocr.stream("crop.png", task="ocr"): # text deltas as the tokens are decoded
print(delta, end="", flush=True)
ocr.close()
```
`recognize(image, task=None, prompt=None, *, max_new_tokens=None, temperature=None, top_k=None, top_p=None,
repetition_penalty=None, seed=None, max_pixels=None, min_pixels=None, skip_special_tokens=True, return_tokens=False,
on_delta=None, cancel=None)` returns a dict: `text`, `raw_text` (special tokens kept), `task`, `prompt`, `profile`,
`stop` (`eos` | `length`), `result` (the task view: table `html` / `otsl` / `rows`, spotting `items`, formula `latex`,
chart `rows`), `usage` (prompt / image / completion tokens), `timings` (ms: preprocess, vision, prefill,
first_token, decode, per_token, request), `image` (sizes, the patch grid), `params`. These are the RUNTIME
parameters of the HTTP routes (`SERVING.md`). Tasks: `ocr`, `table`, `formula`, `chart` (all profiles), `spotting`,
`seal` (1.5 / 1.6); any other `prompt` is sent as is (task `custom`).
The COMPILE-TIME configuration is `serve_config.ServeConfig` (the `PADDLEOCR_*` environment variables of
`SERVING.md`: profile, engines, weight formats, the largest image, buckets, token caps). `PADDLEOCR_BACKEND=cpu` runs
the clean-room CPU reference instead of the device (slow, for checks), `PADDLEOCR_BACKEND=fake` a deterministic
stand-in without weights (tests).
Requirements: tt-metal at `44d66500520fda9f2c7060c0f6b41ec48f7ab37e` with the two patches in `patches/` (ETH
dispatch: the 12 x 10 grid), and `numpy<2 safetensors huggingface_hub tokenizers pillow` (+ `fastapi uvicorn` for the
server). The weights are fetched from PaddlePaddle's repositories at the pinned commits (`PADDLEOCR_WEIGHTS_DIR`
points at a local copy instead).
Device pipeline (`tt_paddleocr/tt`): `ttnn_vision.py` (patch embedding + 27 encoder layers with 2-D RoPE + projector,
one trace per patch bucket), `ttnn_decoder.py` (prefill, one trace per prompt bucket; the ttnn decode step),
`mk_dec/` (the decoder megakernel: one launch per token, on-device greedy / sampling), `model.py`
(`DevicePipeline`: image -> vision -> prompt rows -> prefill -> decode). The clean-room CPU reference is
`tt_paddleocr/reference/model.py`, the host pre-processing `tt_paddleocr/frontend.py`.
Host tests (no device): `TT_VISIBLE_DEVICES=none python -m pytest code/tt_paddleocr/tests/test_host_api.py
code/tt_paddleocr/tests/test_host_server.py`.
|