kernel-fun 0.2.0.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kernel_fun/__init__.py +67 -0
- kernel_fun/_common/__init__.py +11 -0
- kernel_fun/_common/cache.py +99 -0
- kernel_fun/_common/compat.py +168 -0
- kernel_fun/_common/support.py +267 -0
- kernel_fun/cconv/__init__.py +24 -0
- kernel_fun/cconv/_kernels/__init__.py +6 -0
- kernel_fun/cconv/_kernels/strip.py +450 -0
- kernel_fun/cconv/_provenance.py +11 -0
- kernel_fun/cconv/ops.py +256 -0
- kernel_fun/kda/__init__.py +23 -0
- kernel_fun/kda/_kernels/__init__.py +7 -0
- kernel_fun/kda/_kernels/bwd_dhu.py +994 -0
- kernel_fun/kda/_kernels/bwd_intra.py +1089 -0
- kernel_fun/kda/_kernels/bwd_intra_triton.py +328 -0
- kernel_fun/kda/_kernels/bwd_scan.py +1105 -0
- kernel_fun/kda/_kernels/bwd_wy.py +320 -0
- kernel_fun/kda/_kernels/bwd_wy_t.py +309 -0
- kernel_fun/kda/_kernels/fwd_intra_triton.py +101 -0
- kernel_fun/kda/_kernels/fwd_state.py +1065 -0
- kernel_fun/kda/_provenance.py +18 -0
- kernel_fun/kda/autograd.py +114 -0
- kernel_fun/kda/chain.py +150 -0
- kernel_fun/kda/ops.py +342 -0
- kernel_fun-0.2.0.dev1.dist-info/METADATA +347 -0
- kernel_fun-0.2.0.dev1.dist-info/RECORD +30 -0
- kernel_fun-0.2.0.dev1.dist-info/WHEEL +4 -0
- kernel_fun-0.2.0.dev1.dist-info/licenses/LICENSE +201 -0
- kernel_fun-0.2.0.dev1.dist-info/licenses/NOTICE +60 -0
- kernel_fun-0.2.0.dev1.dist-info/licenses/THIRD_PARTY_NOTICES.md +175 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"""The forward intra+solve stage: fla's two kernels, plus a write-only zero fill.
|
|
2
|
+
|
|
3
|
+
fla's token_parallel kernel stores only j <= t of each in-chunk tile and its solve stores
|
|
4
|
+
only the lower blocks, because fla's own consumer masks at load time. Our scan contracts
|
|
5
|
+
the full 64x64 tile, so the upper triangles have to be zero — and reading the whole 268MB
|
|
6
|
+
tile to overwrite an eighth of it (a masked_fill) costs more than writing the zeros. This
|
|
7
|
+
kernel writes them, nothing else, and lets Akk allocate as torch.empty.
|
|
8
|
+
|
|
9
|
+
That is the entire local contribution to this stage, and it is deliberate: three rebuilds
|
|
10
|
+
of the intra+solve pair (a CuTe SIMT monolith at 6.75ms, a Triton 3D-cube form at 8.11, a
|
|
11
|
+
Triton 2D-row form at 4.90) all LOST to fla's own kernels at 3.04ms. The forward pair does
|
|
12
|
+
not carry the tiny-CTA disease the backward's did — token_parallel and the solve run within
|
|
13
|
+
~1.6-1.9x of their floors. Site-shaped outputs like these A matrices want tensor cores for
|
|
14
|
+
the contraction, which is what fla already does; see kernels/kda/ideas/004-fwd-block for
|
|
15
|
+
the full postmortem.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import torch
|
|
21
|
+
import triton
|
|
22
|
+
import triton.language as tl
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@triton.jit(do_not_specialize=['T'])
|
|
27
|
+
def kda_fwd_zero_upper_kernel(
|
|
28
|
+
Aqk,
|
|
29
|
+
Akk,
|
|
30
|
+
T,
|
|
31
|
+
HV: tl.constexpr,
|
|
32
|
+
BT: tl.constexpr,
|
|
33
|
+
BC: tl.constexpr,
|
|
34
|
+
NC: tl.constexpr,
|
|
35
|
+
):
|
|
36
|
+
# write-only replacement for the chain's Aqk masked_fill (which READS the whole
|
|
37
|
+
# 268MB tile to write ~1/8 of it) and Akk's torch.zeros memset. Aqk needs its
|
|
38
|
+
# full in-chunk upper triangle (token_parallel stores only j <= t); Akk needs
|
|
39
|
+
# only the upper 16x16 BLOCKS (the solve's diagonal stores carry exact zeros
|
|
40
|
+
# above their diagonals, and it writes every lower block).
|
|
41
|
+
i_t, i_bh = tl.program_id(0).to(tl.int64), tl.program_id(1).to(tl.int64)
|
|
42
|
+
i_b, i_hv = i_bh // HV, i_bh % HV
|
|
43
|
+
bos = i_b * T
|
|
44
|
+
if i_t * BT >= T:
|
|
45
|
+
return
|
|
46
|
+
Aqk += (bos * HV + i_hv) * BT
|
|
47
|
+
Akk += (bos * HV + i_hv) * BT
|
|
48
|
+
o_i = tl.arange(0, BC)
|
|
49
|
+
o_j = tl.arange(0, BT)
|
|
50
|
+
b_z = tl.zeros([BC, BT], dtype=Aqk.dtype.element_ty)
|
|
51
|
+
for i_i in tl.static_range(NC):
|
|
52
|
+
o_c = i_t * BT + i_i * BC + o_i
|
|
53
|
+
m_c = o_c < T
|
|
54
|
+
p_q = Aqk + o_c[:, None] * (HV * BT) + o_j[None, :]
|
|
55
|
+
p_k = Akk + o_c[:, None] * (HV * BT) + o_j[None, :]
|
|
56
|
+
m_up = m_c[:, None] & (o_j[None, :] > (i_i * BC + o_i)[:, None])
|
|
57
|
+
m_blk = m_c[:, None] & (o_j[None, :] >= (i_i + 1) * BC)
|
|
58
|
+
tl.store(p_q, b_z, mask=m_up)
|
|
59
|
+
tl.store(p_k, b_z, mask=m_blk)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def chunk_kda_fwd_intra_zerofill(
|
|
63
|
+
q: torch.Tensor,
|
|
64
|
+
k: torch.Tensor,
|
|
65
|
+
g: torch.Tensor,
|
|
66
|
+
beta: torch.Tensor,
|
|
67
|
+
scale: float,
|
|
68
|
+
chunk_size: int = 64,
|
|
69
|
+
):
|
|
70
|
+
"""fla's token_parallel + solve VERBATIM, with the masked_fill and Akk memset
|
|
71
|
+
replaced by the write-only zero kernel — the phase-1 keeper path (the three
|
|
72
|
+
rebuilt-diag attempts all lost to fla's own kernels, see NOTES history 002)."""
|
|
73
|
+
from fla.ops.kda.chunk_intra import chunk_kda_fwd_kernel_inter_solve_fused
|
|
74
|
+
from fla.ops.kda.chunk_intra_token_parallel import (
|
|
75
|
+
chunk_kda_fwd_intra_token_parallel,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
B, T, H, K = k.shape
|
|
79
|
+
HV = g.shape[2]
|
|
80
|
+
BT = chunk_size
|
|
81
|
+
BC = 16
|
|
82
|
+
NT = triton.cdiv(T, BT)
|
|
83
|
+
NC = triton.cdiv(BT, BC)
|
|
84
|
+
|
|
85
|
+
Aqk = torch.empty(B, T, HV, BT, device=k.device, dtype=k.dtype)
|
|
86
|
+
Akk = torch.empty(B, T, HV, BT, device=k.device, dtype=k.dtype)
|
|
87
|
+
Akkd = torch.empty(B, T, HV, BC, device=k.device, dtype=torch.float32)
|
|
88
|
+
|
|
89
|
+
kda_fwd_zero_upper_kernel[(NT, B * HV)](
|
|
90
|
+
Aqk=Aqk, Akk=Akk, T=T, HV=HV, BT=BT, BC=BC, NC=NC, num_warps=2
|
|
91
|
+
)
|
|
92
|
+
Aqk, Akkd = chunk_kda_fwd_intra_token_parallel(
|
|
93
|
+
q=q, k=k, gk=g, beta=beta, Aqk=Aqk, Akk=Akkd, scale=scale,
|
|
94
|
+
cu_seqlens=None, chunk_size=BT, sub_chunk_size=BC,
|
|
95
|
+
)
|
|
96
|
+
chunk_kda_fwd_kernel_inter_solve_fused[(NT, B * HV)](
|
|
97
|
+
q=q, k=k, g=g, beta=beta, Aqk=Aqk, Akkd=Akkd, Akk=Akk, scale=scale,
|
|
98
|
+
cu_seqlens=None, chunk_indices=None,
|
|
99
|
+
T=T, H=H, HV=HV, K=K, BT=BT, BC=BC, NC=NC, USE_SAFE_GATE=False,
|
|
100
|
+
)
|
|
101
|
+
return Aqk, Akk
|