pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
9+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2024-12-31 03:08 "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
  0 siblings, 0 replies; 9+ messages in thread

From: michaelbautin (@michaelbautin) @ 2024-12-31 03:08 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

It would be very useful to have a function that would estimate, for a particular combination of vector data type, dimensionality, number of rows, and the value of M, the minimum amount of memory (as represented by maintenance_work_mem) necessary to build the HNSW index fully in memory. In many cases, users would like to avoid the case of spilling the index to disk at build time altogether, because that often slows down the index build too much.

^ permalink  raw  [nested|flat] 9+ messages in thread

* Re: [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2024-12-31 03:09 ` "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: michaelbautin (@michaelbautin) @ 2024-12-31 03:09 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@michaelbautin renamed this.

^ permalink  raw  [nested|flat] 9+ messages in thread

* Re: [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2024-12-31 04:25 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: jkatz (@jkatz) @ 2024-12-31 04:25 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks for the suggestion.

The rough math I typically use is `# vectors * sizeof(VECTOR) * 1.3`, where `VECTOR` is the data type size (e.g. `vector(1024)` would be `1024*4 + 8`).

So for example, if I have 1,000,000 1024-dim fp32 vectors, the math would be:

```
1000000 * (1024 * 4 + 8) * 1.3 => 5,335,200,000
```

which to get into GiB would be:

```
5,335,200,000 / 1024**3 => ~4.96GB
```

This can get more nuanced due to page alignment (i.e. how many vectors can fit within 8KB), how many layers you have (which is dictated by `m`), and quantization, but I at least wanted to give you a simple formula to start with.

^ permalink  raw  [nested|flat] 9+ messages in thread

* Re: [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2024-12-31 04:36 ` "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: michaelbautin (@michaelbautin) @ 2024-12-31 04:36 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Would this estimation differ at all for parallel vs. non-parallel build? From looking at the code it seems to be that it should not.

^ permalink  raw  [nested|flat] 9+ messages in thread

* Re: [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2024-12-31 05:03 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: jkatz (@jkatz) @ 2024-12-31 05:03 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Nope, it's the same regardless of workers.

^ permalink  raw  [nested|flat] 9+ messages in thread

* Re: [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2024-12-31 16:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: ankane (@ankane) @ 2024-12-31 16:18 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 9+ messages in thread

* Re: [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2024-12-31 16:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: ankane (@ankane) @ 2024-12-31 16:18 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @michaelbautin, also check out #299.

^ permalink  raw  [nested|flat] 9+ messages in thread

* Re: [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2025-01-03 01:18 ` "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: michaelbautin (@michaelbautin) @ 2025-01-03 01:18 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks! In #299, the function  being linked to is 
```c
/*
 * Get the max number of elements that fit into maintenance_work_mem
 */
static double
HnswGetMaxInMemoryElements(int m, double ml, int dimensions)
{
	Size		elementSize = sizeof(HnswElementData);
	double		avgLevel = -log(0.5) * ml;

	elementSize += sizeof(HnswNeighborArray) * (avgLevel + 1);
	elementSize += sizeof(HnswCandidate) * (m * (avgLevel + 2));
	elementSize += sizeof(ItemPointerData);
	elementSize += VECTOR_SIZE(dimensions);
	return (maintenance_work_mem * 1024L) / elementSize;
}
```

I see that that function is not present in the current version (v0.8.0) and the actual amount of memory consumed is being used for deciding when to start spilling to disk instead. I think it would be good to have a reliable function, accessible to the user, that could be used to estimate whether a particular HNSW index build could be completed in memory.

^ permalink  raw  [nested|flat] 9+ messages in thread

* Re: [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build
@ 2025-01-24 20:57 ` "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
  7 siblings, 0 replies; 9+ messages in thread

From: michaelbautin (@michaelbautin) @ 2025-01-24 20:57 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@jkatz Regarding your suggestion in https://github.com/pgvector/pgvector/issues/745#issuecomment-2566121887 -- I don't think the memory usage for HNSW in-memory build part depends on page size at all. I've added a more precise formula in my pull request here: https://github.com/pgvector/pgvector/pull/765. This should give users a very good HNSW build memory estimation, and allow them to set maintenance_work_mem correctly and avoid surprises with index spilling to disk and HNSW build slowdown.

^ permalink  raw  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2025-01-24 20:57 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-12-31 03:08 [pgvector/pgvector] issue #745: Automate estimation of amount of memory needed for fast HNSW index build "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
2024-12-31 03:09 ` "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
2024-12-31 04:25 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-12-31 04:36 ` "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
2024-12-31 05:03 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-12-31 16:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-12-31 16:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-03 01:18 ` "michaelbautin (@michaelbautin)" <noreply+michaelbautin@github.com>
2025-01-24 20:57 ` "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