Project NOMAD is an offline-first knowledge and education server. Wikipedia, thousands of books, courses, maps, and optional local AI, all running on hardware you own with no internet required.
TypeScript
fix(RAG): pace continuation batches when embedding is CPU-only
Chris Sherwooda22c6408e639
diff --git a/admin/app/jobs/embed_file_job.ts b/admin/app/jobs/embed_file_job.tsindex 771cedfd..426608a1 100644--- a/admin/app/jobs/embed_file_job.ts+++ b/admin/app/jobs/embed_file_job.ts@@ -27,6 +27,12 @@ export class EmbedFileJob {return 'embed-file'}+ // Delay between continuation batches when embedding runs CPU-only. Gives the OS+ // scheduler a brief idle window so sshd / disk-collector / other services don't+ // starve during long multi-batch ZIM ingestions. Skipped entirely when the+ // embedding model is GPU-offloaded — see OllamaService.isEmbeddingGpuAccelerated().+ static readonly CPU_BATCH_DELAY_MS = 1000+static getJobId(filePath: string): string {return createHash('sha256').update(filePath).digest('hex').slice(0, 16)}@@ -114,6 +120,19 @@ export class EmbedFileJob {`[EmbedFileJob] Batch complete. Dispatching next batch at offset ${nextOffset}`)+ // Pace continuation batches when embedding is CPU-bound. Sustained 100% CPU+ // saturation across all cores during multi-batch ZIM ingestion can starve+ // other services (sshd has been seen to lose responsiveness hard enough to+ // require a power-cycle). When GPU-accelerated, embeddings stream through+ // the GPU and CPUs stay free — no pacing needed.+ const isGpuAccelerated = await ollamaService.isEmbeddingGpuAccelerated()+ if (!isGpuAccelerated) {+ logger.info(+ `[EmbedFileJob] Embedding is CPU-only — pacing ${EmbedFileJob.CPU_BATCH_DELAY_MS}ms before dispatching next batch`+ )+ await new Promise((resolve) => setTimeout(resolve, EmbedFileJob.CPU_BATCH_DELAY_MS))+ }+// Dispatch next batch (not final yet)await EmbedFileJob.dispatch({filePath,diff --git a/admin/app/services/ollama_service.ts b/admin/app/services/ollama_service.tsindex fe0cb1c8..78fbf69c 100644--- a/admin/app/services/ollama_service.ts+++ b/admin/app/services/ollama_service.ts@@ -513,6 +513,42 @@ export class OllamaService {}}+ /**+ * Returns true if Ollama is currently running an embedding model with non-zero VRAM+ * (i.e., GPU-offloaded). Returns false if the model is running CPU-only OR if it's+ * not currently loaded OR if /api/ps is unreachable.+ *+ * Used by EmbedFileJob to pace continuation batches when the embedding model is+ * CPU-bound — sustained 100% CPU on a multi-batch ZIM ingestion can starve other+ * services (sshd, etc.) hard enough to require a power-cycle. AMD ROCm installs+ * hit this today because Ollama's ROCm build doesn't accelerate nomic-bert; on+ * NVIDIA, nomic-embed-text runs at 100% GPU and pacing is unnecessary.+ *+ * Only the Ollama-native endpoint is supported — backends that expose+ * `/v1/embeddings` (LM Studio, llama.cpp) don't surface placement info.+ */+ public async isEmbeddingGpuAccelerated(): Promise<boolean> {+ await this._ensureDependencies()+ if (!this.baseUrl) return false++ try {+ const response = await axios.get(`${this.baseUrl}/api/ps`, { timeout: 5000 })+ const models: Array<{ name?: string; size_vram?: number }> = response.data?.models ?? []+ // Match any loaded model whose name signals it's an embedding model.+ // nomic-embed-text, mxbai-embed-large, snowflake-arctic-embed, etc. all follow this convention.+ return models.some(+ (m) => m.name?.toLowerCase().includes('embed') && (m.size_vram ?? 0) > 0+ )+ } catch (err: any) {+ // /api/ps unreachable (Ollama down, non-native backend, etc.) — fail closed: assume CPU,+ // which means we'll pace. Better to over-pace than risk box-killing CPU saturation.+ logger.warn(+ `[OllamaService] Could not check embedding placement via /api/ps: ${err?.message ?? err}`+ )+ return false+ }+ }+public async getModels(includeEmbeddings = false): Promise<NomadInstalledModel[]> {await this._ensureDependencies()if (!this.baseUrl) {