Skip to content

OPENNLP-1911: Add reproducible evaluation for bounded in-memory embedding search - #1215

Draft
krickert wants to merge 97 commits into
apache:mainfrom
ai-pipestream:OPENNLP-1911-vector-search-evaluation
Draft

OPENNLP-1911: Add reproducible evaluation for bounded in-memory embedding search#1215
krickert wants to merge 97 commits into
apache:mainfrom
ai-pipestream:OPENNLP-1911-vector-search-evaluation

Conversation

@krickert

Copy link
Copy Markdown
Contributor

Summary

Depends on #1214, which depends on #1213 and #1152. Please review this after its parents.

This adds reproducible evaluation tooling for OpenNLP's bounded in-memory vector indexes:

  • deterministic corpus parsing, normalization, and vocabulary learning
  • documented provenance and pinned acquisition tooling for the legal-text evaluation corpus
  • exact-versus-TurboQuant fidelity measurement
  • definition-to-headword and half-passage proxy retrieval tasks
  • build time, single-thread throughput, indexability coverage, and storage reporting
  • equivalent Markdown and TSV reports
  • a Lucene HNSW comparison using an in-memory ByteBuffersDirectory

Lucene 10.4.0 is test scope only. The HNSW adapter and baseline are test sources, and neither Lucene classes nor the adapter are present in the shipped opennlp-embeddings JAR. The baseline is intended to make exact scan, quantized scan, and graph-index tradeoffs visible, not to add a production Lucene provider in this ticket.

The retrieval tasks are reproducible diagnostics without manually labeled judgments. Their results are not presented as claims of general production search relevance.

Verification

  • 381 direct embeddings tests passed on the complete stack.
  • 109 corpus, evaluation, and index tests passed, with only the opt-in full-corpus runner skipped.
  • The documented opt-in runner was exercised separately against the full model and a miniature corpus.
  • The embeddings module RAT result is zero unapproved files.
  • The complete 15-project compilation, packaging, Checkstyle, forbidden-API, Javadoc, and RAT reactor gate passed with opennlp.forkCount=1.

The unrestricted upstream reactor test command additionally encountered unrelated model-download failures in inherited runtime and formats tests. Those failures could not obtain public tokenizer and sentence models; 1,682 runtime unit tests passed before the download-dependent failures.

JIRA

https://issues.apache.org/jira/browse/OPENNLP-1911

krickert added 30 commits August 5, 2026 22:10
…with exact original-text spans

New opennlp-extensions module implementing SentencePiece model inference
without native code: the ModelProto reader, the model-embedded normalizer
(precompiled character map over a Darts-clone double-array trie, whitespace
collapsing and escaping, the dummy word-boundary marker), unigram best-path
segmentation, BPE agenda merging, byte fallback, and user-defined symbol
handling. The public contract is SubwordTokenizer/SubwordPiece; every piece
reports the exact UTF-16 span of the caller's original text it came from,
and the model normalizer is also exposed as an OffsetAwareNormalizer
producing AlignedText.

Parity with the reference implementation is asserted, not assumed: five
tiny bundled models (unigram, unigram with byte fallback, BPE, identity
normalization, whitespace-as-suffix) carry fixtures generated by the
sentencepiece Python package over 40 inputs each, checked piece for piece,
id for id, span for span, plus each model's embedded self-test samples.
An opt-in test (-Dopennlp.subword.eval.dir) runs the same assertions
against real downloaded models; T5-small and ALBERT-base-v2 pass exactly,
including mixed scripts, emoji ZWJ sequences, BOM, and CRLF inputs.
…step

The vocabulary trie dispatches wide nodes (the root and first level of a
real vocabulary) through a 256-entry direct table, one load per byte, and
scans narrow nodes' short label slices linearly instead of binary
searching; a randomized differential test holds both layouts against a
map-backed reference, and moving the duplicate-piece detection into the
counting pass fixes the index error it previously produced. Non-unknown
segments reuse the vocabulary's piece string instead of decoding their
bytes, since the trie match means the bytes are identical.

The normalizer precomputes, per possible first byte, whether any
character-map rule or user-defined symbol starts with it; a clear bit
proves the prefix machinery would pass the byte through raw, so plain
ASCII text skips it entirely. The per-chunk record became a per-call
scratch, the input view keeps its oversized buffers with an explicit
length instead of trimming (pure-ASCII text gets an identity offset map
and no map array at all), the Viterbi scratch is one interleaved array
with scores as raw float bits, and the character-map trie walk relies on
the JVM's own bounds checks with the fail-loud translation on the cold
path.

All 37 bundled parity tests and the T5-small and ALBERT real-model
fixtures pass byte-identically. Single-thread throughput on the T5-small
vocabulary goes from 2.83M to 6.47M pieces per second, from 0.62x to
1.42x of the reference implementation measured through its Python
binding.
SubwordTokenizer and SubwordPiece move to opennlp.tools.tokenize, next to
Tokenizer and WordpieceTokenizer, matching where every other seam of this
round lives. The opennlp-subword module keeps only the SentencePiece
implementation.
…zer into it

WordpieceEncoder in opennlp-api runs the full BERT tokenization pipeline
as a SubwordTokenizer: every piece carries its vocabulary id and the span
of the original text, surviving the normalization steps that change,
insert, and remove characters. Content is computed with the same library
calls the previous pipeline made; offsets come from a per-code-point
rerun, with contextual case mappings (Greek final sigma) falling back to
word-wide spans that widen but never misplace. List and map constructors
cover line-number and explicit-id vocabularies.

BertTokenizer, unreleased and superseded, is removed. The dl tokenizer
creation builds on the encoder behind the existing Tokenizer plumbing via
a package-private adapter with unchanged special-token selection, and a
vocabulary missing its special tokens now fails at construction instead
of at the first id mapping, pinned by a test.

Parity is enforced twice: a differential suite against the reference
pipeline (kept test-only as ReferenceBertPipeline) over a curated corpus
plus 800 randomized inputs, and the removed class's reference token
sequences ported case for case. WordpieceTokenizer is untouched.
…verrides

Applies the review conventions from the OPENNLP-1869 review: class javadoc states the contracts instead of design narrative, every override carries inheritDoc with its null contract, and the private helpers are documented.
The tokenizer is Serializable through the OffsetAwareNormalizer contract but declared no serialVersionUID, which the compiler warns about. Added the serialver-computed value so it matches the convention used across the normalizer classes.
Adds a Subword Tokenization section to the Tokenizer chapter: the SubwordTokenizer
contract and its original-text span guarantee, loading and using a SentencePiece
model including the OffsetAwareNormalizer face, and the WordpieceEncoder pipeline
with its vocab.txt construction and special-token framing.
…s, name the format constants, document every helper
…bjectInputFilter

SentencePieceTokenizer gains serialize(OutputStream) and deserialize(InputStream)
methods. Reads are filtered through an ObjectInputFilter that allow-lists only the
classes reachable from a legitimate tokenizer graph and bounds graph depth,
references, and array length; foreign payloads are rejected with
InvalidClassException before being materialised. Limits are adjustable through a
DeserializationLimits record for unusually large vocabularies; the allow-list is
not configurable. The serialVersionUID is recomputed for the new public methods.
Add SentencePieceUsageExampleTest asserting the load-and-encode workflow and
point the tokenizer manual section at it.
…ixtures, thread safety wording

- Normalize the argument validation messages to the project style, naming the
  offending parameter and dropping the leading article and the trailing period, in
  WordpieceEncoder, SentencePieceTokenizer, ModelProtoReader, BpeEncoder,
  UnigramEncoder and the SubwordPiece compact constructor.
- Stop promising thread safety in the SubwordTokenizer contract and state that it is
  implementation specific instead; the manual now records that both shipped
  implementations are immutable and therefore safe for concurrent use.
- Drop the @throws IllegalArgumentException tags that only repeated the inherited
  contract on the normalize and normalizeAligned overrides, leaving a plain
  {@inheritdoc} as the rest of the class does.
- Remove commentary about release history rather than about the code: the pointer to
  the BertTokenizer class of the 3.0.0 milestone builds in WordpieceTokenizer, and
  the "frozen" qualifier on the ReferenceBertPipeline baseline.
- Move the bundled model loading and the fixture file reading out of
  SentencePieceParityTest into SentencePieceFixtures, so the alignment, validation
  and serialization tests no longer reach into another test class for a tokenizer.
- Extract MODEL_SUFFIX and FIXTURES_SUFFIX constants on SentencePieceFixtures and use
  them in SentencePieceRealModelEvalTest when deriving a fixture path from a model
  path, instead of repeating the two literals.
- Fold the five duplicated @valuesource model lists into a single
  SentencePieceFixtures#models @MethodSource, so adding a bundled model stays a one
  line change.
- Correct the parity test javadoc, which credited a nonexistent gen_fixtures.tsv
  sibling script instead of the gen_fixtures.py script in the test resources.
- Pin accessors that had no coverage: every score is finite and out of range ids are
  rejected, byte pieces occur only in byte fallback models and always render in the
  <0x..> form, and isByte rejects negative ids.
- Assert SubwordPiece.span() next to start and end in WordpieceEncoderTest so the
  derived span stays covered by the piece assertions.
- Document the IOException of the serialized helper in the serialization test and
  fully qualify the OutputStream javadoc link now that the import is gone.
… per review

Applies the review: the old entry point stays through one stable release
instead of being removed, and the DL extension point keeps its descriptor.

- Recreate BertTokenizer in opennlp-api as a thin shim, deprecated since
  3.0.0 forRemoval, with the original three Set based constructors and the
  original tokenizePos message. tokenize delegates to encodeToPieces; ids
  are synthesized from the set order because the tokenize path never reads
  them. Null contract follows this branch's reviewed convention,
  IllegalArgumentException, documented in the throws clauses.
- Delete the package-private EncoderTokenizer; the adapter now lives in
  opennlp-api where downstream code can reach it. AbstractDL's protected
  createTokenizer returns BertTokenizer again, restoring the override
  descriptor so an already compiled subclass keeps overriding at runtime,
  and createPipelineTokenizer hands back the shim.
- Delete ReferenceBertPipeline and point the curated and randomized
  differential tests in WordpieceEncoderTest at the shim, pinning shim and
  encoder to one sequence. Add BertTokenizerTest covering each constructor's
  argument validation, the default special token chain, and the exact
  tokenizePos message. Independent expected sequences continue to live in
  WordpieceEncoderReferenceSequencesTest.
- Fix a real divergence the compatibility check surfaced: the encoder kept
  U+2028 and U+2029 inside words while the old pipeline split on them, so a
  word carrying a line or paragraph separator became the unknown piece.
  cleanAndIsolateCjk now maps Zl and Zp to a space, with a span asserting
  regression test.
- Manual: the WordPiece section describes the deprecation and the migration,
  including the Set to List vocabulary change.
Add a README for regenerating the bundled SentencePiece parity fixtures
and clarify that Utf8Text is the encode-path span bridge behind
SubwordPiece offsets.
State that the reader is an independent re-implementation of the
serialized format, cite Aoe (1989), Yata et al. (2007), and Kanda et
al. (2023) in the class javadoc, and decode the bit-9 offset extension
with a plain conditional instead of the branchless form.
…ink it from the manual

State that the reference implementation produces the expected fixture
outputs, add the end-to-end validation steps for the bundled and real
models, and point the manual's SentencePiece section at the README.
The absolute GitHub URL 404s until merge and pins the branch layout.
New extension module, targeting a modern (2025) static-embedding
distillation format as OpenNLP's word2vec/GloVe successor: same flat
per-token vector table artifact shape, pure JVM lookup at inference
time, no PyTorch/ONNX runtime dependency.

SafetensorsFile/SafetensorsHeaderParser read the safetensors format
(8-byte little-endian header length, JSON header describing each
tensor's dtype/shape/byte range, then raw tensor bytes). Hand-rolled
cursor parser scoped to the header's actual shape, no third-party
JSON dependency, matching the project's existing data-file reader
discipline. safetensors carries no executable content (unlike
PyTorch's pickle-based checkpoints), so no XXE-style hardening is
needed, only ordinary malformed-input handling.

singleMatrixTensorName() deliberately does not guess a tensor key
name convention: distillation tools do not agree on one, so it
auto-detects the lone 2-D F32 tensor and fails loud listing every
candidate when that is ambiguous, rather than risk silently loading
the wrong tensor.

Next: tokenizer wiring and the mean-pool/normalize lookup path,
targeting minishlab/potion-base-8M as the v1 reference model.
WordPieceVocabulary reads a BERT-style vocab.txt (line number is the
token's row id, the format minishlab/potion-base-8M and the wider
BGE/BERT family ship). StaticEmbeddingModel wires it to the existing
BertTokenizer/WordpieceTokenizer (reused as-is, no new tokenizer
code) and the safetensors reader from the previous commit, and
implements the pooling formula.

The formula is verified against MinishLab's Rust reference
implementation (model2vec-rs), not assumed: [CLS]/[SEP] are stripped
before pooling since this is table lookup, not transformer input
(the tokenizer always adds them, so this class trims the first/last
token rather than needing a second tokenizer mode); unknown tokens
are dropped from both the sum and the denominator; each pooled
token's vector is multiplied by an optional per-token weight from a
second "weights" tensor when the safetensors file has one; the sum
is divided by the plain pooled-token count, not the sum of weights,
which is the exact detail source-verification caught (the two give
different results whenever a weight isn't 1.0, and guessing wrong
would have silently produced vectors that don't match the reference
Python/Rust output). Normalization uses an epsilon floor so a
token-less input yields a zero vector instead of a division by zero.

Tests hand-compute the expected pooled vectors for a small synthetic
vocabulary and safetensors fixture, including a dedicated test that
distinguishes the weighted-sum/token-count-denominator behavior from
the (wrong) weighted-sum/sum-of-weights alternative.
similarity(text1, text2): cosine similarity between two pooled
embeddings.

mostSimilar(text, topK): nearest vocabulary tokens to a pooled query
vector, brute-force over the vocabulary (fine at the tens-of-
thousands-of-rows scale this module targets; an ANN index is a
documented, deferred follow-up, not v1 scope). Excludes the special
tokens only; a single-word query's own vocabulary row is, correctly,
its own top match, unlike gensim's convention of excluding the query
word, which does not generalize to multi-word text queries anyway.

analogy(a, b, c, topK): the classic word2vec vector arithmetic
(embed(b) - embed(a) + embed(c)), additionally excluding a, b, and c
themselves from the results, which is load-bearing here (not just
convention) since all three are trivially close to the constructed
target vector.

Tests use a small fixture with genuinely non-collinear vectors (the
pooling-math fixture in StaticEmbeddingModelTest is deliberately
collinear, which is ideal for hand-computing weighted averages but
would make every pairwise similarity a trivial 1.0), built so the
analogy has an exact answer: king - man + woman == queen.
…followup jmh profile

Same opt-in jmh Maven profile pattern already used by opennlp-runtime
(build-helper adds src/jmh/java as a test source root, jmh-core plus
the annotation processor, activated only via -Pjmh; the default mvn
verify is unaffected). Fixture is synthesized at the real
minishlab/potion-base-8M scale (29,528 rows, 256 dimensions, both
verified against the live model repo earlier) rather than downloaded,
so the benchmark has no network dependency, but seeded with real
English words so the benchmark sentences hit actual vocabulary
entries instead of degenerating into all-[UNK] lookups.

Forked run (2 forks x 10 iterations, the annotated configuration, not
the quick-iteration main() override):
  embed (5 short sentences/op):  999,222.698 +/- 29,452.980 ops/s
  mostSimilarTop10 (full ~29.5k-row scan): 3,292.110 +/- 203.639 ops/s

This is the JVM-only raw-throughput baseline the design doc calls
for before any "faster than Python" claim; the concurrent-load
comparison against a Python baseline is a separate, later benchmark.
…ighbor scan, thread-safety hardening

Two analogy() bugs fixed. Passing equal terms crashed with
IllegalArgumentException("duplicate element") from Set.of; and the
exclusion compared raw input strings against vocabulary tokens, so on
an uncased model analogy("Man", "King", "Woman", k) handed "king"
straight back as a result. Exclusion now folds the terms through the
model's own tokenizer and excludes the resulting vocabulary rows,
which makes it case- and accent-consistent with embed() and tolerant
of equal or multiword terms. Both are pinned by new tests.

Nearest-neighbor scan reworked around three observations: per-row L2
norms are constants of the model, so they are precomputed at load
instead of recomputed (with a sqrt) for every row on every query; the
top-K selection now uses a bounded min-heap over primitive parallel
arrays instead of materializing and fully sorting one record per
vocabulary row per query; and the special-token check is a
precomputed boolean mask instead of per-row string hashing. The dot
loop uses four accumulators because the JIT must not reorder
floating-point additions and so cannot unroll the reduction itself.
The zero-norm-row NaN guard is preserved and now has its own test.
embed() drops an OptionalInt allocation per token (primitive -1
sentinel) and hoists the weight branch out of the accumulation loop.

Forked JMH, same configuration and fixture as the recorded baseline:
  embed:            999,222 -> 1,041,654 ops/s (+4.2%)
  mostSimilarTop10:   3,292 ->     9,173 ops/s (2.79x)

Thread safety reviewed and hardened: @threadsafe on
StaticEmbeddingModel, SafetensorsFile, and WordPieceVocabulary; the
class javadoc now documents why the one piece of global mutable state
in the tokenizer chain (WhitespaceTokenizer.INSTANCE's keepNewLines
flag) cannot affect results, since BERT basic tokenization replaces
all whitespace with plain spaces before that split runs; and a new
concurrency test runs 8 threads against one shared instance comparing
every result to the single-threaded reference.
Replaces the whole-file byte[] (capped at 2 GB by Java's int-indexed
arrays, and failing as an opaque OutOfMemoryError beyond it) with
positional FileChannel reads: the header is read eagerly, tensor data
streams straight into the caller's float[] through a reused 1 MB chunk.
File size is now unlimited; the remaining ceiling is per decoded tensor
(a float[] holds at most ~2.1 billion elements) and is checked with a
clear message. Peak load memory drops since file bytes and the decoded
array no longer coexist. A file truncated between read() and
readFloat32() fails loud instead of returning partial data.
…rbage

Writing the direct tests surfaced one gap: parseTop stopped at the
closing brace and silently ignored anything after it. Trailing
whitespace stays legal (writers space-pad the header to align the data
section), any other trailing content now fails loud.
…t quantization, expand tests

Fail loud when a directory holds both model.quantized and model.safetensors
instead of preferring one silently. Widen the QuantizeModel seed argument to
long so the full seed space is expressible. Add a Quantized Models manual
section pointing at the workflow tests. Convert reconstruction and embed-parity
tests to parameterized bit-width cases, add the SentencePiece quantized path
and a both-files-present rejection, and share the cosine and SentencePiece
fixture helpers.
A quantized file header declaring a dimension near 2^29 made
paddedDimension * bits overflow a signed int, so the per-row byte count went
negative and reading the file crashed with an undocumented
NegativeArraySizeException instead of a clean rejection; the same overflow
would corrupt the bit addressing in readCode/writeCode. Compute the row byte
count in long arithmetic through a single range-checked helper used by
quantize, read, and the constructor. Add edge-case tests across non-power-of-two
dimensions, a one-dimensional matrix, and adversarial rows the rotation must
still reconstruct.
…eption

Content errors in read now throw the checked loader exception instead of
IllegalArgumentException, with a size plausibility guard before any allocation.
The ambiguous matrix source and the quantizer's missing safetensors follow the
same contract. The CLI pin includes QuantizeModel, which this branch registers.
…d table path

The row count disagreement and the wordpiece unknown-token check now throw
InvalidFormatException like the rest of the module after the OPENNLP-1877
conversion.
…path and a size bound before allocation (failing tests)

The loader contract says malformed file content fails with the checked
InvalidFormatException, but the decoded-norm, pooling-weight, stored-grid,
and trailing-byte rejections still throw IllegalArgumentException, and a
small hostile file declaring huge dimensions reaches per-row allocation and
dies with EOFException instead of a format error. These tests pin the
intended behavior and fail against the current reader:

- testReadRejectsForeignAndTruncatedFiles now expects
  InvalidFormatException for trailing bytes (was pinning
  IllegalArgumentException, the wrong contract)
- testDeclaredPayloadBeyondFileSizeFailsBeforeAllocating: a 1.1 MB file
  declaring 1,000,000 rows of 512 dims at 4 bits must fail fast, before
  allocating 256 MB of codes plus 8 MB of scales and norms
- non-finite stored grid levels, decoded norms, and pooling weights, and a
  pooling-weight flag with no weights present, must all fail with
  InvalidFormatException
…eption and bound declared sizes before allocating

read(Path) promises the checked InvalidFormatException for malformed
content, but four rejections still threw IllegalArgumentException and
escaped every catch (IOException): an invalid decoded norm, a non-finite
pooling weight, trailing bytes after the declared content, and the
row-byte-count and storable-size checks reached from a hostile header. A
stored grid that fromLevels rejects also surfaced as
IllegalArgumentException. All of these now throw InvalidFormatException
naming the file and the offending field; constructor validation for
programmatic callers stays IllegalArgumentException.

The reader also only checked rowCount and dimension individually against
the file size, so a 1.1 MB file declaring 1,000,000 rows of 512 dims at 4
bits forced a 256 MB code allocation before any content check. The header
fully determines the file size, so the declared total (28 fixed bytes, the
grid levels, a scale and a decoded norm per row, the flag byte, and the
packed codes, plus the per-row pooling weights when flagged) is now held
against the actual file size and rejected before anything row-sized is
allocated.
…ding

The class comment and the manual claimed a 500,000-row, 300-dimension
table drops to 77 MB at 4 bits. The Hadamard rotation pads rows to the
next power of two, so 300 dimensions store 512 codes per row, and each row
also carries two floats (the fitted scale and the decoded norm), not one.
Measured from a written file, that is 264 bytes per row: 132 MB against
600 MB of float32, 4.5 times smaller, not 7.8. The overall shrink range
becomes roughly 4 to 16 times depending on bit width and padding distance.
The padding behavior itself is unchanged.
Red evidence: test compilation fails because VectorIndex, FlatFloatIndex, and TurboQuantIndex do not exist.
Implements the contract pinned in 86e616d. The focused suite now passes 37 tests across the exact and quantized implementations.
Document the exact and TurboQuant choices, their bounded single-JVM scope, the build-freeze-query lifecycle, concurrency contract, and TurboQuant persistence. A mirrored usage test passes with the focused 38-test index suite.
Adds acquisition scripts, pinned source records, licensing notes, and small offline fixtures for the evaluation workflow.
…ng tests)

Red evidence: test compilation fails because the corpus records, parsers, vocabulary learner, and CLI tools do not exist.
Implements the tests pinned in 22a1e8544. The focused corpus and CLI suite now passes 40 tests.
Red evidence: test compilation fails because SearchEvaluator and EvalVectorSearch do not exist.
Implements the tests pinned in a678b8840. The focused evaluator and CLI suite now passes 24 tests and emits matching Markdown and TSV reports.
…sts)

Red evidence: test compilation fails because the Lucene baseline is absent and IndexMetrics does not yet expose serializedBytesPerVector. The beam test uses independent queries and requires a measurable recall improvement.
Implements the contract pinned by 809d7765c. The red compile failed because the HNSW adapter and baseline were absent and IndexMetrics did not expose storage semantics.\n\nThe focused green run passed 57 tests with one opt-in runner skipped. The documented runner was also exercised against the checked-in mini corpus and wrote both report formats. Lucene remains test scope only.
krickert added a commit that referenced this pull request Aug 16, 2026
…dding search) into the gRPC helper base

# Conflicts:
#	rat-excludes
Red evidence: Model2VecUnigramTokenizerTest failed because missing model.vocab leaked a NullPointerException, and StaticEmbeddingModelSentencePieceTest failed because an incomplete legacy tokenizer did not explain the supported alternatives.

The loader now reconstructs the published Unigram tokenizer in memory from tokenizer.json, including its precompiled normalizer and supported Model2Vec post-normalization steps. The pinned potion-multilingual-128M artifact loads without a borrowed teacher model and matches the reference Python vector.
@krickert krickert self-assigned this Aug 21, 2026
Red evidence: after merging the current apache#1152 tip, opennlp-embeddings failed compilation because StaticEmbeddingModel still called the removed raw-array constructor and rowNorms helper.
krickert added a commit to ai-pipestream/opennlp that referenced this pull request Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant