iai-personal-memory-engine icon indicating copy to clipboard operation
iai-personal-memory-engine copied to clipboard

RuntimeWarning: divide by zero/overflow/invalid value encountered in matmul at _exact_index.py:188 — persists through compact-hippo rebuild

Open aiyou550101 opened this issue 2 months ago • 2 comments

Environment

  • iai-mcp version: 3.0.8 (also reproduces after upgrading — see below)
  • OS: macOS (darwin)

Bug

~/.iai-mcp/logs/launchd-stderr.log repeatedly shows (479+ occurrences over the log's lifetime, and still recurring on essentially every pending_embed_pass):

.../iai_mcp/store/_exact_index.py:188: RuntimeWarning: divide by zero encountered in matmul
  sims = active @ cue_unit
.../iai_mcp/store/_exact_index.py:188: RuntimeWarning: overflow encountered in matmul
  sims = active @ cue_unit
.../iai_mcp/store/_exact_index.py:188: RuntimeWarning: invalid value encountered in matmul
  sims = active @ cue_unit

This strongly suggests one or more stored vectors (or the query cue) have zero norm or a non-finite value reaching the active @ cue_unit cosine-similarity matmul unguarded.

What I tried (none fixed it)

  1. Restarted the iai-mcp daemon — new PID, same errors immediately. Rules out transient in-memory state; the corruption is persisted on disk.
  2. Ran iai-mcp maintenance compact-hippo --apply (wal_checkpoint + VACUUM + hnswlib rebuild) after stopping the daemon. Completed successfully ("status": "ok", records_count unchanged at 10806, db size 100.9MB→98.5MB), but the warnings resumed immediately on daemon restart. This rebuilds index layout, not the underlying vectors, so corrupted rows survive it.
  3. Checked iai-mcp doctor's (dd) exact-index coercions check. It reported "no non-finite coercions recorded" (PASS) at the same time the log was actively emitting the warnings above — this check does not appear to detect the condition it's named for, giving a false-clean signal.
  4. Reviewed changelogs from 3.0.8 → 3.2.1 for a relevant fix. Found only an older, already-included fix (v3.0.3: "A stored embedding or a query cue carrying a non-finite value (NaN or ±inf) no longer poisons exact-cosine recall" — coerces at normalization time). That fix evidently doesn't cover this code path, since the warnings still originate from the exact same sims = active @ cue_unit line.

Ask

  • A doctor check (or compact-hippo step) that actually detects and repairs zero-norm/non-finite vectors already in storage, since the existing (dd) check doesn't catch this live case.
  • Or guidance on how to identify/purge the specific corrupted record(s) manually.

Happy to provide more log excerpts or run diagnostics on request.

aiyou550101 avatar Sep 10 '26 14:09 aiyou550101

Follow-up: EMBEDDING_INTEGRITY is a heavy, repeated cost — likely the same root cause

While investigating a separate question (daemon memory footprint, ~1.5GB RSS), ran iai-mcp daemon rss-stats and found the per-sleep-cycle resident-set breakdown. Across four recent cycles, EMBEDDING_INTEGRITY is consistently the single heaviest and slowest step by a wide margin:

2026-09-06T17 — EMBEDDING_INTEGRITY  +119.5 MiB   26.999s
2026-09-09T10 — EMBEDDING_INTEGRITY  +132.9 MiB   24.605s
2026-09-09T21 — EMBEDDING_INTEGRITY  +190.4 MiB   25.796s
2026-09-10T21 — EMBEDDING_INTEGRITY   +69.6 MiB   27.102s

For comparison, every other step in the same cycles (SCHEMA_MINE, OPTIMIZE_HIPPO, CLUSTER_SUMMARY, RECALL_INDEX_REBUILD, etc.) completes in well under a couple of seconds each, and none allocate anywhere close to this much.

This lines up with the theory that EMBEDDING_INTEGRITY is the maintenance job meant to scan for and coerce the non-finite (NaN/±inf) values causing the divide by zero/overflow/invalid value encountered in matmul warnings reported above at _exact_index.py:188. If that's correct, the ~25-27 second runtime and 70-190 MiB allocation every single cycle is plausibly the cost of scanning the full embedding matrix each time because the underlying corruption is never actually resolved — the job keeps finding (or re-encountering) the same poisoned rows instead of fixing them once and being fast on subsequent runs.

Net effect: the daemon's overall RSS sits at a stable-but-elevated ~1.5GB plateau (confirmed healthy/non-growing via doctor's 24h RSS plateau check, currently -75 MiB over 23.9h) — not a leak, but a heavier-than-expected steady-state baseline, consistent with EMBEDDING_INTEGRITY's large gross allocations not being fully returned to the OS by the allocator after each run (normal CPython/malloc behavior, not a separate bug in itself).

If EMBEDDING_INTEGRITY is intended to permanently fix corrupted rows once found, it doesn't appear to be succeeding — the matmul warnings reported above are still occurring in real time on every pending_embed_pass, well after many of these cycles have run. Might be worth checking whether this job is actually able to identify and repair the specific corrupted vectors, or whether it's silently failing to persist its fix.

aiyou550101 avatar Sep 11 '26 05:09 aiyou550101

Fixed: non-finite / zero-norm stored vectors are now coerced at exact-index build time (row-level), not only at write time, so the on-disk rows that caused the active @ cue_unit matmul warnings are sanitized on rebuild. Live in the current release (iai-pme on PyPI). Developed/tested on macOS.

CodeAbra avatar Sep 14 '26 01:09 CodeAbra