Skip to content

attention: guarded identity online-softmax rescale skip on CUDA and Metal - #4520

Open
ashhart wants to merge 6 commits into
mainfrom
lead/softmax-identity-skip
Open

ashhart wants to merge 6 commits into
mainfrom
lead/softmax-identity-skip

Conversation

@ashhart

@ashhart ashhart commented Nov 22, 2026

Copy link
Copy Markdown
Owner

What this changes

Online softmax rescales its running state by exp(old_max - new_max) after each key tile; when the maximum doesn't change that factor is exactly 1. This skips the identity work in the served online-softmax updates on CUDA and Metal, behind guards: the shortcut runs only when the maximum is finite and every skipped operand or fast result is finite, so signed zeros, subnormals, NaN payloads and infinities behave as before. TF_CUDA_SOFTMAX_IDENTITY_GUARD / TF_METAL_SOFTMAX_IDENTITY_GUARD (compile-time, default 1) build the unguarded variant from the same sources so the GPU contract compares the two.

Not landing yet

This changes every served attention kernel's default, and the finite checks may cost what they save, so it lands only after the full kernel contract on sm_121, sm_120, M3 Ultra and M5 Ultra (guarded vs unguarded byte equality, planted controls red) and paired whole-request A-B-B-A per family with tokens compared. Kill on any byte or token difference, any slower cell, or under 3% whole-request gain.

Keep the finite stable-max guards behind TF_METAL_SOFTMAX_IDENTITY_GUARD. The A/B baseline compiles the same shaders with the flag at zero, with no copied baseline kernels or lean exemption.
Keep the finite stable-max guards behind TF_CUDA_SOFTMAX_IDENTITY_GUARD. Build the unguarded reference from the same CUDA sources with flag zero; update generated Qwen controls and remove all copied baselines.
@ashhart
ashhart force-pushed the lead/softmax-identity-skip branch from d60f2a4 to 81f7bb6 Compare November 23, 2026 18:38
@joshuaswarren

Copy link
Copy Markdown
Contributor

Attempted the sm_120 kernel contract on this head (d60f2a4b, RTX 5060 Ti eGPU, aarch64 Linux, driver 615.78.08, zig 0.17.0, /opt/cuda/bin/nvcc, -Dsm=120) — the runner does not compile, so the contract cannot run:

zig/tests/cuda/contract_qwen_vision_attn.zig:197:91: error: expected type '[:0]const u8', found '[]const u8'
            item.* = .{ .rig = .{ .gpu = gpu, .s = s }, .f = try self.mod.function(symbols[i]), ...
zig/src/cuda/module.zig:40:41: note: parameter type declared here
    pub fn function(self: Module, name: [:0]const u8) Error!Function {

contract_qwen_vision_attn.zig is new on this branch (absent on main); symbols[i] needs a sentinel-terminated slice there. I'll rerun kernel-contract on the next head once the runner builds.

This branch has not been deployed

No deployments
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