Repository navigation
Conversation
|
@ggerganov is this version acceptable? |
|
I thought about the same impl when I was working on the initial lazy mode, but IMO this should never be the responsibility of llama.cpp to syscall Instead, there are 2 cleaner designs but both requires thoroughly discussion with ggml maintainers:
|
This option would allow you to use tensor overrides to manually specify which tensors to load there, right? edit: or rather not to load yet :) |
|
I don't know what problem this is trying to solve. Both here and in #28136 people are talking about the effects and benefits without stating what is the root cause of the problem as if it is something very obvious. Am I supposed to read all these changes in order to figure it out? |
|
@ggerganov When you read the lazy ngrams, the core issue is that it's a completely different read type from the typical expert read. Experts are large contiguous tensors, ngrams are random bits and pieces from different places of the cache. But by default, the lazy read mode uses mmap, which will try to fetch entire pages into the page cache on page faults. So basically we need a mode for lazily reading small random snippets such as ngrams that doesn't utilize the page cache, similar to how |
I think the first option would be overkill, but I do like the second one. |
|
Sounds like relying on |
|
@ggerganov agree, mmap is indeed not the optimal way for this use case since it's transparent how the reads are handled at OS level lazy mode currently relies on mmap as it doesn't require too many changes in the code base. but we should eventually use plain one important thing to note though, this should be done inside ggml to prevent any circumstances where 2 threads calling the @pwilkin regarding why I proposed 2 solutions:
|
|
I'd rather try to implement this at libllama level rather than modifying the ggml at this point. We can reconsider later when this gets more established. Sounds like we should basically implement the GET_ROWS as part of the ple |
|
@ngxson Yeah, I see the conundrum. As @ggerganov said, for now I'll try to remove the |
Port of ggml-org#28136. A lazily read table (TENSOR_READ_LAZY) currently hands its rows to ggml_get_rows through the mmap, so every row costs a demand fault: a whole page read to reach the ~90 bytes of a PLE row, one fault at a time, and the pages then accumulate in the page cache. The row indices of an entire ubatch are known host-side before the graph runs, so they can instead be deduplicated, sorted into file order and read in parallel. --lazy-mode on-direct does that, and llama_lazy_reader hands out F32 rows exactly as ggml_get_rows would. The original places the reader, a staged input tensor, a staging buffer and an if/else in each arch that wants it, which is what it was asked not to do. Here an arch says nothing about direct reads at all: every tensor the loader reads lazily gets a reader keyed by the tensor, and llm_graph_lazy_rows is the one place that decides between a ggml_get_rows node and rows staged in set_input(). An arch swaps its ggml_get_rows for the helper's build() and forwards set_rows()/can_reuse() - qwen4exp and gemma4 both shrink. llama_file grows a positional read (pread, or ReadFile with an OVERLAPPED offset), so no part of this is conditional on the platform. The reader holds one buffered file per worker: read_at() is not thread-safe everywhere, and the loader's own descriptor may carry direct I/O, whose alignment rules a single row read cannot meet. qwen4exp, Qwen3.8-Flash-Next IQ4_NL, gfx1151, 27 GiB PLE table: pp2048 277.36 -> 484.12 t/s +75% pp8192 404.91 -> 450.67 t/s +11% tg128 24.02 -> 24.24 t/s flat The win shrinks with prompt length as compute takes over, and with a warm page cache, since a warm mmap fault is minor. Greedy output is identical byte for byte to --lazy-mode on and off. Assisted-by: Claude Opus 5
Assisted-by: Codex
5fd6ce3 to
e32c624
Compare
|
All right - remade the implementation so that |
|
Side note: Bypassing mmap would benefit DGX Spark. mmap is very slow on the Spark, likely an OS bug, direct-io is always much faster. |
|
@FlorinAndrei |
|
@satoyon @FlorinAndrei no, it's not an accident, as @satoyon said, this benefits all iGPUs due to the shared memory, that's why I also modified the heuristic to point AUTO -> ON for lazy mode for iGPUs now that it no longer requires mmap. |
|
@ggerganov @ngxson could you guys please take another look at this one? Would love to get it merged since it's the main enabler for further prefill optimizations on the box where I can run Qwen3.8 Flash Next on (Strix). |
|
@pwilkin Could you add some high-level description of the approach in this PR? The new classes that are introduced, how they interact with the llama model, the inputs, etc. |
|
Will do. |
|
Aight, added a description of the lazy loading flow with the key classes / code files mentioned. |
| }; | ||
|
|
||
| std::vector<std::exception_ptr> errs(n_workers); | ||
| std::vector<std::thread> workers; |
There was a problem hiding this comment.
Is there a noticeable improvement of using multiple workers compared to a single worker on the main thread?
There was a problem hiding this comment.
I believe so, but let me benchmark it quickly.
There was a problem hiding this comment.
I think we should try a single worker + potentially a LRU cache of fixed size. The idea being that we can have say a ~1GB hot cache which we first check if it has the needed lines.
There was a problem hiding this comment.
Here's the benchmark results:
| Readers | pp512 | pp2048 |
|---|---|---|
| 1 | 150.4 tok/s | 166.8 tok/s |
| 2 | 252.8 tok/s | 263.0 tok/s |
| 4 | 310.6 tok/s | 325.4 tok/s |
| 32 | 374.3 tok/s | 372.5 tok/s |
| 64 | 369.3 tok/s | 367.5 tok/s |
There was a problem hiding this comment.
Could the hot cache be optional? When models barely fit (V)RAM being able to read the ngram from disk without a cache is preferred over OOMing.
There was a problem hiding this comment.
Per this benchmark, modified the number of threads from 2 * HC to HC.
There was a problem hiding this comment.
Update: I retested with the dequant moved to the graph and unfortunately we still can't eliminate the worker threads:
| Reader cap | 512-token prefill | 2048-token prefill |
|---|---|---|
| 1 | 194.9 | 203.3 |
| 2 | 272.5 | 284.7 |
| 4 | 329.7 | 336.4 |
| 8 | 358.8 | 368.2 |
| 16 | 376.4 | 376.1 |
| 32 (HC) | 376.5 | 377.5 |
| 64 (2×HC) | 378.1 | 375.3 |
There was a problem hiding this comment.
I don't see it as "unfortunate"... it is the nature of an NVMe SSD. You need a queue depth that isn't just 1 if you want any hope of using the IOPS that are available.
There was a problem hiding this comment.
Something does not add up. The set_input of ubatch n+1 is already interleaved with the processing of ubatch n so a single thread reading the quantized PLE should be enough. Basically, the PLE is the same thing as token embeddings - its contents are just dynamic. So just like we don't need workers to read the token embeddings, we don't need workers for the PLE.
There was a problem hiding this comment.
Regarding the comparison with token embeddings, it might be down to the fact that PLE for qwen4exp needs to make 16 reads per token, versus normal embeddings only needing one. The numbers seem to be in the right ballpark. I have a somewhat crummy SSD that achieves 6000 IOPS single-threaded, which because of the sparse nature of the PLE reads should amount to about 6000 rows/s, which would bottleneck tok/s at a sustained 375 tok/s. On top of this, page cache can't hide the problem very well because of how huge the PLE is (about 50GB versus my ~10GB of free RAM when running this). Non-crummy SSDs can apparently do maybe 10k-20k IOPS with a single thread, but this is still in bottleneck territory if we're targeting 1000 tok/s prefill or more.
So if these numbers are right it might just be that the token embeddings are not at the bottleneck threshold, but close enough that the 16x multiplier for PLE makes a difference. Also page cache can hide any issues with token embeddings much better, as the table is much smaller (0.5 GB for q8 qwen4exp).
| const int64_t relems; // F32 elements per row | ||
| const int64_t nrows; | ||
|
|
||
| ggml_to_float_t to_float; // the dequantizer the ggml_get_rows CPU kernel uses; null for F32 |
There was a problem hiding this comment.
Do we actually need this to_float? If we read the quantized data and set it as input, then the ggml_get_rows() from the graph will do the dequantization as usual.
There was a problem hiding this comment.
Yeah, but the benefit of doing it now is that we do it concurrently to the normal processing; if you do it as part of ggml_get_rows(), then you add the dequant time to the main prefill compute.
There was a problem hiding this comment.
It's pretty much negligible. Same as the time for getting token embeddings.
There was a problem hiding this comment.
Aight, I measured the dequant in the graph and you're right, so I'll retest the single-threaded read without the dequant.
Assisted-by: Codex
|
@ngxson aight, simplified it to a factory and a reader per context. |
Assisted-by: Codex
…d direct lazy-row reads (upstream ggml-org#29030) - llama : add a GPU cache for MoE experts kept in host memory (ggml-org#29887, port of the qvac-fabric cache). --moe-cache-mib / llama_context_params::moe_cache_size: MUL_MAT_ID on host experts runs on the GPU from an LRU cache, only misses are uploaded, for batches of up to 32 tokens. Single device only. - qwen4exp/gemma4: gather lazy tensor rows with direct reads (ggml-org#29030, supersedes ggml-org#28136), -lzm on. Measured on a whole Qwen3.8-Flash-Next UD-IQ3_XXS (78 GB) with one RTX 5060 Ti 16 GB, 62 GB RAM, --cpu-moe: generation 14.95 -> 19.26 tok/s with --moe-cache-mib 8000, prompt unchanged (~89 tok/s). -lzm on was slower on this machine (79.5 / 16.38). The expert prefetch of ggml-org#28414 was left out: with it the prefill aborted in cuBLAS. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@pwilkin We can close this PR, correct? |
|
@ggerganov yes Superseded by #29599 |
…sor rows with direct reads Squash of PR head 7dc9a32 (7 commits on b11105) onto akeela-b11074-2 + ggml-org#29599. Conflicts resolved: llama-context.cpp includes (both); llama-model.cpp keeps akeela MoE-cache functions + add_lazy_reader; qwen4exp.cpp uses llm_graph_lazy_rows in the ggml-org#29599-renamed llm_graph_input_qwen4exp_ple (prefetch kept, a no-op for reader-backed tables); gemma4.cpp takes the PR's llm_graph_input_gemma4_per_layer and drops ggml-org#29599's ple input.
A reimplementation of #28136 replacing the mmap-backed
onmode with direct reads.Supersedes #28136.
The key mechanism here is
llm_graph_lazy_rows(introduced inllama-graph.cpp/h), an input builder that builds readers (llama-lazy-reader.cpp/h) for lazy tensors per-context. The reader prototypes are stored in the model object, when a context gets constructed, they get cloned into the context, one per future gather thread. Whenever an ubatch is created andset_inputsis called, the lazy reader'sgather()method is called and populates the input tensor.This is pretty critical because the performance overhead especially on integrated platforms (esp. on Strix Halo) is huge:
-lzm on-lzm on-directRequirements