Skip to content

[ENH] Use AVX in distance calculations #5258

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 1 commit into from
Aug 13, 2025
Merged

[ENH] Use AVX in distance calculations #5258

merged 1 commit into from
Aug 13, 2025

Conversation

jairad26
Copy link
Contributor

@jairad26 jairad26 commented Aug 13, 2025

Description of changes

This PR updates the distance crate to prioritize AVX over SSE, and forces builds with AVX flags when ENABLE_AVX512 is set in the Dockerfile

Test plan

How are these changes tested?

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Migration plan

Are there any migrations, or any forwards/backwards compatibility changes needed in order to make sure this change deploys reliably?

Observability plan

What is the plan to instrument and monitor this change?

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs section?

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

@jairad26 jairad26 marked this pull request as ready for review August 13, 2025 01:18
Copy link
Contributor

propel-code-bot bot commented Aug 13, 2025

Prioritize AVX Over SSE in Distance Calculations for Rust Distance Crate

This pull request reorders the SIMD feature detection and function selection logic in the Rust distance calculation module to give higher priority to AVX/FMA over SSE for x86/x86_64 architectures. The changes ensure that, when available, AVX and FMA instructions are used for distance computations (euclidean, cosine, and inner product), which can offer higher performance for platforms that support these instruction sets. Additionally, the author notes that builds will be forced to use AVX if ENABLE_AVX512 is set in the Dockerfile, though Dockerfile changes are not present in this diff.

Key Changes

• Reordered feature detection to select AVX/FMA implementations before SSE for relevant architectures.
• All distance calculation branches (euclidean, cosine, inner product) updated to use AVX/FMA when possible, with SSE as fallback.
• Maintained NEON branch for aarch64 unchanged.
• No significant algorithmic or interface changes; the focus is on CPU feature prioritization.

Affected Areas

• rust/distance/src/types.rs (distance calculation logic for all supported DistanceFunctions)

This summary was automatically generated by @propel-code-bot

Comment on lines +257 to +265
#[cfg(all(
any(target_arch = "x86_64", target_arch = "x86"),
target_feature = "sse"
))]
{
if std::arch::is_x86_feature_detected!("sse") {
return unsafe { crate::distance_sse::euclidean_distance(a, b) };
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[Documentation]

The reordering of instruction set priority from SSE-first to AVX-first looks correct, but consider documenting this prioritization decision. When both SSE and AVX are available at runtime, AVX will now be chosen, which should provide better performance. However, this means the fallback path (SSE → scalar) is now longer when AVX is not available.

Consider adding a comment explaining the prioritization rationale:

// Check instruction sets in order of performance: AVX (best) → SSE → scalar (fallback)

Also verify that the runtime feature detection order aligns with the compile-time feature requirements - all AVX-capable processors should also support SSE, so this ordering should be safe.

@blacksmith-sh blacksmith-sh bot deleted a comment from jairad26 Aug 13, 2025
@jairad26 jairad26 merged commit 6f95f37 into main Aug 13, 2025
60 checks passed
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.

2 participants