Repository navigation
Opt-in Rust backend for HashingEmbedder (prototype, #56) - #57
Draft
thorwhalen wants to merge 4 commits into
Draft
thorwhalen wants to merge 4 commits into
thorwhalen wants to merge 4 commits into
Conversation
) 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
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 separateef-rustwheel built fromrust/(PyO3 0.30, maturin, abi3-py310). Python keeps everything that defines the semantics:str.lower, the word-character table (the code-point rangesre's\wmatches in the running interpreter, passed to the kernel), the output allocation (np.zeros, as before) and the L2 norm.backend="python"stays the default, and nothing changes for anyone who does not opt in.model_id(and every cache key and artifact id built from it) does not name the backend.str, astrsubclass, a lone surrogate) go through the Python path for their row, which also raises the Python path's own errors.backend="rust". The kernel reproduces NEP 50 rounding; under numpy 1 the two backends differ by one ulp. Bothef[rust]andef-rustpin 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)
ef.ingestcall pattern (2 segments per call)ef.ingestend 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.pyfreezes the Python path.tests/test_differential.pycompares the backends bit for bit: Hypothesis (300 examples by default, 5000 withHYPOTHESIS_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.is_alphanumeric" fails 106 tests.dim=2**61with 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 inmisc/docs/port-log.md.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 --checkandmypy.stubtest ef_rust._coreare 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
.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 tagef-rust-vX.Y.Zthrough PyPI trusted publishing, with no secrets. The wads stub already carriesNative WheelandSubpackage wheeljobs, 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..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, andcargo deny. ef's mainci.ymlis unchanged; there the differential tests skip.Before merging (if approved)
ef-rust: this repository, workflowrust-release.yml, environmentpypi.ef-rust-v0.1.0and check the wheels land on PyPI.backend="rust"and therustextra, which now resolves.Files
rust/: the crates, theef_rustpackage and its stub, and the build configs copied from the equate pilotef/embedders.py: thebackend=seamef/_reference.py,tests/test_differential.py,tests/test_hashing_embedder.py(seam and reference tests)benchmarks/bench_port.py,misc/docs/port-log.mdpyproject.toml: therustextra;rust/excluded from ef's sdist.github/workflows/rust-ci.yml,.github/workflows/rust-release.yml