Skip to content

Conversation

@cpegeric
Copy link
Contributor

@cpegeric cpegeric commented Jan 8, 2026

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #23502

What this PR does / why we need it:

distance functions implemented by simd instruction with golang v1.26.


PR Type

Enhancement


Description

  • Implement SIMD-optimized distance functions for x86_64 architecture

    • L2 distance, L1 distance, inner product, cosine distance/similarity
    • Spherical distance for sparse vectors
    • Support AVX-512, AVX2, and AVX instruction sets with scalar fallback
  • Refactor distance function resolution into separate module

    • Extract kmeans and general distance resolution functions to resolve.go
  • Add float32 test cases for L2 distance squared calculation

  • Update Makefile to support GOEXPERIMENT=simd and GOAMD64 configuration


Diagram Walkthrough

flowchart LR
  A["distance_func.go<br/>Generic implementations"] -->|amd64 build| B["distance_func_amd64.go<br/>SIMD optimized"]
  A -->|non-amd64 build| C["distance_func.go<br/>Scalar fallback"]
  D["resolve.go<br/>Distance resolution"] -->|uses| B
  D -->|uses| C
  E["Makefile<br/>Build config"] -->|GOEXPERIMENT=simd| B
  E -->|GOAMD64 flag| B
Loading

File Walkthrough

Relevant files
Refactoring
distance_func.go
Mark as non-amd64 fallback, remove resolution functions   

pkg/vectorindex/metric/distance_func.go

  • Added build constraint //go:build !amd64 to mark as non-amd64 fallback
  • Removed distance function resolution functions (moved to resolve.go)
  • Kept generic scalar implementations for non-amd64 platforms
+2/-84   
resolve.go
Extract distance function resolution to separate module   

pkg/vectorindex/metric/resolve.go

  • New file containing distance function resolution logic
  • Extracted ResolveKmeansDistanceFn, ResolveKmeansDistanceFnForDense,
    ResolveKmeansDistanceFnForSparse
  • Extracted ResolveDistanceFn for general distance function selection
  • Supports metric type resolution for both dense and sparse vectors
+104/-0 
Enhancement
distance_func_amd64.go
SIMD-optimized distance functions for x86_64                         

pkg/vectorindex/metric/distance_func_amd64.go

  • New file with SIMD-optimized implementations for amd64 architecture
  • Implements L2 distance squared with AVX-512, AVX2, and scalar paths
  • Implements L1 distance with multi-level SIMD optimization
  • Implements inner product with AVX-512 and AVX2 support
  • Implements cosine distance and similarity with SIMD acceleration
  • Implements spherical distance for sparse vector operations
  • Helper functions for horizontal sum reduction across vector types
  • Support for both float32 and float64 data types
+942/-0 
Tests
distance_func_test.go
Add float32 L2 distance squared tests                                       

pkg/vectorindex/metric/distance_func_test.go

  • Added Test_L2DistanceSqFp32 test function with 6 test cases
  • Tests float32 L2 distance squared calculation with various vector
    sizes
  • Covers edge cases and typical vector dimensions
+68/-0   
Configuration changes
Makefile
Add GOEXPERIMENT=simd and GOAMD64 build configuration       

Makefile

  • Added GO variable support for custom Go binary path
  • Added GOEXPERIMENT_OPT to enable SIMD experiment for x86_64
  • Added GOAMD64 flag support for specifying AMD64 microarchitecture
    level
  • Refactored TAGS to GOTAGS for proper tag formatting
  • Updated build commands to use $(GO) and $(GOEXPERIMENT_OPT) variables
  • Updated musl build target to use new tag variables
+29/-11 

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

Labels

kind/feature Review effort 4/5 size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants