-
Notifications
You must be signed in to change notification settings - Fork 46
Add tree cache to ASTAnalyzer and optimize vector projection #196
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
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
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 |
|---|---|---|
|
|
@@ -18,16 +18,28 @@ | |
| _STOP, | ||
| ) | ||
|
|
||
| # Try to use numpy for faster vector operations (10-50x speedup) | ||
| try: | ||
| import numpy as np | ||
| _NUMPY_AVAILABLE = True | ||
| except ImportError: | ||
| np = None # type: ignore | ||
| _NUMPY_AVAILABLE = False | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Mini vector projection cache | ||
| # --------------------------------------------------------------------------- | ||
| _MINI_PROJ_CACHE: dict[tuple[int, int, int], list[list[float]]] = {} | ||
| # Cache stores numpy arrays when numpy is available, else nested lists | ||
| _MINI_PROJ_CACHE: dict[tuple[int, int, int], Any] = {} | ||
|
|
||
|
|
||
| def _get_mini_proj( | ||
| in_dim: int, out_dim: int, seed: int | None = None | ||
| ) -> list[list[float]]: | ||
| """Get or create a random projection matrix for mini vectors.""" | ||
| ) -> Any: | ||
| """Get or create a random projection matrix for mini vectors. | ||
|
|
||
| Returns numpy array if numpy is available, else nested list. | ||
| """ | ||
| import math | ||
| import random | ||
|
|
||
|
|
@@ -38,31 +50,54 @@ def _get_mini_proj( | |
| rnd = random.Random(s) | ||
| scale = 1.0 / math.sqrt(out_dim) | ||
| # Dense Rademacher matrix (+/-1) scaled; good enough for fast gating | ||
| M = [ | ||
| [scale * (1.0 if rnd.random() < 0.5 else -1.0) for _ in range(out_dim)] | ||
| for _ in range(in_dim) | ||
| ] | ||
| if _NUMPY_AVAILABLE: | ||
| # Use numpy for faster matrix operations | ||
| # Generate same values as pure Python for reproducibility | ||
| M_list = [ | ||
| [scale * (1.0 if rnd.random() < 0.5 else -1.0) for _ in range(out_dim)] | ||
| for _ in range(in_dim) | ||
| ] | ||
| M = np.array(M_list, dtype=np.float32) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| else: | ||
| M = [ | ||
| [scale * (1.0 if rnd.random() < 0.5 else -1.0) for _ in range(out_dim)] | ||
| for _ in range(in_dim) | ||
| ] | ||
| _MINI_PROJ_CACHE[key] = M | ||
| return M | ||
|
|
||
|
|
||
| def project_mini(vec: list[float], out_dim: int | None = None) -> list[float]: | ||
| """Project a dense vector to a compact mini vector using random projection.""" | ||
| """Project a dense vector to a compact mini vector using random projection. | ||
|
|
||
| Uses numpy when available for 10-50x speedup, falls back to pure Python. | ||
| """ | ||
| if not vec: | ||
| return [0.0] * (int(out_dim or MINI_VEC_DIM)) | ||
| od = int(out_dim or MINI_VEC_DIM) | ||
| M = _get_mini_proj(len(vec), od) | ||
| out = [0.0] * od | ||
| # y = x @ M | ||
| for i, val in enumerate(vec): | ||
| if val == 0.0: | ||
| continue | ||
| row = M[i] | ||
| for j in range(od): | ||
| out[j] += val * row[j] | ||
| # L2 normalize to keep scale consistent | ||
| norm = (sum(x * x for x in out) or 0.0) ** 0.5 or 1.0 | ||
| return [x / norm for x in out] | ||
|
|
||
| if _NUMPY_AVAILABLE: | ||
| # Fast path: numpy matrix multiply + normalize | ||
| x = np.array(vec, dtype=np.float32) | ||
| out = x @ M # (in_dim,) @ (in_dim, out_dim) -> (out_dim,) | ||
| norm = np.linalg.norm(out) | ||
| if norm > 0: | ||
| out = out / norm | ||
| return out.tolist() | ||
| else: | ||
| # Fallback: pure Python implementation | ||
| out = [0.0] * od | ||
| # y = x @ M | ||
| for i, val in enumerate(vec): | ||
| if val == 0.0: | ||
| continue | ||
| row = M[i] | ||
| for j in range(od): | ||
| out[j] += val * row[j] | ||
| # L2 normalize to keep scale consistent | ||
| norm = (sum(x * x for x in out) or 0.0) ** 0.5 or 1.0 | ||
| return [x / norm for x in out] | ||
|
|
||
|
|
||
| def _split_ident_lex(s: str) -> List[str]: | ||
|
|
||
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.
flush_upserts()callsget_collection(), but Qdrant’swait=Falsesemantics are “confirmed received” rather than “applied”, so this may not actually guarantee prior async upserts are committed before reads.🤖 Was this useful? React with 👍 or 👎