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