-
Notifications
You must be signed in to change notification settings - Fork 0
Embeddings embeddingindex fromblock
Development build. This page describes
main, not a released package. The latest published Lodestar.Embeddings is 0.4.0 — read its documentation.
Builds an index from a contiguous block of vectors, in one copy.
public static EmbeddingIndex FromBlock(ReadOnlySpan<float> block, int dimension, BlockNormalization normalization, IReadOnlyList<string> ids = null)Parameters — block is the vectors laid out row after row in C order, so vector i occupies
[i * dimension, (i + 1) * dimension); its length must be a multiple of dimension, and an empty
block makes an empty index. dimension is the length every vector has, at least 1.
normalization says what is to be done about the block and what the index's own normalization
flag becomes — BlockNormalization has the three answers. ids is one id
per vector, or null for an anonymous index; it is copied, never retained, and a null entry
inside it is a vector without an id.
Returns — a new EmbeddingIndex holding block.Length / dimension vectors, ready to
Search with no further work.
Exceptions — ArgumentOutOfRangeException when dimension is below 1, or normalization is
not one of the enum's values — a value reached by a cast would otherwise be read as
AlreadyNormalized and score wrongly. ArgumentException when block's length is not a multiple
of dimension, or ids holds a number of entries other than the vector count; both messages name
the two numbers that disagree.
Example — two unit vectors and their ids, taken in one call.
using Lodestar.Embeddings.Search;
float[] block = { 1f, 0f, 0f, 1f };
var index = EmbeddingIndex.FromBlock(block, dimension: 2, BlockNormalization.AlreadyNormalized, ["east", "north"]);
int count = index.Count; // => 2
string first = index.GetId(0)!; // => eastRemarks — this is what a caller holding a whole corpus reaches for: a .npy block, a model's
output, a column read out of a store. The block is copied, so the caller's array is neither
retained nor modified and may be reused or freed the moment the call returns.
Replaying the same corpus through EmbeddingIndex.Add is the route that
existed before, and it costs three times the read that produced the block: Add copies one vector,
normalizes it, and grows a backing store that doubles on the way up
(#474). This allocates the store once, at the
size the block already tells it, and copies once. Where the copy itself is the thing being paid
for and the caller can give the array up for good,
EmbeddingIndex.FromOwnedBlock skips it —
decision 0056
has the trade, and the short version is that this one asks nothing of the caller.
Normalization is decided here and cannot be changed afterwards, because the index's flag
governs the query as well as the store. Normalize normalizes the copy;
AlreadyNormalized stores it bit for bit and is a promise the caller keeps — an unnormalized block
taken that way scores wrong and raises nothing; Off leaves both sides alone, which is a raw dot
product rather than a cosine.
A block holding NaN or an infinity is accepted here, exactly as Add
accepts one — the two ingest paths cannot disagree about what an index may hold. It is
Save that refuses it.
Applies to — net10.0, netstandard2.0.
See also — BlockNormalization,
EmbeddingIndex.FromOwnedBlock,
EmbeddingIndex.Add, EmbeddingIndex.
- 0001-target-framework
- 0002-unicode-comparison-unit
- 0003-provenance-and-licensing
- 0004-levenshtein-myers-backlog
- 0005-hamming-jellyfish-divergence
- 0006-ratcliff-autojunk
- 0007-metaphone-scope
- 0008-italian-enza-nltk-divergence
- 0009-sample-consumes-a-local-feed
- 0010-stop-word-list-provenance
- 0011-persistence-format
- 0012-per-package-versioning
- 0013-sentencepiece-parity-scope
- 0014-precompiled-normalizer
- 0015-sonar-rules-in-the-build
- 0016-metrics-package-placement
- 0017-bpe-parity-scope
- 0018-multiclass-roc-auc-parallelism-is-opt-in
- 0019-the-net-analysers-run-in-the-build-too
- 0020-normalize-is-a-projection-not-a-parameter
- 0021-multioutput-is-a-method-not-an-enum
- 0022-added-token-matching-flags
- 0023-byte-level-decode-substitutes
- 0024-weighted-median-averages-within-scikit-learns-epsilon
- 0025-quickselect-replaces-a-full-sort-for-the-median
- 0026-r2-and-explainedvariance-split-their-undefined-cases-differently
- 0027-r2-and-explainedvariance-vectorize-only-a-single-output
- 0028-log1p-is-kahans-identity-not-math-log-1-plus-x
- 0029-balanced-accuracy-adjusted-is-left-to-ieee-754-at-the-edge
- 0030-cohen-kappa-keeps-scikit-learns-expected-matrix-orientation
- 0031-nosamplecorrect-mirrors-numpys-float64-upcast
- 0032-fbeta-substitutes-tp-predicted-and-support-algebraically
- 0033-compensated-sum-is-neumaiers-variant
- 0034-dropout-is-refused-for-want-of-a-user
- 0035-a-null-pre-split-is-removed-with-invert-not-isolated
- 0036-a-member-may-ship-without-an-oracle-if-it-says-so
- 0037-the-guards-run-before-the-commit
- 0038-the-gate-confronts-an-exception-tag-with-the-page-that-documents-it
- 0039-mutual-information-returns-zero-on-an-empty-input
- 0040-a-curve-is-a-sealed-class-per-curve
- 0041-one-sample-file-per-public-class
- 0042-phonetic-encoders-refuse-a-null-word
- 0043-the-equality-table-is-sized-to-the-pattern
- 0044-compression-belongs-to-the-caller
- 0045-a-console-call-carries-its-reason-on-the-line
- 0046-check-adr-immutable-runs-in-ci-only
- 0047-one-gate-per-kernel-not-one-per-alphabet
- 0048-the-gate-depends-on-the-kernel-and-the-alphabet
- 0049-two-gates-per-kernel-tested-where-the-width-is-known
- 0050-the-sentencepiece-bpe-lineage-stays-a-bpe-model
- 0051-the-save-paths-cost-is-the-buffer-not-the-encoding
- 0052-pre-sizing-the-artifact-file-buys-nothing-on-a-delayed-allocation-filesystem
- 0053-the-payload-buffer-is-not-pooled-because-residency-outlives-the-load
- 0054-the-payload-buffer-is-pooled-after-all-because-the-collection-is-the-cost
- 0055-the-artifact-gets-a-binary-sidecar-once-a-block-can-be-ingested-whole
- 0056-a-block-may-be-adopted-and-the-invariant-is-the-callers-to-keep
- 0057-the-npy-read-serves-a-stream-and-a-buffer-differently
- benchmark_latest
- decisions
- equivalence
- matplotlib
- migration
- nightly_run
- numpy
- pandas
- performance
- pytorch
- seaborn
- sklearn
- statsmodels