Skip to content

causal_conv1d_update_kernel_bdt_fwd optimized version - #388

Open
Liwansi wants to merge 1 commit into
XPU-Forces:hw/950-perffrom
Liwansi:950-0707
Open

causal_conv1d_update_kernel_bdt_fwd optimized version#388
Liwansi wants to merge 1 commit into
XPU-Forces:hw/950-perffrom
Liwansi:950-0707

Conversation

@Liwansi

@Liwansi Liwansi commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

causal_conv1d_update_kernel_bdt_fwd optimized version

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the causal_conv1d_update_kernel_bdt_fwd kernel in convolution.py by changing how weights and input blocks are loaded, updating the state-saving logic, and manually calculating the SiLU activation. The review feedback highlights several critical areas for improvement: adding a boundary mask to weight loading to prevent out-of-bounds memory access, restricting redundant state-copying operations to ti == 0 to optimize memory traffic, and reverting to the more idiomatic and optimized tl.sigmoid function.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +666 to +668
w_off_d = di * D_CHK_SIZE + tl.arange(0, D_CHK_SIZE)[:, None]
w_off_w = tl.arange(0, width)[None, :]
w = tl.load(weight_ptr + w_off_d * width + w_off_w).to(tl.float32)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The weight loading is performed using flat pointer arithmetic without any boundary checks. If dim is not a multiple of D_CHK_SIZE, this will result in out-of-bounds memory access. A mask should be applied to ensure safe loading.

Suggested change
w_off_d = di * D_CHK_SIZE + tl.arange(0, D_CHK_SIZE)[:, None]
w_off_w = tl.arange(0, width)[None, :]
w = tl.load(weight_ptr + w_off_d * width + w_off_w).to(tl.float32)
w_off_d = di * D_CHK_SIZE + tl.arange(0, D_CHK_SIZE)[:, None]
w_off_w = tl.arange(0, width)[None, :]
w_mask = w_off_d < dim
w = tl.load(weight_ptr + w_off_d * width + w_off_w, mask=w_mask, other=0.0).to(tl.float32)

nst_mask = (nst_off_y0 < dim) & (nst_off_y1 < state_len)
block_ptr_st = bi * dim * state_len + nst_off_y0 * state_len + nst_off_y1

if new_state_start_off < 0:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

When new_state_start_off < 0, the old state elements are copied from conv_state_ptr to conv_state_update_ptr. Since cs_mask and cs_val do not depend on ti, every single ti block will redundantly load and store the exact same state elements. Restricting this operation to ti == 0 avoids redundant global memory traffic and potential race conditions.

Suggested change
if new_state_start_off < 0:
if new_state_start_off < 0 and ti == 0:

Comment on lines 741 to +742
if SILU_ACTIVATION:
out_block = out_block * tl.sigmoid(out_block)
tl.store(
tl.make_block_ptr(
out_ptr,
shape=(batch, dim, out_len),
strides=(dim * out_len, out_len, 1),
offsets=(bi, di * D_CHK_SIZE, ti * T_CHK_SIZE),
block_shape=(1, D_CHK_SIZE, T_CHK_SIZE),
order=(2, 1, 0),
),
out_block[None, :, :],
boundary_check=(0, 1, 2),
)
out_block = out_block / (1.0 + tl.exp(-out_block))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Using tl.sigmoid is more idiomatic, cleaner, and potentially more numerically stable and optimized than manually computing the sigmoid denominator via tl.exp.

Suggested change
if SILU_ACTIVATION:
out_block = out_block * tl.sigmoid(out_block)
tl.store(
tl.make_block_ptr(
out_ptr,
shape=(batch, dim, out_len),
strides=(dim * out_len, out_len, 1),
offsets=(bi, di * D_CHK_SIZE, ti * T_CHK_SIZE),
block_shape=(1, D_CHK_SIZE, T_CHK_SIZE),
order=(2, 1, 0),
),
out_block[None, :, :],
boundary_check=(0, 1, 2),
)
out_block = out_block / (1.0 + tl.exp(-out_block))
if SILU_ACTIVATION:
out_block = out_block * tl.sigmoid(out_block)

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.

1 participant