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

Function kernel_fn

kernels/flash_attention.py:130–180  ·  view source on GitHub ↗

Entry point called by bench.py. Must match reference.flash_attention_ref signature. Args: Q: [batch, heads, seq_len, head_dim] K: [batch, heads, seq_len, head_dim] V: [batch, heads, seq_len, head_dim] causal: whether to apply causal masking sm_scale:

(
    Q: torch.Tensor,
    K: torch.Tensor,
    V: torch.Tensor,
    causal: bool = True,
    sm_scale: float = None,
)

Source from the content-addressed store, hash-verified

128
129
130def kernel_fn(
131 Q: torch.Tensor,
132 K: torch.Tensor,
133 V: torch.Tensor,
134 causal: bool = True,
135 sm_scale: float = None,
136) -> torch.Tensor:
137 """
138 Entry point called by bench.py. Must match reference.flash_attention_ref signature.
139
140 Args:
141 Q: [batch, heads, seq_len, head_dim]
142 K: [batch, heads, seq_len, head_dim]
143 V: [batch, heads, seq_len, head_dim]
144 causal: whether to apply causal masking
145 sm_scale: softmax scale factor, default 1/sqrt(head_dim)
146 """
147 assert Q.is_cuda and K.is_cuda and V.is_cuda
148
149 Z, H, M_size, D = Q.shape
150 _, _, N_size, _ = K.shape
151
152 if sm_scale is None:
153 sm_scale = 1.0 / math.sqrt(D)
154
155 O = torch.empty_like(Q)
156
157 # Block sizes -- must be powers of 2
158 # D (head_dim) must be a constexpr and power of 2 for tl.trans to work
159 assert D in (16, 32, 64, 128, 256), f"Head dim {D} not supported, must be power of 2 in [16..256]"
160
161 BLOCK_M = 64
162 BLOCK_N = 64
163
164 grid = (triton.cdiv(M_size, BLOCK_M), H, Z)
165
166 flash_attention_kernel[grid](
167 Q, K, V, O,
168 Q.stride(0), Q.stride(1), Q.stride(2), Q.stride(3),
169 K.stride(0), K.stride(1), K.stride(2), K.stride(3),
170 V.stride(0), V.stride(1), V.stride(2), V.stride(3),
171 O.stride(0), O.stride(1), O.stride(2), O.stride(3),
172 Z, H, M_size, N_size,
173 D=D,
174 sm_scale=sm_scale,
175 IS_CAUSAL=causal,
176 BLOCK_M=BLOCK_M,
177 BLOCK_N=BLOCK_N,
178 )
179
180 return O

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected