Compute dot product similarity using GPU acceleration with CuPy, fallback to CPU
(vectors, query_vectors, dimension, top_k=10)
| 199 | |
| 200 | |
| 201 | def 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 | |
| 229 | def _compute_brute_force_cpu(vectors, query_vectors, dimension, top_k=10): |
no test coverage detected