Repository navigation
ggml: allow reserving scratch memory in graph_optimize - #31898
Conversation
Add add_scratch to ggml_backend_graph_optimize_params. A backend can reserve compute buffer memory that is allocated before a node `first` and kept until a node `last` has been computed, e.g. for data that several nodes share. The scheduler adds the scratch tensor to the graph copy right before `first` and frees it with an alloc dep at `last`.
|
@jeffbolznv what do you think about this one? |
| // `first` and `last` are nodes of the same graph, and `last` must not come before `first` in the optimized graph | ||
| // returns a 1-d GGML_TYPE_I8 tensor that is not a node of the graph: its data is set when the graph is allocated, | ||
| // and it is valid until the scheduler splits the next graph (which calls graph_optimize again) | ||
| struct ggml_tensor * (*add_scratch)(void * user_data, size_t nbytes, struct ggml_tensor * first, struct ggml_tensor * last); |
There was a problem hiding this comment.
Where does the return value get stored and how does the backend access it later during graph_compute?
There was a problem hiding this comment.
The backend has to store this tensor itself with the guarantee that it will last from first to last. For CUDA it is relatively easy I plan to store it first->extra
There was a problem hiding this comment.
How is this going to be handled in the meta backend? I mean it currently does not call graph_optimize yet, but afaik that extra field is in no way consistified yet in ggml: #28999 .
There was a problem hiding this comment.
We could move this ownership to the backend scheduler potentially via a map, and pass it along a backend's graph_compute
There was a problem hiding this comment.
I see this as a problem for the meta backend and not this PR, as it already cannot do add_alloc_dep either.
I don't think this reuse is blocked on this change, ggml-vulkan has been doing something like this for a long time. |
|
quantization re-use is just one of many use-cases. This can be used for storing any kind of intermediate including cublas/cutlass handles, also creating scratch spaces for directly writing quantized q8_1/fp4 into the down projection's src1 (already worth 6% prefill). The added benefit being these are all counted with |
|
@ggerganov can you take a look at this one? I think it's a good way to add scratch tensors every backend can use. The memory is managed by the graph allocator and we can re-use memory for multiple ops unlike the way of adding some scratch to the back of the dst tensor. |
| // scratch tensors not yet added to graph_copy | ||
| std::unordered_set<ggml_tensor *> scratch_pending; |
There was a problem hiding this comment.
The scratch_pending does not need to be in the optimize_state - it can be constructed from the scratch contents.
* ggml: allow reserving scratch memory in graph_optimize Add add_scratch to ggml_backend_graph_optimize_params. A backend can reserve compute buffer memory that is allocated before a node `first` and kept until a node `last` has been computed, e.g. for data that several nodes share. The scheduler adds the scratch tensor to the graph copy right before `first` and frees it with an alloc dep at `last`. * ggml: clarify the lifetime of add_scratch memory * ggml: build the pending scratch set from the scratch map
Add
add_scratchto ggml_backend_graph_optimize_params. A backend can reserve compute buffer memory that is allocated before a nodefirstand kept until a nodelasthas been computed, e.g. for data that several nodes share. The scheduler adds the scratch tensor to the graph copy right beforefirstand frees it with an alloc dep atlast.This helps in the following cases: sharing quantizations for src1 activations, sharing expert routing for mul_mat_id, and various other scratch spaces which allows them to be counted in
--fitas well.Overview
Additional information
Requirements