Skip to main content

engine.solvers.retrieval

engine.solvers.retrieval

Retrieval solvers: what a read from long-term memory stages, ranks, streams, and maintains.

Domain implementations behind mlsysim.solvers (added 2026-10-20). Each model is arithmetic on stated counts: whole serialized records in a token allowance, one BM25 term in two documents, the bandwidth floor of an exact vector scan, the resident bytes of approximate indexes, an exact tree’s neighborhood against an allowance, the write-path cost of keeping an index current, and the auxiliary bytes and re-index time of two stores. None models a tokenizer, a trained encoder, cache effects, or code-graph cycles.

Classes

Name Description
AnnFootprintModel Resident bytes of two approximate indexes over the same vectors.
Bm25PairModel One query term’s BM25 score in a short and a long document of one corpus.
ExactScanFloorModel The time floor of an exact scan: every eligible vector streamed once.
GraphNeighborhoodModel A typed traversal’s neighborhood, as an exact tree, against a token allowance.
IndexMaintenanceModel Who pays to keep an index current after an edit.
RetrievalPayloadModel Whole serialized records in the retrieval allowance of one model call.
StorageAmplificationModel Auxiliary bytes and re-index time of a lexical-plus-graph store and a

AnnFootprintModel

engine.solvers.retrieval.AnnFootprintModel()

Resident bytes of two approximate indexes over the same vectors.

A navigable-small-world graph keeps every vector plus M links per node; product quantization replaces each vector with m codes of one byte and scores through a lookup table of m x centroids entries per query.

Methods

Name Description
solve Compare graph-index and product-quantized storage under stated layouts.
solve
engine.solvers.retrieval.AnnFootprintModel.solve(
    vectors,
    dimension,
    bytes_per_value,
    links_per_node,
    link_bytes,
    subvectors,
    centroids,
    code_bytes,
    table_entry_bytes,
)

Compare graph-index and product-quantized storage under stated layouts.

Parameters
Name Type Description Default
vectors int Positive indexed vector count. required
dimension int Positive values per vector; divisible by subvectors for equal subspaces. required
bytes_per_value Quantity Positive bytes per uncompressed scalar. required
links_per_node int Nonnegative stored graph links per vector. required
link_bytes Quantity Nonnegative bytes per stored graph link. required
subvectors int Positive codes per quantized vector. required
centroids int Positive centroid count per subspace. required
code_bytes Quantity Positive bytes per stored subspace code. required
table_entry_bytes Quantity Positive bytes per query-distance lookup entry. required
Returns
Name Type Description
AnnFootprintResult Raw, graph, and code storage; lookup-table KB; dimensionless compression and integer subspace width.
Notes

Equation: raw=N*d*b; graph=N*M*link_bytes; codes=N*m*code_bytes; lookup=m*centroids*table_entry_bytes.

Assumptions: Fixed-degree graph representation and equal-width product-quantization codes. Subspace width uses integer division.

Excludes: Index headers, graph hierarchy, allocator overhead, codebooks, recall, and query runtime.

References

Derivation: each layout is a sum of fixed-width fields multiplied by its record count. This storage accounting does not model an ANN search algorithm.

Examples
>>> from mlsysim import Q_
>>> result = AnnFootprintModel().solve(100, 16, Q_("4 byte"), 4, Q_("4 byte"), 4, 16, Q_("1 byte"), Q_("4 byte"))
>>> result.graph_total > result.raw > result.codes
True

Bm25PairModel

engine.solvers.retrieval.Bm25PairModel()

One query term’s BM25 score in a short and a long document of one corpus.

The same IDF multiplies both; term-frequency saturation (k1) and length normalization (b) decide the order. Lengths and frequencies are analyzer-term counts, not model tokens.

Methods

Name Description
solve Compare one query term in two documents under the same BM25 parameters.
solve
engine.solvers.retrieval.Bm25PairModel.solve(
    documents,
    matching_documents,
    average_length,
    short_length,
    short_frequency,
    long_length,
    long_frequency,
    k1,
    b,
)

Compare one query term in two documents under the same BM25 parameters.

Parameters
Name Type Description Default
documents int Positive corpus document count N. required
matching_documents int Documents containing the term, between zero and documents. required
average_length float Positive mean document length in analyzer terms. required
short_length float Positive analyzer-term count in the first document. required
short_frequency float Nonnegative occurrences of the query term in the first document. required
long_length float Positive analyzer-term count in the second document. required
long_frequency float Nonnegative occurrences of the query term in the second document. required
k1 float Positive BM25 term-frequency saturation parameter. required
b float Length-normalization strength in [0, 1]. required
Returns
Name Type Description
Bm25PairResult Dimensionless shared inverse document frequency, length normalizers, and term scores.
See Also

mlsysim.physics.retrieval.calc_bm25_term_score : One-term BM25 score under the selected normalizer.

Notes

Equation: score = IDF*f*(k1+1)/(f+k1*(1-b+b*length/average_length)); IDF = log(1+(N-n+0.5)/(n+0.5)).

Assumptions: Both documents use one corpus and analyzer. The nonnegative IDF variant is an implementation convention.

Excludes: Multiple query terms, tokenizer differences, learned relevance, and a probability interpretation of scores.

References

Robertson and Zaragoza (2009), The Probabilistic Relevance Framework: BM25 and Beyond. https://doi.org/10.1561/1500000019 This solver uses the stated nonnegative IDF variant.

Examples
>>> result = Bm25PairModel().solve(100, 10, 50, 25, 2, 100, 2, 1.2, 0.75)
>>> result.short_score > result.long_score
True

ExactScanFloorModel

engine.solvers.retrieval.ExactScanFloorModel()

The time floor of an exact scan: every eligible vector streamed once.

N d b bytes cross memory and 2 N d FLOPs score them, so the intensity is 2 / b FLOP per byte at any corpus size. Below the host’s ridge point bandwidth sets the floor, t >= N d b / B. A floor, not a forecast: query encoding, record fetches, and caches are left out.

Methods

Name Description
solve Compute separate bandwidth and arithmetic floors for an exact vector scan.
solve
engine.solvers.retrieval.ExactScanFloorModel.solve(
    host,
    vectors,
    dimension,
    bytes_per_value,
    searches=None,
    filtered_vectors=None,
    budget=None,
)

Compute separate bandwidth and arithmetic floors for an exact vector scan.

Parameters
Name Type Description Default
host HardwareNode Hardware with positive memory bandwidth in bytes/s and compute peak in flop/s. required
vectors int Positive number of eligible corpus vectors. required
dimension int Positive values per vector. required
bytes_per_value Quantity Positive storage bytes per scalar value. required
searches int Nonnegative sequential scans for cumulative stream time; None omits it. None
filtered_vectors int Nonnegative vector count after filtering; None omits the filtered stream floor. None
budget Quantity Nonnegative stream-time allowance in seconds; None omits the vector-budget calculation. None
Returns
Name Type Description
ExactScanFloorResult Storage bytes, FLOPs, FLOP/byte intensity, separate times in milliseconds, and optional aggregate time in seconds.
Notes

Equation: bytes = N*d*b; work = 2*N*d; stream_time = bytes/BW; math_time = work/peak.

Assumptions: Every eligible dense vector is read once, a dot product uses two FLOPs per value, and peak bandwidth/compute rates apply separately.

Excludes: Query encoding, launch overhead, cache reuse, result fetches, and a claim that stream_time alone predicts total latency.

References

Derivation: count dense dot-product operations and compulsory input bytes; divide each resource demand by its peak service rate.

Examples
>>> from mlsysim import Hardware, Q_
>>> result = ExactScanFloorModel().solve(Hardware.Cloud.H100, 100, 16, Q_("2 byte"))
>>> result.flops.magnitude == 2 * 100 * 16
True

GraphNeighborhoodModel

engine.solvers.retrieval.GraphNeighborhoodModel()

A typed traversal’s neighborhood, as an exact tree, against a token allowance.

Every node has c children and no node repeats, so depth d reaches (c^(d+1) - 1) / (c - 1) nodes; each serializes to a fixed size plus one envelope for edges. Real code graphs have cycles and hubs, so the count bounds distinct nodes from above.

Methods

Name Description
solve Compare an exact tree neighborhood with a serialized context budget.
solve
engine.solvers.retrieval.GraphNeighborhoodModel.solve(
    window,
    output_reserve,
    other_input,
    children,
    depth,
    node_tokens,
    envelope_tokens,
)

Compare an exact tree neighborhood with a serialized context budget.

Parameters
Name Type Description Default
window int Positive context capacity in tokens. required
output_reserve int Nonnegative answer reserve in tokens. required
other_input int Nonnegative tokens consumed by other input roles. required
children int Nonnegative fixed child count per node. required
depth int Traversal depth of at least one, allowing comparison with the preceding depth. required
node_tokens int Positive serialized tokens per node. required
envelope_tokens int Nonnegative shared edge-envelope tokens; must fit the remaining allowance. required
Returns
Name Type Description
GraphNeighborhoodResult Integer node counts, token demands by depth, and token-budget packing capacity.
See Also

mlsysim.physics.retrieval.calc_full_tree_nodes : Geometric count of nodes through a fixed depth.

Notes

Equation: nodes(d)=sum(children^j for j=0..d); required(d)=nodes(d)*node_tokens+envelope_tokens.

Assumptions: No node is revisited and every node has exactly children descendants. A graph upper-bound interpretation additionally requires children to bound out-degree.

Excludes: Cycles, deduplication, variable degree, and an upper-bound claim when children is merely an average.

References

Derivation: sum each tree level as a geometric series, then apply fixed serialization bytes in token units to each reached node.

Examples
>>> result = GraphNeighborhoodModel().solve(100, 10, 10, 2, 2, 5, 5)
>>> result.nodes > result.shallow_nodes and result.packed <= result.allowance
True

IndexMaintenanceModel

engine.solvers.retrieval.IndexMaintenanceModel()

Who pays to keep an index current after an edit.

Synchronous re-indexing parses each changed file, updates postings per chunk, and embeds the chunks (in local batches, one remote request per batch, or one remote request per file) before the write returns. Exclusion records one entry per changed file and adds one lookup per candidate at query time. Durable commit and coordination, which both strategies share, are excluded.

Methods

Name Description
solve Account for synchronous re-indexing versus exclusion-list maintenance.
solve
engine.solvers.retrieval.IndexMaintenanceModel.solve(
    files,
    chunks_per_file,
    parse_time,
    postings_time,
    local_batch_time,
    batch,
    remote_request_time,
    exclusion_write_time,
    candidates,
    exclusion_check_time,
)

Account for synchronous re-indexing versus exclusion-list maintenance.

Parameters
Name Type Description Default
files int Positive edited-file count. required
chunks_per_file int Positive embedding chunks produced per file. required
parse_time Quantity Nonnegative parsing duration per file in seconds. required
postings_time Quantity Nonnegative lexical update duration per chunk in seconds. required
local_batch_time Quantity Nonnegative embedding duration per local batch in seconds. required
batch int Positive maximum chunks per embedding batch. required
remote_request_time Quantity Nonnegative remote embedding duration per request in seconds. required
exclusion_write_time Quantity Positive exclusion-write duration per edited file in seconds. required
candidates int Nonnegative retrieved candidates to check against exclusions. required
exclusion_check_time Quantity Nonnegative exclusion lookup duration per candidate in seconds. required
Returns
Name Type Description
IndexMaintenanceResult Integer work counts, milliseconds for batched paths, seconds for per-file remote path, and microseconds for exclusion work.
Notes

Equation: chunks=files*chunks_per_file; batches=ceil(chunks/batch); reindex=files*parse+chunks*postings+batches*embed; exclusion_write=files*write.

Assumptions: Costs add serially. A remote per-file request embeds all chunks of that file; a batched request follows the supplied batch limit.

Excludes: Durable commit, concurrent workers, request queueing, and retrieval-quality effects of exclusion.

References

Derivation: enumerate parsing, postings, and embedding operations on the write path; exclusion instead charges a per-file write and per-candidate read.

Examples
>>> from mlsysim import Q_
>>> t = Q_("1 ms")
>>> result = IndexMaintenanceModel().solve(2, 3, t, t, t, 4, t, t, 5, t)
>>> result.chunks == 6 and result.batches == 2
True

RetrievalPayloadModel

engine.solvers.retrieval.RetrievalPayloadModel()

Whole serialized records in the retrieval allowance of one model call.

The allowance is what the window leaves after the output reserve and every other input role: S_max - K_max - S_other. The envelope is paid once; each record pays its body and its metadata, so the count is floor((allowance - envelope) / (body + metadata)). A count of bodies alone, floor(allowance / body), overshoots once serialized.

Methods

Name Description
solve Fit serialized retrieval records into one model-call allowance.
solve
engine.solvers.retrieval.RetrievalPayloadModel.solve(
    window,
    output_reserve,
    other_input,
    envelope,
    body,
    metadata,
    metadata_scale=None,
)

Fit serialized retrieval records into one model-call allowance.

Parameters
Name Type Description Default
window int Positive context-window token count. required
output_reserve int Nonnegative tokens reserved for the generated answer. required
other_input int Nonnegative tokens occupied by other input roles. required
envelope int Nonnegative tokens for shared serialization structure; must fit the remaining allowance. required
body int Positive body tokens per record. required
metadata int Nonnegative metadata tokens per record. required
metadata_scale int Nonnegative metadata multiplier for a counterfactual; None omits it. None
Returns
Name Type Description
RetrievalPayloadResult Integer token counts, whole-record capacity, and overshoot of body-only accounting.
See Also

mlsysim.physics.context.calc_observation_budget : Context left after answer and other-input reserves. mlsysim.physics.retrieval.calc_records_that_fit : Whole records admitted after fixed serialization overhead.

Notes

Equation: allowance = window-output_reserve-other_input; records = floor((allowance-envelope)/(body+metadata)).

Assumptions: Fixed token counts from a caller-selected tokenizer and identical record sizes. Inputs leave room for the shared envelope.

Excludes: Tokenization, variable record lengths, relevance ranking, and clamping overcommitted envelopes to zero records.

References

Derivation: reserve fixed context terms, then divide the remaining integer token budget by the serialized record size.

Examples
>>> result = RetrievalPayloadModel().solve(100, 20, 10, 5, 10, 5)
>>> result.used <= result.allowance and result.body_only_used > result.allowance
True

StorageAmplificationModel

engine.solvers.retrieval.StorageAmplificationModel()

Auxiliary bytes and re-index time of a lexical-plus-graph store and a lexical-plus-dense store over the same files.

Both keep postings. The first adds structural edges per file and re-parses edited files; the second chunks the files, stores one vector and one set of graph-index links per chunk, and re-embeds edited chunks.

Methods

Name Description
solve Compare auxiliary storage and serial re-indexing costs for two layouts.
solve
engine.solvers.retrieval.StorageAmplificationModel.solve(
    files,
    tokens_per_file,
    bytes_per_file,
    postings_per_token,
    edges_per_file,
    edge_bytes,
    passage_tokens,
    dimension,
    bytes_per_value,
    links_per_node,
    link_bytes,
    edited_files,
    lexical_rate,
    parse_rate,
    embed_rate,
)

Compare auxiliary storage and serial re-indexing costs for two layouts.

Parameters
Name Type Description Default
files int Positive corpus file count. required
tokens_per_file int Positive fixed token count per file. required
bytes_per_file Quantity Positive raw storage bytes per file. required
postings_per_token Quantity Nonnegative posting storage bytes per token. required
edges_per_file int Nonnegative structural graph edges per file. required
edge_bytes Quantity Nonnegative storage bytes per structural edge. required
passage_tokens int Positive tokens per dense passage; incomplete trailing passages are dropped. required
dimension int Positive values per dense vector. required
bytes_per_value Quantity Positive bytes per vector value. required
links_per_node int Nonnegative ANN graph links per passage. required
link_bytes Quantity Nonnegative storage bytes per ANN link. required
edited_files int Positive files to re-index. required
lexical_rate float Positive files per second for lexical re-indexing. required
parse_rate float Positive files per second for structural parsing. required
embed_rate float Positive passages per second for dense embedding. required
Returns
Name Type Description
StorageAmplificationResult Storage quantities, integer passage counts, re-indexing milliseconds, and dimensionless storage/time amplification ratios.
Notes

Equation: postings=files*tokens_per_file*postings_per_token; chunks=floor(total_tokens/passage_tokens); graph_layout=postings+edges; dense_layout=postings+vectors+links.

Assumptions: Fixed per-file sizes; serial lexical-plus-parse or lexical-plus-embedding work; positive auxiliary-storage and time denominators. Passage counts are taken over aggregate tokens.

Excludes: Per-file boundary padding, overlapping passages, compression, concurrency, and retrieval effectiveness.

References

Derivation: sum stored field widths for each layout and divide changed work counts by the caller-supplied serial service rates.

Examples
>>> from mlsysim import Q_
>>> b = Q_("1 byte")
>>> result = StorageAmplificationModel().solve(10, 100, 1000*b, b, 2, 4*b, 50, 16, 4*b, 4, 4*b, 2, 10.0, 5.0, 2.0)
>>> result.chunks == 10 * 100 // 50
True
Back to top