Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Each release's page on GitHub has its notes and measurements. See [the 1.0.0 release notes](docs/release-notes-1.0.0.md) for the native binary's supported models and migration details. For Desktop installers and changes awaiting a Desktop build, see the [Desktop changelog](app/CHANGELOG.md).

## Unreleased
- CUDA attention: guard stable-max rescale operations and keep warp reductions unconditional (#4520).

- Inkling 2 Mini on Metal: split router and gate passes are vectorized and the ungated all-reduce feeds the norm directly, with tokens unchanged; decode is about 1% faster on M3 and M5 Ultra (#5207, refs #5191). Thanks to @gilby.

Expand Down
6 changes: 6 additions & 0 deletions zig/build/kernel_contract.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub fn hostTests(b: *std.Build, family: *std.Build.Step) void {
b.step("test-kernel-seed-host", "Named contract fixtures keep their seed when entries are inserted").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module(b, b.graph.host, .debug), .filters = &.{"contract seed:"} })).step);
const step = b.step("test-kernel-contract", "The kernel contract's host tests: gate logic, controls, every launch against every chip");
step.dependOn(&run.step);
const cuda_identity = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/cuda_softmax_identity_host.zig"), .target = b.graph.host, .optimize = .debug }) }));
b.step("test-cuda-softmax-identity-host", "CUDA rescale arithmetic, exceptional inputs and convergent warp source guards").dependOn(&cuda_identity.step);
step.dependOn(&cuda_identity.step);
const q27_pages = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("zig/tests/contract/q27_paged_cases.zig"),
.target = b.graph.host,
Expand Down Expand Up @@ -310,6 +313,9 @@ pub fn cuda(b: *std.Build, runner: *std.Build.Step.Compile, controls: ?std.Build
for (names, h3_image) |n, file| if (file) |f| install.dependOn(&b.addInstallFile(f, b.fmt("fatbin/{s}.fatbin", .{n})).step);
const run = b.addRunArtifact(runner);
run.addArg("kernel-contract");
const identity = b.addRunArtifact(runner);
identity.addArgs(&.{ "kernel-contract", "attn", "attention", "hpattn", "window", "tree", "mla", "sink", "dflash", "qwen35", "qwen36", "paged", "vision", "prefill", "muse", "hy4", "drama3", "codec" });
b.step("cuda-softmax-contract", "CUDA softmax families and their byte records").dependOn(&identity.step);
b.step("kernel-contract", "Every registered CUDA kernel through the contract's gates on this GPU, each control red").dependOn(&run.step);
const prefill_many = b.addRunArtifact(runner);
prefill_many.addArg("qmm-prefill-many-contract");
Expand Down
7 changes: 7 additions & 0 deletions zig/cuda_softmax_identity_host.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Run the CUDA identity arithmetic/source suite with zig/ as its package root.
const std = @import("std");
const suite = @import("tests/contract/cuda_softmax_identity.zig");

test {
std.testing.refAllDecls(suite);
}
6 changes: 3 additions & 3 deletions zig/dense_serving_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ test "dense serving: owned CUDA images retain their reviewed lane and decode byt
try std.testing.expectEqualStrings("e5ef9dcce75c478e59e476d2fa8747baee2317bd283b0756f125ea050dad9228", &hash(@embedFile("kernels/cuda/gdn.cu")));
try std.testing.expectEqualStrings("dba5aae94115571ed48933d80f646fff31b2a7da226569867c74a1dc061d2ca2", &hash(@embedFile("kernels/cuda/lane_gemv.cu")));
// #2239 adds head-64 attention for Qwen3-ASR; #3790 reuses it for GLM; existing instances keep their SASS
try std.testing.expectEqualStrings("9d9faedafd8e06afd667839a187118b6b220c794c2948532f0a849f901627a23", &hash(@embedFile("kernels/cuda/prefill_attention.cu")));
try std.testing.expectEqualStrings("776774090e4b189db61d48550b3bac5ade8e8379a3357e1c98aaebadd33c59e9", &hash(@embedFile("kernels/cuda/prefill_attention.cu")));
const group = @embedFile("kernels/cuda/qmm_group.cu");
try std.testing.expectEqualStrings("ceff4ee13e97b5aa93cbe7a1edc9a524c78aaa145dd1e5c82dcb05a3011aae23", &hash(group[0..16095]));
try std.testing.expectEqualStrings("8780fd710be3ce2f04e53fa1e69eb062809d3bec6391d9bd49983d0588e374d0", &hash(group));
// #2825 hoists copy addresses before the K loop; copy order and math match prefill_kernel.
try std.testing.expectEqualStrings("e3724114b87cae6f6f761b2f86357f05b1693d84a0233e4d58ce46349fa853f6", &hash(@embedFile("kernels/cuda/qmm_prefill.cu")));
// KIA14 adds all_rows (false keeps selection/arithmetic); #4706 adds TF_QWEN_PDL prologues
try std.testing.expectEqualStrings("79e97b0992f82e891ff8a80cdad68b534e07606b98feef3dffe25101477c99e1", &hash(@embedFile("kernels/cuda/qwen35.cu")));
try std.testing.expectEqualStrings("e7dd075319e32b706863404302be1033e31cd783bed45db212366ab498b91f32", &hash(@embedFile("kernels/cuda/qwen35.cu")));
// #1879's bytes: a comment names cuda_scratch.zig, where max_chunks moved; the code is unchanged
try std.testing.expectEqualStrings("f9e4a040cf219aac8db211df25d264dba390fce4a7e8da45557652c6ff0f28ba", &hash(@embedFile("kernels/cuda/qwen35.cuh")));
// 67711a6aa7's bytes: the opt-in DSpine draft kernels (#2633); default serving unchanged
try std.testing.expectEqualStrings("df06cb0ef07d12ca2296f7d1691678d0649a148f4abcbb3cb69f0d839e45295c", &hash(@embedFile("kernels/cuda/qwen35_draft.cu")));
try std.testing.expectEqualStrings("1f1b93181dc01caeaa876eb91396c4e4d048eb623c81ba270a8736459410e562", &hash(@embedFile("kernels/cuda/qwen35_draft.cu")));
try std.testing.expectEqualStrings("403cf841b89ad61da0818bdad8976ed4b286b3282c8c22df7171921b7e06eb6d", &hash(@embedFile("kernels/cuda/qwen35_keep.cu")));
try std.testing.expectEqualStrings("d1a379a98cf7504a7ed3b3f2d24c8ab6a39fe5511bcded6449e598ce5c3c79e7", &hash(@embedFile("kernels/cuda/scan_rows.cu")));
}
Expand Down
18 changes: 10 additions & 8 deletions zig/kernels/cuda/attn_fold.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// block_attention.cu's and sparse_attention.cu's fold: 16 mma rows a warp over 32-key tiles, P as bf16 into P x V.
#pragma once
#include "common/softmax_identity.cuh"

#include <cuda_bf16.h>
#include <cuda_runtime.h>
Expand Down Expand Up @@ -83,8 +84,9 @@ struct Fold {
tm1 = fmaxf(tm1, __shfl_xor_sync(0xffffffffu, tm1, x));
}
const float n0 = fmaxf(m0, tm0), n1 = fmaxf(m1, tm1);
const float a0 = tm0 == -INFINITY ? 1.f : (m0 == -INFINITY ? 0.f : texp(__fsub_rn(m0, n0)));
const float a1 = tm1 == -INFINITY ? 1.f : (m1 == -INFINITY ? 0.f : texp(__fsub_rn(m1, n1)));
const bool same0 = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(n0) && n0 == m0, same1 = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(n1) && n1 == m1;
const float a0 = same0 ? 1.f : (tm0 == -INFINITY ? 1.f : (m0 == -INFINITY ? 0.f : texp(__fsub_rn(m0, n0))));
const float a1 = same1 ? 1.f : (tm1 == -INFINITY ? 1.f : (m1 == -INFINITY ? 0.f : texp(__fsub_rn(m1, n1))));
float c0 = 0.f, c1 = 0.f;
#pragma unroll
for (int n = 0; n < 4; ++n) {
Expand All @@ -99,16 +101,16 @@ struct Fold {
c0 = __fadd_rn(c0, __shfl_xor_sync(0xffffffffu, c0, x));
c1 = __fadd_rn(c1, __shfl_xor_sync(0xffffffffu, c1, x));
}
l0 = __fmaf_rn(l0, a0, c0);
l1 = __fmaf_rn(l1, a1, c1);
l0 = (same0 && isfinite(__fadd_rn(l0, c0))) ? __fadd_rn(l0, c0) : __fmaf_rn(l0, a0, c0);
l1 = (same1 && isfinite(__fadd_rn(l1, c1))) ? __fadd_rn(l1, c1) : __fmaf_rn(l1, a1, c1);
m0 = n0;
m1 = n1;
#pragma unroll
for (int i = 0; i < NB; ++i) {
o[i][0] = KEEP ? attn_odd::keep(o[i][0], a0) : __fmul_rn(o[i][0], a0);
o[i][1] = KEEP ? attn_odd::keep(o[i][1], a0) : __fmul_rn(o[i][1], a0);
o[i][2] = KEEP ? attn_odd::keep(o[i][2], a1) : __fmul_rn(o[i][2], a1);
o[i][3] = KEEP ? attn_odd::keep(o[i][3], a1) : __fmul_rn(o[i][3], a1);
if (!same0 || !isfinite(o[i][0])) o[i][0] = KEEP ? attn_odd::keep(o[i][0], a0) : __fmul_rn(o[i][0], a0);
if (!same0 || !isfinite(o[i][1])) o[i][1] = KEEP ? attn_odd::keep(o[i][1], a0) : __fmul_rn(o[i][1], a0);
if (!same1 || !isfinite(o[i][2])) o[i][2] = KEEP ? attn_odd::keep(o[i][2], a1) : __fmul_rn(o[i][2], a1);
if (!same1 || !isfinite(o[i][3])) o[i][3] = KEEP ? attn_odd::keep(o[i][3], a1) : __fmul_rn(o[i][3], a1);
}
uint32_t pa[2][4];
#pragma unroll
Expand Down
22 changes: 12 additions & 10 deletions zig/kernels/cuda/common/mla.cuh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "softmax_identity.cuh"
// MLA bodies shared by GLM (glm_glue), Hy4-flash and the mla image: chunk tiles over the latent (+ roped columns).
#pragma once

Expand Down Expand Up @@ -152,13 +153,13 @@ __device__ void chunks_wide(const __nv_bfloat16* qa, const __nv_bfloat16* lc, co
if ((t & 3) == 0) stat[t / 4] = v;
}
__syncthreads();
float alpha[MT][2];
float alpha[MT][2]; bool same[MT][2];
for (int i = 0; i < MT; ++i)
for (int hf = 0; hf < 2; ++hf) {
const float tm = stat[i * 16 + hf * 8 + g];
const bool active = tm != -INFINITY;
const float nm = active ? fmaxf(m[i][hf], tm) : m[i][hf];
alpha[i][hf] = active ? (m[i][hf] == -INFINITY ? 0.0f : glm_triton::exp_approx(m[i][hf] - nm)) : 1.0f;
same[i][hf] = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(nm) && nm == m[i][hf]; alpha[i][hf] = same[i][hf] ? 1.0f : (active ? (m[i][hf] == -INFINITY ? 0.0f : glm_triton::exp_approx(m[i][hf] - nm)) : 1.0f);
if (i == hh) {
float p[2];
for (int e = 0; e < 2; ++e) {
Expand All @@ -182,7 +183,7 @@ __device__ void chunks_wide(const __nv_bfloat16* qa, const __nv_bfloat16* lc, co
}
for (int i = 0; i < MT; ++i)
for (int j = 0; j < 8; ++j)
for (int e = 0; e < 4; ++e) o[i][j][e] = __fmul_rn(o[i][j][e], alpha[i][e / 2]);
for (int e = 0; e < 4; ++e) if (!same[i][e / 2] || !isfinite(o[i][j][e])) o[i][j][e] = __fmul_rn(o[i][j][e], alpha[i][e / 2]);
#pragma unroll
for (int k16 = 0; k16 < KT; k16 += 16) {
uint32_t a[MT][4];
Expand All @@ -199,7 +200,7 @@ __device__ void chunks_wide(const __nv_bfloat16* qa, const __nv_bfloat16* lc, co
}
__syncthreads();
for (int i = 0; i < MT; ++i)
for (int hf = 0; hf < 2; ++hf) l[i][hf] = __fmaf_rn(l[i][hf], alpha[i][hf], stat[HB + i * 16 + hf * 8 + g]);
for (int hf = 0; hf < 2; ++hf) l[i][hf] = (same[i][hf] && isfinite(__fadd_rn(l[i][hf], stat[HB + i * 16 + hf * 8 + g]))) ? __fadd_rn(l[i][hf], stat[HB + i * 16 + hf * 8 + g]) : __fmaf_rn(l[i][hf], alpha[i][hf], stat[HB + i * 16 + hf * 8 + g]);
if (tile + 2 < tiles) load_kv(tile + 2);
commit();
}
Expand Down Expand Up @@ -240,9 +241,9 @@ __device__ __forceinline__ void merge_part(const float* po, const float* pm, con
for (int j = 0; j < 4; ++j) o[j] = __fmaf_rn(b, po[base * LW + k0 + j], 0.0f);
l = __fmaf_rn(b, cl, 0.0f);
} else {
const float a = active ? (m == -INFINITY ? 0.0f : glm_triton::exp_approx(m - nm)) : 1.0f;
for (int j = 0; j < 4; ++j) o[j] = __fmaf_rn(o[j], a, __fmul_rn(b, po[base * LW + k0 + j]));
l = __fmaf_rn(l, a, __fmul_rn(b, cl));
const bool same = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(nm) && nm == m; const float a = same ? 1.0f : (active ? (m == -INFINITY ? 0.0f : glm_triton::exp_approx(m - nm)) : 1.0f);
for (int j = 0; j < 4; ++j) o[j] = (same && isfinite(__fadd_rn(o[j], __fmul_rn(b, po[base * LW + k0 + j])))) ? __fadd_rn(o[j], __fmul_rn(b, po[base * LW + k0 + j])) : __fmaf_rn(o[j], a, __fmul_rn(b, po[base * LW + k0 + j]));
l = (same && isfinite(__fadd_rn(l, __fmul_rn(b, cl)))) ? __fadd_rn(l, __fmul_rn(b, cl)) : __fmaf_rn(l, a, __fmul_rn(b, cl));
}
m = nm;
}
Expand All @@ -267,10 +268,11 @@ __device__ __forceinline__ void merge_sink(const float* numerator, const float*
float acc[4] = {};
for (int i = 0; i < chunks; ++i) {
const int64_t base = i * plane + row_head;
const float p = expf(maxima[base] - top);
denom = __fmaf_rn(mass[base], p, denom);
const bool same = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(top) && top == maxima[base];
const float p = same ? 1.0f : expf(maxima[base] - top);
denom = (same && isfinite(__fadd_rn(mass[base], denom))) ? __fadd_rn(mass[base], denom) : __fmaf_rn(mass[base], p, denom);
#pragma unroll
for (int j = 0; j < 4; ++j) acc[j] = __fmaf_rn(numerator[base * latent + column + j], p, acc[j]);
for (int j = 0; j < 4; ++j) acc[j] = (same && isfinite(__fadd_rn(numerator[base * latent + column + j], acc[j]))) ? __fadd_rn(numerator[base * latent + column + j], acc[j]) : __fmaf_rn(numerator[base * latent + column + j], p, acc[j]);
}
#pragma unroll
for (int j = 0; j < 4; ++j) output[row_head * latent + column + j] = __float2bfloat16_rn(acc[j] / denom);
Expand Down
5 changes: 5 additions & 0 deletions zig/kernels/cuda/common/softmax_identity.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Compile-time control for exact finite stable-max rescale shortcuts.
#pragma once
#ifndef TF_CUDA_SOFTMAX_IDENTITY_GUARD
#define TF_CUDA_SOFTMAX_IDENTITY_GUARD 1
#endif
51 changes: 26 additions & 25 deletions zig/kernels/cuda/drama3_attn.cuh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common/softmax_identity.cuh"
// drama3 gated full attention (backbone and tower): 16 query heads, 2 KV heads, head dim 256, rotary on 64 dims.
#pragma once
#include "drama3_common.cuh"
Expand Down Expand Up @@ -63,16 +64,16 @@ extern "C" __global__ void __launch_bounds__(256) d3_attn_decode(const bf16* __r
const float sc = warp_sum(dot8(qv, kv)) * scale;
const uint4 vv = reinterpret_cast<const uint4*>(vc + ((size_t)p * 2 + g) * 256)[lane];
const float nm = fmaxf(m, sc);
const float corr = expf(m - nm), e = expf(sc - nm);
l = l * corr + e;
const bool same = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(nm) && nm == m; const float corr = same ? 1.0f : expf(m - nm), e = expf(sc - nm);
l = (same && isfinite(l + e)) ? l + e : l * corr + e;
acc[0] = acc[0] * corr + e * lo(vv.x);
acc[1] = acc[1] * corr + e * hi(vv.x);
acc[2] = acc[2] * corr + e * lo(vv.y);
acc[3] = acc[3] * corr + e * hi(vv.y);
acc[4] = acc[4] * corr + e * lo(vv.z);
acc[5] = acc[5] * corr + e * hi(vv.z);
acc[6] = acc[6] * corr + e * lo(vv.w);
acc[7] = acc[7] * corr + e * hi(vv.w);
acc[2] = (same && isfinite(acc[2] + e * lo(vv.y))) ? acc[2] + e * lo(vv.y) : acc[2] * corr + e * lo(vv.y);
acc[3] = (same && isfinite(acc[3] + e * hi(vv.y))) ? acc[3] + e * hi(vv.y) : acc[3] * corr + e * hi(vv.y);
acc[4] = (same && isfinite(acc[4] + e * lo(vv.z))) ? acc[4] + e * lo(vv.z) : acc[4] * corr + e * lo(vv.z);
acc[5] = (same && isfinite(acc[5] + e * hi(vv.z))) ? acc[5] + e * hi(vv.z) : acc[5] * corr + e * hi(vv.z);
acc[6] = (same && isfinite(acc[6] + e * lo(vv.w))) ? acc[6] + e * lo(vv.w) : acc[6] * corr + e * lo(vv.w);
acc[7] = (same && isfinite(acc[7] + e * hi(vv.w))) ? acc[7] + e * hi(vv.w) : acc[7] * corr + e * hi(vv.w);
m = nm;
}
__shared__ float sm[8], sl[8], sacc[8][256];
Expand All @@ -91,9 +92,9 @@ extern "C" __global__ void __launch_bounds__(256) d3_attn_decode(const bf16* __r
#pragma unroll
for (int k = 0; k < 8; ++k) {
if (sm[k] == -INFINITY) continue;
const float c = expf(sm[k] - M);
Lsum += sl[k] * c;
A += sacc[k][i] * c;
const bool same = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(M) && M == sm[k]; const float c = same ? 1.0f : expf(sm[k] - M);
Lsum = (same && isfinite(Lsum + sl[k])) ? Lsum + sl[k] : Lsum + sl[k] * c;
A = (same && isfinite(A + sacc[k][i])) ? A + sacc[k][i] : A + sacc[k][i] * c;
}
const float a = rb(A / Lsum);
const float gt = b2f(proj[(size_t)t * ps + gate_off + h * gate_head_stride + i]);
Expand Down Expand Up @@ -123,16 +124,16 @@ extern "C" __global__ void __launch_bounds__(256) d3_attn_partial(const bf16* __
const uint4 vv = reinterpret_cast<const uint4*>(vc + ((size_t)p * 2 + g) * 256)[lane];
const float sc = warp_sum(dot8(qv, kv)) * 0.0625f;
const float nm = fmaxf(m, sc);
const float corr = expf(m - nm), e = expf(sc - nm);
l = l * corr + e;
const bool same = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(nm) && nm == m; const float corr = same ? 1.0f : expf(m - nm), e = expf(sc - nm);
l = (same && isfinite(l + e)) ? l + e : l * corr + e;
acc[0] = acc[0] * corr + e * lo(vv.x);
acc[1] = acc[1] * corr + e * hi(vv.x);
acc[2] = acc[2] * corr + e * lo(vv.y);
acc[3] = acc[3] * corr + e * hi(vv.y);
acc[4] = acc[4] * corr + e * lo(vv.z);
acc[5] = acc[5] * corr + e * hi(vv.z);
acc[6] = acc[6] * corr + e * lo(vv.w);
acc[7] = acc[7] * corr + e * hi(vv.w);
acc[2] = (same && isfinite(acc[2] + e * lo(vv.y))) ? acc[2] + e * lo(vv.y) : acc[2] * corr + e * lo(vv.y);
acc[3] = (same && isfinite(acc[3] + e * hi(vv.y))) ? acc[3] + e * hi(vv.y) : acc[3] * corr + e * hi(vv.y);
acc[4] = (same && isfinite(acc[4] + e * lo(vv.z))) ? acc[4] + e * lo(vv.z) : acc[4] * corr + e * lo(vv.z);
acc[5] = (same && isfinite(acc[5] + e * hi(vv.z))) ? acc[5] + e * hi(vv.z) : acc[5] * corr + e * hi(vv.z);
acc[6] = (same && isfinite(acc[6] + e * lo(vv.w))) ? acc[6] + e * lo(vv.w) : acc[6] * corr + e * lo(vv.w);
acc[7] = (same && isfinite(acc[7] + e * hi(vv.w))) ? acc[7] + e * hi(vv.w) : acc[7] * corr + e * hi(vv.w);
m = nm;
}
__shared__ float sm[8], sl[8], sacc[8][256];
Expand All @@ -151,9 +152,9 @@ extern "C" __global__ void __launch_bounds__(256) d3_attn_partial(const bf16* __
#pragma unroll
for (int k = 0; k < 8; ++k) {
if (sm[k] == -INFINITY) continue;
const float c = expf(sm[k] - M);
Ls += sl[k] * c;
A += sacc[k][i] * c;
const bool same = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(M) && M == sm[k]; const float c = same ? 1.0f : expf(sm[k] - M);
Ls = (same && isfinite(Ls + sl[k])) ? Ls + sl[k] : Ls + sl[k] * c;
A = (same && isfinite(A + sacc[k][i])) ? A + sacc[k][i] : A + sacc[k][i] * c;
}
const size_t slot = ((size_t)t * 16 + h) * S + sp;
pacc[slot * 256 + i] = A;
Expand All @@ -175,9 +176,9 @@ extern "C" __global__ void d3_attn_merge(const float* __restrict__ pml, const fl
for (int k = 0; k < S; ++k) {
const float mk = pml[(base + k) * 2];
if (mk == -INFINITY) continue;
const float c = expf(mk - M);
Ls += pml[(base + k) * 2 + 1] * c;
A += pacc[(base + k) * 256 + i] * c;
const bool same = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(M) && M == mk; const float c = same ? 1.0f : expf(mk - M);
Ls = (same && isfinite(Ls + pml[(base + k) * 2 + 1])) ? Ls + pml[(base + k) * 2 + 1] : Ls + pml[(base + k) * 2 + 1] * c;
A = (same && isfinite(A + pacc[(base + k) * 256 + i])) ? A + pacc[(base + k) * 256 + i] : A + pacc[(base + k) * 256 + i] * c;
}
const float a = rb(A / Ls);
const float gt = b2f(proj[(size_t)t * ps + gate_off + h * gate_head_stride + i]);
Expand Down
5 changes: 3 additions & 2 deletions zig/kernels/cuda/drama3_codec.cu
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common/softmax_identity.cuh"
// ours: drama3's codec (Fish S2 modded DAC) in fp32: the glue between cuBLAS SGEMMs, time-major [rows, channels].
#include <stdint.h>
#include <math.h>
Expand Down Expand Up @@ -166,8 +167,8 @@ extern "C" __global__ void c_attn(const float* __restrict__ qkv, const float* __
const size_t r = (size_t)(p % ring) * 1024 + h * 64 + 2 * lane;
const float sc = warp_sum(q0 * kc[r] + q1 * kc[r + 1]);
const float nm = fmaxf(m, sc);
const float corr = expf(m - nm), e = expf(sc - nm);
l = l * corr + e;
const bool same = TF_CUDA_SOFTMAX_IDENTITY_GUARD && isfinite(nm) && nm == m; const float corr = same ? 1.0f : expf(m - nm), e = expf(sc - nm);
l = (same && isfinite(l + e)) ? l + e : l * corr + e;
a0 = a0 * corr + e * vc[r];
a1 = a1 * corr + e * vc[r + 1];
m = nm;
Expand Down
Loading
Loading