Skip to content

Commit 408bdac

Browse files
authored
fix qwen3.5 27b gdr preprocess (#4676)
1 parent 9b3b193 commit 408bdac

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

lmdeploy/pytorch/kernels/cuda/gated_delta_preprocess.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def _gated_delta_preprocess_kernel(
5353
BATCH_SIZE: tl.constexpr,
5454
HEAD_DIM: tl.constexpr,
5555
KV_RATIO: tl.constexpr,
56+
BLOCK_KV_RATIO: tl.constexpr,
5657
BLOCK_T: tl.constexpr,
5758
BLOCK_D: tl.constexpr,
5859
EPS: tl.constexpr,
@@ -89,9 +90,10 @@ def _gated_delta_preprocess_kernel(
8990
q_vals = q_vals * q_rstd[:, None]
9091
k_vals = k_vals * k_rstd[:, None]
9192

92-
reps = tl.arange(0, KV_RATIO)
93+
reps = tl.arange(0, BLOCK_KV_RATIO)
9394
dst_heads = src_head_id * KV_RATIO + reps
94-
mask_t_rep = mask_t[:, None] & (reps[None, :] < KV_RATIO)
95+
mask_rep = reps < KV_RATIO
96+
mask_t_rep = mask_t[:, None] & mask_rep[None, :]
9597
beta = tl.load(
9698
b + b_batch_offset + offs_t[:, None] * b_stride_t + src_head_id * b_stride_src_h
9799
+ reps[None, :] * b_stride_rep,
@@ -105,8 +107,8 @@ def _gated_delta_preprocess_kernel(
105107
mask=mask_t_rep,
106108
other=0.0,
107109
).to(tl.float32)
108-
dt = tl.load(dt_bias + dst_heads).to(tl.float32)
109-
a_scale = tl.load(a_log_exp + dst_heads).to(tl.float32)
110+
dt = tl.load(dt_bias + dst_heads, mask=mask_rep, other=0.0).to(tl.float32)
111+
a_scale = tl.load(a_log_exp + dst_heads, mask=mask_rep, other=0.0).to(tl.float32)
110112

111113
x = a_val + dt[None, :]
112114
softplus = tl.where(x > 20.0, x, libdevice.log1p(libdevice.exp(x)))
@@ -207,6 +209,7 @@ def gated_delta_preprocess(
207209
g_out = torch.empty((batch_size, num_tokens, num_v_heads), dtype=torch.float32, device=a.device)
208210

209211
block_d = triton.next_power_of_2(head_dim)
212+
block_kv_ratio = triton.next_power_of_2(kv_ratio)
210213
block_t = 8
211214
grid = (batch_size, num_k_heads, triton.cdiv(num_tokens, block_t))
212215
_gated_delta_preprocess_kernel[grid](
@@ -239,6 +242,7 @@ def gated_delta_preprocess(
239242
BATCH_SIZE=batch_size,
240243
HEAD_DIM=head_dim,
241244
KV_RATIO=kv_ratio,
245+
BLOCK_KV_RATIO=block_kv_ratio,
242246
BLOCK_T=block_t,
243247
BLOCK_D=block_d,
244248
EPS=1e-6,

tests/pytorch/kernel/test_gated_delta_preprocess.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _fla_l2norm_reference(query, key, b, a, dt_bias, a_log_exp, kv_ratio, init_t
8080

8181
@pytest.mark.skipif(not do_test(), reason='triton or cuda is not available')
8282
@pytest.mark.parametrize('dtype', [torch.bfloat16, torch.float16])
83-
@pytest.mark.parametrize('kv_ratio', [1, 4])
83+
@pytest.mark.parametrize('kv_ratio', [1, 3, 4])
8484
@pytest.mark.parametrize('use_init_token_mask', [False, True])
8585
@pytest.mark.parametrize('apply_qk_l2norm', [False, True])
8686
def test_gated_delta_preprocess_3d_ba(dtype, kv_ratio, use_init_token_mask, apply_qk_l2norm):
@@ -127,7 +127,7 @@ def test_gated_delta_preprocess_3d_ba(dtype, kv_ratio, use_init_token_mask, appl
127127

128128
@pytest.mark.skipif(not do_test(), reason='triton or cuda is not available')
129129
@pytest.mark.parametrize('dtype', [torch.bfloat16, torch.float16])
130-
@pytest.mark.parametrize('kv_ratio', [1, 4])
130+
@pytest.mark.parametrize('kv_ratio', [1, 3, 4])
131131
@pytest.mark.parametrize('use_init_token_mask', [False, True])
132132
@pytest.mark.parametrize('apply_qk_l2norm', [False, True])
133133
def test_gated_delta_preprocess_4d_grouped_ba(dtype, kv_ratio, use_init_token_mask, apply_qk_l2norm):
@@ -175,7 +175,7 @@ def test_gated_delta_preprocess_4d_grouped_ba(dtype, kv_ratio, use_init_token_ma
175175

176176
@pytest.mark.skipif(not do_test(), reason='triton or cuda is not available')
177177
@pytest.mark.parametrize('dtype', [torch.bfloat16, torch.float16])
178-
@pytest.mark.parametrize('kv_ratio', [1, 4])
178+
@pytest.mark.parametrize('kv_ratio', [1, 3, 4])
179179
@pytest.mark.parametrize('grouped_ba', [False, True])
180180
@pytest.mark.parametrize('is_decoding', [False, True])
181181
def test_gated_delta_preprocess_matches_default_prepare_inputs(dtype, kv_ratio, grouped_ba, is_decoding):

0 commit comments

Comments
 (0)