Skip to content

server: add option to output probabilities for completion - #1962

Merged
SlyEcho merged 27 commits into
ggml-org:masterfrom
WangHaoranRobin:master
Jul 2, 2023
Merged

SlyEcho merged 27 commits into
ggml-org:masterfrom
WangHaoranRobin:master

Conversation

@WangHaoranRobin

@WangHaoranRobin WangHaoranRobin commented Jun 21, 2023 •

Copy link
Copy Markdown
Contributor

related to #1784

I was playing around with the server example and wanted to expose the probabilities of the generated tokens to the server client to implement custom stopping sequences and criteria(similar to openai's api here).

This PR returns the probabilities by reading it from candidates_p.data[i].p after sampling functions are called.

Comment thread examples/common.h Resolved
Comment thread examples/server/server.cpp Resolved
Comment thread examples/server/server.cpp Resolved
Comment thread examples/common.h Resolved
@WangHaoranRobin

Copy link
Copy Markdown
Contributor Author

I have a question about partial UTF-8 character is generation:

In doCompletion:
https://github.com/ggerganov/llama.cpp/blob/7487137227eb32ed9b12156338b865cb29b2dfd1/examples/server/server.cpp#L388-L402

It seems like we are assuming that all partial character generation is 1 char long when converted to string. Is that true? I kinda used this assumption in tokens_to_output_formatted_string

@ghost

ghost commented Jun 23, 2023

Copy link
Copy Markdown

It seems like we are assuming that all partial character generation is 1 char long when converted to string. Is that true?

It seems to be 1 byte per token when I tested in practice and when I took a quick look at the tokenizer.json. But someone that knows more should check.
https://huggingface.co/oobabooga/llama-tokenizer/raw/main/tokenizer.json
The more robust way would be to instead check the value of the last 3 characters of the string to see how many there are pending.

@WangHaoranRobin

Copy link
Copy Markdown
Contributor Author

@SlyEcho are there any other requested changes? Should we merge this?

Comment thread examples/server/server.cpp Resolved
@SlyEcho

SlyEcho commented Jun 25, 2023

Copy link
Copy Markdown
Contributor

I'm taking a look now. I was occupied on the weekend by holidays and a certain bald Russian clown.

  1. Pretty sure any limit on the number of probabilities is unnecessary. OpenAI may have some incentive to limit their API, but for a user's own machine it's insignificant.
  2. The Unicode handling is just wrong, IMO. We already detect a partial mutliple byte character and in that situation it should just add to the probabilites list of the last token somehow.

@WangHaoranRobin

Copy link
Copy Markdown
Contributor Author

by holidays and a certain bald Russian clown

LOL

What do you mean by

add to the probabilities list of the last token?

Are you saying there should be something is the server context like std::vector<completion_token_output> current_token_probs; ? Isn't that just repeated information with generated_token_probs?

I feel like the current logic is the easiest. Under the assumption that llama.generated_text to llama.generated_token_probs is bijective, i.e. we send the same amount of probs object as tokens.

@SlyEcho

SlyEcho commented Jun 25, 2023

Copy link
Copy Markdown
Contributor

I'm currently thinking what would be best way. It's also possible that other alternative tokens could come out as incomplete and should we keep generating until we have n_probs amount of complete items?

Comment thread examples/server/server.cpp Resolved
@WangHaoranRobin

Copy link
Copy Markdown
Contributor Author

should we keep generating until we have n_probs amount of complete items?

As in that we should continue generation for all n_probs possible scenarios we got from last inference? If we are talking about 4 byte characters and n_probs = 5, that's 5^4=625 possible complete utf-8 characters.

@SlyEcho

SlyEcho commented Jun 25, 2023

Copy link
Copy Markdown
Contributor

As in that we should continue generation for all n_probs possible scenarios we got from last inference? If we are talking about 4 byte characters and n_probs = 5, that's 5^4=625 possible complete utf-8 characters.

Yeah, this is kind of a lot, we could keep the best 5 but it would mean a lot of token generating. Or to only keep the alternates from the last one.

@WangHaoranRobin

WangHaoranRobin commented Jun 25, 2023 •

Copy link
Copy Markdown
Contributor Author

Not that we need to copy exactly openai's API. But here is an example of how they handle multibyte:

"top_logprobs": [
{
"bytes:\xef": -0.5548667,
"\n": -0.9359168,
"?": -3.5638626,
"bytes:\xe5\x91": -6.030914,
"bytes:\xe5": -6.4047127
},
{
"bytes:\xbc": -2.2159093e-06,
"bytes:\xbd": -13.45378,
"bytes:\xbb": -14.923321,
"bytes:\xb9": -15.295079,
"bytes:\xbf": -16.138496
},
{
"bytes:\x9f": -0.0011532516,
"bytes:\x8c": -7.17465,
"bytes:\x9a": -7.992851,
"bytes:\x81": -10.874427,
"bytes:\x89": -11.387907
},
...

And I think we shouldn't just return probs of the last token either, as the probs of other partial chars are also useful information.

we could keep the best 5 but it would mean a lot of token generating

This would mean n_probs times inference time(50ms -> 250ms at n_probs=5, when the user only intended to get information about token probs). Which I don't find reasonable.

@SlyEcho

SlyEcho commented Jun 25, 2023

Copy link
Copy Markdown
Contributor

Ok, let's leave it like that for now and try to think how to improve in the future.

@SlyEcho
SlyEcho merged commit d7d2e6a into ggml-org:master Jul 2, 2023
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
)

* server: add option to output probabilities for completion
* server: fix issue when handling probability output for incomplete tokens for multibyte character generation
* server: fix llama_sample_top_k order
* examples/common.h: put all bool variables in gpt_params together
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
)

* server: add option to output probabilities for completion
* server: fix issue when handling probability output for incomplete tokens for multibyte character generation
* server: fix llama_sample_top_k order
* examples/common.h: put all bool variables in gpt_params together
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
)

* server: add option to output probabilities for completion
* server: fix issue when handling probability output for incomplete tokens for multibyte character generation
* server: fix llama_sample_top_k order
* examples/common.h: put all bool variables in gpt_params together
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
)

* server: add option to output probabilities for completion
* server: fix issue when handling probability output for incomplete tokens for multibyte character generation
* server: fix llama_sample_top_k order
* examples/common.h: put all bool variables in gpt_params together
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
)

* server: add option to output probabilities for completion
* server: fix issue when handling probability output for incomplete tokens for multibyte character generation
* server: fix llama_sample_top_k order
* examples/common.h: put all bool variables in gpt_params together
bong-water-water-bong pushed a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 10, 2026
…re (engine ggml-org#1962)

The dense form of engine ggml-org#1539. The f32-activation matmul core narrows its
activations to f16 for the multiply; a value above 65504 became inf and the
product NaN (or, without a NaN, a wrong number nothing reports). Narrow at
2^-6 and scale the f32 accumulator back by 2^6 on publish, as the routed
core now does: exact while the scaled value is a normal f16, window
[3.9e-3, 4.19e6].

test-backend-ops gains MUL_MAT cases with activations of 9.1e4 (the
magnitude measured in flint-40b) for the 47 weight-type / token-count
combinations this backend now answers correctly. Nine combinations still
overflow in other kernel families and are listed on the issue rather than
in the suite.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 10, 2026
…re (engine ggml-org#1962; stacked on #284) (#285)

* hrx: keep routed-expert activations inside the f16 range, so a large SwiGLU output is not inf (engine ggml-org#1539)

The routed-expert matmul narrows its f32 activations to f16 for the
multiply, and f16 ends at 65504. An expert's down-projection reads the
SwiGLU output, which nothing normalises: flint-40b's layer 46 reaches
+-9.1e4 on an ordinary 22-token prompt. The narrowing made that inf, the
sum over the routes inf - inf = NaN, and the NaN was stored as those
tokens' keys and values, where the next shorter request on the slot found
it inside its masked attention window and returned all-NaN logits.

The activations are now narrowed at 2^-6 of their value and the f32
accumulator is scaled back by 2^6 when a tile is published. Both factors
are powers of two, so a value whose scaled form is a normal f16 is rounded
exactly as before; the representable window moves from [6.1e-5, 6.55e4]
to [3.9e-3, 4.19e6].

test-backend-ops gains MUL_MAT_ID cases at flint's contraction with
activations of the measured magnitude: the 22-token one returned NaN
against the CPU's 56214.23 before this change.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

* tests: pin the lower edge of the routed-matmul f16 window too (review of #284)

The prescale trades the top of the f16 range for the bottom, and the large
case only pulls one way. Add the same contraction with every activation
scaled to 3.2e-2, the smallest column maximum measured on flint's expert
down-projections (37278 columns over 141 layer executions; median 4.1,
maximum 91161).

Co-Authored-By: Claude Fable 5.2 <[email protected]>

* hrx: keep dense matmul activations inside the f16 range in the f32 core (engine ggml-org#1962)

The dense form of engine ggml-org#1539. The f32-activation matmul core narrows its
activations to f16 for the multiply; a value above 65504 became inf and the
product NaN (or, without a NaN, a wrong number nothing reports). Narrow at
2^-6 and scale the f32 accumulator back by 2^6 on publish, as the routed
core now does: exact while the scaled value is a normal f16, window
[3.9e-3, 4.19e6].

test-backend-ops gains MUL_MAT cases with activations of 9.1e4 (the
magnitude measured in flint-40b) for the 47 weight-type / token-count
combinations this backend now answers correctly. Nine combinations still
overflow in other kernel families and are listed on the issue rather than
in the suite.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

* tests: pin the lower edge of the dense f16 window too (review of #285)

Every activation at 2.4e-2, the smallest column maximum measured on the
dense matmul inputs of Qwen3-0.6B, Qwen3-4B, ZAYA1-8B and flint-40b, for
q4_K, q8_0 and bf16 weights at the 22-token batch.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 10, 2026
…K kernels (engine ggml-org#1962) (#288)

The Q6_K i8-prepacked and packed (aligned-skinny) kernels narrow their f32
activations to f16 for the multiply, so an activation above 65504 became
inf and the result NaN. Narrow at 2^-6 and scale the f32 accumulator back
by 2^6 before it is published, as the f32 matmul cores do: exact while the
scaled value is a normal f16, window [3.9e-3, 4.19e6].

test_mul_mat_resident_weight_large_activation reaches these kernels with a
resident weight and activations of 9.1e4 (NaN before, pass after) and with
every activation at 2.4e-2 (the lower edge).

The 512-token prefill kernels for Q4_K, Q5_K and Q6_K read activations a
separate step has already narrowed, and are not changed here.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
bong-water-water-bong pushed a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 10, 2026
…the f16 limit (engine ggml-org#1962)

Review of #292: the comment now says why the rebuilt value cannot overflow and
that inf and NaN are both rebuilt; a 6.5e4 case sits on the last ladder step
where the stored sum is still finite, next to the 6.6e4 one where it is not.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 10, 2026
…ange (engine ggml-org#1962) (#292)

* hrx: the Q5_K/IQ4_XS prefill rebuilds a block sum that left the f16 range (engine ggml-org#1962)

The prefill kernel reads its activations as 8-bit blocks. Each block stores its
scale and its scaled sum (scale times the sum of the 32 codes) as f16. The scale
fits for activations up to 8.3e6, but the scaled sum is about as large as the
largest activation in the block, so it became inf above 65504 and the offset
correction of the K-quant weights turned the output into NaN.

A stored sum that is not finite is now rebuilt in the reader from the codes the
lane already holds; a finite one is used as stored, so ordinary data takes the
path it always took and the block layout does not change.

Cases: Q5_K, 512 tokens, resident weight, activations at 6.6e4, 9.1e4, 1e6 and
the 2.4e-2 lower edge.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

* hrx: state the rebuilt sum's range, and a case on the stored side of the f16 limit (engine ggml-org#1962)

Review of #292: the comment now says why the rebuilt value cannot overflow and
that inf and NaN are both rebuilt; a 6.5e4 case sits on the last ladder step
where the stored sum is still finite, next to the 6.6e4 one where it is not.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 11, 2026
…n activation above 65504 stays finite (engine ggml-org#1962) (#298)

The Q4_K and Q6_K prefill kernels (128 tokens and up) multiply an f16 copy of the activation. The copy was a plain
f32 -> f16 narrowing, so an activation above the f16 maximum became inf and the product NaN.

- The copies the dispatch makes (ggml_copy_f32_f16, ggml_copy_f16_k16_major from f32) store value * 2^-6.
- The fused SwiGLU prefill kernel publishes the f16 form of its result at 2^-6 too. That result is the
  down-projection's activation, the place where a model leaves the range (engine ggml-org#1539).
- The scale is recorded with the alternate value / generated resource (scale_log2). A reader that does not ask
  for a scale only finds unscaled values, so no other kernel reads a scaled copy by accident.
- The prefill body and the Q6_K narrow-tile body scale their f32 sums back by 2^6 (exact) when the dispatch
  passes ggml.f16_prefill.activation_scale_log2 = 6. An unscaled F16 value from another producer is read as before.
- The Q6_K narrow-tile body also prescales its own f32 narrowing.

Tests: test-backend-ops resident-weight cases for Q4_K at 256/512 tokens and Q6_K at 128/256/512 with 9.1e4 and
2.4e-2 activations; test-hrx-ops ffn.q4_k.prefill.large_activation.{256,512} runs down(swiglu(gate, up)) through
the fused kernels with a gated maximum of 4.0e5; hrx-backend-test plan rows look the f16 copy up under its scale;
the test-hrx-ops plan helper prints the plan status when a metadata append is refused.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 11, 2026
…written, and stays finite above the f16 range (engine ggml-org#1962) (#303)

common.mul_mat.q6_k_token1_shortlist (a Q6_K projection onto a vocabulary of at
least 65,536 at one token, downstream of the draft head's concat projection) had
no test, and two defects:

- The matcher asked for 96 selected activation groups at every width. The
  selection kernel writes one id per 32-wide group that exists and the scan
  reads as many ids as were asked for, so below a width of 3072 the tail of the
  id buffer was never written and was used as group indices: a device memory
  fault at width 256, 16 stray indices at 2560 (Qwen3.8-Flash-Next). The request
  is now min(96, input_size / 32).
- The refine kernel narrowed the f32 activation to f16 unscaled and accumulated
  the logit in f16, so one activation above 65504 made every refined logit inf.
  The body now narrows at 2^-6 and the exported kernel scales the published
  logit back by 2^6.

Tests: test-hrx-ops mul_mat.q6_k.token1_shortlist (ordinary data) and
.large_activation (one activation above 65504, logits inside f16), comparing the
refined candidates and the best token with the CPU; hrx-backend-test
q6k_token1_shortlist_group_count.{256,2560,4096}, device-free.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 11, 2026
… small-batch token counts (engine ggml-org#1962) (#306)

ffn.q4_k.prefill.large_activation ran at 256 and 512 tokens only. It now also runs at 1, 2, 4, 5, 8, 16, 64 and 128, where the decode and small-batch kernels take the gate/up and the down-projection. The case prints the kernels that ran. The fused prefill kernels are still required from 256 tokens up.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
bong-water-water-bong added a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 11, 2026
…m that left the f16 range (engine ggml-org#2186) (#319)

The 8-bit activation blocks store their scale and their scaled sum (scale times the sum of the 32 codes) as f16.
The sum overflows once a block of same-signed activations averages above 2047, while the scale still fits.
qwen3_moe_q4k_q8_1_dot, the one function every Q4_K reader of these blocks goes through, used the stored sum
as is; it now rebuilds a non-finite sum from the 16 codes it already holds (the rule mul_mat_q5_k_q8_plane_wmma
has had since engine ggml-org#1962). A finite stored sum is used unchanged.

New device cases decode_routed_moe.q4_K.large_activation.{1,1000,3000,30000}.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants