pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #769: Adding a precise formula to the README for estimating the amount of memory for HNSW indexing
4+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #769: Adding a precise formula to the README for estimating the amount of memory for HNSW indexing
@ 2025-01-28 20:34 "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
0 siblings, 0 replies; 4+ messages in thread
From: michaelbautin (@michaelbautin) @ 2025-01-28 20:34 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
The pull request https://github.com/pgvector/pgvector/pull/765 attempted to add functions for estimating the amount of memory needed for HNSW indexing, and was closed by @ankane with an explanation that was somewhat unclear to me. There exists a very precise formula for estimating the amount of memory for fast HNSW indexing in memory without spilling to disk, depending only on M, the number of vectors, and vector size in bytes. It depends on some internal structure sizes of the pgvector extension. If we are not allowed to have that formula as part of HSNW, we will need to implement it outside of pgvector, and expose some of the details mentioned in the following expression to that outside code, or hard-code them and keep them in sync.
```
return MAXALIGN(sizeof(HnswElementData)) +
(avgLevel + 1) * sizeof(HnswNeighborArrayPtr) +
(
MAXALIGN(HNSW_NEIGHBOR_ARRAY_SIZE(HnswGetLayerM(m, 0))) +
avgLevel * MAXALIGN(HNSW_NEIGHBOR_ARRAY_SIZE(m))
);
```
Estimating the amount of memory HNSW indexing takes is crucial for correctly configuring maintance_work_mem of the instance to avoid a dramatic drop in indexing performance. We have observed cases where indexing would take an extremely long time, with a high amount of disk I/O, just because of incorrect tuning of memory in relation to the dataset.
The comment https://github.com/pgvector/pgvector/pull/765#issuecomment-2617244649 mentions "actual data from different datasets and index options", but just to reiterate, precise estimation of the amount of memory needed only depends on M, the number of vectors, and vector size in bytes, assuming all vectors are of the same size, and does not depend on any other dataset parameters or index options.
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #769: Adding a precise formula to the README for estimating the amount of memory for HNSW indexing
@ 2025-01-28 21:39 "ankane (@ankane)" <noreply+ankane@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: ankane (@ankane) @ 2025-01-28 21:39 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @michaelbautin, unfortunately, opening another issue while not taking into account previous feedback isn't productive for the project, so closing this out. You're welcome to create an independent tool to calculate this.
fwiw, we're in agreement that memory depends on features of the dataset (number of vectors, number of dimensions) and index options (M), but for any proposed formula, there would need to evidence (data) that it works well.
Also, in general, you can set `maintenance_work_mem` to the max amount of memory you'd like the index build to consume rather than trying to be exact.
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #769: Adding a precise formula to the README for estimating the amount of memory for HNSW indexing
@ 2025-01-28 21:39 "ankane (@ankane)" <noreply+ankane@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: ankane (@ankane) @ 2025-01-28 21:39 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #769: Adding a precise formula to the README for estimating the amount of memory for HNSW indexing
@ 2025-01-28 22:25 "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: michaelbautin (@michaelbautin) @ 2025-01-28 22:25 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane
> Hi @michaelbautin, unfortunately, opening another issue while not taking into account previous feedback isn't productive for the project, so closing this out. You're welcome to create an independent tool to calculate this.
I did attempt to take your feedback in https://github.com/pgvector/pgvector/pull/765#issuecomment-2617244649 into account about potentially adding this formula to the readme, and I opened this issue to hopefully see how exactly the readme could be updated. I am sorry if I misunderstood the meaning of that comment. Please let me know if you have changed your opinion about the usefulness of adding such a formula to the readme.
Here is an attempt to explain why the formula in #765 is very precise. Some preliminary definitions for context.
```
double geometricP = 1 - exp(-1 / ml);
/*
* Average level of a node. We subtract 1 because we number levels starting
* from 0. On average, a node is present at (avgLevel + 1) levels.
*/
double avgLevel = 1 / geometricP - 1;
```
I think this provides the explanation for the most part.
```
/*
* For each element we store:
* - HnswElementData struct
* - An array of pointers to neighbor arrays, as many as the number of
* levels where the element is present.
* - The neighbor arrays themselves. We need to take into account that the
* lowest level has a different number of neighbors per node than higher
* levels. But it also helps that every node is present at the lowest
* level.
*/
```
The actual formula itself is, with extra comments and unnecessary parentheses removed:
```
return MAXALIGN(sizeof(HnswElementData)) + /* HnswElementData struct */
(avgLevel + 1) * sizeof(HnswNeighborArrayPtr) + /* An array of pointers to neighbor arrays */
MAXALIGN(HNSW_NEIGHBOR_ARRAY_SIZE(HnswGetLayerM(m, 0))) + /* Neighbors at lowest level */
avgLevel * MAXALIGN(HNSW_NEIGHBOR_ARRAY_SIZE(m)) /* Neighbors at other levels */
```
The formula exactly retraces all allocations that HnswAlloc is called with, except for the variation in the random level, but the average value of the geometric distribution is estimated very precisely.
Here is the log message that I've obtained by running the modified pgvector code with #765 and indexing the deep-image-96-angular dataset with 9900000 vectors.
```
NOTICE: Tuples indexed in memory: 9990000, bytes used per tuple: 1066.157, estimated bytes per tuple: 1066.133, estimation error: -0.002217%
```
The important part is the estimation error that is less than 0.01% here: `-0.002217%`
If we need a safety margin in tuning of maintenance_work_mem, we could multiply the estimated memory by some factor such as 1.05 and/or add a fixed amount to it.
https://github.com/pgvector/pgvector?tab=readme-ov-file#index-build-time rightly points out that "Indexes build significantly faster when the graph fits into maintenance_work_mem". I am trying to make it easier for users to make sure that it does before spending hours or days trying to build the index.
Here some issues and related responses that amount to "setting maintenance_work_mem" higher. I believe these users would have benefited from knowing that their index build would be slow in advance.
- https://github.com/pgvector/pgvector/issues/300#issuecomment-1760522247 (users waited for 24 hours to build the index)
- https://github.com/pgvector/pgvector/issues/504#issuecomment-2020982889 (4 days or 6 days spent building the index)
- https://github.com/pgvector/pgvector/issues/629 (the user seems to have abandoned their pgvector evaluation due to indexing slowness).
I still believe the approach along the lines of what I am proposing would be a significant improvement in pgvector HNSW user experience with moderate to large datasets. Please let me know if you think if I am wrong. Thank you.
^ permalink raw [nested|flat] 4+ messages in thread
end of thread, other threads:[~2025-01-28 22:25 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-01-28 20:34 [pgvector/pgvector] issue #769: Adding a precise formula to the README for estimating the amount of memory for HNSW indexing "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
2025-01-28 21:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-28 21:39 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-28 22:25 ` "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox