Skip to content

ggml : set output of view src - #25729

Merged
ggerganov merged 9 commits into
ggml-org:masterfrom
am17an:graph-set-outputs
Jul 28, 2026
Merged

ggerganov merged 9 commits into
ggml-org:masterfrom
am17an:graph-set-outputs

Conversation

@am17an

@am17an am17an commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Overview

The ggml_set_output now also sets the src tensors of views as outputs as well. This is to prevent from the allocator from reusing their memory.

Requirements

@am17an
am17an requested a review from CISC as a code owner July 15, 2026 17:19
@ggerganov

Copy link
Copy Markdown
Member

A bit more info would be helpful - where do we use views for output?

@am17an

am17an commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

I'm adding the deepseek4 MTP, there we need to add a hidden state from a reshape (which is a view)

if (inp_out_ids) {
ggml_tensor * flat = ggml_reshape_2d(ctx0, inpL, n_embd*hc, n_tokens);
flat = ggml_get_rows(ctx0, flat, inp_out_ids);
inpL = ggml_reshape_3d(ctx0, flat, n_embd, hc, n_outputs);
}

@ggerganov

Copy link
Copy Markdown
Member

I think you can simply output the hidden state before the reshape? The hidden state extraction logic only needs to know the total size of the tensor - the specific dimensions are not relevant.

@am17an

am17an commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

The unflattened tensor is 3d, get_embeddings_nextn expects a 2-d tensor of dim [n_outputs][n_embd]

@am17an

am17an commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Also in general this is a bug right? If any t is a view then this can possibly cause issues. We have no documentation that says that these things can't be views.

@ggerganov

Copy link
Copy Markdown
Member

Also in general this is a bug right? If any t is a view then this can possibly cause issues. We have no documentation that says that these things can't be views.

Yes, we should assert in the set_outputs() that none of the outputs are views.

The unflattened tensor is 3d, get_embeddings_nextn expects a 2-d tensor of dim [n_outputs][n_embd]

I don't follow. Is this a problem? Normally, all embeddings and hidden states that we extract are 2D.

@am17an

am17an commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

It's not a problem for the MTP, I'll just update this PR to add an assert for outputs not being views then

@ggerganov

Copy link
Copy Markdown
Member

@am17an am17an changed the title llama-graph: set_outputs to t->view_src llama-graph: assert that views cannot be outputs Jul 16, 2026
@am17an

am17an commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

It looks like some cases with backend_sampling: true are failing

@am17an

am17an commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

This is the problem. The top-p default returns a view as output

auto ggml_sort = [ctx](struct ggml_tensor * a, struct ggml_tensor * b) {
GGML_ASSERT(ggml_nrows(a) == 1);
struct ggml_tensor * a_reshaped = ggml_reshape_2d(ctx, a, 1, a->ne[0]);
struct ggml_tensor * a_sorted = ggml_get_rows(ctx, a_reshaped, b);
return ggml_reshape_1d(ctx, a_sorted, a->ne[0]);
};
// Get the sorted logits in descending order.
struct ggml_tensor * sorted_idx = ggml_argsort(ctx, data->logits, GGML_SORT_ORDER_DESC);
ggml_set_name(sorted_idx, "top_p_sorted_idx");
// Do the sorting via reshape + get_rows
struct ggml_tensor * sorted_logits = ggml_sort(data->logits, sorted_idx);
ggml_set_name(sorted_logits, "top_p_sorted_logits");
struct ggml_tensor * softmax = ggml_soft_max(ctx, sorted_logits);
ggml_set_name(softmax, "top_p_softmax");
// If candidates are provided, sort them as well. Otherwise, set sorted indices as candidates.
if (data->candidates) {
data->candidates = ggml_sort(data->candidates, sorted_idx);
} else {
data->candidates = sorted_idx;
}
ggml_set_name(data->candidates, "top_p_candidates");

@ggerganov ggerganov left a comment

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.

Should be good to merge. Might have some impact on the #25532

@gaugarg-nv

gaugarg-nv commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

I faced a similar issue in #25532

How about fixing it within GGML?

We can propagate the output flag to the backing tensor in ggml_set_output. Something like this-

void ggml_set_output (struct ggml_tensor * tensor) {
  for (ggml_tensor * cur = tensor; cur != nullptr; cur = cur->view_src) {
      cur->flags |= GGML_TENSOR_FLAG_OUTPUT;
  }
}

@ggerganov

Copy link
Copy Markdown
Member

I was a bit hesitant to make it since I initially thought that we don't need to use output views. But it does seem that there are some use cases where it's complicated to avoid having a view for output.

@gaugarg-nv

Copy link
Copy Markdown
Contributor

I see that @am17an's first commit in this PR did something similar. But it's not clear to me why an output can't be a "view".
As long as we make sure the backing tensor stays alive during graph execution, we should be fine.

The current assert also enforces this only for tensors registered in llm_graph_result. What if ggml_set_output is called on a view somewhere else?

@ggerganov

Copy link
Copy Markdown
Member

Ok, I added the change to ggml_set_output. Will keep the changes to the llama-sampler.cpp since I think they improve consistency and should resolve some edge-cases when shaping the input logits of the samplers.

@ggerganov
ggerganov requested a review from gaugarg-nv July 28, 2026 10:55
@github-actions github-actions Bot added the ggml changes relating to the ggml tensor library for machine learning label Jul 28, 2026
Comment thread src/llama-graph.cpp Outdated
Comment thread src/llama-sampler.cpp Outdated
Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>
@ggerganov
ggerganov merged commit da5b448 into ggml-org:master Jul 28, 2026
21 of 26 checks passed
@ggerganov ggerganov changed the title llama-graph: assert that views cannot be outputs ggml : set output of view src Jul 28, 2026
@am17an
am17an deleted the graph-set-outputs branch July 29, 2026 10:24
TrevorS added a commit to TrevorS/llama.cpp that referenced this pull request Aug 1, 2026
TrevorS added a commit to TrevorS/llama.cpp that referenced this pull request Aug 2, 2026
huaxel pushed a commit to huaxel/UmaLlama that referenced this pull request Aug 2, 2026
* llama-graph: set_outputs to t->view_src

* change set_output to GGML_ASSERT about views not being outputs

* sampler : avoid views in outputs

* cont : fix dist sampler

* cont : consistent logits handling

* ggml : set output of view src

* graph : simplify set_outputs()

* cont : cleanup

Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>
satindergrewal pushed a commit to satindergrewal/llama.cpp that referenced this pull request Aug 12, 2026
* llama-graph: set_outputs to t->view_src

* change set_output to GGML_ASSERT about views not being outputs

* sampler : avoid views in outputs

* cont : fix dist sampler

* cont : consistent logits handling

* ggml : set output of view src

* graph : simplify set_outputs()

* cont : cleanup

Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
* llama-graph: set_outputs to t->view_src

* change set_output to GGML_ASSERT about views not being outputs

* sampler : avoid views in outputs

* cont : fix dist sampler

* cont : consistent logits handling

* ggml : set output of view src

* graph : simplify set_outputs()

* cont : cleanup

Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
* llama-graph: set_outputs to t->view_src

* change set_output to GGML_ASSERT about views not being outputs

* sampler : avoid views in outputs

* cont : fix dist sampler

* cont : consistent logits handling

* ggml : set output of view src

* graph : simplify set_outputs()

* cont : cleanup

Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants