|
Download code/PYTHON.md from changh95/laya: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/changh95/laya/resolve/main/code/PYTHON.md
- Command line
-
hf download hf://changh95/laya/code/PYTHON.md
-
curl -L -o PYTHON.md https://huggingface.co/changh95/laya/resolve/main/code/PYTHON.md
3.67 kB
| # laya on Tenstorrent p150: Python API | |
| `tt_laya` runs the classic laya decision model (the checkpoints of | |
| [convaiinnovations/laya](https://huggingface.co/convaiinnovations/laya): English at the repo root, `multilingual/`, | |
| `typed-decisions/`) on one Blackhole p150. It needs a tt-metal build at `44d6650` with the two patches in | |
| [`patches/`](../patches/) (ETH dispatch: the 12 x 10 grid the megakernel needs; the SYS-1419 reshape guard), its | |
| `ttnn` and torch 2.11.0+cpu; then `pip install -e .` from the repo root (numpy < 2, safetensors, huggingface_hub, | |
| tokenizers). | |
| ```python | |
| from tt_laya.api import Laya | |
| agent = Laya.from_pretrained() # LAYA_* environment; default: profile english, engine mk | |
| state = {"subject": "Duplicate charge on invoice #4411", | |
| "body": "Hi, we were billed twice for March. Please refund the duplicate today or we will cancel our plan."} | |
| questions = { | |
| "department": {"type": "choice", "instructions": "Which department should handle this request?", | |
| "criteria": {"billing": "invoices, payments, refunds", "technical": "bugs, outages, system errors", | |
| "sales": "pricing, new contracts", "other": "everything else"}}, | |
| "urgency": {"type": "score", "instructions": "How urgent is this request?", | |
| "criteria": ["not urgent", "soon", "critical deadline or blocking issue"]}, | |
| "churn_risk": {"type": "noul", "instructions": "Does the user threaten to cancel or leave?"}, | |
| } | |
| out = agent.predict(state, questions) | |
| out["answers"]["department"]["choice"] # 'billing' | |
| out["answers"]["churn_risk"]["noul"] # P(yes) | |
| outs = agent.predict_batch([state, "The app crashes on start."], questions) | |
| agent.close() | |
| ``` | |
| | method | official counterpart | notes | | |
| | --- | --- | --- | | |
| | `Laya.from_pretrained(config=None, *, profile=None, engine=None, weights_dir=None, device=None)` | `laya.Agent("convaiinnovations/laya", subfolder=...)` | `profile`: english, typed-decisions, multilingual; `engine`: mk, ttnn, cpu; `device`: an open ttnn device (else one is opened with ETH dispatch) | | |
| | `predict(state, questions, lang=None, max_len=None, head_max_len=None, min_confidence=None)` (= `system_one`) | `Agent.predict` / `system_one` | the official response dict | | |
| | `predict_batch(states, questions, ...)` | `Agent.predict_batch` | one result per state, in order | | |
| | `prepare` / `infer` / `finish` | - | the host / device / host halves (the server's batcher uses them) | | |
| | `info`, `close()` | - | | | |
| Questions: `{"type": "choice", "instructions": ..., "criteria": {label: description or None} | [labels]}`, | |
| `{"type": "score", "criteria": [level 0, level 1, ...]}`, `{"type": "noul", "criteria": {"false": ..., "true": ...} | |
| (optional), "labels": {"false": ..., "true": ...} (optional)}`; any question may carry `option_order` (a | |
| permutation). Instructions may be a string or JSON. Malformed questions raise `tt_laya.text.QuestionError` with the | |
| official message. The answers carry `probabilities` (rounded to 4 digits), `confidence` (1 - normalised entropy; | |
| noul: max(p, 1 - p)), `answer_confidence` (max p), `action.act_probability`; `min_confidence` (a float or | |
| `{"choice:3-5": 0.6, ..., "default": 0.5}`) adds `abstention`, `abstention_threshold` and `low_confidence`. | |
| `usage` reports tokens and truncation as the official runtime does. | |
| Precision: bfloat16 weights and matmul operands, float32 accumulation / residual / LayerNorm / softmax / RoPE. | |
| Tests: `TT_VISIBLE_DEVICES=none python -m pytest -q code/tt_laya/tests/test_host_*.py`; device checks: | |
| `code/scripts/eval_gates.py`, `code/scripts/bench.py` (through a device lock on shared machines). | |