@triton.jit
def linear_log_sum_exp_kernel(
E, C, LSE,
E_stride_d, E_stride_n,
C_stride_d, C_stride_v,
N, D, V,
BLOCK_SIZE_NB: tl.constexpr, BLOCK_SIZE_MB: tl.constexpr, BLOCK_SIZE_DB: tl.constexpr
):
pid_n = tl.program_id(axis=0) # Block ID along the N dimension
pid_m = tl.program_id(axis=1) # Block ID along the V dimension
code
# Offsets for the block
offsets_n = pid_n * BLOCK_SIZE_NB + tl.arange(0, BLOCK_SIZE_NB)
offsets_m = pid_m * BLOCK_SIZE_MB + tl.arange(0, BLOCK_SIZE_MB)
offsets_d = tl.arange(0, BLOCK_SIZE_DB)
# Masks for bounds checking
mask_n = offsets_n < N
mask_m = offsets_m < V
# Initialize accumulator for the block result
acc = tl.zeros((BLOCK_SIZE_MB, BLOCK_SIZE_NB), dtype=tl.float32)
# Loop over blocks in the D dimension
for d_start in range(0, D, BLOCK_SIZE_DB):
# Load a block of E (DB x NB)
e_block = tl.load(
E + d_start * E_stride_d + offsets_d[:, None] * E_stride_d + offsets_n[None, :] * E_stride_n,
mask=(offsets_d[:, None] + d_start < D) & mask_n[None, :],
other=0.0
)
# Load a block of C (DB x MB)
c_block = tl.load(
C + d_start * C_stride_d + offsets_d[:, None] * C_stride_d + offsets_m[None, :] * C_stride_v,
mask=(offsets_d[:, None] + d_start < D) & mask_m[None, :],
other=0.0
)
# Perform blockwise matrix multiplication
acc += tl.dot(c_block, e_block)
# Compute log-sum-exp for this block
max_val = tl.max(acc, axis=0)
exp_acc = tl.exp(acc - max_val[None, :])
lse_block = tl.log(tl.sum(exp_acc, axis=0)) + max_val
# Update LSE with atomic operations (simulate atomic_max using atomic_add and exp)
n_range = tl.arange(0, BLOCK_SIZE_NB)
idx = offsets_n[n_range]
old_val = tl.load(LSE + idx * LSE.stride(0), mask=idx < N, other=-float('inf'))
new_val = lse_block[n_range]
update = tl.where(new_val > old_val, tl.exp(new_val - old_val), 0.0)
tl.atomic_add(LSE + idx * LSE.stride(0), update, priority=1, mask=idx < N)
Assistant
def linear_log_sum_exp(E, C):
D, N = E.shape
_, V = C.shape
ValueError('Did you forget to add @triton.jit ? (_builder argument must be provided outside of JIT functions.)')
please help me
The error you're encountering is due to using .stride(0) inside the Triton JIT-compiled kernel function. In Triton, you cannot access PyTorch tensor properties like .stride(), .shape, etc., inside the JIT function because Triton operates on its own intermediate representation and does not have access to these PyTorch methods during JIT compilation.
Here's how you can fix the problem:
Pass the LSE stride as an argument to the kernel function:
Modify the kernel's function signature to include LSE_stride:
python
@triton.jit
def linear_log_sum_exp_kernel(
E, C, LSE,
E_stride_d, E_stride_n,
C_stride_d, C_stride_v,
LSE_stride, # Add this line
N, D, V,
BLOCK_SIZE_NB: tl.constexpr, BLOCK_SIZE_MB: tl.constexpr, BLOCK_SIZE_DB: tl.constexpr
):
Update the kernel body to use LSE_stride instead of LSE.stride(0):
Replace instances of LSE.stride(0) with LSE_stride in the kernel body:
In your linear_log_sum_exp function, include LSE.stride(0) as an argument when calling the kernel:
python
def linear_log_sum_exp(E, C):
D, N = E.shape
_, V = C.shape
# Initialize LSE
LSE = torch.full((N,), float('-inf'), device=E.device, dtype=E.dtype)
# Define grid size
grid_n = (N + BLOCK_SIZE_NB - 1) // BLOCK_SIZE_NB
grid_m = (V + BLOCK_SIZE_MB - 1) // BLOCK_SIZE_MB
# Launch the Triton kernel
linear_log_sum_exp_kernel[(grid_n, grid_m)](
E, C, LSE,
E.stride(0), E.stride(1),
C.stride(0), C.stride(1),
LSE.stride(0), # Pass LSE_stride here
N, D, V,
BLOCK_SIZE_NB=BLOCK_SIZE_NB,
BLOCK_SIZE_MB=BLOCK_SIZE_MB,
BLOCK_SIZE_DB=BLOCK_SIZE_DB
)
return LSE
Additional Fixes:
Also, as you've correctly surmised, you should avoid indexing Triton tensors with other Triton tensors. In the kernel, you have:
python
# This line causes issues because you're indexing a Triton tensor with another Triton tensor
idx = offsets_n[n_range]
Since both offsets_n and n_range are Triton tensors, and n_range is a sequence from 0 to BLOCK_SIZE_NB - 1, indexing offsets_n like this is redundant and can be replaced. You can simply assign:
python
idx = offsets_n
Similarly, for new_val, replace:
python
new_val = lse_block[n_range]
with:
python
new_val = lse_block
Here's the corrected kernel code:
python
@triton.jit
def linear_log_sum_exp_kernel(
E, C, LSE,
E_stride_d, E_stride_n,
C_stride_d, C_stride_v,
LSE_stride, # Added LSE_stride
N, D, V,
BLOCK_SIZE_NB: tl.constexpr, BLOCK_SIZE_MB: tl.constexpr, BLOCK_SIZE_DB: tl.constexpr
):
pid_n = tl.program_id(0) # Block ID along the N dimension
pid_m = tl.program_id(1) # Block ID along the V dimension
# Offsets for the block
offsets_n = pid_n * BLOCK_SIZE_NB + tl.arange(0, BLOCK_SIZE_NB)
offsets_m = pid_m * BLOCK_SIZE_MB + tl.arange(0, BLOCK_SIZE_MB)
offsets_d = tl.arange(0, BLOCK_SIZE_DB)
# Masks for bounds checking
mask_n = offsets_n < N
mask_m = offsets_m < V
# Initialize accumulator for the block result
acc = tl.zeros((BLOCK_SIZE_MB, BLOCK_SIZE_NB), dtype=tl.float32)
# Loop over blocks in the D dimension
for d_start in range(0, D, BLOCK_SIZE_DB):
# Load a block of E (DB x NB)
e_block = tl.load(
E + (d_start + offsets_d[:, None]) * E_stride_d + offsets_n[None, :] * E_stride_n,
mask=(offsets_d[:, None] + d_start < D) & mask_n[None, :],
other=0.0
)
# Load a block of C (DB x MB)
c_block = tl.load(
C + (d_start + offsets_d[:, None]) * C_stride_d + offsets_m[None, :] * C_stride_v,
mask=(offsets_d[:, None] + d_start < D) & mask_m[None, :],
other=0.0
)
# Perform blockwise matrix multiplication
acc += tl.dot(c_block, e_block)
# Compute log-sum-exp for this block
max_val = tl.max(acc, axis=0)
exp_acc = tl.exp(acc - max_val[None, :])
lse_block = tl.log(tl.sum(exp_acc, axis=0)) + max_val
# Update LSE with atomic operations
idx = offsets_n # Simplify indexing
old_val = tl.load(LSE + idx * LSE_stride, mask=idx < N, other=-float('inf'))
new_val = lse_block
update = tl.where(new_val > old_val, tl.exp(new_val - old_val), 0.0)
tl.atomic_add(LSE + idx * LSE_stride, update, mask=idx < N)
Explanation:
Stride Handling: Since LSE is a 1D tensor, its stride is typically 1, but explicitly passing the stride avoids potential errors and makes the code more robust.
Indexing Simplification: By removing unnecessary indexing with n_range, we prevent errors related to indexing Triton tensors with other tensors. The variable offsets_n already represents the correct indices.
Final Notes:
After making these changes, your code should work as expected. Ensure that you're using the latest version of Triton, as APIs can change between versions. If you continue to encounter issues, consider verifying with smaller tensor sizes to debug step by step.