Skip to content

Update hnsw_build_asymptotic benchmark#6299

Merged
xzfc merged 2 commits intodevfrom
update-hnsw_build_asymptotic
Apr 7, 2025
Merged

Update hnsw_build_asymptotic benchmark#6299
xzfc merged 2 commits intodevfrom
update-hnsw_build_asymptotic

Conversation

@xzfc
Copy link
Member

@xzfc xzfc commented Apr 2, 2025

This PR updates hnsw_build_asymptotic to avoid waiting too much on setup.

@xzfc xzfc requested review from generall and timvisee April 2, 2025 11:32
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 2, 2025

📝 Walkthrough

Walkthrough

The changes introduce a new function make_cached_graph in lib/segment/benches/fixture.rs, which generates and caches HNSW graph layers based on parameters such as vector count, dimensions, and algorithm settings. This function uses a deterministic random number generator for consistent vector generation and checks for existing cached graphs to load or create new ones as needed. The build_index function has been removed from lib/segment/benches/hnsw_build_asymptotic.rs, replaced by a setup utilizing LazyCell for caching graph configurations for 5,000 and 1,000,000 vectors, with updated benchmark function names. Additionally, the hnsw_benchmark function in lib/segment/benches/hnsw_search_graph.rs has been refactored to use make_cached_graph, simplifying its structure and removing manual caching logic. The visibility of the SINGLE_THREADED_HNSW_BUILD_THRESHOLD constant in lib/segment/src/index/hnsw_index/hnsw.rs has been changed from private to public. Lastly, the humantime dependency has been added to both the [dependencies] and [dev-dependencies] sections of the Cargo.toml files.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6424d3b and 9193317.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • Cargo.toml (1 hunks)
  • lib/segment/Cargo.toml (1 hunks)
  • lib/segment/benches/fixture.rs (1 hunks)
  • lib/segment/benches/hnsw_build_asymptotic.rs (6 hunks)
  • lib/segment/benches/hnsw_search_graph.rs (2 hunks)
  • lib/segment/src/index/hnsw_index/hnsw.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • lib/segment/Cargo.toml
  • Cargo.toml
  • lib/segment/src/index/hnsw_index/hnsw.rs
  • lib/segment/benches/fixture.rs
🧰 Additional context used
🧬 Code Definitions (2)
lib/segment/benches/hnsw_search_graph.rs (2)
lib/segment/benches/hnsw_build_asymptotic.rs (2)
  • fixture (30-30)
  • fixture (49-49)
lib/segment/benches/fixture.rs (1)
  • make_cached_graph (22-86)
lib/segment/benches/hnsw_build_asymptotic.rs (1)
lib/segment/benches/fixture.rs (1)
  • make_cached_graph (22-86)
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: Basic TLS/HTTPS tests
  • GitHub Check: test-snapshot-operations-s3-minio
  • GitHub Check: test-shard-snapshot-api-s3-minio
  • GitHub Check: test-low-resources
  • GitHub Check: test-consistency
  • GitHub Check: test (macos-latest)
  • GitHub Check: test-consensus-compose
  • GitHub Check: test (windows-latest)
  • GitHub Check: test-consensus
  • GitHub Check: test
  • GitHub Check: test (ubuntu-latest)
  • GitHub Check: test
  • GitHub Check: test
🔇 Additional comments (12)
lib/segment/benches/hnsw_search_graph.rs (2)

22-23: Good addition of modular code organization.

Adding the fixture module improves code organization by centralizing common functionality used across benchmark files.


31-32: Great refactoring to reduce code duplication.

Replacing the complex graph construction and caching logic with a call to the centralized fixture::make_cached_graph function significantly improves code maintainability while preserving the same functionality. The function properly handles both the vector holder creation and graph layer construction/loading from cache.

lib/segment/benches/hnsw_build_asymptotic.rs (10)

4-5: Good use of LazyCell for performance optimization.

Using LazyCell is an excellent choice for optimizing benchmark setup as it ensures expensive operations are only executed when actually needed.


28-31: Excellent optimization for benchmark filtering.

Using LazyCell for the 5k vector setup ensures that the expensive graph generation is only performed when this specific benchmark is run, not when filtered out. The eprintln() call provides a helpful visual separator in the output.


33-35: Clear benchmark naming convention.

Renaming the benchmark to include the vector count ("5k") makes it more descriptive and easier to understand the purpose and scale of each benchmark.


44-44: Good memory management practice.

Explicitly dropping the 5k setup before creating the much larger 1M setup helps manage memory usage during benchmarking.


46-50: Well-structured setup for large benchmark.

Defining the vector count as a constant and using LazyCell for the 1M vector setup follows the same pattern as the 5k setup, maintaining consistency while handling the larger dataset efficiently.


63-64: Nice reuse of existing setup.

Reusing the same setup for multiple benchmarks is efficient and reduces redundant initialization.


72-72: Consistent use of constants.

Using NUM_VECTORS directly for the random range rather than a hardcoded value or calculation improves consistency and makes the code easier to maintain.


145-150: Good optimization for vector generation.

Using LazyCell for generating and caching vectors in the basic_scoring_vectors benchmark is an excellent optimization. It ensures vectors are only generated once, rather than for each benchmark iteration.


163-163: Proper resource management.

Explicitly dropping the setup after the benchmark completes ensures memory is freed promptly, which is important for these memory-intensive benchmarks.


166-171: Consistent application of LazyCell pattern.

Applying the same LazyCell pattern to the second benchmark in this function maintains consistency and provides the same performance benefits.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

{
use indicatif::{ParallelProgressIterator as _, ProgressStyle};

let path = Path::new(env!("CARGO_TARGET_TMPDIR"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about this variable 📝

Regarding the docs:

Cargo initially creates this directory but doesn’t manage its content in any way, this is the responsibility of the test code.

Would it make sense to add a note here that updating the generation code should also edit the filename of the cached layers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proper cache invalidation logic is overkill for benchmarks, so instead I rely on a person running the benchmarks to clear cache manually when needed. Unlike tests, benchmarks are not running unattended, and you'll be reading their console output anyway.

Tho, I've added a bunch of comments and logs to so it's harder to miss now.

$ cargo bench -p segment --profile perf --bench hnsw_build_asymptotic
    Finished `perf` profile [optimized] target(s) in 0.15s
     Running benches/hnsw_build_asymptotic.rs (target/perf/deps/hnsw_build_asymptotic-d9f4dcc37843d3a7)
Benchmarking hnsw-index-build-asymptotic/build-n-search-hnsw-5k: Warming up for 3.0000 s
Loading cached links (built 2days 7h 21m ago) from "/home/user/qdrant/target/tmp/segment/hnsw_build_asymptotic/5000-16-16-64-true-Cosine/graph.bin".
Delete the directory above if code related to HNSW graph building is changed
hnsw-index-build-asymptotic/build-n-search-hnsw-5k
                        time:   [26.508 µs 26.817 µs 27.120 µs]

@xzfc xzfc force-pushed the update-hnsw_build_asymptotic branch from 005ece8 to 6424d3b Compare April 4, 2025 18:15
@xzfc xzfc force-pushed the update-hnsw_build_asymptotic branch from 6424d3b to 9193317 Compare April 4, 2025 18:16
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
lib/segment/benches/fixture.rs (3)

49-54: Consider validating the integrity of the cached graph before loading.

If the disk contents are partial or corrupted, loading might cause runtime errors or incorrect benchmark data. Adding basic checks or versioning at load time (e.g., verifying a hash) could improve robustness.


45-47: Provide a way to parameterize the random seed for reproducible experiments.

Currently, the seed is hardcoded to 42. Offering a configurability option in benchmarks (e.g., via environment variables or function arguments) could help users run multiple controlled experiments or unify seeds across different benches.


88-92: Enhance error messaging in updated_ago().

Returning "???" when an error occurs may hinder debugging efforts. Consider propagating the actual error or including more descriptive information to easily diagnose issues related to file metadata retrieval.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 005ece8 and 6424d3b.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • Cargo.toml (1 hunks)
  • lib/segment/Cargo.toml (1 hunks)
  • lib/segment/benches/fixture.rs (1 hunks)
  • lib/segment/benches/hnsw_build_asymptotic.rs (6 hunks)
  • lib/segment/benches/hnsw_search_graph.rs (2 hunks)
  • lib/segment/src/index/hnsw_index/hnsw.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/segment/src/index/hnsw_index/hnsw.rs
🧰 Additional context used
🧬 Code Definitions (1)
lib/segment/benches/hnsw_search_graph.rs (1)
lib/segment/benches/fixture.rs (1)
  • make_cached_graph (22-86)
🔇 Additional comments (12)
lib/segment/Cargo.toml (1)

32-32: Adding humantime dependency looks appropriate.

This new dependency will help with human-readable time durations and timestamps, which is useful for the benchmark caching mechanism being implemented.

Cargo.toml (1)

180-180: Adding humantime to workspace dependencies is consistent.

Adding the humantime dependency to the workspace ensures consistent versioning across the project. The version 2.1.0 is the latest stable release.

lib/segment/benches/hnsw_search_graph.rs (2)

10-11: Good refactoring to use the new fixture module.

Moving the graph creation and caching logic to a shared fixture module improves code organization and reusability.

Also applies to: 22-22


31-32: Clean improvement to the benchmark implementation.

Replacing the complex graph creation and caching logic with a single call to fixture::make_cached_graph greatly simplifies the code while maintaining the same functionality. This makes the benchmark more maintainable.

lib/segment/benches/hnsw_build_asymptotic.rs (7)

4-5: Good use of LazyCell and fixture module.

Using LazyCell for expensive setup operations is an excellent optimization. It ensures the setup code only runs when needed, which can significantly improve benchmark performance when using filters.

Also applies to: 21-21


28-32: Effective caching strategy for 5K vector benchmark.

The LazyCell approach ensures that the graph is only created once and properly cached. The explicit drop after using the setup helps with memory management.

Also applies to: 44-44


46-51: Good implementation of the 1M vector benchmark setup.

Similar to the 5K setup, this effectively caches the large 1M vector graph. The NUM_VECTORS constant is now used with a value of 1,000,000, which matches the benchmark name.

Also applies to: 78-78


52-62: Benchmark name now accurately reflects vector count.

Changing from generic "build-n-search-hnsw" to specific "build-n-search-hnsw-1M" makes the benchmark name more descriptive and matches the actual implementation.


63-76: Updated point scoring benchmark with appropriate vector range.

The modification to use NUM_VECTORS as the upper bound for the random range is more consistent with the actual vector count being used in the benchmark.


145-153: Optimized vector generation with LazyCell.

Using LazyCell for vector generation in the basic_scoring_vectors benchmark is a good optimization. It ensures vectors are only generated once and explicitly dropped after use.

Also applies to: 163-163


166-174: Consistent LazyCell pattern for the 10x benchmark.

The same LazyCell pattern is applied consistently to the 10x benchmark, maintaining code consistency and optimization benefits.

Also applies to: 184-184

lib/segment/benches/fixture.rs (1)

34-40: Add a note to maintain the cached layers' filename when generation code changes.

This echoes an earlier comment suggesting that changing the generation logic should also involve updating the cached file naming scheme. Otherwise, stale or incompatible cached files might lead to confusing or incorrect results.

@xzfc xzfc merged commit 613ef07 into dev Apr 7, 2025
17 checks passed
@xzfc xzfc deleted the update-hnsw_build_asymptotic branch April 7, 2025 13:23
pull bot pushed a commit to kp-forks/qdrant that referenced this pull request Apr 21, 2025
* Update hnsw_build_asymptotic benchmark

* Comments and logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants