Repository navigation
Fix ggml_tensor_extra_gpu memory leak - #2146
eugen-ajechiloae-clearml wants to merge 1 commit into
Conversation
Allocate the `extra` member of `ggml_tensor` statically
JohannesGaessler
left a comment
There was a problem hiding this comment.
Would be fine with me but ggml changes will need to be approved by @ggerganov.
|
#2099 uses |
|
Not a good change - the CUDA implementation should clean-up the allocated memory |
|
Hi @ggerganov ! Is there any reason why you allocate the memory dynamically? Cleaning up the memory seems very difficult . Looks like the same memory region is used by multiple instances of |
|
The goal is to keep
Is it really so difficult. I guess we could pre-allocate a single large enough buffer in If it is indeed too difficult we can think of some alternative approaches, but the main requirement is to not couple |
|
AFAIK, there are two types of Since the lifecycle of them is tied to All temp allocations are made in typedef void (*offload_func_t)(void * offload_ctx, struct ggml_tensor * tensor);
int llama_eval_internal(...) {
ctx0 = ggml_init(...);
#ifdef GGML_USE_CUBLAS
void * offload_ctx = ggml_cuda_init_offload_ctx(...);
#endif
// Now the offload_func will have to be called like this
offload_func(offload_ctx, tensor);
#ifdef GGML_USE_CUBLAS
ggml_cuda_free_offload_ctx(offload_ctx);
#endif
ggml_free(ctx0);
}It's a bit more verbose but we correctly track the life cycle of those temp allocations.
The |
|
Memory management seems a bit too complicated for me in this case, so I just wanted to let you know that I will not open another PR for this issue. Thank you guys for your input! |
…own rule (engine ggml-org#2146) The registry skips a registration when a pattern is a substring of its name. The two supports_op claims that must decline with their only registration checked the other way round: they looked for the full name ("hip.top_k_f32", "common.argsort_rows_f32") inside the variable. So =top_k removed the matcher but kept the claim, and on Flash-Next at 18.8k the request failed with "unsupported HRX node 1: TOP_K" (the sparse-attention indexer's 2051-of-2560 selection) instead of running TOP_K on the CPU. dispatch_name_disabled() is now the one rule; the registry and both claims call it. Compiled against the bench build's commands; the claim path has not been run on a device yet. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…un (engine ggml-org#2146) Two defects, measured on gfx1151 by running the kernel directly through HIP, 50 runs per shape: 1. A race in the selection levels. After the level's last barrier every lane reads bucket_found / take_found, but lane 0 resets both at the start of the next level without a barrier in between, so a slow lane could publish a wrong threshold_key. At 2560 -> 2051 x 512 rows the old kernel returned rows that are not a top-k (1-12 per 50 runs) and, with distinct inputs, a different index set in 2-3 of 49. One __syncthreads() after the reads: 0 invalid rows, set stable. 2. The emit took a slot per element with atomicAdd, so the order differed every run (49 of 49), and in the group equal to the rank-k key whichever lanes came first took the remaining slots, so with ties the set differed every run too. Each lane now counts its two groups, an exclusive scan over lanes gives fixed offsets, and a second pass writes in visit order. Order and set: 0 of 49 differ on every shape (2560/2051, 4096/2048, 128/8, a 2561 unaligned pitch, one 200,000 row, k == cols); with distinct inputs the set equals a CPU reference. The sparse-attention indexer on Flash-Next picks 2051 of 2560 KV rows with this kernel, and identical greedy 18.8k requests gave 4 replies in 4 with the GPU to itself. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…input (engine ggml-org#2146) Seven cases (the Flash-Next indexer's 2560 -> 2051, heavy ties, 4096 -> 2048, a router-like 128 -> 8, an odd pitch, k == cols), 12 runs each: every row a valid top-k, bitwise-identical output across runs, and with distinct inputs the CPU's set. Skips without HRX0. Before (bench build of 962507f, the old kernel): FAIL, rc 1, 4 of 7 cases with 11 of 11 runs differing from run 1. After: owed on a build of this branch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…un (engine ggml-org#2146) (#328) * hrx: hip_top_k_f32 returns the same indices in the same order every run (engine ggml-org#2146) Two defects, measured on gfx1151 by running the kernel directly through HIP, 50 runs per shape: 1. A race in the selection levels. After the level's last barrier every lane reads bucket_found / take_found, but lane 0 resets both at the start of the next level without a barrier in between, so a slow lane could publish a wrong threshold_key. At 2560 -> 2051 x 512 rows the old kernel returned rows that are not a top-k (1-12 per 50 runs) and, with distinct inputs, a different index set in 2-3 of 49. One __syncthreads() after the reads: 0 invalid rows, set stable. 2. The emit took a slot per element with atomicAdd, so the order differed every run (49 of 49), and in the group equal to the rank-k key whichever lanes came first took the remaining slots, so with ties the set differed every run too. Each lane now counts its two groups, an exclusive scan over lanes gives fixed offsets, and a second pass writes in visit order. Order and set: 0 of 49 differ on every shape (2560/2051, 4096/2048, 128/8, a 2561 unaligned pitch, one 200,000 row, k == cols); with distinct inputs the set equals a CPU reference. The sparse-attention indexer on Flash-Next picks 2051 of 2560 KV rows with this kernel, and identical greedy 18.8k requests gave 4 replies in 4 with the GPU to itself. Co-Authored-By: Claude Opus 5.5 <[email protected]> * tests: test-hrx-top-k-repeat, TOP_K on HRX is a pure function of its input (engine ggml-org#2146) Seven cases (the Flash-Next indexer's 2560 -> 2051, heavy ties, 4096 -> 2048, a router-like 128 -> 8, an odd pitch, k == cols), 12 runs each: every row a valid top-k, bitwise-identical output across runs, and with distinct inputs the CPU's set. Skips without HRX0. Before (bench build of 962507f, the old kernel): FAIL, rc 1, 4 of 7 cases with 11 of 11 runs differing from run 1. After: owed on a build of this branch. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: bong-water-water-bong <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
…the CPU with the CPU's result (engine ggml-org#2146) The same seven cases as test-hrx-top-k-repeat, run with GGML_HRX_DISABLE_DISPATCH=top_k through a scheduler with HRX first and the CPU second. Each case requires that HRX does not claim the node, that the scheduler places it on the CPU, and that the indices equal the CPU backend's, entry for entry. Review on #331: nothing else takes this path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… CPU instead of failing the graph (engine ggml-org#2146) (#331) * hrx: the TOP_K and ARGSORT claims follow GGML_HRX_DISABLE_DISPATCH's own rule (engine ggml-org#2146) The registry skips a registration when a pattern is a substring of its name. The two supports_op claims that must decline with their only registration checked the other way round: they looked for the full name ("hip.top_k_f32", "common.argsort_rows_f32") inside the variable. So =top_k removed the matcher but kept the claim, and on Flash-Next at 18.8k the request failed with "unsupported HRX node 1: TOP_K" (the sparse-attention indexer's selection) instead of running TOP_K on the CPU. dispatch_name_disabled() is now the one rule; the registry and both claims call it. Co-Authored-By: Claude Opus 5.5 <[email protected]> * tests: test-hrx-top-k-declined, the switched-off TOP_K falls back to the CPU with the CPU's result (engine ggml-org#2146) The same seven cases as test-hrx-top-k-repeat, run with GGML_HRX_DISABLE_DISPATCH=top_k through a scheduler with HRX first and the CPU second. Each case requires that HRX does not claim the node, that the scheduler places it on the CPU, and that the indices equal the CPU backend's, entry for entry. Review on #331: nothing else takes this path. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: bong-water-water-bong <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
Fixes #2145. Allocate the
extramember ofggml_tensorstatically