Skip to content

fix(ui): load the embedder once per folder index, not once per file - #7310

Merged
kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:kalin/rc18-embedder-once
Nov 12, 2026
Merged

kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:kalin/rc18-embedder-once

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

Indexing a folder of 300 small text files took 15 minutes, almost all of it
reloading the embedding model: about 3 s of loading for every file, cached
or not, against well under 0.1 s of embedding. A folder index now loads it
once and reuses it for every file, so the same folder takes seconds.

Each file built its own RAGSDK, and each one unloaded and reloaded the
embedder. index_folder now runs inside one_embedder_load(), which hands the
embedder one file loaded to the next. A file read with the vision model,
or one that fails, gives it up, so the next file reloads it as before;
single uploads are unchanged.

Closes #7288

Test plan

  • python -m pytest tests/unit/chat/ui/test_folder_index_one_embedder_load.py and the folder-indexing suites (67 pass; main loads the embedder 6 times for 6 files)
  • Rebuilt app: index a folder of 300 small files and time it (rc18: 14 min 45 s)

Indexing a folder of 300 small text files took 15 minutes, almost all of it
reloading the embedding model: about 3 s of loading for every file, cached
or not, against well under 0.1 s of embedding. A folder index now loads it
once and reuses it for every file, so the same folder takes seconds.

Each file built its own RAGSDK, and each one unloaded and reloaded the
embedder. index_folder now runs inside one_embedder_load(), which hands the
embedder one file loaded to the next. A file read with the vision model,
or one that fails, gives it up, so the next file reloads it as before;
single uploads are unchanged.

Closes #ISSUE
@github-actions github-actions Bot added the tests Test changes label Nov 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

Indexing a folder now loads the embedding model once and reuses it for every file, instead of reloading it for each one. That reload was about 3 s per file, so a 300-file folder took about 15 minutes. The change only affects folder indexing, and every way it can fail leads back to the old load-per-file behaviour, so it is safe to merge.

  • Follow-up, not blocking: picking many files at once in the file browser still indexes them one request at a time, so each file still pays the reload. Users who select 300 files instead of their folder will still wait 15 minutes. File an issue if this PR won't cover it.

Real-world evidence

N/A: no evidence bundle was produced for this run. The verdict rests on static review alone. The PR's own manual timing item (index 300 small files in the rebuilt app) is still unchecked, and the new unit tests could not be run here.

🔍 Technical details

🟡 Follow-up: multi-file selection still reloads the embedder per file (src/gaia/apps/webui/src/components/FileBrowser.tsx:123-135)
The file browser loops over the selected files and calls api.uploadDocumentByPath (POST /documents/upload-path) once per file. Each request runs _index_document outside one_embedder_load(), so every file still pays the ~3 s reload. Folder indexing from the Agent UI MCP (agent_ui_mcp.py:689) goes through /documents/index-folder, so that path is covered. A fix could be a batch upload endpoint, or a short-lived embedder cache keyed by time instead of request scope. Suggested issue title: "Indexing many selected files reloads the embedder per file".

🟢 The vision check only sees pages the vision model actually read (src/gaia/ui/_chat_helpers.py:5985)
read_with_vision is true only when vlm_pages or vlm_slides is non-zero. A file can load the vision model and still read nothing. For example, _images_readable_now probes with VLMClient.check_availability(), or extraction can fail after the load. In that case the embedder is handed on even though the vision model may have evicted it. The next file then calls embeddings on an unloaded model, and Lemonade auto-loads the model at its default ubatch. The existing "physical batch size" retry in RAGSDK._embed_batch covers this with one reload, so the result is a slower file rather than a failure. If you want to close this, also check vlm_checked or total_images in the result.

Notes from tracing

  • batch is read with _BATCH_EMBEDDER.get() on the event-loop side and captured by the executor closure, so the context var works even though _do_index runs in a thread pool. Good.
  • index_folder awaits files one after another, and pop hands the embedder over exclusively. Two workers never share one embedder.
  • On a timeout or stop, _do_index raises before it returns the embedder, so the next file loads its own. That is correct.
  • A cache hit never calls _load_embedder. The injected embedder is still not None, so it is returned to the batch. That is correct.

Strengths

  • The batch is scoped with a context manager, so single uploads, re-index and background indexing keep their current behaviour without any changes.
  • The tests check the outcome a user cares about (load count) through the real /api/documents/index-folder route, not just the helper. They also cover the vision-reload case.
  • The function's docstring states the new reuse rule, and the code has one comment explaining why the vision case is excluded.

A folder index handed the embedder on to the next file unless the vision
model read some images. The vision model can load and then read nothing,
evicting the embedder all the same, so a file that left images unread now
gives the embedder up too and the next file loads its own.
@kovtcharov

kovtcharov commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

A file whose images went unread now gives the embedder up too (e523fe2). The multi-file case is left for a follow-up.

  • Vision model loaded but read nothing: a file that left images unread now counts the same as one read with the vision model, so the next file loads its own embedder. A PDF with no images, or plain text, still hands it on. New test: three files that each leave images unread load the embedder three times. The docstring says so.
  • Selecting many files in the file browser (follow-up, not done here): each selected file is still its own upload request, so each still pays the reload. Fixing it needs either a batch upload endpoint or an embedder kept alive across requests, which is more than this PR should carry. Suggested issue: "Indexing many selected files reloads the embedder per file".
  • Not done here: the rebuilt-app timing of a 300-file folder. It needs a person at the app with a real model server, so it stays unchecked for the maintainer.

The branch already contained current main, so there was nothing to merge in.

A file that fails mid-batch takes the embedder with it so the next file
reloads, two folder indexes running at once never share one, and a
single upload after a batch loads its own.
kovtcharov added a commit to kovtcharov/gaia that referenced this pull request Nov 12, 2026
On main every indexed file still reloads the embedder (amd#7288), and 300 of
them ran past 45 minutes on a hosted runner. The test keeps 300 as its
default; the workflow sets GAIA_SCALE_FILES=60 and says to delete it with
the fix (amd#7310). The load-count assertion fails at any size, so the strict
expected failure still holds.
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 12, 2026
Merged via the queue into amd:main with commit 89e1e07 Nov 12, 2026
44 of 48 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/rc18-embedder-once branch November 12, 2026 08:41
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Nov 12, 2026
amd#7310 merged, so a folder index loads the embedder once and the strict
expected failure on the load count now passes. 300 files index in about 25 s
against the fake server, well inside the 240 s budget, so the lane drops its
60-file override and runs the size rc18 failed at.
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Nov 12, 2026
Every unit test indexes three files and stores five memories, and rc18
fell over at 300 of each: a folder index reloaded the embedder once per
file, so 300 small files took 15 minutes, and a memory search over a few
hundred rows ended the turn on a JSON error. This adds a scale run that
does both at a real user's size on every release tag, with no model: the
installer journeys' fake model server answers, and the test asserts
counts of what GAIA asked it for.

What it asserts:

| At scale | Assertion | Today |
|---|---|---|
| large folder index | every file indexed, exactly one library row each
| ✅ |
| large folder index | the embedder is loaded at most twice, within a
time budget | ✅ (fixed by amd#7310 while this was in review; it caught
amd#7288, a reload per file, before that) |
| search over 500 memories | the result can be sent as JSON | ✅ (fixed
by amd#7308 while this was in review; the test caught it before that) |
| 500 memories | the memory API reaches every one, page by page | ✅ |

**Size:** 300 files, the size rc18 failed at. With amd#7310 in, they index
in about 25 s against the fake server, well inside the 240 s budget;
before it, 300 ran past 45 minutes on a hosted runner (that slowness was
the bug).

**Not built here, and why:** the 60-turn chat with the context meter
compared to what the model is sent (F59) needs a real flagship turn loop
behind the relay, and the memory page showing only 200 of 500 (F63) and
duplicate rows from a re-add during indexing (F65) are a frontend bug
and a race. Each has its own focused test in its fix PR (amd#7309, amd#7312,
amd#7311); a scale version belongs with the Electron journey proposed in
amd#7343.

Stacked on amd#7331 (it uses that PR's `tests/fixtures/fake_lemonade.py`);
the commits before "test: run GAIA at a real user's size ..." are that
PR.

## Test plan

- [ ] The new **Scale Run** workflow on this PR passes (4 passed)
- [ ] Locally: `GAIA_SCALE_FILES=8 python -m pytest
tests/integration/test_scale_run.py -q` passes
- [ ] Runs on every `v*` tag (each RC and final) and on PRs that touch
the test, its fixture or the workflow

<details>
<summary>🔍 Technical details</summary>

- The folder is indexed once per run through `POST
/api/documents/index-folder` with the real RAG code; the embedder's load
and unload requests are read from the fake server's log.
- The time budget (240 s) sits in the same expected-failure test as the
load count, since the reloads are what make it slow.
</details>

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documents: folder indexing reloads the embedder for every file

2 participants