MCPcopy Create free account
hub / github.com/RightNow-AI/autokernel / reduce_sum_kernel

Function reduce_sum_kernel

kernels/reduce.py:21–48  ·  view source on GitHub ↗

Parallel sum reduction. One program per output element. Reduces over `reduce_size` elements with stride `stride_x_col`.

(
    X_ptr,
    OUT_ptr,
    reduce_size,
    stride_x_row,
    stride_x_col,
    BLOCK_SIZE: tl.constexpr,
)

Source from the content-addressed store, hash-verified

19
20@triton.jit
21def reduce_sum_kernel(
22 X_ptr,
23 OUT_ptr,
24 reduce_size,
25 stride_x_row,
26 stride_x_col,
27 BLOCK_SIZE: tl.constexpr,
28):
29 """
30 Parallel sum reduction. One program per output element.
31 Reduces over `reduce_size` elements with stride `stride_x_col`.
32 """
33 row_idx = tl.program_id(0)
34
35 # Base pointer for this row
36 row_start = X_ptr + row_idx * stride_x_row
37
38 # Accumulate in float32 for stability
39 acc = tl.zeros((BLOCK_SIZE,), dtype=tl.float32)
40
41 for offset in range(0, reduce_size, BLOCK_SIZE):
42 col_offsets = offset + tl.arange(0, BLOCK_SIZE)
43 mask = col_offsets < reduce_size
44 x = tl.load(row_start + col_offsets * stride_x_col, mask=mask, other=0.0).to(tl.float32)
45 acc += x
46
47 result = tl.sum(acc, axis=0)
48 tl.store(OUT_ptr + row_idx, result)
49
50
51def kernel_fn(x: torch.Tensor, dim: int = -1) -> torch.Tensor:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected