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

Function kernel_fn

kernels/reduce.py:51–140  ·  view source on GitHub ↗

Entry point called by bench.py. Must match reference.reduce_sum_ref signature. Args: x: Input tensor of any shape dim: Dimension to reduce over (default: -1, last dim) Returns: Tensor with the specified dimension reduced (summed).

(x: torch.Tensor, dim: int = -1)

Source from the content-addressed store, hash-verified

49
50
51def kernel_fn(x: torch.Tensor, dim: int = -1) -> torch.Tensor:
52 """
53 Entry point called by bench.py. Must match reference.reduce_sum_ref signature.
54
55 Args:
56 x: Input tensor of any shape
57 dim: Dimension to reduce over (default: -1, last dim)
58
59 Returns:
60 Tensor with the specified dimension reduced (summed).
61 """
62 assert x.is_cuda
63
64 # Normalize dim
65 if dim < 0:
66 dim = x.ndim + dim
67 assert 0 <= dim < x.ndim, f"dim {dim} out of range for tensor with {x.ndim} dims"
68
69 # Compute shapes
70 # We want to reshape to [outer, reduce_size, inner] then reduce the middle dim
71 outer_size = 1
72 for i in range(dim):
73 outer_size *= x.size(i)
74
75 reduce_size = x.size(dim)
76
77 inner_size = 1
78 for i in range(dim + 1, x.ndim):
79 inner_size *= x.size(i)
80
81 # Make contiguous and reshape
82 x_contig = x.contiguous()
83
84 # Output shape: same as input but with dim removed
85 out_shape = list(x.shape)
86 out_shape.pop(dim)
87 if len(out_shape) == 0:
88 out_shape = [1]
89
90 # Total number of output elements
91 n_output = outer_size * inner_size
92
93 # For the simple case where we reduce over the last dimension
94 # and inner_size == 1, we can use a straightforward approach
95 if inner_size == 1:
96 x_2d = x_contig.view(outer_size, reduce_size)
97 out_flat = torch.empty(n_output, device=x.device, dtype=torch.float32)
98
99 BLOCK_SIZE = triton.next_power_of_2(min(reduce_size, 8192))
100
101 grid = (n_output,)
102 reduce_sum_kernel[grid](
103 x_2d,
104 out_flat,
105 reduce_size,
106 x_2d.stride(0),
107 x_2d.stride(1),
108 BLOCK_SIZE=BLOCK_SIZE,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected