Skip to content
Merged
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
48 changes: 48 additions & 0 deletions tests/ops/test_npu_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,51 @@ def test_npu_kernel_registered(self, op_name, variant):
assert "npu" in KERNEL_REGISTRY.list_available(op_name, variant), (
f"Expected 'npu' kernel registered for ({op_name!r}, {variant!r})"
)


# ---------------------------------------------------------------------------
# Causal conv1d short segment packed regression tests
# ---------------------------------------------------------------------------


class TestCausalConv1dShortSegmentPacked:
"""Regression tests for short-segment packed conv1d with initial_state.

Ensures that widening BT (to keep dh0 on a single-slot path) does not let
mask_head_rows or dh0 load masks leak rows from subsequent sequences into
b_dw or dh0 for packed segments shorter than W (e.g. lengths [1, 7], W=4).
"""

def test_short_segment_packed_dw_matches_unpacked(self):
from veomni.ops.kernels.gated_delta_rule._ascend.causal_conv1d import causal_conv1d

device = get_device_type()
W = 4
D = 256 # divisible by BD=32
# Packed lengths [1, 7], total T=8
cu_seqlens = torch.tensor([0, 1, 8], dtype=torch.int32, device=device)
x = torch.randn(1, 8, D, dtype=torch.bfloat16, device=device, requires_grad=True)
weight = torch.randn(W, D, dtype=torch.bfloat16, device=device, requires_grad=True)
initial_state = torch.randn(2, D, W, dtype=torch.bfloat16, device=device, requires_grad=True)

# Packed forward + backward
y, _ = causal_conv1d(x, weight, initial_state=initial_state, cu_seqlens=cu_seqlens)
y.sum().backward()

# Unpacked separate calls
x_0 = x[:, :1].detach().clone().requires_grad_()
init_0 = initial_state[:1].detach().clone().requires_grad_()
w_0 = weight.detach().clone().requires_grad_()
y_0, _ = causal_conv1d(x_0, w_0, initial_state=init_0)
y_0.sum().backward()

x_1 = x[:, 1:].detach().clone().requires_grad_()
init_1 = initial_state[1:].detach().clone().requires_grad_()
w_1 = weight.detach().clone().requires_grad_()
y_1, _ = causal_conv1d(x_1, w_1, initial_state=init_1)
y_1.sum().backward()

expected_dw = w_0.grad + w_1.grad
torch.testing.assert_close(weight.grad, expected_dw, rtol=1e-2, atol=1e-2)
expected_dh0 = torch.cat((init_0.grad, init_1.grad), dim=0)
torch.testing.assert_close(initial_state.grad, expected_dh0, rtol=1e-2, atol=1e-2)
21 changes: 18 additions & 3 deletions veomni/ops/kernels/gated_delta_rule/_ascend/triton/convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def causal_conv1d_bwd_kernel(
b_dw = tl.sum(b_dy_shift * b_x, 0)

if USE_INITIAL_STATE:
mask_head_rows = o_t < i_w
mask_head_rows = (o_t < i_w) & (o_t < T_len)

b_dy_head = tl.load(
dy + bos * D + o_t[:, None] * D + o_d,
Expand Down Expand Up @@ -452,11 +452,12 @@ def causal_conv1d_bwd_kernel(
b_dh0_s = tl.zeros((BD,), dtype=tl.float32)
for i_t2 in tl.static_range(0, W - 1):
if i_t2 < i_w:
dy0_row = tl.load(dy + bos * D + (i_t * BT + i_t2) * D + o_d, mask=m_d, other=0.0).to(
m_dy0 = m_d & ((i_t * BT + i_t2) < T_len)
dy0_row = tl.load(dy + bos * D + (i_t * BT + i_t2) * D + o_d, mask=m_dy0, other=0.0).to(
tl.float32
)
if ACTIVATION == "swish" or ACTIVATION == "silu": # pylint: disable=consider-using-in
y0_row = tl.load(y + bos * D + (i_t * BT + i_t2) * D + o_d, mask=m_d, other=0.0).to(
y0_row = tl.load(y + bos * D + (i_t * BT + i_t2) * D + o_d, mask=m_dy0, other=0.0).to(
tl.float32
)
y0_s = tl.sigmoid(y0_row)
Expand Down Expand Up @@ -633,6 +634,20 @@ def causal_conv1d_bwd_impl(
if initial_state is not None:
BD = 32
BT = min(8, triton.next_power_of_2(triton.cdiv(max(16, B * T), NUM_CORES)))
# dh0 below is split into cdiv(W, BT) slots and summed. The multi-slot
# path over-counts, so never tile narrower than the convolution window:
# that keeps cdiv(W, BT) == 1 and the single-slot path.
#
# Widening BT is only within the budget derived above while W <= 8, which
# is also the range that budget was derived for. Say so rather than
# silently exceeding it.
if W > 8:
raise NotImplementedError(
f"causal_conv1d backward with initial_state supports conv widths up to 8, got {W}. "
"Beyond that the tile needed to keep dh0 on its single-slot path no longer fits the "
"BT=8/BD=32 UB budget this branch is sized for."
)
BT = max(BT, triton.next_power_of_2(W))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] For W=4 and packed lengths [1,7] on 48 vector cores, widening BT from 1 to 4 lets mask_head_rows admit rows from the next sequence into dw, corrupting previously correct weight gradients. Include o_t < T_len in the head dy/y load masks and add a short-segment regression test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in b5490b82. Included o_t < T_len in mask_head_rows and guarded dy0_row loads by T_len as well to prevent admitting rows from subsequent sequences in packed short-segment mode. Added a short-segment regression test (lengths [1, 7], W=4) in tests/ops/test_npu_kernels.py. Thanks for the careful catch!

else:
BD = 32
BT = min(32, triton.next_power_of_2(triton.cdiv(max(16, B * T), NUM_CORES)))
Expand Down
Loading