Skip to content

ggml: allow reserving scratch memory in graph_optimize - #31898

Merged
ggerganov merged 3 commits into
masterfrom
aman/sched-scratch
Nov 25, 2026
Merged

ggerganov merged 3 commits into
masterfrom
aman/sched-scratch

Conversation

@am17an

@am17an am17an commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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.

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 --fit as well.

Overview

Additional information

Requirements

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`.
@am17an
am17an requested a review from ggerganov as a code owner November 8, 2026 15:14
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning labels Nov 8, 2026
@am17an

am17an commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

@jeffbolznv what do you think about this one?

Comment thread ggml/src/ggml-backend-impl.h Resolved
Comment thread ggml/src/ggml-backend-impl.h Resolved
// `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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the return value get stored and how does the backend access it later during graph_compute?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 .

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move this ownership to the backend scheduler potentially via a map, and pass it along a backend's graph_compute

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this as a problem for the meta backend and not this PR, as it already cannot do add_alloc_dep either.

@ORippler ORippler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm generally for this, as it would unlock activation-quantization-reuse across nodes in the CUDA backend: #18538

@jeffbolznv

Copy link
Copy Markdown
Contributor

I'm generally for this, as it would unlock activation-quantization-reuse across nodes in the CUDA backend: #18538

I don't think this reuse is blocked on this change, ggml-vulkan has been doing something like this for a long time.

@am17an

am17an commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

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 --fit

@am17an

am17an commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@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.

@ggerganov ggerganov self-assigned this Nov 22, 2026
Comment thread ggml/src/ggml-backend.cpp Outdated
Comment on lines +1884 to +1885
// scratch tensors not yet added to graph_copy
std::unordered_set<ggml_tensor *> scratch_pending;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scratch_pending does not need to be in the optimize_state - it can be constructed from the scratch contents.

@am17an am17an added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Nov 23, 2026
@ggerganov
ggerganov merged commit ea72294 into master Nov 25, 2026
35 of 38 checks passed
Mr-J-369 pushed a commit to Mr-J-369/llama.cpp-Android that referenced this pull request Nov 26, 2026
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants