-
Notifications
You must be signed in to change notification settings - Fork 159
Separate the act of resolving vs loading a dataset #717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...les/src/main/java/io/github/jbellis/jvector/example/benchmarks/datasets/DataSetFiles.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.github.jbellis.jvector.example.benchmarks.datasets; | ||
|
|
||
| import java.nio.file.Path; | ||
|
|
||
| class DataSetFiles { | ||
| private final Path baseFvecsPath; | ||
| private final Path queryFvecsPath; | ||
| private final Path gtIvecsPath; | ||
|
|
||
| DataSetFiles(Path baseFvecsPath, Path queryFvecsPath, Path gtIvecsPath) { | ||
| this.baseFvecsPath = baseFvecsPath; | ||
| this.queryFvecsPath = queryFvecsPath; | ||
| this.gtIvecsPath = gtIvecsPath; | ||
| } | ||
|
|
||
| Path getBaseFvecsPath() { | ||
| return baseFvecsPath; | ||
| } | ||
|
|
||
| Path getQueryFvecsPath() { | ||
| return queryFvecsPath; | ||
| } | ||
|
|
||
| Path getGtIvecsPath() { | ||
| return gtIvecsPath; | ||
| } | ||
| } |
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
139 changes: 139 additions & 0 deletions
139
...s/src/main/java/io/github/jbellis/jvector/example/benchmarks/datasets/DataSetInfoMFD.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * Copyright DataStax, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.github.jbellis.jvector.example.benchmarks.datasets; | ||
|
|
||
| import io.github.jbellis.jvector.example.util.SiftLoader; | ||
| import io.github.jbellis.jvector.vector.VectorSimilarityFunction; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /// A lightweight, lazy handle that separates *identifying* a dataset from *loading* its data. | ||
| /// | ||
| /// Metadata such as the dataset name and similarity function are available immediately | ||
| /// without any I/O, while the expensive work of reading vectors, deduplicating, scrubbing | ||
| /// zero vectors, and normalizing is deferred until the first call to {@link #getDataSet()}. | ||
| /// | ||
| /// This design allows callers to enumerate or filter available datasets cheaply, and | ||
| /// ensures that the full load-and-scrub pipeline runs at most once per handle thanks to | ||
| /// thread-safe caching. | ||
| /// | ||
| /// Instances are created by {@link DataSetLoader} implementations; callers obtain them | ||
| /// through {@link DataSets#loadDataSet(String)}. | ||
| /// | ||
| /// ### Typical usage | ||
| /// ```java | ||
| /// DataSetInfo info = DataSets.loadDataSet("ada002-100k").orElseThrow(); | ||
| /// | ||
| /// // Cheap — no vectors loaded yet | ||
| /// System.out.println(info.getName()); | ||
| /// System.out.println(info.similarityFunction()); | ||
| /// | ||
| /// // First call triggers full load; subsequent calls return the cached DataSet | ||
| /// DataSet ds = info.getDataSet(); | ||
| /// ``` | ||
| /// | ||
| /// @see DataSet | ||
| /// @see DataSetLoader | ||
| /// @see DataSets | ||
| public class DataSetInfoMFD implements DataSetInfo { | ||
| private final DataSetFiles dsFiles; | ||
| private final DataSetProperties baseProperties; | ||
| private volatile DataSet cached; | ||
|
|
||
| /// Creates a new dataset info handle. | ||
| /// | ||
| /// The dataset will not be loaded until {@link #getDataSet()} is called for the first time. | ||
| /// | ||
| /// @param baseProperties the dataset properties (name, similarity function, etc.) | ||
| /// @param dataSetFiles the bundle of base/query/gt paths required to load the dataset | ||
| public DataSetInfoMFD(DataSetProperties baseProperties, DataSetFiles dataSetFiles) { | ||
| this.baseProperties = baseProperties; | ||
| this.dsFiles = dataSetFiles; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Optional<VectorSimilarityFunction> similarityFunction() { | ||
| return baseProperties.similarityFunction(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public int numVectors() { | ||
| return this.baseProperties.numVectors(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public String getName() { | ||
| return baseProperties.getName(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public boolean isNormalized() { | ||
| return baseProperties.isNormalized(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public boolean isZeroVectorFree() { | ||
| return baseProperties.isZeroVectorFree(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public boolean isDuplicateVectorFree() { | ||
| return baseProperties.isDuplicateVectorFree(); | ||
| } | ||
|
|
||
| /// Returns the fully loaded and scrubbed {@link DataSet}. | ||
| /// | ||
| /// On the first invocation this triggers the deferred load pipeline, which may involve | ||
| /// reading large vector files from disk, deduplication, zero-vector removal, and | ||
| /// normalization. The result is cached so that subsequent calls return immediately. | ||
| /// | ||
| /// This method is thread-safe: concurrent callers will block until the first load | ||
| /// completes, after which all callers share the same cached instance. | ||
| /// | ||
| /// @return the ready-to-use {@link DataSet} | ||
| public DataSet getDataSet() { | ||
| if (cached == null) { | ||
| synchronized (this) { | ||
| if (cached == null) { | ||
| var baseVectors = SiftLoader.readFvecs(dsFiles.getBaseFvecsPath().toString()); | ||
| var queryVectors = SiftLoader.readFvecs(dsFiles.getQueryFvecsPath().toString()); | ||
| var gtVectors = SiftLoader.readIvecs(dsFiles.getGtIvecsPath().toString()); | ||
| cached = DataSetUtils.processDataSet(baseProperties.getName(), baseProperties, baseVectors, queryVectors, gtVectors); | ||
| } | ||
| } | ||
| } | ||
| return cached; | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Datasets should never be scrubbed online. Is this comment out of date?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, the comment is still accurate, and preserves the existing behavior.
DataSetswith theLEGACY_SCRUBconfig continue to be scrubbed on load (byprocessDataSet)