Files
leleka/docs/offline-port.md
T

65 lines
5.8 KiB
Markdown

# Offline tooling port: status, differentials and divergences
The Python tools under `engine/strata` (`tools/*.py`, `setup.py`, `serve/`) are the oracle. Everything below is
ported to Rust with no Python in the serving path; Python and numpy stay test-only. All differentials run against the
**unmodified** oracle on small synthetic inputs (no model data is committed). Pinned `gguf-py` comes from llama.cpp
commit `3cf03257` through `STRATA_GGUF_PY` (a venv holds `numpy pyyaml tqdm jinja2 regex`, outside Git).
Last full run: native pack 2755 cases, GGUF header 400, tokenizer 14.5M pre-tokenizer strings, dequant 69 fixtures
across 23 types, tools, MTP fetch 10 scenarios, chat template 3000 cases: 0 failures. `scripts/check-local.sh` runs them
when `OFFLINE_ORACLE_PYTHON` and `STRATA_GGUF_PY` are set.
Two kinds of check exist for each package:
- `scripts/offline/diff_*.py <strata-next>` runs the Python oracle **live** over thousands of generated cases;
- `scripts/offline/gen_goldens.py` writes the small committed goldens (`control-plane/tests/fixtures/offline/`,
`crates/*/tests/fixtures/`) that plain `cargo test` replays without Python. Re-run it after changing a generator,
review the diff, commit.
| Package | Rust | Oracle | Differential |
| --- | --- | --- | --- |
| 1. Native pack | `control-plane/src/native_pack.rs` | `tools/iq_pack.py` | `diff_native_pack.py` (2755 generated cases, plus 12 full-CLI cases for `--compat-bf16`, `--base`, `--experts-bin`); unit tests reproduce the six real golden expert tables in `engine/strata/tests/data/native_experts` |
| 2. GGUF header | `crates/model-format/src/gguf.rs`, `control-plane/src/gguf_json.rs` | `tools/gguf_reader.py` | `diff_gguf_header.py`, `gguf_headers.json` (metadata of every value type incl. bool arrays, geometry table for Q8_K, IQ1_S, F64, MXFP4, NVFP4 ...) |
| 3. Tokenizer | `crates/tokenizer` | `tools/strata_tokenizer.py` | `diff_tokenizer.py` (all five extracted assets, ids, detokenization, plus **every Unicode scalar x 13 contexts** against Python `regex`), `tokenizer_oracle.json` |
| 4. Dequantization | `control-plane/src/ggml_bridge.c` | pinned `gguf-py` + `tools/iq_fixture.py` | `diff_dequant.py`, `dequant_oracle.json` (23 block types, bit-exact f32) |
| 5. iq_pack options | `native_pack.rs` | `iq_pack.py` | in `diff_native_pack.py` |
| 6. Small packers | `crates/offline-tools` | `embd_bf16_pack.py`, `ple_fp8_pack.py`, `draft_vocab.py`, `make_profile.py` | `diff_tools.py`, `tools_oracle.json` |
| 7. MTP | `crates/offline-tools/src/mtp_rt.rs`, `control-plane/src/mtp_fetch.rs` | `mtp_rt.py`, `mtp_fetch.py` | `diff_tools.py`; `diff_mtp_fetch.py` and `tests/mtp_fetch.rs` against a local HTTP mirror |
| 8. RoPE / RAM policy | `crates/context` (`rope_scaling`, `context_policy` from `claude/port-rope`, plus `setup_policy.rs`) | `setup.py` | `setup_policy_oracle.json` |
| 9. Chat template | `control-plane/src/prompt.rs` (minijinja `=2.24.0`) | `serve/frontend.py` `ChatTemplate` (jinja2) | `diff_chat_template.py` (3000 cases by default, 12000 run once; 0 failures), `chat_template_oracle.json`, all 10 `chat_golden.json` cases as unit tests |
| 9. Reasoning budget | `control-plane/src/reasoning_budget.rs` | `serve/server.py` `Service.reasoning_budget` / `Service.run` | `reasoning_budget_oracle.json` (247 budget lookups, 400 generations of the real `Service.run` over a scripted engine) |
## Deliberate divergences
1. **`ple_conv1d` F32 to F16 is inexact and accepted**, as `iq_pack.py` does; `conversions.json` records
`method`, `exact: false` and `max_abs_err` (previously the Rust packer refused it). `smoke-native-pack.py` no longer
expects a refusal.
2. **Rounding that overflows to infinity is refused.** F16 overflow is refused by both. For BF16 (`--compat-bf16`) the
Python tool writes the infinity and records `max_abs_err = inf`; Rust refuses. The harness counts these as
`known_stricter_bf16_overflow`, never as failures.
3. **GGUF `general.alignment`** must be a `u32`; other integer widths are rejected.
4. **Unicode version skew.** Rust's `fancy_regex` uses a newer Unicode table than the Python `regex` build. The
per-codepoint check classifies code points that are unassigned (`Cn`) in the Python build as non-failures; assigned
code points must match.
5. **The Jinja environment** is minijinja configured like jinja2's `ImmutableSandboxedEnvironment` (`trim_blocks`,
`lstrip_blocks`) with Python-compatible `tojson` (`json.dumps(ensure_ascii=False)`, `", "` / `": "` separators and
key order kept) and `trim` (`str.strip`, including U+001C to U+001F). `tools` given as a string is not accepted
(Python iterates over its characters); this input is rejected by the request validation first.
6. **Reasoning budget errors** quote the offending value with Python's `repr()`. Dict keys are printed in sorted order
and exotic Unicode categories (unassigned code points) in strings may differ.
## Tools deliberately not ported
`pack_index.py`, `strata_pack.py`, `pack_layer.py` and `canonical_xcheck.py` are only reached from `setup.py` for the
`Q2_0` model on AVX-512 CPUs (`model == "Q2_0" and avx512 and family == "qwen"`, line ~3239). The current Strata
build uses the `IQ3_S` path through `iq_pack.py`, and the native pack contract already states that `Q2_0` is out of
scope. They remain Python-only until the Q2_0/AVX-512 path is brought into scope.
## Open items
- The reasoning budget is wired into the live worker (`worker.rs` `generate`, `BudgetTracker`); see `docs/port-remaining.md` item 2 for the live-path differential.
- The reference-parity checks use synthetic fixtures. No real IQ3_S shard, tokenizer or chat transcript was run here
(no model downloads).
- Thirteen worker tests in `strata-control` and four in `leleka-model-format` fail on macOS on the base branch too
(process and path assumptions); they are unrelated to these changes.