fix(GDN): clamp gate [-5,2] + state [-65504,65504] to prevent inf/NaN
Root cause from real machine test: gdn_forward.cu output abs mean = inf - gate_raw can be positive → exp(gate) > 1 → state grows exponentially - Over 64 tokens: exp(2.0)^64 = inf - PyTorch ref clamps g ∈ [-5, 2] but CUDA kernel did not Fix: gdn_forward.cu: clamp gate_raw ∈ [-5, 2] before exp (both kernel variants) gdn_forward.cu: clamp state ∈ [-65504, 65504] after update (fp16 safe range) qwen3_5.py: clamp g_3d before passing to SM70 kernel (belt + suspenders) qwen3_5.py: clamp temporal_state after decode update
This commit is contained in:
@@ -135,7 +135,7 @@ __global__ void gdn_forward_kernel(const scalar_t* __restrict__ q,
|
||||
float beta_value = 0.0F;
|
||||
if (threadIdx.x == 0) {
|
||||
const float gate_raw = load_as_float(gate, gate_index);
|
||||
gate_value = GateIsExp ? gate_raw : __expf(gate_raw);
|
||||
{ const float gc = fminf(fmaxf(gate_raw, -5.0F), 2.0F); gate_value = GateIsExp ? fminf(gate_raw, 7.389F) : __expf(gc); }
|
||||
beta_value = load_as_float(beta, gate_index);
|
||||
}
|
||||
gate_value = __shfl_sync(0xffffffffU, gate_value, 0);
|
||||
@@ -191,7 +191,7 @@ __global__ void gdn_forward_kernel(const scalar_t* __restrict__ q,
|
||||
for (int c = 0; c < COLS; ++c) {
|
||||
const float new_state =
|
||||
fmaf(k_reg[r], delta[c], gate_value * state_shard[c][r]);
|
||||
state_shard[c][r] = new_state;
|
||||
state_shard[c][r] = fminf(fmaxf(new_state, -65504.0F), 65504.0F);
|
||||
attn_partial[c] += new_state * q_reg[r];
|
||||
}
|
||||
}
|
||||
@@ -294,7 +294,7 @@ __global__ void gdn_forward_vlk_varlen_kernel(
|
||||
float beta_value = 0.0F;
|
||||
if (threadIdx.x == 0) {
|
||||
const float gate_raw = load_as_float(gate, gate_index);
|
||||
gate_value = GateIsExp ? gate_raw : __expf(gate_raw);
|
||||
{ const float gc = fminf(fmaxf(gate_raw, -5.0F), 2.0F); gate_value = GateIsExp ? fminf(gate_raw, 7.389F) : __expf(gc); }
|
||||
beta_value = load_as_float(beta, gate_index);
|
||||
}
|
||||
gate_value = __shfl_sync(0xffffffffU, gate_value, 0);
|
||||
@@ -350,7 +350,7 @@ __global__ void gdn_forward_vlk_varlen_kernel(
|
||||
for (int c = 0; c < COLS; ++c) {
|
||||
const float new_state =
|
||||
fmaf(k_reg[r], delta[c], gate_value * state_shard[c][r]);
|
||||
state_shard[c][r] = new_state;
|
||||
state_shard[c][r] = fminf(fmaxf(new_state, -65504.0F), 65504.0F);
|
||||
attn_partial[c] += new_state * q_reg[r];
|
||||
}
|
||||
}
|
||||
@@ -543,7 +543,7 @@ __global__ void gdn_decode_mixed_qkv_global_state_kernel(
|
||||
for (int c = 0; c < COLS; ++c) {
|
||||
const float new_state =
|
||||
fmaf(k_reg[r], delta[c], gate_value * state_shard[c][r]);
|
||||
state_shard[c][r] = new_state;
|
||||
state_shard[c][r] = fminf(fmaxf(new_state, -65504.0F), 65504.0F);
|
||||
attn_partial[c] += new_state * q_reg[r];
|
||||
}
|
||||
}
|
||||
@@ -765,7 +765,7 @@ __global__ void gdn_decode_mixed_qkv_ddtree_state_kernel(
|
||||
for (int c = 0; c < COLS; ++c) {
|
||||
const float new_state =
|
||||
fmaf(k_reg[r], delta[c], gate_value * state_shard[c][r]);
|
||||
state_shard[c][r] = new_state;
|
||||
state_shard[c][r] = fminf(fmaxf(new_state, -65504.0F), 65504.0F);
|
||||
attn_partial[c] += new_state * q_reg[r];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user