Skip to content

ggml : add ggml_clamp() - #1539

Merged
ggerganov merged 2 commits into
masterfrom
ggml_clamp
May 20, 2023
Merged

ggerganov merged 2 commits into
masterfrom
ggml_clamp

Conversation

@ggerganov

@ggerganov ggerganov commented May 20, 2023 •

Copy link
Copy Markdown
Member

sync from ggml repo

Used in replit / MPT inference

@ggerganov
ggerganov merged commit 3de84b2 into master May 20, 2023
@ggerganov
ggerganov deleted the ggml_clamp branch May 20, 2023 12:34
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* ggml : add ggml_clamp()

* ggml : indentation
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
* ggml : add ggml_clamp()

* ggml : indentation
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
* ggml : add ggml_clamp()

* ggml : indentation
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* ggml : add ggml_clamp()

* ggml : indentation
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* ggml : add ggml_clamp()

* ggml : indentation
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 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 pushed a commit to 1bit-MONSTER/llama.cpp that referenced this pull request Nov 13, 2026
AMD's file binds common.mul_mat_id.f32_f32_wmma to AMD's tiled/skinny pair kernels, which do not keep activations
inside the f16 range (ggml-org#1539/ggml-org#2167): on the device, MUL_MAT_ID f32/bf16 at activation_scale 1e4/1e5 and a q5_K
magnitude row return inf with it and pass with ours (test-backend-ops -b HRX0, gpu-kernels add-on b0ed206). AMD's
file also dropped our common.mul_mat_id_postops.completion_counters registration.

Co-Authored-By: Claude Opus 5.5 <[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.

1 participant