-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: [performance improvement] #181
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
stffns
wants to merge
3
commits into
main
Choose a base branch
from
bolt-optimize-assign-l2-14922254172996810320
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 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
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
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.
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.
π― Functional Correctness | π‘ Minor | β‘ Quick win
π§© Analysis chain
π Script executed:
Repository: stffns/snapvec
Length of output: 152
π Script executed:
Repository: stffns/snapvec
Length of output: 270
π Script executed:
Repository: stffns/snapvec
Length of output: 270
π Script executed:
Repository: stffns/snapvec
Length of output: 267
π Script executed:
Repository: stffns/snapvec
Length of output: 10386
π Script executed:
Repository: stffns/snapvec
Length of output: 416
π Web query:
NumPy documentation sum partial pairwise summation float32 accumulate einsumπ‘ Result:
NumPy employs a partial pairwise summation algorithm to improve the numerical precision of summation operations while maintaining high performance [1][2][3]. Unlike naive summation, which adds numbers sequentially and accumulates rounding errors at every step, partial pairwise summation reduces the accumulation of these errors, which is particularly beneficial for lower-precision formats like float32 [1][2]. Key details regarding this implementation include: 1. Performance vs. Precision: NumPy does not use this approach for all summation cases. To prioritize speed, partial pairwise summation is typically applied only when summing along the fast (contiguous) axis in memory [1][4][5]. When summing along a non-contiguous axis, the operation may revert to a more naive accumulation [1][6]. 2. Scope of Application: This improved precision is generally available for np.sum and np.add.reduce [1]. It is not universal across all reduction operations [4]. 3. Einsum: The np.einsum function does not inherently guarantee the same partial pairwise summation optimization as np.sum [4]. If numerical precision is critical, especially when dealing with large float32 arrays, users are often advised to convert data to float64 or perform summation explicitly along a contiguous axis using np.sum [1][4]. 4. Alternatives: For scenarios requiring maximum precision, Python's math.fsum function provides a more accurate approach than NumPy's summation methods, albeit at the cost of slower performance [1][3]. In summary, while NumPy optimizes for both speed and precision, the degree of precision can vary based on memory layout and axis selection [1][7]. When precision issues arise, explicitly casting to float64 or ensuring the summation axis is contiguous are recommended practices [1][4].
Citations:
np.sumgives inconsistent results withnp.float32reshaped arraysΒ numpy/numpy#20458π Script executed:
Repository: stffns/snapvec
Length of output: 397
π Web query:
GitHub NumPy einsum dot product implementation sum loop array_api dotpairwiseπ‘ Result:
NumPy's implementation of einsum and its performance relative to other operations like dot product has evolved significantly, particularly with the introduction of optimized code paths for contraction [1][2]. Implementation Details NumPy's einsum is primarily implemented in C, with specialized "sum of product" kernels [3][4]. Historically, einsum parsed the index string and constructed an nditer object to perform iteration, which was often slower than specialized BLAS-backed operations like np.dot or np.matmul [5][2]. While these specialized kernels include performance optimizations such as loop unrolling and SIMD (SSE) instructions for specific stride patterns, they did not automatically leverage BLAS for general cases [3][4][5]. Performance and Optimization The significant performance gap between einsum and dot/matmul in many use cases is attributed to einsum's initial lack of BLAS integration for arbitrary contractions [5][1]. However, the inclusion of the optimize=True argument enables advanced contraction path searching [6][2]. When optimize=True is used, einsum can identify opportunities to delegate contractions to tensordot (which utilizes BLAS) or perform them in a more computationally efficient order [1][2]. Recent updates have further bridged this gap, allowing einsum to use BLAS more effectively in cases that previously defaulted to slower, unoptimized loops [1][7]. Pairwise Operations and the Array API Regarding the Python array API, NumPy (version 2.0+) includes built-in support for the array API standard in its main namespace [8][9]. The standard includes vecdot for vector dot products and matmul for matrix multiplication [10]. For pairwise dot products of rows in two matrices, one common, efficient approach is to use element-wise multiplication followed by a sum (e.g., (a * b).sum(axis=1)) [11]. Users writing code for broader array library compatibility should prioritize these standard-defined functions (matmul, vecdot, tensordot) over specialized or implementation-specific hacks [10][8]. In summary, while einsum remains a powerful tool for complex tensor contractions, users should set optimize=True for performance-critical tasks [6][2]. For standard linear algebra operations, preferred alternatives like dot, matmul, or vecdot (for array API compliance) should be used to leverage optimized BLAS kernels automatically [10][12].
Citations:
einsumso much slower than other libraries?Β numpy/numpy#22604Require exact assignment parity across reduction paths.
snapvec/_kmeans.py:assign_l2replaces row-wisefloat32sums withnp.einsumbeforeargmin(1), whilesnapvec/_ivfpq.py:344uses those assignments for residual codebook training. Add near-tie regression coverage and document whetherd2.argmin(1)must match the previoussumbehavior.π€ Prompt for AI Agents
Source: MCP tools