-
Notifications
You must be signed in to change notification settings - Fork 2
Add support for LeanVec-OOD #16
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Copyright (C) 2025 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Generate LeanVec-OOD matrices.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| import numpy.typing as npt | ||
| import svs | ||
| import typer | ||
|
|
||
| from . import consts, merge | ||
|
|
||
|
|
||
| def main( | ||
| vecs_file: Path, | ||
| train_query_file: Path, | ||
| max_vectors: int = consts.DEFAULT_LEANVEC_TRAIN_MAX_VECTORS, | ||
| leanvec_dims: int = consts.DEFAULT_LEANVEC_DIMS, | ||
| out_dir: Path = Path(), | ||
| ) -> None: | ||
| out_dir.mkdir(parents=True, exist_ok=True) | ||
| (data_matrix, query_matrix), (leanvec_dims_effective, _) = ( | ||
| generate_leanvec_matrices( | ||
| vecs_file, train_query_file, max_vectors, leanvec_dims | ||
| ) | ||
| ) | ||
| data_matrix_path, query_matrix_path = save_leanvec_matrices( | ||
| vecs_file, | ||
| train_query_file, | ||
| max_vectors, | ||
| leanvec_dims_effective, | ||
| data_matrix, | ||
| query_matrix, | ||
| out_dir, | ||
| ) | ||
| print("Saved LeanVec matrices:", data_matrix_path, query_matrix_path) | ||
|
|
||
|
|
||
| def generate_leanvec_matrices( | ||
| vecs_file: Path, | ||
| train_query_file: Path, | ||
| max_vectors: int = consts.DEFAULT_LEANVEC_TRAIN_MAX_VECTORS, | ||
| leanvec_dims: int | None = None, | ||
| ) -> tuple[tuple[npt.NDArray, npt.NDArray], tuple[int, int]]: | ||
mihaic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Generate LeanVec matrices from base vectors and query vectors.""" | ||
| base_vectors = merge.read_vecs(vecs_file, max_vectors) | ||
| query_vectors = merge.read_vecs(train_query_file) | ||
| dim = base_vectors.shape[1] | ||
| if leanvec_dims is None: | ||
| leanvec_dims = consts.DEFAULT_LEANVEC_DIMS | ||
| if leanvec_dims < 0: | ||
| leanvec_dims = dim // -leanvec_dims | ||
| return svs.compute_leanvec_matrices( | ||
| base_vectors, query_vectors, leanvec_dims | ||
| ), (leanvec_dims, max_vectors) | ||
|
|
||
|
|
||
| def save_leanvec_matrices( | ||
| vecs_file: Path, | ||
| train_query_file: Path, | ||
| max_vectors: int, | ||
| leanvec_dims: int, | ||
| data_matrix: npt.NDArray, | ||
| query_matrix: npt.NDArray, | ||
| out_dir: Path, | ||
| ) -> tuple[Path, Path]: | ||
| """Save LeanVec matrices to files.""" | ||
| name_components = [ | ||
| vecs_file.name, | ||
| train_query_file.name, | ||
| str(leanvec_dims), | ||
| ] | ||
| if max_vectors > 0: | ||
mihaic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| name_components.append(str(max_vectors)) | ||
| base_name = "__".join(name_components) | ||
| data_matrix_path = out_dir / (base_name + ".data.npy") | ||
| query_matrix_path = out_dir / (base_name + ".query.npy") | ||
| np.save(data_matrix_path, data_matrix) | ||
| np.save(query_matrix_path, query_matrix) | ||
| return data_matrix_path, query_matrix_path | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # https://github.com/fastapi/typer/issues/341 | ||
| typer.main.get_command_name = lambda name: name | ||
| typer.run(main) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.