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

Function compute_brute_force_results

tests/test-usv.py:201–226  ·  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

199
200
201def compute_brute_force_results(vectors, query_vectors, dimension, top_k=10):
202 """Compute dot product similarity using GPU acceleration with CuPy, fallback to CPU"""
203 if os.path.exists(BRUTE_FORCE_RESULTS_FILE):
204 print(f"Loading existing brute force results from {BRUTE_FORCE_RESULTS_FILE}")
205 with open(BRUTE_FORCE_RESULTS_FILE, "rb") as f:
206 return pickle.load(f)
207
208 print(
209 f"Computing brute force dot product similarity for {len(query_vectors)} queries..."
210 )
211 results = []
212
213 try:
214 # Try GPU computation first
215 print("Attempting GPU computation...")
216 results = _compute_brute_force_gpu(vectors, query_vectors, dimension, top_k)
217 except Exception as e:
218 print(f"GPU computation failed: {e}")
219 print("Falling back to CPU computation...")
220 results = _compute_brute_force_cpu(vectors, query_vectors, dimension, top_k)
221
222 # Save to disk
223 with open(BRUTE_FORCE_RESULTS_FILE, "wb") as f:
224 pickle.dump(results, f)
225 print(f"Brute force results computed and saved to {BRUTE_FORCE_RESULTS_FILE}")
226 return results
227
228
229def _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