Skip to content

Opt-in Rust backend for HashingEmbedder (prototype, #56) - #57

Draft
thorwhalen wants to merge 4 commits into
mainfrom
rust-hashing-embedder-prototype
Draft

thorwhalen wants to merge 4 commits into
mainfrom
rust-hashing-embedder-prototype

Conversation

@thorwhalen

@thorwhalen thorwhalen commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Draft, needs the maintainer's decision. Do not merge without it. Prototype for #56, which the Rust-port assessment ranks as "port, below the equate pilot" because nothing calls this loop hard yet. Approve = merge.

What it does

HashingEmbedder(backend="rust") hands tokenizing, n-gram counting, the 8-byte BLAKE2b hashing and the float32 accumulation to a separate ef-rust wheel built from rust/ (PyO3 0.30, maturin, abi3-py310). Python keeps everything that defines the semantics: str.lower, the word-character table (the code-point ranges re's \w matches in the running interpreter, passed to the kernel), the output allocation (np.zeros, as before) and the L2 norm.

  • Default unchanged. backend="python" stays the default, and nothing changes for anyone who does not opt in.
  • Bit-identical output. The rust path returns the same float32 bytes as the Python path, so model_id (and every cache key and artifact id built from it) does not name the backend.
  • Routing. Inputs outside the native domain (a non-str, a str subclass, a lone surrogate) go through the Python path for their row, which also raises the Python path's own errors.
  • numpy >= 2 required for backend="rust". The kernel reproduces NEP 50 rounding; under numpy 1 the two backends differ by one ulp. Both ef[rust] and ef-rust pin it, and the facade says why if it is missing.

Measured (Mac arm64, Python 3.12, process CPU time, both columns through the public facade)

Case python rust speed-up
1000 short lines 55 ms 10.4 ms 5.3×
100 segments × 350 words 52 ms 8.8 ms 5.8×
10 documents × 2000 words 23 ms 3.7 ms 6.1×
ef.ingest call pattern (2 segments per call) 54 ms 10.1 ms 5.4×
3000 README segments 2.57 s 0.42 s 6.1×

ef.ingest end to end on 1000 README segments: 1.06 s → 0.34 s, 3.1×. The machine was heavily loaded, so read the ratios as ±1×. BLAKE2b is the floor: about 165 ns per distinct token. The v1 algorithm fixes the hash.

How it was checked

  • ef/_reference.py freezes the Python path. tests/test_differential.py compares the backends bit for bit: Hypothesis (300 examples by default, 5000 with HYPOTHESIS_PROFILE=thorough), a trap grid of about 1,350 cases (case traps, combining marks, numerics, whitespace classes), every Unicode code point, and seeded size edges. Both profiles pass: 1338 tests.
  • The oracle can see.
    • A stub kernel fails 1,316 of 1,319 tests.
    • The mutant "Rust's own is_alphanumeric" fails 106 tests.
    • The mutant "accumulate in f64" survived the first suite. A mixed-counts strategy and a seeded example now kill it.
  • Independent review (checklist plus adversarial). It found a blocker: dim=2**61 with empty input crashed the interpreter while the Rust side allocated the array. It also found two majors: numpy 1.x rounding, and exception classes at extreme sizes. All three are fixed (numpy allocates the output now) and covered by seeded examples. Details are in misc/docs/port-log.md.
  • A second review (rust-reviewer) found four majors: an unseeded counting-table hasher (crafted documents could make counting quadratic), a cached kernel that ignored later config changes, an extra that names an unpublished package, and an oracle that never ran in CI. It also found five minors. All are fixed except the extra, which is the checklist below.
  • cargo test, cargo clippy (pedantic, warnings denied), cargo fmt --check and mypy.stubtest ef_rust._core are clean. ef's own suite passes (471 tests plus 126 doctests). A new test pins the default Python path to the frozen reference, and it runs in CI without Rust.

Cost if approved

  • Release path. .github/workflows/rust-release.yml, copied from the equate pilot, builds abi3 wheels for 5 targets (Linux x86_64 and aarch64, macOS arm64 and x86_64, Windows) plus an sdist. It publishes from a tag ef-rust-vX.Y.Z through PyPI trusted publishing, with no secrets. The wads stub already carries Native Wheel and Subpackage wheel jobs, but Native wheels for maturin (Rust/PyO3) packages: a gated wheel-matrix publish path i2mint/wads#166 and Native path: release a second package from a subdirectory ([tool.wads.ci.packages]) i2mint/wads#220 are still open; when they land, the repo-local workflow can give way to them.
  • CI. The new .github/workflows/rust-ci.yml (from equate) runs on Rust-related changes only. It runs fmt, clippy, cargo test, the differential and thorough Hypothesis runs against a release wheel on Python 3.10 and 3.13, and cargo deny. ef's main ci.yml is unchanged; there the differential tests skip.
  • Open. Bytes are identical within a machine. Across machines, the L2 norm goes through each platform's BLAS for both backends alike, so compare golden vectors from macOS and Linux before promising cross-machine identity (the existing docstring already promises it).

Before merging (if approved)

  1. Configure a PyPI trusted publisher for the new project ef-rust: this repository, workflow rust-release.yml, environment pypi.
  2. Push the tag ef-rust-v0.1.0 and check the wheels land on PyPI.
  3. Mark this PR ready and merge it. That releases ef with the opt-in backend="rust" and the rust extra, which now resolves.

Files

  • rust/: the crates, the ef_rust package and its stub, and the build configs copied from the equate pilot
  • ef/embedders.py: the backend= seam
  • ef/_reference.py, tests/test_differential.py, tests/test_hashing_embedder.py (seam and reference tests)
  • benchmarks/bench_port.py, misc/docs/port-log.md
  • pyproject.toml: the rust extra; rust/ excluded from ef's sdist
  • .github/workflows/rust-ci.yml, .github/workflows/rust-release.yml

)

HashingEmbedder(backend="rust") routes tokenizing, counting, blake2b hashing
and float32 accumulation to the separate ef-rust wheel built from rust/.
The default stays backend="python"; outputs are bit-identical, so model_id
does not change.

- ef/_reference.py: the Python path, frozen as the oracle
- tests/test_differential.py: Hypothesis + trap grid + every code point, bit for bit
- benchmarks/bench_port.py: 5-6x per call, ef.ingest 3.1x
- misc/docs/port-log.md: decisions, review, open items
@thorwhalen thorwhalen added the needs-owner A recommendation awaiting the maintainer's decision: Approve = merge label Nov 23, 2026
- seeded hasher for the token-counting table (no precomputable collisions)
- the cached kernel follows changes to dim/ngram_range/sublinear_tf
- backend='rust' checks the wheel at construction; backend= per call is a TypeError
- class-level defaults keep pre-seam pickles loadable
- rust-ci.yml: fmt, clippy, cargo test, differential tests, cargo deny on Linux
- rust-release.yml: abi3 wheel matrix + trusted publishing from ef-rust-v* tags

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-owner A recommendation awaiting the maintainer's decision: Approve = merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant