MCPcopy Create free account
hub / github.com/cosdata/cosdata / compute_brute_force_results

Function compute_brute_force_results

tests/test-sparse-vector.py:192–217  ·  view source on GitHub ↗

Compute dot product similarity using GPU acceleration with CuPy, fallback to CPU

(vectors, query_vectors, dimension, top_k=10)

Source from the content-addressed store, hash-verified

190
191
192def compute_brute_force_results(vectors, query_vectors, dimension, top_k=10):
193 """Compute dot product similarity using GPU acceleration with CuPy, fallback to CPU"""
194 if os.path.exists(BRUTE_FORCE_RESULTS_FILE):
195 print(f"Loading existing brute force results from {BRUTE_FORCE_RESULTS_FILE}")
196 with open(BRUTE_FORCE_RESULTS_FILE, "rb") as f:
197 return pickle.load(f)
198
199 print(
200 f"Computing brute force dot product similarity for {len(query_vectors)} queries..."
201 )
202 results = []
203
204 try:
205 # Try GPU computation first
206 print("Attempting GPU computation...")
207 results = _compute_brute_force_gpu(vectors, query_vectors, dimension, top_k)
208 except Exception as e:
209 print(f"GPU computation failed: {e}")
210 print("Falling back to CPU computation...")
211 results = _compute_brute_force_cpu(vectors, query_vectors, dimension, top_k)
212
213 # Save to disk
214 with open(BRUTE_FORCE_RESULTS_FILE, "wb") as f:
215 pickle.dump(results, f)
216 print(f"Brute force results computed and saved to {BRUTE_FORCE_RESULTS_FILE}")
217 return results
218
219
220def _compute_brute_force_cpu(vectors, query_vectors, dimension, top_k=10):

Callers 1

mainFunction · 0.70

Calls 3

loadMethod · 0.80
_compute_brute_force_gpuFunction · 0.70
_compute_brute_force_cpuFunction · 0.70

Tested by

no test coverage detected