Skip to content

inkling2: skip redundant softcap before argmax in lm_head_argmax - #75

Merged
Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
ybenyedder:fix/dspark-skip-softcap-argmax
Nov 22, 2026
Merged

Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
ybenyedder:fix/dspark-skip-softcap-argmax

Conversation

@ybenyedder

Copy link
Copy Markdown
Contributor

Since softcap * tanh(x / softcap) is strictly monotonic for positive softcap, argmax is invariant. Skipping be.softcap in Device::lm_head_argmax avoids an extra 200k-element kernel dispatch + barrier on every DSpark draft token (17.2 -> 17.5 tok/s on Radeon 890M Vulkan).

if softcap != 0.0 {
be.softcap(s.logits, c.n_vocab, softcap)?;
}
be.argmax_rows(s.logits, c.n_vocab, 1, s.amax)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Correctness · 1 of 4

Dropping softcap can change argmax through f32 saturation ties. softcap*tanh(x/softcap) is monotonic in exact arithmetic, but in f32 large logits saturate to the same value. Two distinct raw logits that the capped reference maps to a tie can now differ, so argmax_rows can pick a different token than the softcapped path, and tie-breaking may differ too. Output can diverge from the reference model on those steps.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 685
Symbol: prefill_chunk_seq
Issue: Dropping softcap can change argmax through f32 saturation ties. softcap*tanh(x/softcap) is monotonic in exact arithmetic, but in f32 large logits saturate to the same value. Two distinct raw logits that the capped reference maps to a tie can now differ, so argmax_rows can pick a different token than the softcapped path, and tie-breaking may differ too. Output can diverge from the reference model on those steps.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

/// Project a single corrected hidden vector `h_corr` (`[n_embd]`) through the target's LM head on GPU
/// and return the argmax token ID (`0..c.n_vocab`). `softcap * tanh(x / softcap)` is strictly monotonic
/// so argmax does not need the softcap kernel.
pub(super) fn lm_head_argmax(&self, m: &Inkling2Model, h_corr: &[f32], _softcap: f32) -> Result<u32> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Correctness · 2 of 4

Doc claims strict monotonicity, which does not hold in f32. The new doc says the transform is strictly monotonic so argmax does not need it. In f32 saturation makes it non-injective for large |x|, so a future maintainer may trust the claim and remove other numerical safeguards or miss the tie behaviour when debugging divergent tokens.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 678
Symbol: record_dspark_visit
Issue: Doc claims strict monotonicity, which does not hold in f32. The new doc says the transform is strictly monotonic so argmax does not need it. In f32 saturation makes it non-injective for large |x|, so a future maintainer may trust the claim and remove other numerical safeguards or miss the tie behaviour when debugging divergent tokens.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

let d = dev::get().unwrap().lock().unwrap();
let be = &*d.parts[0].be;
be.put(h_corr, &self.scr, ((s.hn - o) / 4) as usize)?;
be.gemvq(d.tensor(m.output.gpu.unwrap()), s.hn, c.n_embd, 0, s.logits, c.n_vocab, 1)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Correctness · 3 of 4

s.logits now holds uncapped values for any later reader. s.logits used to hold softcapped values. If anything reads it after this function (logprobs, top-k sampling, speculative acceptance, debug dumps), it now gets raw logits. Those values are not in the same scale as before, so probabilities or thresholds computed from them are wrong. The diff does not show whether any such reader exists.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 684
Symbol: prefill_chunk_seq
Issue: s.logits now holds uncapped values for any later reader. s.logits used to hold softcapped values. If anything reads it after this function (logprobs, top-k sampling, speculative acceptance, debug dumps), it now gets raw logits. Those values are not in the same scale as before, so probabilities or thresholds computed from them are wrong. The diff does not show whether any such reader exists.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

/// Project a single corrected hidden vector `h_corr` (`[n_embd]`) through the target's LM head on GPU
/// and return the argmax token ID (`0..c.n_vocab`). `softcap * tanh(x / softcap)` is strictly monotonic
/// so argmax does not need the softcap kernel.
pub(super) fn lm_head_argmax(&self, m: &Inkling2Model, h_corr: &[f32], _softcap: f32) -> Result<u32> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Low · Maintainability · 4 of 4

Unused _softcap parameter left in signature and callers. The softcap argument is now ignored, but every caller still computes and passes it. This keeps a misleading parameter that a future edit could assume is applied. Removing it from the signature and call sites would make the no-op explicit.

Prompt for your AI agent
File: src/inkling2/forward/device.rs
Line: 678
Symbol: record_dspark_visit
Issue: Unused _softcap parameter left in signature and callers. The softcap argument is now ignored, but every caller still computes and passes it. This keeps a misleading parameter that a future edit could assume is applied. Removing it from the signature and call sites would make the no-op explicit.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

@aletheore

aletheore Bot commented Nov 22, 2026

Copy link
Copy Markdown

Aletheore Flash Lite review

What this PR does. This change removes the softcap kernel call from lm_head_argmax in the inkling2 device forward path, relying on the fact that softcap is strictly monotonic so argmax results are unchanged. The softcap parameter is kept in the signature but renamed to _softcap and is no longer used. It affects the inkling2 model's GPU LM-head argmax path, so greedy token selection runs one fewer kernel per step.

Merge confidence Review effort Merge risk
2 / 5 1 / 5 Medium
Area Files Lines
src/inkling2 1 +4 −6

4 finding(s) posted as inline review comment(s).

# Severity Category Where
1 🟠 High Correctness device.rs:685 Dropping softcap can change argmax through f32 saturation ties. softcap*tanh(x/softcap) is monotonic in exact…
2 🟠 High Correctness device.rs:678 Doc claims strict monotonicity, which does not hold in f32. The new doc says the transform is strictly…
3 🟠 High Correctness device.rs:684 s.logits now holds uncapped values for any later reader. s.logits used to hold softcapped values. If anything…
4 🔵 Low Maintainability device.rs:678 Unused _softcap parameter left in signature and callers. The softcap argument is now ignored, but every…
Review details

Reviewed 1 file(s); skipped 0. Dismissed 0; outside PR diff 0. Grounding: 4 of 4 proposed finding(s).

Fix all with your agent
File: src/inkling2/forward/device.rs
Line: 685
Symbol: prefill_chunk_seq
Issue: Dropping softcap can change argmax through f32 saturation ties. softcap*tanh(x/softcap) is monotonic in exact arithmetic, but in f32 large logits saturate to the same value. Two distinct raw logits that the capped reference maps to a tie can now differ, so argmax_rows can pick a different token than the softcapped path, and tie-breaking may differ too. Output can diverge from the reference model on those steps.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/inkling2/forward/device.rs
Line: 678
Symbol: record_dspark_visit
Issue: Doc claims strict monotonicity, which does not hold in f32. The new doc says the transform is strictly monotonic so argmax does not need it. In f32 saturation makes it non-injective for large |x|, so a future maintainer may trust the claim and remove other numerical safeguards or miss the tie behaviour when debugging divergent tokens.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/inkling2/forward/device.rs
Line: 684
Symbol: prefill_chunk_seq
Issue: s.logits now holds uncapped values for any later reader. s.logits used to hold softcapped values. If anything reads it after this function (logprobs, top-k sampling, speculative acceptance, debug dumps), it now gets raw logits. Those values are not in the same scale as before, so probabilities or thresholds computed from them are wrong. The diff does not show whether any such reader exists.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

File: src/inkling2/forward/device.rs
Line: 678
Symbol: record_dspark_visit
Issue: Unused _softcap parameter left in signature and callers. The softcap argument is now ignored, but every caller still computes and passes it. This keeps a misleading parameter that a future edit could assume is applied. Removing it from the signature and call sites would make the no-op explicit.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

@aletheore

aletheore Bot commented Nov 22, 2026

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph LR
    n0["device.rs"]
    n1["forward.rs"]
    n0 --> n1
    classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
    classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
    classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
    class n0 changed
    class n1 dependent
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • src/inkling2/forward/device.rs · modified · +4/-6 · 1 other dependent

Aggregate deltas

  • Modules: +0
  • Dependency graph edges: +0
  • Commits: 1

@Vasyl-Trefilov
Vasyl-Trefilov merged commit 2a581f1 into RustingStudio:main Nov 22, 2026
6 checks passed
@Vasyl-Trefilov

Copy link
Copy Markdown
Member

@ybenyedder Merged, ty. The token is a draft that the target checks, so the text cannot change: 200 greedy tokens with the DSpark file are the same before and after on UD-IQ2_XXS and UD-IQ3_XXS, on CUDA and Vulkan.

On top of the PR I removed the unused parameter, the softcap of the CPU fallback and the softcap field of the draft model, and reworded the comment (in f32 the softcap keeps the order but is not strictly monotonic). On the RTX 3060 the gain is inside the noise of a draft run, so I have no number of my own to add to yours.

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.

2 participants