From dc3309f98ae8d9e0a0c1f2da7d53324e8b53fdf1 Mon Sep 17 00:00:00 2001 From: Evo Date: Sat, 20 Jun 2026 09:02:40 +0800 Subject: [PATCH] feat(reranker): detect Intel XPU for local cross-encoder acceleration Mirror the XPU device-detection block #2260 added to LocalSTEmbeddings into the byte-identical LocalSTCrossEncoder twin, so the local reranker also uses Intel Arc XPU instead of silently falling back to CPU. Guarded by hasattr(torch, 'xpu') + is_available(); no-op on CUDA/MPS/CPU. --- hindsight-api-slim/hindsight_api/engine/cross_encoder.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hindsight-api-slim/hindsight_api/engine/cross_encoder.py b/hindsight-api-slim/hindsight_api/engine/cross_encoder.py index 6f5950447..37b13b52f 100644 --- a/hindsight-api-slim/hindsight_api/engine/cross_encoder.py +++ b/hindsight-api-slim/hindsight_api/engine/cross_encoder.py @@ -212,7 +212,7 @@ async def initialize(self) -> None: device = "cpu" logger.info("Reranker: forcing CPU mode (HINDSIGHT_API_RERANKER_LOCAL_FORCE_CPU=1)") else: - # Check for GPU (CUDA) or Apple Silicon (MPS) + # Check for GPU (CUDA), Apple Silicon (MPS), or Intel XPU # Wrap in try-except to gracefully handle any device detection issues # (e.g., in CI environments or when PyTorch is built without GPU support) device = "cpu" # Default to CPU @@ -220,10 +220,13 @@ async def initialize(self) -> None: has_gpu = torch.cuda.is_available() or ( hasattr(torch.backends, "mps") and torch.backends.mps.is_available() ) + # Intel Arc XPU support — torch.xpu is available when the XPU build is loaded + if not has_gpu and hasattr(torch, "xpu"): + has_gpu = torch.xpu.is_available() if has_gpu: - device = None # Let sentence-transformers auto-detect GPU/MPS + device = None # Let sentence-transformers auto-detect GPU/MPS/XPU except Exception as e: - logger.warning(f"Failed to detect GPU/MPS, falling back to CPU: {e}") + logger.warning(f"Failed to detect GPU/MPS/XPU, falling back to CPU: {e}") # Patch transformers 5.x compatibility for models using XLM-RoBERTa # (e.g., jina-reranker-v2-base-multilingual). transformers 5.x removed